src/Rhyme/WMassArtsHub/Module/Profile/RandomLister.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (C) 2021 Rhyme Digital, LLC.
  4.  *
  5.  * @author        Blair Winans <blair@rhyme.digital>
  6.  * @author        Adam Fisher <adam@rhyme.digital>
  7.  * @link        https://rhyme.digital
  8.  * @license        http://www.gnu.org/licenses/lgpl-3.0.html LGPL
  9.  */
  10. namespace Rhyme\WMassArtsHub\Module\Profile;
  11. use Contao\Input;
  12. use Contao\System;
  13. use Contao\Database;
  14. use Contao\Pagination;
  15. use Contao\StringUtil;
  16. use Contao\BackendTemplate;
  17. use Contao\Model\QueryBuilder;
  18. use Haste\Util\InsertTag as HasteInsertTag;
  19. use Haste\Generator\RowClass;
  20. use Rhyme\WMassArtsHub\Helper\FilterHelper;
  21. use Rhyme\WMassArtsHub\Model\Profile\Item as WMassArtsHub_ProfileItemModel;
  22. use Rhyme\WMassArtsHub\Model\Profile\Type as WMassArtsHub_ProfileTypeModel;
  23. /**
  24.  * Class Lister
  25.  * @package Rhyme\WMassArtsHub\Module\Profile
  26.  */
  27. class RandomLister extends Lister
  28. {
  29.     /**
  30.      * Template
  31.      * @var string
  32.      */
  33.     protected $strTemplate 'mod_artshub_profile_randomlister';
  34.     /**
  35.      * Display a wildcard in the back end
  36.      * @return string
  37.      */
  38.     public function generate()
  39.     {
  40.         if (TL_MODE === 'BE')
  41.         {
  42.             $objTemplate = new BackendTemplate('be_wildcard');
  43.             $objTemplate->wildcard '### ARTSHUB RANDOM PROFILE ITEM LISTER ###';
  44.             $objTemplate->title $this->headline;
  45.             $objTemplate->id $this->id;
  46.             $objTemplate->link $this->name;
  47.             $objTemplate->href 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' $this->id;
  48.             return $objTemplate->parse();
  49.         }
  50.         return parent::generate();
  51.     }
  52.     /**
  53.      * Compile
  54.      */
  55.     protected function compile()
  56.     {
  57.         // Haste AJAX reload
  58.         \Haste\Ajax\ReloadHelper::subscribe(
  59.             \Haste\Ajax\ReloadHelper::getUniqid(\Haste\Ajax\ReloadHelper::TYPE_MODULE$this->id),
  60.             ['reloadArtsHubProfileListItems']
  61.         );
  62.         //Get items
  63.         $arrProfileItems $this->findItems();
  64.         $arrIDs = [];
  65.         foreach($arrProfileItems as $item){
  66.             $arrIDs[] = $item->id;
  67.         }
  68.         //Get all BIPOC items
  69.         $objBipocItems WMassArtsHub_ProfileItemModel::findByBipoc('1');
  70.         $arrBipocItems $objBipocItems !== null $objBipocItems->getModels() : [];
  71.         $arrBipocIDs = [];
  72.         foreach($arrBipocItems as $idx => $item){
  73.             if(in_array($item->id$arrIDs)) {
  74.                 unset($arrBipocItems[$idx]);
  75.             }
  76.         }
  77.         //Combine and set to limit
  78.         $arrCombinedItems array_merge($arrProfileItems$arrBipocItems);
  79.         shuffle($arrCombinedItems);
  80.         //$arrCombinedItems = array_unique($arrCombinedItems);
  81.         $arrProfileItems array_slice($arrCombinedItems0$this->numberOfItems);
  82.         // No items found
  83.         if (!is_array($arrProfileItems) || empty($arrProfileItems)) {
  84.             $this->compileEmptyMessage();
  85.             return;
  86.         }
  87.         // Make sure this module has an ID
  88.         $arrCssID = (array)$this->cssID;
  89.         $arrCssID[0] = $arrCssID[0] ?: 'profilelister'.$this->id;
  90.         $this->cssID $arrCssID;
  91.         $arrBuffer = array();
  92.         foreach ($arrProfileItems as $objItem) {
  93.             $arrConfig = array(
  94.                 'module'        => $this,
  95.                 'template'      => $this->artshub_profile_list_layout ?: 'artshub_profile_list_default',
  96.                 'jumpTo'        => $this->findJumpToPage($objItem),
  97.             );
  98.             $arrCSS StringUtil::deserialize($objItem->cssIDtrue);
  99.             $arrBuffer[] = array(
  100.                 'cssID'     => ($arrCSS[0] != '') ? ' id="' $arrCSS[0] . '"' '',
  101.                 'class'     => trim('profileItem ' $arrCSS[1]),
  102.                 'html'      => $objItem->generate($arrConfig),
  103.                 'item'      => $objItem,
  104.             );
  105.         }
  106.         if (isset($GLOBALS['TL_HOOKS']['generateProfileList']) && is_array($GLOBALS['TL_HOOKS']['generateProfileList'])) {
  107.             foreach ($GLOBALS['TL_HOOKS']['generateProfileList'] as $callback) {
  108.                 $objCallback System::importStatic($callback[0]);
  109.                 $arrBuffer   $objCallback->{$callback[1]}($arrBuffer$arrProfileItems$this->Template$this);
  110.             }
  111.         }
  112.         RowClass::withKey('class')->addCount('profileItem_')->addEvenOdd('profileItem_')->addFirstLast('profileItem_')->addGridRows($this->artshub_profile_cols)->addGridCols($this->artshub_profile_cols)->applyTo($arrBuffer);
  113.         $this->Template->items $arrBuffer;
  114.         //Add jumpto Page
  115.         global $objPage;
  116.         $this->Template->jumpTo $this->jumpTo ?: $objPage->id;
  117.     }
  118.     /**
  119.      * Get filter & sorting configuration
  120.      * @return array
  121.      */
  122.     protected function getFiltersAndSorting($blnNativeSQL true)
  123.     {
  124.         $strWhere     '';
  125.         $strSorting 'RAND()'//<---------------- THIS IS REALLY ALL WE ARE DOING WITH THIS RANDOM LISTER, FYI
  126.         $arrWhere    = array();
  127.         $arrValues     = array();
  128.         $blnDefaultModuleSort false;
  129.         $c WMassArtsHub_ProfileItemModel::getTable();
  130.         $arrSortFields = array();
  131.         $arrSortValues = array();
  132.         // !HOOK: custom filter/sorting types
  133.         if (isset($GLOBALS['TL_HOOKS']['processProfileFiltersAndSorting']) && is_array($GLOBALS['TL_HOOKS']['processProfileFiltersAndSorting']))
  134.         {
  135.             foreach ($GLOBALS['TL_HOOKS']['processProfileFiltersAndSorting'] as $callback)
  136.             {
  137.                 $objCallback System::importStatic($callback[0]);
  138.                 list($arrValues$arrWhere$strSorting) = $objCallback->{$callback[1]}($arrValues$arrWhere$strSorting$this->findCategories(), $this);
  139.             }
  140.         }
  141.         // Now put together the entire WHERE
  142.         if(count($arrWhere) > 0)
  143.         {
  144.             $strWhere implode(' AND '$arrWhere);
  145.         }
  146.         return array($arrValues$strWhere$strSorting);
  147.     }
  148. }