src/Rhyme/WMassArtsHub/ProfileFilter/Sorting.php line 86

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\ProfileFilter;
  11. use Contao\Input;
  12. use Contao\Template;
  13. use Contao\Module;
  14. use Contao\System;
  15. use Contao\Controller;
  16. use Contao\StringUtil;
  17. use Rhyme\WMassArtsHub\Model\Profile\Item as ProfileItemModel;
  18. /**
  19.  * Class Sorting
  20.  * Sorting filter
  21.  */
  22. class Sorting extends ProfileFilter
  23. {
  24.     
  25.     /**
  26.      * Filter key
  27.      * @var string
  28.      */
  29.     protected static $strKey 'sorting';
  30.     /**
  31.      * Add this filter to the module's template or get the URL params
  32.      * @param Template $objTemplate
  33.      * @param Module $objModule
  34.      * @param bool $blnGenURL
  35.      * @return bool|mixed|string
  36.      */
  37.     public static function generateFilter(Template &$objTemplateModule $objModulebool $blnGenURL=false)
  38.     {
  39.         $t ProfileItemModel::getTable();
  40.         System::loadLanguageFile($t);
  41.         Controller::loadDataContainer($t);
  42.         
  43.         $arrFields StringUtil::deserialize($objModule->artshub_profile_sortingFieldstrue);
  44.         if($blnGenURL)
  45.         {
  46.             //return the URL fragment needed for this filter to pass to the lister
  47.             if (Input::post(static::$strKey) && in_array(str_replace(array('-asc''-desc'), ''Input::post(static::$strKey)), $arrFields))
  48.             {
  49.                 return static::$strKey '/' urlencode(Input::post(static::$strKey));
  50.             }
  51.             
  52.             return false;
  53.         }
  54.         
  55.         $arrAvailable = array(''=>$GLOBALS['TL_LANG']['MSC']['relevanceFilterLabel']);
  56.         
  57.         foreach ($arrFields as $field)
  58.         {
  59.             list($asc$desc) = static::getSortingLabels($field);
  60.             $strLabel is_array($GLOBALS['TL_DCA'][$t]['fields'][$field]['label']) ? $GLOBALS['TL_DCA'][$t]['fields'][$field]['label'][0] : $field;
  61.             $arrAvailable[$field.'-asc'] = $strLabel ' ' $asc;
  62.             $arrAvailable[$field.'-desc'] = $strLabel ' ' $desc;
  63.         }
  64.         
  65.         if(count($arrAvailable) > 0)
  66.         {
  67.             $objTemplate->hasSorting true;
  68.             $objTemplate->sort $arrAvailable;
  69.             $objTemplate->sortselected Input::get(static::$strKey) ?: ($objModule->iso_listingSortField $objModule->iso_listingSortField.'-'.strtolower($objModule->iso_listingSortDirection) : '');
  70.             $objTemplate->psortLabel $GLOBALS['TL_LANG']['MSC'][static::$strKey.'FilterLabel'];
  71.         }
  72.     }
  73.     /**
  74.      * Get the sorting labels (asc/desc) for an attribute
  75.      * @param string
  76.      * @return array
  77.      */
  78.     protected static function getSortingLabels($field)
  79.     {
  80.         $t ProfileItemModel::getTable();
  81.         $arrData $GLOBALS['TL_DCA'][$t]['fields'][$field];
  82.         switch ($arrData['eval']['rgxp'])
  83.         {
  84.             case 'price':
  85.             case 'digit':
  86.                 return array($GLOBALS['TL_LANG']['MSC']['low_to_high'], $GLOBALS['TL_LANG']['MSC']['high_to_low']);
  87.             case 'date':
  88.             case 'time':
  89.             case 'datim':
  90.                 return array($GLOBALS['TL_LANG']['MSC']['old_to_new'], $GLOBALS['TL_LANG']['MSC']['new_to_old']);
  91.         }
  92.         // !HOOK: custom sorting labels
  93.         if (isset($GLOBALS['ISO_HOOKS']['sortingLabels']) && is_array($GLOBALS['ISO_HOOKS']['sortingLabels']))
  94.         {
  95.             foreach ($GLOBALS['ISO_HOOKS']['sortingLabels'] as $callback)
  96.             {
  97.                 $objCallback System::importStatic($callback[0]);
  98.                 $varReturn $objCallback->{$callback[1]}($field$arrDatanull);
  99.                 
  100.                 if ($varReturn !== false)
  101.                 {
  102.                     return $varReturn;
  103.                 }
  104.             }
  105.         }
  106.         return array($GLOBALS['TL_LANG']['MSC']['a_to_z'], $GLOBALS['TL_LANG']['MSC']['z_to_a']);
  107.     }
  108. }