コード例 #1
0
ファイル: NewsRepository.php プロジェクト: mrmoree/vkmh_typo3
 /**
  * Returns an array of constraints created from a given demand object.
  *
  * @param QueryInterface $query
  * @param DemandInterface $demand
  * @throws \UnexpectedValueException
  * @throws \InvalidArgumentException
  * @throws \Exception
  * @return array<\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface>
  */
 protected function createConstraintsFromDemand(QueryInterface $query, DemandInterface $demand)
 {
     $constraints = [];
     if ($demand->getCategories() && $demand->getCategories() !== '0') {
         $constraints['categories'] = $this->createCategoryConstraint($query, $demand->getCategories(), $demand->getCategoryConjunction(), $demand->getIncludeSubCategories());
     }
     if ($demand->getAuthor()) {
         $constraints['author'] = $query->equals('author', $demand->getAuthor());
     }
     // archived
     if ($demand->getArchiveRestriction() == 'archived') {
         $constraints['archived'] = $query->logicalAnd($query->lessThan('archive', $GLOBALS['EXEC_TIME']), $query->greaterThan('archive', 0));
     } elseif ($demand->getArchiveRestriction() == 'active') {
         $constraints['active'] = $query->logicalOr($query->greaterThanOrEqual('archive', $GLOBALS['EXEC_TIME']), $query->equals('archive', 0));
     }
     // Time restriction greater than or equal
     $timeRestrictionField = $demand->getDateField();
     $timeRestrictionField = empty($timeRestrictionField) ? 'datetime' : $timeRestrictionField;
     if ($demand->getTimeRestriction()) {
         $timeLimit = 0;
         // integer = timestamp
         if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($demand->getTimeRestriction())) {
             $timeLimit = $GLOBALS['EXEC_TIME'] - $demand->getTimeRestriction();
         } else {
             // try to check strtotime
             $timeFromString = strtotime($demand->getTimeRestriction());
             if ($timeFromString) {
                 $timeLimit = $timeFromString;
             } else {
                 throw new \Exception('Time limit Low could not be resolved to an integer. Given was: ' . htmlspecialchars($timeLimit));
             }
         }
         $constraints['timeRestrictionGreater'] = $query->greaterThanOrEqual($timeRestrictionField, $timeLimit);
     }
     // Time restriction less than or equal
     if ($demand->getTimeRestrictionHigh()) {
         $timeLimit = 0;
         // integer = timestamp
         if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($demand->getTimeRestrictionHigh())) {
             $timeLimit = $GLOBALS['EXEC_TIME'] + $demand->getTimeRestrictionHigh();
         } else {
             // try to check strtotime
             $timeFromString = strtotime($demand->getTimeRestrictionHigh());
             if ($timeFromString) {
                 $timeLimit = $timeFromString;
             } else {
                 throw new \Exception('Time limit High could not be resolved to an integer. Given was: ' . htmlspecialchars($timeLimit));
             }
         }
         $constraints['timeRestrictionLess'] = $query->lessThanOrEqual($timeRestrictionField, $timeLimit);
     }
     // top news
     if ($demand->getTopNewsRestriction() == 1) {
         $constraints['topNews1'] = $query->equals('istopnews', 1);
     } elseif ($demand->getTopNewsRestriction() == 2) {
         $constraints['topNews2'] = $query->equals('istopnews', 0);
     }
     // storage page
     if ($demand->getStoragePage() != 0) {
         $pidList = GeneralUtility::intExplode(',', $demand->getStoragePage(), true);
         $constraints['pid'] = $query->in('pid', $pidList);
     }
     // month & year OR year only
     if ($demand->getYear() > 0) {
         if (is_null($demand->getDateField())) {
             throw new \InvalidArgumentException('No Datefield is set, therefore no Datemenu is possible!');
         }
         if ($demand->getMonth() > 0) {
             if ($demand->getDay() > 0) {
                 $begin = mktime(0, 0, 0, $demand->getMonth(), $demand->getDay(), $demand->getYear());
                 $end = mktime(23, 59, 59, $demand->getMonth(), $demand->getDay(), $demand->getYear());
             } else {
                 $begin = mktime(0, 0, 0, $demand->getMonth(), 1, $demand->getYear());
                 $end = mktime(23, 59, 59, $demand->getMonth() + 1, 0, $demand->getYear());
             }
         } else {
             $begin = mktime(0, 0, 0, 1, 1, $demand->getYear());
             $end = mktime(23, 59, 59, 12, 31, $demand->getYear());
         }
         $constraints['datetime'] = $query->logicalAnd($query->greaterThanOrEqual($demand->getDateField(), $begin), $query->lessThanOrEqual($demand->getDateField(), $end));
     }
     // Tags
     $tags = $demand->getTags();
     if ($tags) {
         $tagList = explode(',', $tags);
         foreach ($tagList as $singleTag) {
             $constraints['tags'] = $query->contains('tags', $singleTag);
         }
     }
     // Search
     $searchConstraints = $this->getSearchConstraints($query, $demand);
     if (!empty($searchConstraints)) {
         $constraints['search'] = $query->logicalAnd($searchConstraints);
     }
     // Exclude already displayed
     if ($demand->getExcludeAlreadyDisplayedNews() && isset($GLOBALS['EXT']['news']['alreadyDisplayed']) && !empty($GLOBALS['EXT']['news']['alreadyDisplayed'])) {
         $constraints['excludeAlreadyDisplayedNews'] = $query->logicalNot($query->in('uid', $GLOBALS['EXT']['news']['alreadyDisplayed']));
     }
     // Clean not used constraints
     foreach ($constraints as $key => $value) {
         if (is_null($value)) {
             unset($constraints[$key]);
         }
     }
     return $constraints;
 }