<?php
/**
* Copyright (C) 2021 Rhyme Digital, LLC.
*
* @author Blair Winans <blair@rhyme.digital>
* @author Adam Fisher <adam@rhyme.digital>
* @link https://rhyme.digital
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/
namespace Rhyme\WMassArtsHub\Module\Profile;
use Contao\Input;
use Contao\System;
use Contao\Database;
use Contao\Pagination;
use Contao\StringUtil;
use Contao\BackendTemplate;
use Contao\Model\QueryBuilder;
use Haste\Util\InsertTag as HasteInsertTag;
use Haste\Generator\RowClass;
use Rhyme\WMassArtsHub\Helper\FilterHelper;
use Rhyme\WMassArtsHub\Model\Profile\Item as WMassArtsHub_ProfileItemModel;
use Rhyme\WMassArtsHub\Model\Profile\Type as WMassArtsHub_ProfileTypeModel;
/**
* Class Lister
* @package Rhyme\WMassArtsHub\Module\Profile
*/
class RandomLister extends Lister
{
/**
* Template
* @var string
*/
protected $strTemplate = 'mod_artshub_profile_randomlister';
/**
* Display a wildcard in the back end
* @return string
*/
public function generate()
{
if (TL_MODE === 'BE')
{
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ARTSHUB RANDOM PROFILE ITEM LISTER ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
return $objTemplate->parse();
}
return parent::generate();
}
/**
* Compile
*/
protected function compile()
{
// Haste AJAX reload
\Haste\Ajax\ReloadHelper::subscribe(
\Haste\Ajax\ReloadHelper::getUniqid(\Haste\Ajax\ReloadHelper::TYPE_MODULE, $this->id),
['reloadArtsHubProfileListItems']
);
//Get items
$arrProfileItems = $this->findItems();
$arrIDs = [];
foreach($arrProfileItems as $item){
$arrIDs[] = $item->id;
}
//Get all BIPOC items
$objBipocItems = WMassArtsHub_ProfileItemModel::findByBipoc('1');
$arrBipocItems = $objBipocItems !== null ? $objBipocItems->getModels() : [];
$arrBipocIDs = [];
foreach($arrBipocItems as $idx => $item){
if(in_array($item->id, $arrIDs)) {
unset($arrBipocItems[$idx]);
}
}
//Combine and set to limit
$arrCombinedItems = array_merge($arrProfileItems, $arrBipocItems);
shuffle($arrCombinedItems);
//$arrCombinedItems = array_unique($arrCombinedItems);
$arrProfileItems = array_slice($arrCombinedItems, 0, $this->numberOfItems);
// No items found
if (!is_array($arrProfileItems) || empty($arrProfileItems)) {
$this->compileEmptyMessage();
return;
}
// Make sure this module has an ID
$arrCssID = (array)$this->cssID;
$arrCssID[0] = $arrCssID[0] ?: 'profilelister'.$this->id;
$this->cssID = $arrCssID;
$arrBuffer = array();
foreach ($arrProfileItems as $objItem) {
$arrConfig = array(
'module' => $this,
'template' => $this->artshub_profile_list_layout ?: 'artshub_profile_list_default',
'jumpTo' => $this->findJumpToPage($objItem),
);
$arrCSS = StringUtil::deserialize($objItem->cssID, true);
$arrBuffer[] = array(
'cssID' => ($arrCSS[0] != '') ? ' id="' . $arrCSS[0] . '"' : '',
'class' => trim('profileItem ' . $arrCSS[1]),
'html' => $objItem->generate($arrConfig),
'item' => $objItem,
);
}
if (isset($GLOBALS['TL_HOOKS']['generateProfileList']) && is_array($GLOBALS['TL_HOOKS']['generateProfileList'])) {
foreach ($GLOBALS['TL_HOOKS']['generateProfileList'] as $callback) {
$objCallback = System::importStatic($callback[0]);
$arrBuffer = $objCallback->{$callback[1]}($arrBuffer, $arrProfileItems, $this->Template, $this);
}
}
RowClass::withKey('class')->addCount('profileItem_')->addEvenOdd('profileItem_')->addFirstLast('profileItem_')->addGridRows($this->artshub_profile_cols)->addGridCols($this->artshub_profile_cols)->applyTo($arrBuffer);
$this->Template->items = $arrBuffer;
//Add jumpto Page
global $objPage;
$this->Template->jumpTo = $this->jumpTo ?: $objPage->id;
}
/**
* Get filter & sorting configuration
* @return array
*/
protected function getFiltersAndSorting($blnNativeSQL = true)
{
$strWhere = '';
$strSorting = 'RAND()'; //<---------------- THIS IS REALLY ALL WE ARE DOING WITH THIS RANDOM LISTER, FYI
$arrWhere = array();
$arrValues = array();
$blnDefaultModuleSort = false;
$c = WMassArtsHub_ProfileItemModel::getTable();
$arrSortFields = array();
$arrSortValues = array();
// !HOOK: custom filter/sorting types
if (isset($GLOBALS['TL_HOOKS']['processProfileFiltersAndSorting']) && is_array($GLOBALS['TL_HOOKS']['processProfileFiltersAndSorting']))
{
foreach ($GLOBALS['TL_HOOKS']['processProfileFiltersAndSorting'] as $callback)
{
$objCallback = System::importStatic($callback[0]);
list($arrValues, $arrWhere, $strSorting) = $objCallback->{$callback[1]}($arrValues, $arrWhere, $strSorting, $this->findCategories(), $this);
}
}
// Now put together the entire WHERE
if(count($arrWhere) > 0)
{
$strWhere = implode(' AND ', $arrWhere);
}
return array($arrValues, $strWhere, $strSorting);
}
}