/** * 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)) . ")"; } } // Try to find by category if ($GLOBALS['NEWS_FILTER_CATEGORIES'] && \Input::get('category')) { $strClass = \NewsCategories\NewsCategories::getModelClass(); $objCategory = $strClass::findPublishedByIdOrAlias(\Input::get('category')); 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; }
/** * 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; } }
/** * 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() : ''; }
*/ \System::loadLanguageFile('tl_news_archive'); /** * Table tl_news_category */ $GLOBALS['TL_DCA']['tl_news_category'] = array('config' => array('label' => $GLOBALS['TL_LANG']['tl_news_archive']['categories'][0], 'dataContainer' => 'Table', 'enableVersioning' => true, 'onload_callback' => array(array('tl_news_category', 'checkPermission')), 'sql' => array('keys' => array('id' => 'primary', 'pid' => 'index', 'alias' => 'index')), 'backlink' => 'do=news'), 'list' => array('sorting' => array('mode' => 5, 'icon' => 'system/modules/news_categories/assets/icon.png', 'paste_button_callback' => array('tl_news_category', 'pasteCategory'), 'panelLayout' => 'search'), 'label' => array('fields' => array('title', 'frontendTitle'), 'format' => '%s <span style="padding-left:3px;color:#b3b3b3;">[%s]</span>', 'label_callback' => array('tl_news_category', 'generateLabel')), 'global_operations' => array('toggleNodes' => array('label' => &$GLOBALS['TL_LANG']['MSC']['toggleAll'], 'href' => 'ptg=all', 'class' => 'header_toggle'), 'all' => array('label' => &$GLOBALS['TL_LANG']['MSC']['all'], 'href' => 'act=select', 'class' => 'header_edit_all', 'attributes' => 'onclick="Backend.getScrollOffset()" accesskey="e"')), 'operations' => array('edit' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['edit'], 'href' => 'act=edit', 'icon' => 'edit.gif'), 'copy' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['copy'], 'href' => 'act=paste&mode=copy', 'icon' => 'copy.gif', 'attributes' => 'onclick="Backend.getScrollOffset()"'), 'copyChilds' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['copyChilds'], 'href' => 'act=paste&mode=copy&childs=1', 'icon' => 'copychilds.gif', 'attributes' => 'onclick="Backend.getScrollOffset()"'), 'cut' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['cut'], 'href' => 'act=paste&mode=cut', 'icon' => 'cut.gif', 'attributes' => 'onclick="Backend.getScrollOffset()"'), 'delete' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['delete'], 'href' => 'act=delete', 'icon' => 'delete.gif', 'attributes' => 'onclick="if(!confirm(\'' . $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] . '\'))return false;Backend.getScrollOffset()"'), 'show' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['show'], 'href' => 'act=show', 'icon' => 'show.gif'), 'toggle' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['toggle'], 'icon' => 'visible.gif', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', 'button_callback' => array('tl_news_category', 'toggleIcon')))), 'palettes' => array('default' => '{title_legend},title,alias,frontendTitle,cssClass;{redirect_legend:hide},jumpTo;{publish_legend},published'), 'fields' => array('id' => array('sql' => "int(10) unsigned NOT NULL auto_increment"), 'pid' => array('sql' => "int(10) unsigned NOT NULL default '0'"), 'sorting' => array('sql' => "int(10) unsigned NOT NULL default '0'"), 'tstamp' => array('sql' => "int(10) unsigned NOT NULL default '0'"), 'title' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['title'], 'exclude' => true, 'search' => true, 'inputType' => 'text', 'eval' => array('mandatory' => true, 'maxlength' => 255, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'frontendTitle' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['frontendTitle'], 'exclude' => true, 'search' => true, 'inputType' => 'text', 'eval' => array('maxlength' => 255, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'alias' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['alias'], 'exclude' => true, 'search' => true, 'inputType' => 'text', 'eval' => array('rgxp' => 'alias', 'unique' => true, 'spaceToUnderscore' => true, 'maxlength' => 128, 'tl_class' => 'w50'), 'save_callback' => array(array('tl_news_category', 'generateAlias')), 'sql' => "varbinary(128) NOT NULL default ''"), 'cssClass' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['cssClass'], 'exclude' => true, 'inputType' => 'text', 'eval' => array('maxlength' => 128, 'tl_class' => 'w50'), 'sql' => "varchar(128) NOT NULL default ''"), 'jumpTo' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['jumpTo'], 'exclude' => true, 'inputType' => 'pageTree', 'eval' => array('fieldType' => 'radio'), 'sql' => "int(10) unsigned NOT NULL default '0'", 'relation' => array('type' => 'hasOne', 'load' => 'eager', 'table' => 'tl_page')), 'published' => array('label' => &$GLOBALS['TL_LANG']['tl_news_category']['published'], 'exclude' => true, 'inputType' => 'checkbox', 'sql' => "char(1) NOT NULL default ''"))); /** * Enable multilingual features */ if (\NewsCategories\NewsCategories::checkMultilingual()) { // Config $GLOBALS['TL_DCA']['tl_news_category']['config']['dataContainer'] = 'Multilingual'; $GLOBALS['TL_DCA']['tl_news_category']['config']['language'] = \NewsCategories\NewsCategories::getAvailableLanguages(); $GLOBALS['TL_DCA']['tl_news_category']['config']['langColumn'] = 'language'; $GLOBALS['TL_DCA']['tl_news_category']['config']['langPid'] = 'lid'; $GLOBALS['TL_DCA']['tl_news_category']['config']['fallbackLang'] = \NewsCategories\NewsCategories::getFallbackLanguage(); $GLOBALS['TL_DCA']['tl_news_category']['config']['sql']['keys']['language'] = 'index'; $GLOBALS['TL_DCA']['tl_news_category']['config']['sql']['keys']['lid'] = 'index'; // Fields $GLOBALS['TL_DCA']['tl_news_category']['fields']['language']['sql'] = "varchar(2) NOT NULL default ''"; $GLOBALS['TL_DCA']['tl_news_category']['fields']['lid']['sql'] = "int(10) unsigned NOT NULL default '0'"; $GLOBALS['TL_DCA']['tl_news_category']['fields']['title']['eval']['translatableFor'] = '*'; $GLOBALS['TL_DCA']['tl_news_category']['fields']['frontendTitle']['eval']['translatableFor'] = '*'; } class tl_news_category extends Backend { /** * Check permissions */ public function checkPermission() {