示例#1
0
文件: Content.php 项目: thelia/thelia
 public function buildModelCriteria()
 {
     $search = ContentQuery::create();
     /* manage translations */
     $this->configureI18nProcessing($search, array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM', 'META_TITLE', 'META_DESCRIPTION', 'META_KEYWORDS'));
     $id = $this->getId();
     if (!is_null($id)) {
         $search->filterById($id, Criteria::IN);
     }
     $manualOrderAllowed = false;
     if (null !== ($folderDefault = $this->getFolderDefault())) {
         // Select the contents which have $folderDefault as the default folder.
         $search->useContentFolderQuery('FolderSelect')->filterByDefaultFolder(true)->filterByFolderId($folderDefault, Criteria::IN)->endUse();
         // We can only sort by position if we have a single folder ID
         $manualOrderAllowed = 1 == count($folderDefault);
     } elseif (null !== ($folderIdList = $this->getFolder())) {
         // Select all content which have one of the required folders as the default one, or an associated one
         $depth = $this->getDepth();
         $allFolderIDs = FolderQuery::getFolderTreeIds($folderIdList, $depth);
         $search->useContentFolderQuery('FolderSelect')->filterByFolderId($allFolderIDs, Criteria::IN)->endUse();
         // We can only sort by position if we have a single folder ID, with a depth of 1
         $manualOrderAllowed = 1 == $depth && 1 == count($folderIdList);
     } else {
         $search->leftJoinContentFolder('FolderSelect')->addJoinCondition('FolderSelect', '`FolderSelect`.DEFAULT_FOLDER = 1');
     }
     $search->withColumn('CAST(CASE WHEN ISNULL(`FolderSelect`.POSITION) THEN \'' . PHP_INT_MAX . '\' ELSE `FolderSelect`.POSITION END AS SIGNED)', 'position_delegate');
     $search->withColumn('`FolderSelect`.FOLDER_ID', 'default_folder_id');
     $search->withColumn('`FolderSelect`.DEFAULT_FOLDER', 'is_default_folder');
     $current = $this->getCurrent();
     if ($current === true) {
         $search->filterById($this->getCurrentRequest()->get("content_id"));
     } elseif ($current === false) {
         $search->filterById($this->getCurrentRequest()->get("content_id"), Criteria::NOT_IN);
     }
     $current_folder = $this->getCurrentFolder();
     if ($current_folder === true) {
         $current = ContentQuery::create()->findPk($this->getCurrentRequest()->get("content_id"));
         $search->filterByFolder($current->getFolders(), Criteria::IN);
     } elseif ($current_folder === false) {
         $current = ContentQuery::create()->findPk($this->getCurrentRequest()->get("content_id"));
         $search->filterByFolder($current->getFolders(), Criteria::NOT_IN);
     }
     $visible = $this->getVisible();
     if ($visible !== BooleanOrBothType::ANY) {
         $search->filterByVisible($visible ? 1 : 0);
     }
     $title = $this->getTitle();
     if (!is_null($title)) {
         $this->addSearchInI18nColumn($search, 'TITLE', Criteria::LIKE, "%" . $title . "%");
     }
     $exclude = $this->getExclude();
     if (!is_null($exclude)) {
         $search->filterById($exclude, Criteria::NOT_IN);
     }
     $exclude_folder = $this->getExcludeFolder();
     if (!is_null($exclude_folder)) {
         $search->filterByFolder(FolderQuery::create()->filterById($exclude_folder, Criteria::IN)->find(), Criteria::NOT_IN);
     }
     $orders = $this->getOrder();
     foreach ($orders as $order) {
         switch ($order) {
             case "alpha":
                 $search->addAscendingOrderByColumn('i18n_TITLE');
                 break;
             case "alpha-reverse":
                 $search->addDescendingOrderByColumn('i18n_TITLE');
                 break;
             case "manual":
                 if (!$manualOrderAllowed) {
                     throw new \InvalidArgumentException('Manual order cannot be set without single folder argument');
                 }
                 $search->addAscendingOrderByColumn('position_delegate');
                 break;
             case "manual_reverse":
                 if (!$manualOrderAllowed) {
                     throw new \InvalidArgumentException('Manual order cannot be set without single folder argument');
                 }
                 $search->addDescendingOrderByColumn('position_delegate');
                 break;
             case "given_id":
                 if (null === $id) {
                     throw new \InvalidArgumentException('Given_id order cannot be set without `id` argument');
                 }
                 foreach ($id as $singleId) {
                     $givenIdMatched = 'given_id_matched_' . $singleId;
                     $search->withColumn(ContentTableMap::ID . "='{$singleId}'", $givenIdMatched);
                     $search->orderBy($givenIdMatched, Criteria::DESC);
                 }
                 break;
             case "random":
                 $search->clearOrderByColumns();
                 $search->addAscendingOrderByColumn('RAND()');
                 break 2;
             case "created":
                 $search->addAscendingOrderByColumn('created_at');
                 break;
             case "created_reverse":
                 $search->addDescendingOrderByColumn('created_at');
                 break;
             case "updated":
                 $search->addAscendingOrderByColumn('updated_at');
                 break;
             case "updated_reverse":
                 $search->addDescendingOrderByColumn('updated_at');
                 break;
             case "position":
                 $search->addAscendingOrderByColumn('position_delegate');
                 break;
             case "position_reverse":
                 $search->addDescendingOrderByColumn('position_delegate');
                 break;
         }
     }
     $search->groupById();
     return $search;
 }