<?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\Controller;use Contao\PageModel;use Contao\StringUtil;use Contao\Environment;use Contao\BackendTemplate;use Haste\Http\Response\JsonResponse;use Rhyme\WMassArtsHub\Model\Profile\Item as ProfileItemModel;use Rhyme\WMassArtsHub\ProfileFilter\ProfileFilter;/** * Class Filter * @package Rhyme\WMassArtsHub\Module\Collection */class Filter extends Module{ /** * Template * @var string */ protected $strTemplate = 'mod_artshub_profile_filter'; /** * Form ID * @var string */ protected $strFormIdPrefix = 'artshub_profile_filter_'; /** * Display a wildcard in the back end * @return string * @throws \Exception */ public function generate() { if (TL_MODE == 'BE') { $objTemplate = new BackendTemplate('be_wildcard'); $objTemplate->wildcard = '### ARTSHUB PROFILE ITEM FILTER ###'; $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(); } $this->generateAjax(); return parent::generate(); } /** * Generate the module * @return void */ protected function compile() { /** @var $objPage PageModel */ global $objPage; $this->loadDataContainer(ProfileItemModel::getTable()); //Handle requests before generating as we will likely redirect if (Input::post('FORM_SUBMIT') == $this->strFormIdPrefix . $this->id) { $this->handleRequests(); } //Generate the filters if there are no request redirects $objTemplate = $this->Template; $arrFilterTypes = StringUtil::deserialize($this->artshub_profile_filterTypes, true); foreach ($arrFilterTypes as $filterType) { if (!isset($GLOBALS['PROFILE_FILTERS'][$filterType])) { continue; } /** @var ProfileFilter $strClass */ $strClass = $GLOBALS['PROFILE_FILTERS'][$filterType]['class']; if (!class_exists($strClass)) { continue; } $strClass::generateFilter($objTemplate, $this); } $this->Template = $objTemplate; $this->Template->id = $this->id; $this->Template->formId = $this->strFormIdPrefix . $this->id; $this->Template->actionClear = $objPage->getFrontendUrl(); $this->Template->clearLabel = $GLOBALS['TL_LANG']['MSC']['clearFiltersLabel']; $this->Template->slabel = $GLOBALS['TL_LANG']['MSC']['submitLabel']; } /** * Handle incoming filter requests and generate appropriate URL * @return void */ protected function handleRequests() { if (!$this->jumpTo) { global $objPage; } else { $objPage = PageModel::findWithDetails($this->jumpTo); } $arrRedirectParams = array(); $strRedirectParams = ''; $arrFilterTypes = StringUtil::deserialize($this->artshub_profile_filterTypes, true); // Loop through each filter type to get the URL params foreach ($arrFilterTypes as $filterType) { if (!isset($GLOBALS['PROFILE_FILTERS'][$filterType])) { continue; } $strClass = $GLOBALS['PROFILE_FILTERS'][$filterType]['class']; if (!class_exists($strClass)) { continue; } $varReturn = $strClass::generateFilter($this->Template, $this, true); if(!empty($varReturn)) { $arrRedirectParams[] = $varReturn; } } // Get any previously submitted URL params that aren't updated by this module $arrKeys = array_diff(array_keys($GLOBALS['PROFILE_FILTERS']), $arrFilterTypes); foreach ($arrKeys as $key) { if (Input::get($key)) { $arrRedirectParams[] = $key . '/' . ProfileFilter::uncleanChars(Input::get($key)); } } $strRedirectParams = implode('/', $arrRedirectParams); $strRedirect = Controller::generateFrontendUrl($objPage->row(), (strlen($strRedirectParams) ? '/' . $strRedirectParams : null) ); Controller::redirect($strRedirect); } /** * Generate a limit form */ protected function generateLimit() { } /** * Generate ajax * * @throws \Exception */ public function generateAjax() { if (!Environment::get('isAjaxRequest')) { return; } if ($this->artshub_searchAutocomplete && Input::get('autocomplete') == $this->id) { $objItems = ProfileItemModel::findPublished(['order' => 'name']); if (null === $objItems) { $objResponse = new JsonResponse([]); $objResponse->send(); exit; } $objResponse = new JsonResponse(array_values($objItems->fetchEach($this->artshub_searchAutocomplete))); $objResponse->send(); } }}