/** * Parse news categories insert tags * * @param string $tag * * @return string|bool */ public function parseCategoriesTags($tag) { $chunks = trimsplit('::', $tag); if ($chunks[0] === 'news_categories') { $className = \NewsCategories\NewsCategories::getModelClass(); $param = NewsCategories::getParameterName(); if (($newsModel = $className::findPublishedByIdOrAlias(\Input::get($param))) !== null) { return $newsModel->{$chunks[1]}; } } return false; }
/** * Add the categories to the template * @param object * @param array */ public function addCategoriesToTemplate($objTemplate, $arrData) { if (isset($arrData['categories'])) { $arrCategories = array(); $arrCategoriesList = array(); $categories = deserialize($arrData['categories']); if (is_array($categories) && !empty($categories)) { $strClass = \NewsCategories\NewsCategories::getModelClass(); $objCategories = $strClass::findPublishedByIds($categories); // Add the categories to template if ($objCategories !== null) { /** @var NewsCategoryModel $objCategory */ foreach ($objCategories as $objCategory) { $strName = $objCategory->frontendTitle ? $objCategory->frontendTitle : $objCategory->title; $arrCategories[$objCategory->id] = $objCategory->row(); $arrCategories[$objCategory->id]['name'] = $strName; $arrCategories[$objCategory->id]['class'] = 'category_' . $objCategory->id . ($objCategory->cssClass ? ' ' . $objCategory->cssClass : ''); $arrCategories[$objCategory->id]['linkTitle'] = specialchars($strName); $arrCategories[$objCategory->id]['href'] = ''; $arrCategories[$objCategory->id]['hrefWithParam'] = ''; $arrCategories[$objCategory->id]['targetPage'] = null; // Add the target page if (($targetPage = $objCategory->getTargetPage()) !== null) { $arrCategories[$objCategory->id]['href'] = $targetPage->getFrontendUrl(); $arrCategories[$objCategory->id]['hrefWithParam'] = $targetPage->getFrontendUrl('/' . NewsCategories::getParameterName() . '/' . $objCategory->alias); $arrCategories[$objCategory->id]['targetPage'] = $targetPage; } // Register a function to generate category URL manually $arrCategories[$objCategory->id]['getUrl'] = function (\PageModel $page) use($objCategory) { return $objCategory->getUrl($page); }; // Generate categories list $arrCategoriesList[$objCategory->id] = $strName; } // Sort the category list alphabetically asort($arrCategoriesList); // Sort the categories alphabetically uasort($arrCategories, function ($a, $b) { return strnatcasecmp($a['name'], $b['name']); }); } } $objTemplate->categories = $arrCategories; $objTemplate->categoriesList = $arrCategoriesList; } }
/** * Filter the news by categories * @param array * @return array */ protected static function filterByCategories($arrColumns) { $t = static::$strTable; // Use the default filter if (is_array($GLOBALS['NEWS_FILTER_DEFAULT']) && !empty($GLOBALS['NEWS_FILTER_DEFAULT'])) { $arrCategories = static::getCategoriesCache(); if (!empty($arrCategories)) { $arrIds = array(); // Get the news IDs for particular categories foreach ($GLOBALS['NEWS_FILTER_DEFAULT'] as $category) { if (isset($arrCategories[$category])) { $arrIds = array_merge($arrCategories[$category], $arrIds); } } $strKey = 'category'; // Preserve the default category if ($GLOBALS['NEWS_FILTER_PRESERVE']) { $strKey = 'category_default'; } $arrColumns[$strKey] = "{$t}.id IN (" . implode(',', empty($arrIds) ? array(0) : array_unique($arrIds)) . ")"; } } // Exclude particular news items if (is_array($GLOBALS['NEWS_FILTER_EXCLUDE']) && !empty($GLOBALS['NEWS_FILTER_EXCLUDE'])) { $arrColumns[] = "{$t}.id NOT IN (" . implode(',', array_map('intval', $GLOBALS['NEWS_FILTER_EXCLUDE'])) . ")"; } $strParam = NewsCategories::getParameterName(); // Try to find by category if ($GLOBALS['NEWS_FILTER_CATEGORIES'] && \Input::get($strParam)) { $strClass = \NewsCategories\NewsCategories::getModelClass(); $objCategory = $strClass::findPublishedByIdOrAlias(\Input::get($strParam)); if ($objCategory === null) { return null; } $arrCategories = static::getCategoriesCache(); $arrColumns['category'] = "{$t}.id IN (" . implode(',', empty($arrCategories[$objCategory->id]) ? array(0) : $arrCategories[$objCategory->id]) . ")"; } return $arrColumns; }
/** * Get the filtered news IDs * * @return array */ protected function getFilteredNewsIds() { $arrCategories = \NewsModel::getCategoriesCache(); if (empty($arrCategories)) { return array(); } $arrIds = array(); // Use the default filter if (is_array($this->news_filterDefault) && !empty($this->news_filterDefault)) { foreach ($this->news_filterDefault as $category) { if (isset($arrCategories[$category])) { $arrIds = array_merge($arrCategories[$category], $arrIds); } } } $strParam = NewsCategories::getParameterName(); // Current category if ($this->news_filterCategories && \Input::get($strParam)) { $strClass = \NewsCategories\NewsCategories::getModelClass(); $objCategory = $strClass::findPublishedByIdOrAlias(\Input::get($strParam)); if ($objCategory === null) { return array(); } // Preserve the default filter if ($this->news_filterPreserve) { $arrIds = array_merge($arrCategories[$objCategory->id], $arrIds); } else { $arrIds = $arrCategories[$objCategory->id]; } } return array_unique($arrIds); }
/** * Recursively compile the news categories and return it as HTML string * @param integer * @param integer * @return string */ protected function renderNewsCategories($intPid, $arrIds, $strUrl, $intLevel = 1) { $strClass = \NewsCategories\NewsCategories::getModelClass(); $objCategories = $strClass::findPublishedByPidAndIds($intPid, $arrIds); if ($objCategories === null) { return ''; } $strParam = NewsCategories::getParameterName(); $arrCategories = array(); // Layout template fallback if ($this->navigationTpl == '') { $this->navigationTpl = 'nav_newscategories'; } $objTemplate = new \FrontendTemplate($this->navigationTpl); $objTemplate->type = get_class($this); $objTemplate->cssID = $this->cssID; $objTemplate->level = 'level_' . $intLevel; $objTemplate->showQuantity = $this->news_showQuantity; $count = 0; $total = $objCategories->count(); // Add the "reset categories" link if ($this->news_resetCategories && $intLevel == 1) { $intNewsQuantity = 0; // Get the news quantity if ($this->news_showQuantity) { $intNewsQuantity = \NewsModel::countPublishedByCategoryAndPids($this->news_archives); } $blnActive = \Input::get($strParam) ? false : true; $arrCategories[] = array('isActive' => $blnActive, 'subitems' => '', 'class' => 'reset first' . ($total == 1 ? ' last' : '') . ' even' . ($blnActive ? ' active' : ''), 'title' => specialchars($GLOBALS['TL_LANG']['MSC']['resetCategories'][1]), 'linkTitle' => specialchars($GLOBALS['TL_LANG']['MSC']['resetCategories'][1]), 'link' => $GLOBALS['TL_LANG']['MSC']['resetCategories'][0], 'href' => ampersand(str_replace('/' . $strParam . '/%s', '', $strUrl)), 'quantity' => $intNewsQuantity); $count = 1; $total++; } $intLevel++; // Render categories while ($objCategories->next()) { $strSubcategories = ''; // Get the subcategories if ($objCategories->subcategories) { $strSubcategories = $this->renderNewsCategories($objCategories->id, $arrIds, $strUrl, $intLevel); } $blnActive = $this->objActiveCategory !== null && $this->objActiveCategory->id == $objCategories->id; $strClass = 'news_category_' . $objCategories->id . ($objCategories->cssClass ? ' ' . $objCategories->cssClass : '') . (++$count == 1 ? ' first' : '') . ($count == $total ? ' last' : '') . ($count % 2 == 0 ? ' odd' : ' even') . ($blnActive ? ' active' : '') . ($strSubcategories != '' ? ' submenu' : '') . (in_array($objCategories->id, $this->arrCategoryTrail) ? ' trail' : ''); $strTitle = $objCategories->frontendTitle ?: $objCategories->title; $arrRow = $objCategories->row(); $arrRow['isActive'] = $blnActive; $arrRow['subitems'] = $strSubcategories; $arrRow['class'] = $strClass; $arrRow['title'] = specialchars($strTitle, true); $arrRow['linkTitle'] = specialchars($strTitle, true); $arrRow['link'] = $strTitle; $arrRow['href'] = ampersand(sprintf($strUrl, $GLOBALS['TL_CONFIG']['disableAlias'] ? $objCategories->id : $objCategories->alias)); $arrRow['quantity'] = 0; // Get the news quantity if ($this->news_showQuantity) { $arrRow['quantity'] = \NewsModel::countPublishedByCategoryAndPids($this->news_archives, $objCategories->id); } $arrCategories[] = $arrRow; } $objTemplate->items = $arrCategories; return !empty($arrCategories) ? $objTemplate->parse() : ''; }
/** * Get the category URL * * @param \PageModel $page * * @return string */ public function getUrl(\PageModel $page) { $page->loadDetails(); return $page->getFrontendUrl('/' . NewsCategories::getParameterName($page->rootId) . '/' . $this->alias); }