src/Rhyme/WMassArtsHub/Module/Profile/AjaxFilter.php line 61

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (C) 2021 Rhyme Digital, LLC.
  4.  *
  5.  * @link       https://rhyme.digital
  6.  * @license    http://www.gnu.org/licenses/lgpl-3.0.html LGPL
  7.  */
  8. namespace Rhyme\WMassArtsHub\Module\Profile;
  9. use Contao\Input;
  10. use Contao\System;
  11. use Contao\ModuleModel;
  12. use Contao\CoreBundle\Exception\AjaxRedirectResponseException;
  13. use Contao\CoreBundle\Exception\RedirectResponseException;
  14. use Haste\Http\Response\JsonResponse;
  15. use Haste\Ajax\ReloadHelper;
  16. /**
  17.  * Class AjaxFilter
  18.  * @package Rhyme\WMassArtsHub\Module
  19.  */
  20. abstract class AjaxFilter extends Filter
  21. {
  22.     /**
  23.      * Display a wildcard in the back end
  24.      * @return string
  25.      * @throws \Exception
  26.      */
  27.     public function generate()
  28.     {
  29.         $strBuffer parent::generate();
  30.         if (TL_MODE === 'BE')
  31.         {
  32.             return $strBuffer;
  33.         }
  34.         // Add the data attributes
  35.         if ($this->Template->cssID)
  36.         {
  37.             $strBuffer str_replace($this->Template->cssID$this->Template->cssID.' data-module="'.$this->id.'"'$strBuffer);
  38.             $strBuffer str_replace($this->Template->cssID$this->Template->cssID.' data-id="'.$this->id.'"'$strBuffer);
  39.             $strBuffer str_replace($this->Template->cssID$this->Template->cssID.' data-pageid="{{page::id}}"'$strBuffer);
  40.             $strBuffer str_replace($this->Template->cssID$this->Template->cssID.' data-table="'.ModuleModel::getTable().'"'$strBuffer);
  41.         }
  42.         return $strBuffer;
  43.     }
  44.     /**
  45.      *
  46.      */
  47.     protected function compile()
  48.     {
  49.         // Haste AJAX reload
  50.         ReloadHelper::subscribe(
  51.             ReloadHelper::getUniqid(ReloadHelper::TYPE_MODULE$this->id),
  52.             ['reloadFilter-'.$this->id]
  53.         );
  54.         parent::compile();
  55.         // Make sure this module has an ID
  56.         $arrCssID = (array)$this->cssID;
  57.         $arrCssID[0] = $arrCssID[0] ?: 'profilefilter'.$this->id;
  58.         $this->cssID $arrCssID;
  59.         // Add AJAX scripts
  60.         $GLOBALS['TL_JAVASCRIPT']['profileFilterAjax'] = 'web/bundles/rhymewmassartshub/assets/js/frontend/profileFilter/ajax.js|static';
  61.         $GLOBALS['TL_BODY']['historyjs'] = '<script src="https://cdnjs.cloudflare.com/ajax/libs/history.js/1.8/native.history.min.js" integrity="sha512-LW9lY12yhdxezhOnfueR/bF7GdraV3ZCZpdTTUqrq6ifCsp4wmnlBbw8/qTjs+9jqLXZn+cBVYN9rlQz52UVkA==" crossorigin="anonymous"></script>';
  62.         $GLOBALS['TL_BODY']['profileFilterAjax'.$this->id] = "
  63.             <script>
  64.                 jQuery(document).ready(function(){
  65.                     ArtsHub.ProfileFilterAjax.create(jQuery('#".$arrCssID[0]."'), {
  66.                     });
  67.                 });
  68.             </script>";
  69.     }
  70.     /**
  71.      * Handle incoming filter requests and generate appropriate URL
  72.      * @return void
  73.      */
  74.     protected function handleRequests()
  75.     {
  76.         try
  77.         {
  78.             parent::handleRequests();
  79.         }
  80.         catch (AjaxRedirectResponseException $e)
  81.         {
  82.             // There was a POST, so we need to send a json response back
  83.             $response $e->getResponse();
  84.             $newUrl $response->getContent();
  85.             $container System::getContainer();
  86.             // Append the page as a GET param
  87.             if (Input::post('page'))
  88.             {
  89.                 $newUrl .= (\strpos($newUrl'?') !== false '&' '?') . 'page='.Input::post('page');
  90.             }
  91.             $objResponse = new JsonResponse([
  92.                 'newUrl'        => $newUrl,
  93.                 'REQUEST_TOKEN' => $container->get('contao.csrf.token_manager')->getToken($container->getParameter('contao.csrf_token_name'))->getValue(),
  94.             ]);
  95.             $objResponse->send();
  96.             exit;
  97.         }
  98.     }
  99. }