vendor/isotope/isotope-core/system/modules/isotope/library/Isotope/Module/Module.php line 80

Open in your IDE?
  1. <?php
  2. /*
  3.  * Isotope eCommerce for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2009 - 2019 terminal42 gmbh & Isotope eCommerce Workgroup
  6.  *
  7.  * @link       https://isotopeecommerce.org
  8.  * @license    https://opensource.org/licenses/lgpl-3.0.html
  9.  */
  10. namespace Isotope\Module;
  11. use Contao\Controller;
  12. use Contao\Database;
  13. use Contao\Date;
  14. use Contao\Environment;
  15. use Contao\FrontendUser;
  16. use Contao\System;
  17. use Haste\Frontend\AbstractFrontendModule;
  18. use Haste\Input\Input;
  19. use Haste\Util\Debug;
  20. use Isotope\Frontend;
  21. use Isotope\Interfaces\IsotopeProduct;
  22. use Isotope\Isotope;
  23. use Isotope\Message;
  24. use Isotope\Model\Product;
  25. use Isotope\Model\Product\AbstractProduct;
  26. use Isotope\RequestCache\CategoryFilter;
  27. use PageModel;
  28. /**
  29.  * Module implements a parent class for Isotope modules
  30.  *
  31.  * @property string $iso_category_scope
  32.  * @property string $iso_list_where
  33.  * @property string $iso_includeMessages
  34.  * @property bool   $iso_hide_list
  35.  * @property bool   $iso_disable_options
  36.  * @property bool   $iso_emptyMessage
  37.  * @property string $iso_noProducts
  38.  * @property bool   $iso_emptyFilter
  39.  * @property string $iso_newFilter
  40.  * @property string $iso_noFilter
  41.  * @property array  $iso_buttons
  42.  * @property bool   $iso_link_primary
  43.  */
  44. abstract class Module extends AbstractFrontendModule
  45. {
  46.     /**
  47.      * Disable caching of the frontend page if this module is in use.
  48.      * Usefule to enable in a child classes.
  49.      * @var bool
  50.      */
  51.     protected $blnDisableCache false;
  52.     /**
  53.      * Cache category lookup
  54.      * @var array
  55.      */
  56.     private $arrCategories;
  57.     /**
  58.      * Load libraries and scripts
  59.      *
  60.      * @param \ModuleModel $objModule
  61.      * @param string $strColumn
  62.      */
  63.     public function __construct($objModule$strColumn 'main')
  64.     {
  65.         parent::__construct($objModule$strColumn);
  66.         if ($this->iso_list_where != '') {
  67.             $this->iso_list_where Controller::replaceInsertTags($this->iso_list_where);
  68.         }
  69.         Isotope::initialize();
  70.         // Load Isotope JavaScript and style sheet
  71.         if ('FE' === TL_MODE) {
  72.             $GLOBALS['TL_JAVASCRIPT'][] = Debug::uncompressedFile(
  73.                 'system/modules/isotope/assets/js/isotope.min.js|static'
  74.             );
  75.             $GLOBALS['TL_CSS'][] = Debug::uncompressedFile(
  76.                 'system/modules/isotope/assets/css/isotope.min.css|screen|static'
  77.             );
  78.             // Disable caching for pages with certain modules (eg. Cart)
  79.             if ($this->blnDisableCache) {
  80.                 global $objPage;
  81.                 $objPage->cache 0;
  82.             }
  83.         }
  84.     }
  85.     /**
  86.      * @inheritDoc
  87.      */
  88.     protected function getSerializedProperties()
  89.     {
  90.         return ['iso_buttons'];
  91.     }
  92.     /**
  93.      * Include messages if enabled
  94.      *
  95.      * @return string
  96.      */
  97.     public function generate()
  98.     {
  99.         $strBuffer parent::generate();
  100.         // Prepend any messages to the module output
  101.         if ('BE' !== TL_MODE && $this->iso_includeMessages) {
  102.             $strBuffer Message::generate() . $strBuffer;
  103.         }
  104.         return $strBuffer;
  105.     }
  106.     /**
  107.      * The ids of all pages we take care of. This is what should later be used eg. for filter data.
  108.      *
  109.      * @return array|null
  110.      */
  111.     protected function findCategories(array &$arrFilters null)
  112.     {
  113.         if (null !== $arrFilters) {
  114.             $arrCategories null;
  115.             foreach ($arrFilters as $k => $filter) {
  116.                 if ($filter instanceof CategoryFilter) {
  117.                     unset($arrFilters[$k]);
  118.                     if (!\is_array($arrCategories)) {
  119.                         $arrCategories $filter['value'];
  120.                     } else {
  121.                         $arrCategories array_intersect($arrCategories$filter['value']);
  122.                     }
  123.                 }
  124.             }
  125.             if (\is_array($arrCategories)) {
  126.                 return empty($arrCategories) ? array(0) : array_map('intval'$arrCategories);
  127.             }
  128.         }
  129.         if (null !== $this->arrCategories) {
  130.             return $this->arrCategories;
  131.         }
  132.         if ($this->defineRoot && $this->rootPage 0) {
  133.             $objPage PageModel::findWithDetails($this->rootPage);
  134.         } else {
  135.             global $objPage;
  136.         }
  137.         $t PageModel::getTable();
  138.         $arrCategories null;
  139.         $strWhere "$t.type!='error_403' AND $t.type!='error_404'";
  140.         if (!BE_USER_LOGGED_IN) {
  141.             $time Date::floorToMinute();
  142.             $strWhere .= " AND ($t.start='' OR $t.start<'$time') AND ($t.stop='' OR $t.stop>'" . ($time 60) . "') AND $t.published='1'";
  143.         }
  144.         switch ($this->iso_category_scope) {
  145.             case 'global':
  146.                 $arrCategories = [$objPage->rootId];
  147.                 $arrCategories Database::getInstance()->getChildRecords($objPage->rootId'tl_page'false$arrCategories$strWhere);
  148.                 break;
  149.             case 'current_and_first_child':
  150.                 $arrCategories   Database::getInstance()->execute("SELECT id FROM tl_page WHERE pid={$objPage->id} AND $strWhere")->fetchEach('id');
  151.                 $arrCategories[] = $objPage->id;
  152.                 break;
  153.             case 'current_and_all_children':
  154.                 $arrCategories = [$objPage->id];
  155.                 $arrCategories Database::getInstance()->getChildRecords($objPage->id'tl_page'false$arrCategories$strWhere);
  156.                 break;
  157.             case 'parent':
  158.                 $arrCategories = [$objPage->pid];
  159.                 break;
  160.             case 'product':
  161.                 /** @var \Isotope\Model\Product\Standard $objProduct */
  162.                 $objProduct Product::findAvailableByIdOrAlias(Input::getAutoItem('product'));
  163.                 $arrCategories = [0];
  164.                 if ($objProduct !== null) {
  165.                     $arrCategories $objProduct->getCategories(true);
  166.                 }
  167.                 break;
  168.             case 'article':
  169.                 $arrCategories = array($GLOBALS['ISO_CONFIG']['current_article']['pid'] ? : $objPage->id);
  170.                 break;
  171.             case '':
  172.             case 'current_category':
  173.                 $arrCategories = [$objPage->id];
  174.                 break;
  175.             default:
  176.                 if (isset($GLOBALS['ISO_HOOKS']['findCategories']) && \is_array($GLOBALS['ISO_HOOKS']['findCategories'])) {
  177.                     foreach ($GLOBALS['ISO_HOOKS']['findCategories'] as $callback) {
  178.                         $arrCategories System::importStatic($callback[0])->{$callback[1]}($this);
  179.                         if ($arrCategories !== false) {
  180.                             break;
  181.                         }
  182.                     }
  183.                 }
  184.                 break;
  185.         }
  186.         $this->arrCategories = empty($arrCategories) ? array(0) : array_map('intval'$arrCategories);
  187.         return $this->arrCategories;
  188.     }
  189.     /**
  190.      * Find jumpTo page for current category scope
  191.      *
  192.      * @param IsotopeProduct $objProduct
  193.      *
  194.      * @return PageModel
  195.      */
  196.     protected function findJumpToPage(IsotopeProduct $objProduct)
  197.     {
  198.         global $objPage;
  199.         global $objIsotopeListPage;
  200.         $productCategories $objProduct instanceof AbstractProduct $objProduct->getCategories(true) : [];
  201.         $arrCategories = array();
  202.         if (!$this->iso_link_primary) {
  203.             $arrCategories array_intersect(
  204.                 $productCategories,
  205.                 $this->findCategories()
  206.             );
  207.         }
  208.         // If our current category scope does not match with any product category,
  209.         // use the first allowed product category in the current root page
  210.         if (empty($arrCategories)) {
  211.             $arrCategories $productCategories;
  212.         }
  213.         $arrCategories Frontend::getPagesInCurrentRoot(
  214.             $arrCategories,
  215.             FrontendUser::getInstance()
  216.         );
  217.         if (!empty($arrCategories)
  218.          && ($objCategories PageModel::findMultipleByIds($arrCategories)) !== null
  219.         ) {
  220.             $blnMoreThanOne $objCategories->count() > 1;
  221.             foreach ($objCategories as $objCategory) {
  222.                 if ('index' === $objCategory->alias && $blnMoreThanOne) {
  223.                     continue;
  224.                 }
  225.                 return $objCategory;
  226.             }
  227.         }
  228.         return $objIsotopeListPage ? : $objPage;
  229.     }
  230.     /**
  231.      * Generate the URL from existing $_GET parameters.
  232.      * Use Input::setGet('var', null) to remove a parameter from the final URL.
  233.      *
  234.      * @return string
  235.      * @deprecated use \Haste\Util\Url::addQueryString instead
  236.      */
  237.     protected function generateRequestUrl()
  238.     {
  239.         if (Environment::get('request') == '') {
  240.             return '';
  241.         }
  242.         $strRequest   preg_replace('/\?.*$/i'''Environment::get('request'));
  243.         $strRequest   preg_replace('/' preg_quote($GLOBALS['TL_CONFIG']['urlSuffix'], '/') . '$/i'''$strRequest);
  244.         $arrFragments explode('/'$strRequest);
  245.         // Skip index.php
  246.         if ('index.php' === strtolower($arrFragments[0])) {
  247.             array_shift($arrFragments);
  248.         }
  249.         // HOOK: add custom logic
  250.         if (isset($GLOBALS['TL_HOOKS']['getPageIdFromUrl']) && \is_array($GLOBALS['TL_HOOKS']['getPageIdFromUrl'])) {
  251.             foreach ($GLOBALS['TL_HOOKS']['getPageIdFromUrl'] as $callback) {
  252.                 $arrFragments System::importStatic($callback[0])->{$callback[1]}($arrFragments);
  253.             }
  254.         }
  255.         $strParams '';
  256.         $arrGet    = array();
  257.         // Add fragments to URL params
  258.         for ($i 1$count \count($arrFragments); $i $count$i += 2) {
  259.             if (isset($_GET[$arrFragments[$i]])) {
  260.                 $key urldecode($arrFragments[$i]);
  261.                 Input::setGet($keynull);
  262.                 $strParams .= '/' $key '/' urldecode($arrFragments[$i 1]);
  263.             }
  264.         }
  265.         // Add get parameters to URL
  266.         if (\is_array($_GET) && !empty($_GET)) {
  267.             foreach ($_GET as $key => $value) {
  268.                 // Ignore the language parameter
  269.                 if ('language' === $key && $GLOBALS['TL_CONFIG']['addLanguageToUrl']) {
  270.                     continue;
  271.                 }
  272.                 $arrGet[] = $key '=' $value;
  273.             }
  274.         }
  275.         /** @var PageModel $objPage */
  276.         global $objPage;
  277.         return $objPage->getFrontendUrl($strParams) . (!empty($arrGet) ? ('?' implode('&'$arrGet)) : '');
  278.     }
  279. }