vendor/codefog/contao-news_categories/src/NewsCategoriesManager.php line 49

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;
  10. use Codefog\NewsCategoriesBundle\Model\NewsCategoryModel;
  11. use Contao\CoreBundle\Framework\FrameworkAwareInterface;
  12. use Contao\CoreBundle\Framework\FrameworkAwareTrait;
  13. use Contao\Database;
  14. use Contao\Module;
  15. use Contao\ModuleNewsArchive;
  16. use Contao\ModuleNewsList;
  17. use Contao\ModuleNewsReader;
  18. use Contao\PageModel;
  19. use Terminal42\DcMultilingualBundle\Model\Multilingual;
  20. class NewsCategoriesManager implements FrameworkAwareInterface
  21. {
  22.     use FrameworkAwareTrait;
  23.     /**
  24.      * @var array
  25.      */
  26.     private $urlCache = [];
  27.     /**
  28.      * Generate the category URL.
  29.      *
  30.      * @param NewsCategoryModel $category
  31.      * @param PageModel         $page
  32.      * @param bool              $absolute
  33.      *
  34.      * @return string
  35.      */
  36.     public function generateUrl(NewsCategoryModel $categoryPageModel $page$absolute false)
  37.     {
  38.         $page->loadDetails();
  39.         $cacheKey $page->id '-' . ($absolute 'abs' 'rel');
  40.         if (!isset($this->urlCache[$cacheKey])) {
  41.             $params '/%s/%s';
  42.             $this->urlCache[$cacheKey] = $absolute $page->getAbsoluteUrl($params) : $page->getFrontendUrl($params);
  43.         }
  44.         return sprintf($this->urlCache[$cacheKey], $this->getParameterName($page->rootId), $this->getCategoryAlias($category$page));
  45.     }
  46.     /**
  47.      * Get the image.
  48.      *
  49.      * @param NewsCategoryModel $category
  50.      *
  51.      * @return \Contao\FilesModel|null
  52.      */
  53.     public function getImage(NewsCategoryModel $category)
  54.     {
  55.         if (null === ($image $category->getImage()) || !\is_file(TL_ROOT.'/'.$image->path)) {
  56.             return null;
  57.         }
  58.         return $image;
  59.     }
  60.     /**
  61.      * Get the category alias
  62.      *
  63.      * @param NewsCategoryModel $category
  64.      * @param PageModel         $page
  65.      *
  66.      * @return string
  67.      */
  68.     public function getCategoryAlias(NewsCategoryModel $categoryPageModel $page)
  69.     {
  70.         if ($category instanceof Multilingual) {
  71.             return $category->getAlias($page->language);
  72.         }
  73.         return $category->alias;
  74.     }
  75.     /**
  76.      * Get the parameter name.
  77.      *
  78.      * @param int|null $rootId
  79.      *
  80.      * @return string
  81.      */
  82.     public function getParameterName($rootId null)
  83.     {
  84.         $rootId $rootId ?: $GLOBALS['objPage']->rootId;
  85.         if (!$rootId || null === ($rootPage PageModel::findByPk($rootId))) {
  86.             return '';
  87.         }
  88.         return $rootPage->newsCategories_param ?: 'category';
  89.     }
  90.     /**
  91.      * Get the category target page.
  92.      *
  93.      * @param NewsCategoryModel $category
  94.      *
  95.      * @return PageModel|null
  96.      */
  97.     public function getTargetPage(NewsCategoryModel $category)
  98.     {
  99.         $pageId $category->jumpTo;
  100.         // Inherit the page from parent if there is none set
  101.         if (!$pageId) {
  102.             $pid $category->pid;
  103.             do {
  104.                 /** @var NewsCategoryModel $parent */
  105.                 $parent $category->findByPk($pid);
  106.                 if (null !== $parent) {
  107.                     $pid $parent->pid;
  108.                     $pageId $parent->jumpTo;
  109.                 }
  110.             } while ($pid && !$pageId);
  111.         }
  112.         // Get the page model
  113.         if ($pageId) {
  114.             static $pageCache = [];
  115.             if (!isset($pageCache[$pageId])) {
  116.                 /** @var PageModel $pageAdapter */
  117.                 $pageAdapter $this->framework->getAdapter(PageModel::class);
  118.                 $pageCache[$pageId] = $pageAdapter->findPublishedById($pageId);
  119.             }
  120.             return $pageCache[$pageId];
  121.         }
  122.         return null;
  123.     }
  124.     /**
  125.      * Get the category trail IDs.
  126.      *
  127.      * @param NewsCategoryModel $category
  128.      *
  129.      * @return array
  130.      */
  131.     public function getTrailIds(NewsCategoryModel $category)
  132.     {
  133.         static $cache;
  134.         if (!isset($cache[$category->id])) {
  135.             /** @var Database $db */
  136.             $db $this->framework->createInstance(Database::class);
  137.             $ids $db->getParentRecords($category->id$category->getTable());
  138.             $ids = \array_map('intval', \array_unique($ids));
  139.             // Remove the current category
  140.             unset($ids[\array_search($category->id$idstrue)]);
  141.             $cache[$category->id] = $ids;
  142.         }
  143.         return $cache[$category->id];
  144.     }
  145.     /**
  146.      * Return true if the category is visible for module.
  147.      *
  148.      * @param NewsCategoryModel $category
  149.      * @param Module            $module
  150.      *
  151.      * @return bool
  152.      */
  153.     public function isVisibleForModule(NewsCategoryModel $categoryModule $module)
  154.     {
  155.         // List or archive module
  156.         if ($category->hideInList && ($module instanceof ModuleNewsList || $module instanceof ModuleNewsArchive)) {
  157.             return false;
  158.         }
  159.         // Reader module
  160.         if ($category->hideInReader && $module instanceof ModuleNewsReader) {
  161.             return false;
  162.         }
  163.         return true;
  164.     }
  165. }