public function setDefaultCategories(array $defaultCategories, $includeSubcategories = true, $order = null)
    {
        $defaultCategories = $this->parseIds($defaultCategories);

        if (0 === \count($defaultCategories)) {
            throw new NoNewsException();
        }

        // Include the subcategories
        if ($includeSubcategories) {
            /** @var NewsCategoryModel $newsCategoryModel */
            $newsCategoryModel = $this->framework->getAdapter(NewsCategoryModel::class);
            $defaultCategories = $newsCategoryModel->getAllSubcategoriesIds($defaultCategories);
        }

        /** @var Model $model */
        $model = $this->framework->getAdapter(Model::class);

        $newsIds = $model->getReferenceValues('tl_news', 'categories', $defaultCategories);
        $newsIds = $this->parseIds($newsIds);

        if (0 === \count($newsIds)) {
            throw new NoNewsException();
        }

        $t = $this->getNewsModelAdapter()->getTable();

        $this->columns['defaultCategories'] = "$t.id IN(".\implode(',', $newsIds).')';

        // Order news items by best match
        if ($order === 'best_match') {
            $mapper = [];

            // Build the mapper
            foreach (array_unique($newsIds) as $newsId) {
                $mapper[$newsId] = count(array_intersect($defaultCategories, array_unique($model->getRelatedValues($t, 'categories', $newsId))));
            }

            arsort($mapper);

            $this->options['order'] = Database::getInstance()->findInSet("$t.id", array_keys($mapper));
        }
    }
public function setDefaultCategories(array $defaultCategories, $includeSubcategories = true, $order = null)
    {
        $defaultCategories = $this->parseIds($defaultCategories);

        if (0 === \count($defaultCategories)) {
            throw new NoNewsException();
        }

        // Include the subcategories
        if ($includeSubcategories) {
            /** @var NewsCategoryModel $newsCategoryModel */
            $newsCategoryModel = $this->framework->getAdapter(NewsCategoryModel::class);
            $defaultCategories = $newsCategoryModel->getAllSubcategoriesIds($defaultCategories);
        }

        /** @var Model $model */
        $model = $this->framework->getAdapter(Model::class);

        $newsIds = $model->getReferenceValues('tl_news', 'categories', $defaultCategories);
        $newsIds = $this->parseIds($newsIds);

        if (0 === \count($newsIds)) {
            throw new NoNewsException();
        }

        $t = $this->getNewsModelAdapter()->getTable();

        $this->columns['defaultCategories'] = "$t.id IN(".\implode(',', $newsIds).')';

        // Order news items by best match
        if ($order === 'best_match') {
            $mapper = [];

            // Build the mapper
            foreach (array_unique($newsIds) as $newsId) {
                $mapper[$newsId] = count(array_intersect($defaultCategories, array_unique($model->getRelatedValues($t, 'categories', $newsId))));
            }

            arsort($mapper);

            $this->options['order'] = Database::getInstance()->findInSet("$t.id", array_keys($mapper));
        }
    }
public function setDefaultCategories ( array $ defaultCategories , $ includeSubcategories = true , $ order = null ) { $ defaultCategories = $ this -> parseIds ( $ defaultCategories ) ; if ( 0 === \ count ( $ defaultCategories ) ) { throw new NoNewsException ( ) ; } // Include the subcategories if ( $ includeSubcategories ) { /** @var NewsCategoryModel $newsCategoryModel */ $ newsCategoryModel = $ this -> framework -> getAdapter ( NewsCategoryModel :: class ) ; $ defaultCategories = $ newsCategoryModel -> getAllSubcategoriesIds ( $ defaultCategories ) ; } /** @var Model $model */ $ model = $ this -> framework -> getAdapter ( Model :: class ) ; $ newsIds = $ model -> getReferenceValues ( 'tl_news' , 'categories' , $ defaultCategories ) ; $ newsIds = $ this -> parseIds ( $ newsIds ) ; if ( 0 === \ count ( $ newsIds ) ) { throw new NoNewsException ( ) ; } $ t = $ this -> getNewsModelAdapter ( ) -> getTable ( ) ; $ this -> columns [ 'defaultCategories' ] = "$t.id IN(" . \ implode ( ',' , $ newsIds ) . ')' ; // Order news items by best match if ( $ order === 'best_match' ) { $ mapper = [ ] ; // Build the mapper foreach ( array_unique ( $ newsIds ) as $ newsId ) { $ mapper [ $ newsId ] = count ( array_intersect ( $ defaultCategories , array_unique ( $ model -> getRelatedValues ( $ t , 'categories' , $ newsId ) ) ) ) ; } arsort ( $ mapper ) ; $ this -> options [ 'order' ] = Database :: getInstance ( ) -> findInSet ( "$t.id" , array_keys ( $ mapper ) ) ; } }
Set the default categories.

@param array       $defaultCategories
@param bool        $includeSubcategories
@param string|null $order

@throws NoNewsException
Set the default categories .