Esempio n. 1
0
 /**
  * Returns an array of constraints created from a given demand object.
  *
  * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
  * @param Tx_MooxNews_Domain_Model_DemandInterface $demand
  * @throws UnexpectedValueException
  * @throws InvalidArgumentException
  * @throws Exception
  * @return array<\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface>
  */
 protected function createConstraintsFromDemand(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, Tx_MooxNews_Domain_Model_DemandInterface $demand)
 {
     $constraints = array();
     if (is_array($demand->getTypes()) && count($demand->getTypes())) {
         $constraints[] = $query->in('type', $demand->getTypes());
     } elseif ($demand->getType() != "") {
         $constraints[] = $query->equals('type', (string) $demand->getType());
     }
     if ($demand->getIncludeOnlySelectedNewsInRss()) {
         $constraints[] = $query->equals('shareRss', 1);
     }
     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());
     }
     $listViewSearchFields = $GLOBALS['TCA']['tx_mooxnews_domain_model_news']['listViewSearchFields']['moox_news']['default'];
     if ($demand->getQuery()) {
         $listViewSearchFields = $GLOBALS['TCA']['tx_mooxnews_domain_model_news']['listViewSearchFields']['moox_news']['default'];
         if (is_array($demand->getTypes())) {
             $type = reset($demand->getTypes());
         }
         if ($type != "" && isset($GLOBALS['TCA']['tx_mooxnews_domain_model_news']['listViewSearchFields'][$type]['default']) && $GLOBALS['TCA']['tx_mooxnews_domain_model_news']['listViewSearchFields'][$type]['default'] != "") {
             $listViewSearchFields = $GLOBALS['TCA']['tx_mooxnews_domain_model_news']['listViewSearchFields'][$type]['default'];
         } else {
             $listViewSearchFields = $GLOBALS['TCA']['tx_mooxnews_domain_model_news']['listViewSearchFields']['moox_news']['default'];
         }
         if ($listViewSearchFields != "") {
             $listViewSearchFields = explode(",", $listViewSearchFields);
         } else {
             $listViewSearchFields = array("title");
         }
         $queryConstraints = array();
         foreach ($listViewSearchFields as $listViewSearchField) {
             $queryConstraints[] = $query->like($listViewSearchField, "%" . $demand->getQuery() . "%");
         }
         $constraints[] = $query->logicalOr($queryConstraints);
     }
     if ($demand->getChar()) {
         if ($demand->getChar() != "other") {
             $char = substr($demand->getChar(), 0, 1);
             if (in_array($char, $demand->getCharList())) {
                 $constraints[] = $query->like("title", $char . "%");
             }
         } else {
             $charConstraints = array();
             foreach ($demand->getCharList() as $char) {
                 if ($char != "all" && $char != "other") {
                     $charConstraints[] = $query->logicalNot($query->like("title", $char . "%"));
                 }
             }
             if (count($charConstraints)) {
                 $constraints[] = $query->logicalAnd($charConstraints);
             }
         }
     }
     // archived
     if ($demand->getArchiveRestriction() == 'archived') {
         $constraints[] = $query->logicalAnd($query->lessThan('archive', $GLOBALS['EXEC_TIME']), $query->greaterThan('archive', 0));
     } elseif ($demand->getArchiveRestriction() == 'active') {
         $constraints[] = $query->logicalOr($query->greaterThanOrEqual('archive', $GLOBALS['EXEC_TIME']), $query->equals('archive', 0));
     }
     if ($demand->getDateField() == "type" && is_array($demand->getTypes()) && count($demand->getTypes()) == 1) {
         $type = reset($demand->getTypes());
         if (isset($GLOBALS['TCA']['tx_mooxnews_domain_model_news']['dateTimeDefaultField'][$type]) && $GLOBALS['TCA']['tx_mooxnews_domain_model_news']['dateTimeDefaultField'][$type] != "") {
             $dateTimeDefaultField = $GLOBALS['TCA']['tx_mooxnews_domain_model_news']['dateTimeDefaultField'][$type];
         } else {
             $dateTimeDefaultField = $GLOBALS['TCA']['tx_mooxnews_domain_model_news']['dateTimeDefaultField']['moox_news'];
         }
         $demand->setDateField($dateTimeDefaultField);
     } elseif ($demand->getDateField() == "type") {
         $dateTimeDefaultField = $GLOBALS['TCA']['tx_mooxnews_domain_model_news']['dateTimeDefaultField']['moox_news'];
         $demand->setDateField($dateTimeDefaultField);
     }
     // 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);
     }
     // top news
     if ($demand->getExcludeFromRss() == 1) {
         $constraints[] = $query->equals('excludeFromRss', 0);
     }
     // storage page
     if ($demand->getStoragePage() != 0) {
         $pidList = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $demand->getStoragePage(), TRUE);
         $constraints[] = $query->in('pid', $pidList);
     }
     // datepicker filter
     $datepickerConstraints = array();
     if ($demand->getDateFrom() != "") {
         $dateFrom = strtotime($demand->getDateFrom());
         if ($dateFrom > 0) {
             $dateFrom = mktime(0, 0, 0, date("n", $dateFrom), date("j", $dateFrom), date("Y", $dateFrom));
             $datepickerConstraints[] = $query->greaterThanOrEqual($timeRestrictionField, $dateFrom);
         }
     }
     if ($demand->getDateTo() != "") {
         $dateTo = strtotime($demand->getDateTo());
         if ($dateTo > 0) {
             $dateTo = mktime(23, 59, 59, date("n", $dateTo), date("j", $dateTo), date("Y", $dateTo));
             $datepickerConstraints[] = $query->lessThanOrEqual($timeRestrictionField, $dateTo);
         }
     }
     if (count($datepickerConstraints) > 1) {
         $constraints[] = $query->logicalAnd($datepickerConstraints);
     } elseif (count($datepickerConstraints) > 0) {
         $constraints[] = $datepickerConstraints[0];
     }
     // month & year OR year only
     if (is_numeric($demand->getYear())) {
         if ($demand->getYear() > 0) {
             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($timeRestrictionField, $begin), $query->lessThanOrEqual($timeRestrictionField, $end));
         } else {
             $constraints[] = $query->equals($timeRestrictionField, 0);
         }
     }
     if (is_array($GLOBALS['TCA']['tx_mooxnews_domain_model_news']['listViewSearchFields'])) {
         foreach ($GLOBALS['TCA']['tx_mooxnews_domain_model_news']['listViewSearchFields'] as $extkey => $queryFields) {
             if ($extkey != "moox_news") {
                 foreach ($queryFields as $fieldname => $queryField) {
                     if ($fieldname != "default" && $queryField != "") {
                         $fieldname = \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToLowerCamelCase($fieldname);
                         $getCall = "get" . ucfirst($fieldname);
                         if (method_exists($demand, $getCall)) {
                             $fieldvalue = $demand->{$getCall}();
                             if ($fieldvalue != "") {
                                 $listViewSearchFields = explode(",", $queryField);
                                 $queryConstraints = array();
                                 foreach ($listViewSearchFields as $listViewSearchField) {
                                     $queryConstraints[] = $query->like($listViewSearchField, "%" . $fieldvalue . "%");
                                 }
                                 $constraints[] = $query->logicalOr($queryConstraints);
                             }
                         }
                     }
                 }
             }
         }
     }
     // set constraints based on extended filter fields from external extensions
     foreach ($GLOBALS['TCA']['tx_mooxnews_domain_model_news']['columns'] as $fieldname => $field) {
         if ($field['addToMooxNewsFrontendDemand']) {
             $fieldname = \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToLowerCamelCase($fieldname);
             $getCall = "get" . ucfirst($fieldname);
             if (method_exists($demand, $getCall)) {
                 $fieldvalue = $demand->{$getCall}();
                 if (trim($fieldvalue) != "" && trim($fieldvalue) != "all") {
                     if (in_array($field['config']['type'], array("input", "text")) && !in_array($field['addToMooxNewsFrontendFilter']['type'], array("select"))) {
                         $constraints[] = $query->like($fieldname, '%' . $fieldvalue . '%');
                     } else {
                         if (in_array($field['addToMooxNewsFrontendFilter']['type'], array("select")) && $fieldvalue == "empty") {
                             $constraints[] = $query->logicalOr($query->equals($fieldname, ""), $query->equals($fieldname, NULL));
                         } else {
                             $constraints[] = $query->equals($fieldname, $fieldvalue);
                         }
                     }
                 }
             }
         }
     }
     // sys language
     if (TYPO3_MODE == 'BE') {
         $query->getQuerySettings()->setRespectSysLanguage(false);
         $constraints[] = $query->equals('t3_origuid', 0);
         if (is_numeric($demand->getSysLanguageUid())) {
             $constraints[] = $query->equals('sys_language_uid', $demand->getSysLanguageUid());
         }
     }
     // Tags
     if (is_array($demand->getTags()) && count($demand->getTags())) {
         $tags = $demand->getTags();
         if (count($tags) > 1) {
             $tagConstraints = array();
             foreach ($tags as $tag) {
                 $tagConstraints[] = $query->contains('tags', $tag);
             }
             $constraints[] = $query->logicalOr($tagConstraints);
         } else {
             $constraints[] = $query->contains('tags', $tags[0]);
         }
     } elseif ($demand->getTag() != "") {
         $constraints[] = $query->contains('tags', $demand->getTag());
     }
     // Targets
     if (is_array($demand->getTargets()) && count($demand->getTargets())) {
         $targets = $demand->getTargets();
         if (count($targets) > 1) {
             $targetConstraints = array();
             foreach ($targets as $target) {
                 $targetConstraints[] = $query->contains('targets', $target);
             }
             $constraints[] = $query->logicalOr($targetConstraints);
         } else {
             $constraints[] = $query->contains('targets', $targets[0]);
         }
     } elseif ($demand->getTarget() != "") {
         $constraints[] = $query->contains('targets', $demand->getTarget());
     }
     // mailer frequency
     if (TYPO3_MODE == 'BE') {
         if (is_numeric($demand->getMailerFrequency())) {
             $constraints[] = $query->equals('mailerFrequency', $demand->getMailerFrequency());
         }
     }
     // Search
     $searchConstraints = $this->getSearchConstraints($query, $demand);
     if (!empty($searchConstraints)) {
         $constraints[] = $query->logicalAnd($searchConstraints);
     }
     // Exclude already displayed
     if ($demand->getExcludeAlreadyDisplayedNews() && isset($GLOBALS['EXT']['moox_news']['alreadyDisplayed']) && !empty($GLOBALS['EXT']['moox_news']['alreadyDisplayed'])) {
         $constraints[] = $query->logicalNot($query->in('uid', $GLOBALS['EXT']['moox_news']['alreadyDisplayed']));
     }
     // Clean not used constraints
     foreach ($constraints as $key => $value) {
         if (is_null($value)) {
             unset($constraints[$key]);
         }
     }
     /*
     $parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser');  
     $queryParts = $parser->parseQuery($query); 
     \TYPO3\CMS\Core\Utility\DebugUtility::debug($queryParts, 'Query');
     exit();
     */
     return $constraints;
 }