vendor/codefog/contao-news_categories/src/FrontendModule/NewsCategoriesModule.php line 75

Open in your IDE?
  1. <?php
  2. /*
  3.  * News Categories bundle for Contao Open Source CMS.
  4.  *
  5.  * @copyright  Copyright (c) 2017, Codefog
  6.  * @author     Codefog <https://codefog.pl>
  7.  * @license    MIT
  8.  */
  9. namespace Codefog\NewsCategoriesBundle\FrontendModule;
  10. use Codefog\NewsCategoriesBundle\Model\NewsCategoryModel;
  11. use Contao\Database;
  12. use Contao\FrontendTemplate;
  13. use Contao\System;
  14. use Haste\Generator\RowClass;
  15. use Haste\Input\Input;
  16. class NewsCategoriesModule extends NewsModule
  17. {
  18.     /**
  19.      * Template.
  20.      *
  21.      * @var string
  22.      */
  23.     protected $strTemplate 'mod_newscategories';
  24.     /**
  25.      * Generate the module.
  26.      */
  27.     protected function compile()
  28.     {
  29.         $categories $this->getCategories();
  30.         // Return if no categories are found
  31.         if (null === $categories) {
  32.             $this->Template->categories '';
  33.             return;
  34.         }
  35.         $param System::getContainer()->get('codefog_news_categories.manager')->getParameterName();
  36.         // Get the active category
  37.         if (null !== ($activeCategory NewsCategoryModel::findPublishedByIdOrAlias(Input::get($param)))) {
  38.             $this->activeCategory $activeCategory;
  39.             // Add the canonical URL tag
  40.             if ($this->news_enableCanonicalUrls) {
  41.                 $GLOBALS['TL_HEAD'][] = \sprintf('<link rel="canonical" href="%s">'$GLOBALS['objPage']->getAbsoluteUrl());
  42.             }
  43.         }
  44.         $ids = [];
  45.         // Get the parent categories IDs
  46.         /** @var NewsCategoryModel $category */
  47.         foreach ($categories as $category) {
  48.             $ids = \array_merge($idsDatabase::getInstance()->getParentRecords($category->id$category->getTable()));
  49.         }
  50.         $this->Template->categories $this->renderNewsCategories((int) $this->news_categoriesRoot, \array_unique($ids));
  51.     }
  52.     /**
  53.      * Recursively compile the news categories and return it as HTML string.
  54.      *
  55.      * @param int   $pid
  56.      * @param array $ids
  57.      * @param int   $level
  58.      *
  59.      * @return string
  60.      */
  61.     protected function renderNewsCategories($pid, array $ids$level 1)
  62.     {
  63.         if (null === ($categories NewsCategoryModel::findPublishedByIds($ids$pid))) {
  64.             return '';
  65.         }
  66.         // Layout template fallback
  67.         if (!$this->navigationTpl) {
  68.             $this->navigationTpl 'nav_newscategories';
  69.         }
  70.         $template = new FrontendTemplate($this->navigationTpl);
  71.         $template->type = \get_class($this);
  72.         $template->cssID $this->cssID;
  73.         $template->level 'level_'.$level;
  74.         $template->showQuantity $this->news_showQuantity;
  75.         $items = [];
  76.         // Add the "reset categories" link
  77.         if ($this->news_resetCategories && === $level) {
  78.             $items[] = $this->generateItem(
  79.                 $this->getTargetPage()->getFrontendUrl(),
  80.                 $GLOBALS['TL_LANG']['MSC']['resetCategories'][0],
  81.                 $GLOBALS['TL_LANG']['MSC']['resetCategories'][1],
  82.                 'reset',
  83.                 === \count($this->currentNewsCategories) && null === $this->activeCategory
  84.             );
  85.         }
  86.         ++$level;
  87.         /** @var NewsCategoryModel $category */
  88.         foreach ($categories as $category) {
  89.             // Generate the category individual URL or the filter-link
  90.             if ($this->news_forceCategoryUrl && null !== ($targetPage $this->manager->getTargetPage($category))) {
  91.                 $url $targetPage->getFrontendUrl();
  92.             } else {
  93.                 $url $this->manager->generateUrl($category$this->getTargetPage());
  94.             }
  95.             $items[] = $this->generateItem(
  96.                 $url,
  97.                 $category->getTitle(),
  98.                 $category->getTitle(),
  99.                 $this->generateItemCssClass($category),
  100.                 null !== $this->activeCategory && (int) $this->activeCategory->id === (int) $category->id,
  101.                 (!$this->showLevel || $this->showLevel >= $level) ? $this->renderNewsCategories($category->id$ids$level) : '',
  102.                 $category
  103.             );
  104.         }
  105.         // Add first/last/even/odd classes
  106.         RowClass::withKey('class')->addFirstLast()->addEvenOdd()->applyTo($items);
  107.         $template->items $items;
  108.         return $template->parse();
  109.     }
  110. }