src/Rhyme/WMassArtsHub/Module/Profile/Filter.php line 104

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\Controller;
  13. use Contao\PageModel;
  14. use Contao\StringUtil;
  15. use Contao\Environment;
  16. use Contao\BackendTemplate;
  17. use Haste\Http\Response\JsonResponse;
  18. use Rhyme\WMassArtsHub\Model\Profile\Item as ProfileItemModel;
  19. use Rhyme\WMassArtsHub\ProfileFilter\ProfileFilter;
  20. /**
  21.  * Class Filter
  22.  * @package Rhyme\WMassArtsHub\Module\Collection
  23.  */
  24. class Filter extends Module
  25. {
  26.     /**
  27.      * Template
  28.      * @var string
  29.      */
  30.     protected $strTemplate 'mod_artshub_profile_filter';
  31.     /**
  32.      * Form ID
  33.      * @var string
  34.      */
  35.     protected $strFormIdPrefix 'artshub_profile_filter_';
  36.     /**
  37.      * Display a wildcard in the back end
  38.      * @return string
  39.      * @throws \Exception
  40.      */
  41.     public function generate()
  42.     {
  43.         if (TL_MODE == 'BE')
  44.         {
  45.             $objTemplate = new BackendTemplate('be_wildcard');
  46.             $objTemplate->wildcard '### ARTSHUB PROFILE ITEM FILTER ###';
  47.             $objTemplate->title $this->headline;
  48.             $objTemplate->id $this->id;
  49.             $objTemplate->link $this->name;
  50.             $objTemplate->href 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' $this->id;
  51.             return $objTemplate->parse();
  52.         }
  53.         $this->generateAjax();
  54.         return parent::generate();
  55.     }
  56.     /**
  57.      * Generate the module
  58.      * @return void
  59.      */
  60.     protected function compile()
  61.     {
  62.         /** @var $objPage PageModel */
  63.         global $objPage;
  64.         $this->loadDataContainer(ProfileItemModel::getTable());
  65.         //Handle requests before generating as we will likely redirect
  66.         if (Input::post('FORM_SUBMIT') == $this->strFormIdPrefix $this->id)
  67.         {
  68.             $this->handleRequests();
  69.         }
  70.         //Generate the filters if there are no request redirects
  71.         $objTemplate $this->Template;
  72.         $arrFilterTypes StringUtil::deserialize($this->artshub_profile_filterTypestrue);
  73.         foreach ($arrFilterTypes as $filterType)
  74.         {
  75.             if (!isset($GLOBALS['PROFILE_FILTERS'][$filterType]))
  76.             {
  77.                 continue;
  78.             }
  79.             /** @var ProfileFilter $strClass */
  80.             $strClass $GLOBALS['PROFILE_FILTERS'][$filterType]['class'];
  81.             if (!class_exists($strClass))
  82.             {
  83.                 continue;
  84.             }
  85.             $strClass::generateFilter($objTemplate$this);
  86.         }
  87.         $this->Template $objTemplate;
  88.         $this->Template->id $this->id;
  89.         $this->Template->formId $this->strFormIdPrefix $this->id;
  90.         $this->Template->actionClear $objPage->getFrontendUrl();
  91.         $this->Template->clearLabel $GLOBALS['TL_LANG']['MSC']['clearFiltersLabel'];
  92.         $this->Template->slabel $GLOBALS['TL_LANG']['MSC']['submitLabel'];
  93.     }
  94.     /**
  95.      * Handle incoming filter requests and generate appropriate URL
  96.      * @return void
  97.      */
  98.     protected function handleRequests()
  99.     {
  100.         if (!$this->jumpTo)
  101.         {
  102.             global $objPage;
  103.         }
  104.         else
  105.         {
  106.             $objPage PageModel::findWithDetails($this->jumpTo);
  107.         }
  108.         $arrRedirectParams = array();
  109.         $strRedirectParams '';
  110.         $arrFilterTypes StringUtil::deserialize($this->artshub_profile_filterTypestrue);
  111.         // Loop through each filter type to get the URL params
  112.         foreach ($arrFilterTypes as $filterType)
  113.         {
  114.             if (!isset($GLOBALS['PROFILE_FILTERS'][$filterType]))
  115.             {
  116.                 continue;
  117.             }
  118.             $strClass $GLOBALS['PROFILE_FILTERS'][$filterType]['class'];
  119.             if (!class_exists($strClass))
  120.             {
  121.                 continue;
  122.             }
  123.             $varReturn $strClass::generateFilter($this->Template$thistrue);
  124.             if(!empty($varReturn))
  125.             {
  126.                 $arrRedirectParams[] = $varReturn;
  127.             }
  128.         }
  129.         // Get any previously submitted URL params that aren't updated by this module
  130.         $arrKeys array_diff(array_keys($GLOBALS['PROFILE_FILTERS']), $arrFilterTypes);
  131.         foreach ($arrKeys as $key)
  132.         {
  133.             if (Input::get($key))
  134.             {
  135.                 $arrRedirectParams[] = $key '/' ProfileFilter::uncleanChars(Input::get($key));
  136.             }
  137.         }
  138.         $strRedirectParams implode('/'$arrRedirectParams);
  139.         $strRedirect Controller::generateFrontendUrl($objPage->row(), (strlen($strRedirectParams) ? '/' $strRedirectParams null) );
  140.         Controller::redirect($strRedirect);
  141.     }
  142.     /**
  143.      * Generate a limit form
  144.      */
  145.     protected function generateLimit()
  146.     {
  147.     }
  148.     /**
  149.      * Generate ajax
  150.      *
  151.      * @throws \Exception
  152.      */
  153.     public function generateAjax()
  154.     {
  155.         if (!Environment::get('isAjaxRequest')) {
  156.             return;
  157.         }
  158.         if ($this->artshub_searchAutocomplete && Input::get('autocomplete') == $this->id) {
  159.             $objItems ProfileItemModel::findPublished(['order' => 'name']);
  160.             if (null === $objItems) {
  161.                 $objResponse = new JsonResponse([]);
  162.                 $objResponse->send();
  163.                 exit;
  164.             }
  165.             $objResponse = new JsonResponse(array_values($objItems->fetchEach($this->artshub_searchAutocomplete)));
  166.             $objResponse->send();
  167.         }
  168.     }
  169. }