Example #1
0
 /**
  * Returns an array of orderings created from a given demand object.
  *
  * @param DemandInterface $demand
  * @return array<\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface>
  */
 protected function createOrderingsFromDemand(DemandInterface $demand)
 {
     $orderings = [];
     if (Validation::isValidOrdering($demand->getOrder(), $demand->getOrderByAllowed())) {
         $orderList = GeneralUtility::trimExplode(',', $demand->getOrder(), true);
         if (!empty($orderList)) {
             // go through every order statement
             foreach ($orderList as $orderItem) {
                 list($orderField, $ascDesc) = GeneralUtility::trimExplode(' ', $orderItem, true);
                 // count == 1 means that no direction is given
                 if ($ascDesc) {
                     $orderings[$orderField] = strtolower($ascDesc) == 'desc' ? QueryInterface::ORDER_DESCENDING : QueryInterface::ORDER_ASCENDING;
                 } else {
                     $orderings[$orderField] = QueryInterface::ORDER_ASCENDING;
                 }
             }
         }
     }
     return $orderings;
 }
Example #2
0
 /**
  * Get the search constraints
  *
  * @param QueryInterface $query
  * @param DemandInterface $demand
  * @return array
  * @throws \UnexpectedValueException
  */
 protected function getSearchConstraints(QueryInterface $query, DemandInterface $demand)
 {
     $constraints = [];
     if ($demand->getSearch() === null) {
         return $constraints;
     }
     /* @var $searchObject \GeorgRinger\News\Domain\Model\Dto\Search */
     $searchObject = $demand->getSearch();
     $searchSubject = $searchObject->getSubject();
     if (!empty($searchSubject)) {
         $searchFields = GeneralUtility::trimExplode(',', $searchObject->getFields(), true);
         $searchConstraints = [];
         if (count($searchFields) === 0) {
             throw new \UnexpectedValueException('No search fields defined', 1318497755);
         }
         foreach ($searchFields as $field) {
             if (!empty($searchSubject)) {
                 $searchConstraints[] = $query->like($field, '%' . $searchSubject . '%');
             }
         }
         if (count($searchConstraints)) {
             $constraints[] = $query->logicalOr($searchConstraints);
         }
     }
     $minimumDate = strtotime($searchObject->getMinimumDate());
     if ($minimumDate) {
         $field = $searchObject->getDateField();
         if (empty($field)) {
             throw new \UnexpectedValueException('No date field is defined', 1396348732);
         }
         $constraints[] = $query->greaterThanOrEqual($field, $minimumDate);
     }
     $maximumDate = strtotime($searchObject->getMaximumDate());
     if ($maximumDate) {
         $field = $searchObject->getDateField();
         if (empty($field)) {
             throw new \UnexpectedValueException('No date field is defined', 1396348733);
         }
         $constraints[] = $query->lessThanOrEqual($field, $maximumDate);
     }
     return $constraints;
 }
 /**
  * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
  * @param \GeorgRinger\News\Domain\Model\DemandInterface $demand
  * @return array
  * @throws InvalidArgumentException
  * @throws Exception
  */
 protected function createConstraintsFromDemand(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, \GeorgRinger\News\Domain\Model\DemandInterface $demand)
 {
     $constraints = array();
     if ($demand->getCategories() && $demand->getCategories() !== '0') {
         $constraints[] = $this->createCategoryConstraint($query, $demand->getCategories(), $demand->getCategoryConjunction(), $demand->getIncludeSubCategories());
     }
     if ($demand->getAuthor()) {
         $constraints[] = $query->equals('author', $demand->getAuthor());
     }
     // archived
     if ($demand->getArchiveRestriction() == 'archived') {
         $constraints[] = $query->logicalNot($this->createIsActiveConstraint($query));
         // non-archived (active)
     } elseif ($demand->getArchiveRestriction() == 'active') {
         $constraints[] = $this->createIsActiveConstraint($query);
     }
     // 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[] = $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[] = $query->lessThanOrEqual($timeRestrictionField, $timeLimit);
     }
     // top news
     if ($demand->getTopNewsRestriction() == 1) {
         $constraints[] = $query->equals('istopnews', 1);
     } elseif ($demand->getTopNewsRestriction() == 2) {
         $constraints[] = $query->equals('istopnews', 0);
     }
     // storage page
     if ($demand->getStoragePage() != 0) {
         $pidList = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $demand->getStoragePage(), true);
         $constraints[] = $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[] = $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[] = $query->contains('tags', $singleTag);
         }
     }
     // Search
     $searchConstraints = $this->getSearchConstraints($query, $demand);
     if (!empty($searchConstraints)) {
         $constraints[] = $query->logicalAnd($searchConstraints);
     }
     // Exclude already displayed
     if ($demand->getExcludeAlreadyDisplayedNews() && isset($GLOBALS['EXT']['news']['alreadyDisplayed']) && !empty($GLOBALS['EXT']['news']['alreadyDisplayed'])) {
         $constraints[] = $query->logicalNot($query->in('uid', $GLOBALS['EXT']['news']['alreadyDisplayed']));
     }
     // events only
     $constraints[] = $query->logicalAnd($query->equals('tx_roqnewsevent_is_event', 1));
     // the event must have an event start date
     $constraints[] = $query->logicalAnd($query->logicalNot($query->equals('tx_roqnewsevent_startdate', 0)));
     // Clean not used constraints
     foreach ($constraints as $key => $value) {
         if (is_null($value)) {
             unset($constraints[$key]);
         }
     }
     return $constraints;
 }