예제 #1
0
파일: IdList.php 프로젝트: zonky2/core
 /**
  * {@inheritDoc}
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     if ($this->get('items')) {
         $arrItems = explode(',', (string) $this->get('items'));
     } else {
         $arrItems = array();
     }
     $objFilter->addFilterRule(new FilterRuleIdList($arrItems));
 }
예제 #2
0
파일: ConditionOr.php 프로젝트: zonky2/core
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $objFilterRule = new FilterRuleOr($this->get('stop_after_match'));
     foreach ($this->arrChildren as $objChildSetting) {
         $objSubFilter = new Filter($this->getMetaModel());
         $objChildSetting->prepareRules($objSubFilter, $arrFilterUrl);
         $objFilterRule->addChild($objSubFilter);
     }
     $objFilter->addFilterRule($objFilterRule);
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $this->filterParameters = $arrFilterUrl;
     $this->queryString = $this->get('customsql');
     $this->queryParameter = array();
     $objFilter->addFilterRule($this->getFilterRule());
     unset($this->filterParameters);
     unset($this->queryString);
     unset($this->queryParameter);
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $objMetaModel = $this->getMetaModel();
     $objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
     $strParamName = $this->getParamName();
     $arrParamValue = $this->buildParamValue($arrFilterUrl, $strParamName);
     $arrOptions = $this->getParameterFilterOptions($objAttribute, null);
     $arrParamValue = $this->filterParamValue($arrParamValue, $arrOptions);
     if ($objAttribute && $strParamName && is_array($arrParamValue) && $arrOptions) {
         // Determine which parenting rule to use, AND or OR.
         if ($this->get('useor')) {
             $objParentRule = new ConditionOr();
         } else {
             $objParentRule = new ConditionAnd();
         }
         // We allow the current and the fallback language to be searched by default.
         $arrValidLanguages = array($this->getMetaModel()->getActiveLanguage(), $this->getMetaModel()->getFallbackLanguage());
         foreach ($arrParamValue as $strParamValue) {
             // Restrict to valid options for obvious reasons.
             if (array_key_exists($strParamValue, $arrOptions)) {
                 $objSubFilter = new Filter($objMetaModel);
                 $objSubFilter->addFilterRule(new SearchAttribute($objAttribute, $strParamValue, $arrValidLanguages));
                 $objParentRule->addChild($objSubFilter);
             }
         }
         $objFilter->addFilterRule($objParentRule);
         return;
     }
     // If no setting has been defined, we appear transparently as "not defined" and return all items.
     $objFilter->addFilterRule(new StaticIdList(null));
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $objMetaModel = $this->getMetaModel();
     $objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
     $strParamName = $this->getParamName();
     $strParamValue = $arrFilterUrl[$strParamName];
     $strTextsearch = $this->get('textsearch');
     // React on wildcard, overriding the search type.
     if (strpos($strParamValue, '*') !== false) {
         $strTextsearch = 'exact';
     }
     // Type of search.
     switch ($strTextsearch) {
         case 'beginswith':
             $strWhat = $strParamValue . '%';
             break;
         case 'endswith':
             $strWhat = '%' . $strParamValue;
             break;
         case 'exact':
             $strWhat = $strParamValue;
             break;
         default:
             $strWhat = '%' . $strParamValue . '%';
             break;
     }
     if ($objAttribute && $strParamName && $strParamValue) {
         $objFilter->addFilterRule(new SearchAttribute($objAttribute, $strWhat));
         return;
     }
     $objFilter->addFilterRule(new StaticIdList(null));
 }
 /**
  * {@inheritDoc}
  */
 public function prepareRules(IMetaModelFilter $objFilter, $arrFilterUrl)
 {
     if ($this->get('check_ignorepublished') && $arrFilterUrl['ignore_published' . $this->get('id')]) {
         return;
     }
     // Skip filter when in front end preview.
     if ($this->get('check_allowpreview') && BE_USER_LOGGED_IN) {
         return;
     }
     $objAttribute = $this->getMetaModel()->getAttributeById($this->get('attr_id'));
     if ($objAttribute) {
         $objFilterRule = new SearchAttribute($objAttribute, '1', $this->getMetaModel()->getActiveLanguage());
         $objFilter->addFilterRule($objFilterRule);
         return;
     }
     // Attribute not found, do not return anyting to prevent leaking of items.
     $objFilter->addFilterRule(new MetaModelFilterRuleStaticIdList(array()));
 }
예제 #7
0
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     if ($this->get('check_ignorepublished') && $arrFilterUrl['ignore_published' . $this->get('id')]) {
         return;
     }
     // Skip filter when in front end preview.
     if ($this->get('check_allowpreview') && BE_USER_LOGGED_IN) {
         return;
     }
     $objAttribute = $this->getMetaModel()->getAttributeById($this->get('attr_id'));
     if ($objAttribute) {
         $objFilterRule = new SimpleQuery(sprintf('SELECT id FROM %s WHERE %s=?', $this->getMetaModel()->getTableName(), $objAttribute->getColName()), array(1));
         $objFilter->addFilterRule($objFilterRule);
         return;
     }
     // No attribute found, do not return anyting.
     $objFilter->addFilterRule(new StaticIdList(array()));
 }
예제 #8
0
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $objMetaModel = $this->getMetaModel();
     $arrLanguages = $this->getAvailableLanguages($objMetaModel);
     $objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
     $strParamName = $this->getParamName();
     // If is a checkbox defined as "no", 1 has to become -1 like with radio fields.
     if (isset($arrFilterUrl[$strParamName])) {
         $arrFilterUrl[$strParamName] = $arrFilterUrl[$strParamName] == '1' && $this->get('ynmode') == 'no' ? '-1' : $arrFilterUrl[$strParamName];
     }
     if ($objAttribute && $strParamName && !empty($arrFilterUrl[$strParamName])) {
         // Param -1 has to be '' meaning 'really empty'.
         $arrFilterUrl[$strParamName] = $arrFilterUrl[$strParamName] == '-1' ? '' : $arrFilterUrl[$strParamName];
         $objFilterRule = new SearchAttribute($objAttribute, $arrFilterUrl[$strParamName], $arrLanguages);
         $objFilter->addFilterRule($objFilterRule);
         return;
     }
     $objFilter->addFilterRule(new StaticIdList(null));
 }
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $objAttribute = $this->getMetaModel()->getAttributeById($this->get('gp_attr_id'));
     if ($objAttribute) {
         $objGeo = Geolocation::getInstance();
         $arrCountry = $objGeo->getUserGeolocation()->getCountriesShort();
         // Set 'no_country' if no country was found.
         $arrCountry = $arrCountry ? $arrCountry : array('xx');
         // Build query string part.
         foreach (array_keys($arrCountry) as $k) {
             $arrCountry[$k] = "find_in_set ('" . $arrCountry[$k] . "', countries)";
         }
         $objFilterRule = new SimpleQuery('SELECT item_id FROM tl_metamodel_geoprotection WHERE attr_id = ? AND
                 ((mode = \'\') OR (mode = \'gp_show\' AND (' . implode(' OR ', $arrCountry) . ')) OR
                 (mode = \'gp_hide\' AND NOT (' . implode(' OR ', $arrCountry) . ')))', array($this->get('gp_attr_id')), 'item_id');
         $objFilter->addFilterRule($objFilterRule);
         return;
     }
     // No attribute found.
     $objFilter->addFilterRule(new StaticIdList(array()));
 }
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $objMetaModel = $this->getMetaModel();
     $objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
     $strParamName = $this->getParamName();
     $strParamValue = $arrFilterUrl[$strParamName];
     if ($objAttribute && $strParamName && $strParamValue) {
         $strQuery = sprintf('SELECT id FROM %s WHERE FROM_UNIXTIME(%s, "%%Y") = ?', $this->getMetaModel()->getTableName(), $objAttribute->getColName());
         $objFilter->addFilterRule(new SimpleQuery($strQuery, array($strParamValue)));
         return;
     }
     $objFilter->addFilterRule(new MetaModelFilterRuleStaticIdList(null));
 }
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $objMetaModel = $this->getMetaModel();
     $objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
     $strParam = $this->getParamName();
     if ($objAttribute && $strParam) {
         $arrFilterValue = $arrFilterUrl[$strParam];
         if ($arrFilterValue && is_array($arrFilterValue)) {
             if (count($arrFilterValue) > 1) {
                 $objParentRule = new ConditionOr();
                 foreach ($arrFilterValue as $strValue) {
                     $objSubFilter = new Filter($objMetaModel);
                     $objSubFilter->addFilterRule(new SearchAttribute($objAttribute, $strValue));
                     $objParentRule->addChild($objSubFilter);
                 }
                 $objFilter->addFilterRule($objParentRule);
                 return;
             } else {
                 $objFilter->addFilterRule(new SearchAttribute($objAttribute, $arrFilterValue));
                 return;
             }
         }
     }
 }
예제 #12
0
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $objMetaModel = $this->getMetaModel();
     $objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
     $strParamName = $this->getParamName();
     $strParamValue = $arrFilterUrl[$strParamName];
     $strTextsearch = $this->get('textsearch');
     // React on wildcard, overriding the search type.
     if (strpos($strParamValue, '*') !== false) {
         $strTextsearch = 'exact';
     }
     // Type of search.
     switch ($strTextsearch) {
         case 'beginswith':
             $strWhat = $strParamValue . '%';
             break;
         case 'endswith':
             $strWhat = '%' . $strParamValue;
             break;
         case 'exact':
             $strWhat = $strParamValue;
             break;
         default:
             $strWhat = '%' . $strParamValue . '%';
             break;
     }
     if ($objAttribute && $strParamName && $strParamValue) {
         if ($strTextsearch == 'against') {
             $strExtendedFields = $this->get('extendFields') ? ',' . $this->get('extendFields') : '';
             $objFilter->addFilterRule(new SimpleQuery(sprintf('SELECT id FROM %s WHERE (MATCH(%s%s) AGAINST(?))', $this->getMetaModel()->getTableName(), $strParamName, $strExtendedFields), array($strParamValue)));
             return;
         }
         $objFilter->addFilterRule(new SearchAttribute($objAttribute, $strWhat));
         return;
     }
     $objFilter->addFilterRule(new StaticIdList(null));
 }
예제 #13
0
파일: ItemList.php 프로젝트: zonky2/core
 /**
  * Add additional filter rules to the list on the fly.
  *
  * @param \MetaModels\Filter\IFilterRule $objFilterRule The filter rule to add.
  *
  * @return ItemList
  */
 public function addFilterRule($objFilterRule)
 {
     if (!$this->objFilter) {
         $this->objFilter = $this->objMetaModel->getEmptyFilter();
     }
     $this->objFilter->addFilterRule($objFilterRule);
     return $this;
 }
예제 #14
0
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     $objMetaModel = $this->getMetaModel();
     $objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
     $strParam = $this->getParamName();
     if ($objAttribute && $strParam) {
         $arrFilterValue = isset($arrFilterUrl[$strParam]) ? $arrFilterUrl[$strParam] : null;
         if (!$arrFilterValue && $this->get('defaultid')) {
             $arrFilterValue = $this->get('defaultid');
         }
         if ($arrFilterValue) {
             if ($objMetaModel->isTranslated() && $this->get('all_langs')) {
                 $arrLanguages = $objMetaModel->getAvailableLanguages();
             } else {
                 $arrLanguages = array($objMetaModel->getActiveLanguage());
             }
             $objFilterRule = new FilterRuleSimpleLookup($objAttribute, $arrFilterValue, $arrLanguages);
             $objFilter->addFilterRule($objFilterRule);
             return;
         }
         // We found an attribute but no match in URL. So ignore this filter setting if allow_empty is set.
         if ($this->allowEmpty()) {
             $objFilter->addFilterRule(new FilterRuleStaticIdList(null));
             return;
         }
     }
     // Either no attribute found or no match in url, do not return anything.
     $objFilter->addFilterRule(new FilterRuleStaticIdList(array()));
 }
 /**
  * Run the search for the complex attribute geolocation.
  *
  * @param Container  $container     The container with all information.
  *
  * @param IFilter    $filter        The filter container.
  *
  * @param IAttribute $latAttribute  The attribute to filter on.
  *
  * @param IAttribute $longAttribute The attribute to filter on.
  *
  * @return void
  */
 protected function doSearchForTwoSimpleAtt($container, $filter, $latAttribute, $longAttribute)
 {
     // Get location.
     $lat = $container->getLatitude();
     $lng = $container->getLongitude();
     $intDist = $container->getDistance();
     $distance = sprintf('round(sqrt(' . 'power(2 * pi() / 360 * (%1$s - %3$s) * 6371,2)' . '+ power(2 * pi() / 360 * (%2$s - %4$s) * 6371 * COS( 2 * pi() / 360 * (%1$s + %3$s) * 0.5), 2)' . '))', $lat, $lng, $latAttribute->getColName(), $longAttribute->getColName());
     $objResult = \Database::getInstance()->prepare(sprintf('SELECT id FROM %1$s WHERE %2$s<=? ORDER BY %2$s', $this->getMetaModel()->getTableName(), $distance))->execute($intDist);
     if ($objResult->numRows == 0) {
         $filter->addFilterRule(new StaticIdList(array()));
     } else {
         $filter->addFilterRule(new StaticIdList($objResult->fetchEach('id')));
     }
 }
예제 #16
0
 /**
  * Fetch filter options from foreign table taking the given flag into account.
  *
  * @param IFilter $filter The filter to which the rules shall be added to.
  *
  * @param array   $idList The list of ids of items for which the rules shall be added.
  *
  * @return void
  */
 public function buildFilterRulesForUsedOnly($filter, $idList = array())
 {
     $params = array($this->get('id'));
     if (empty($idList)) {
         $arrUsedValues = $this->getDatabase()->prepare('SELECT value_id AS value
                  FROM tl_metamodel_tag_relation
                  WHERE att_id = ?
                  GROUP BY value')->execute($params)->fetchEach('value');
     } else {
         $query = sprintf('SELECT value_id AS value
                 FROM tl_metamodel_tag_relation
                 WHERE att_id = ?
                   AND item_id IN (%s)
                 GROUP BY value', $this->parameterMask($idList));
         $arrUsedValues = $this->getDatabase()->prepare($query)->execute(array_merge($params, $idList))->fetchEach('value');
     }
     $arrUsedValues = array_filter($arrUsedValues, function ($value) {
         return !empty($value);
     });
     $filter->addFilterRule(new StaticIdList($arrUsedValues));
 }
예제 #17
0
파일: MetaModel.php 프로젝트: zonky2/core
 /**
  * Clone the given filter or create an empty one if no filter has been passed.
  *
  * @param IFilter|null $objFilter The filter to clone.
  *
  * @return IFilter the cloned filter.
  */
 protected function copyFilter($objFilter)
 {
     if ($objFilter) {
         $objNewFilter = $objFilter->createCopy();
     } else {
         $objNewFilter = $this->getEmptyFilter();
     }
     return $objNewFilter;
 }
예제 #18
0
 /**
  * Do a complex search with each word. Search for all words or for any word.
  *
  * @param string   $strTextSearch The mode any or all.
  *
  * @param IFilter  $objFilter     The filter to append the rules to.
  *
  * @param string[] $arrFilterUrl  The parameters to evaluate.
  *
  * @return void
  */
 private function doComplexSearch($strTextSearch, $objFilter, $arrFilterUrl)
 {
     $objMetaModel = $this->getMetaModel();
     $objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
     $strParamName = $this->getParamName();
     $strParamValue = $arrFilterUrl[$strParamName];
     $parentFilter = null;
     $words = array();
     // Type of search.
     switch ($strTextSearch) {
         case 'any':
             $words = $this->getWords($strParamValue);
             $parentFilter = new ConditionOr();
             break;
         case 'all':
             $words = $this->getWords($strParamValue);
             $parentFilter = new ConditionAnd();
             break;
         default:
             // Do nothing. Because the parent function saved us. The value have to be any or all.
             break;
     }
     if ($objAttribute && $strParamName && $strParamValue && $parentFilter) {
         foreach ($words as $word) {
             $subFilter = $objMetaModel->getEmptyFilter();
             $subFilter->addFilterRule(new SearchAttribute($objAttribute, '%' . $word . '%'));
             $parentFilter->addChild($subFilter);
         }
         $objFilter->addFilterRule($parentFilter);
         return;
     }
     $objFilter->addFilterRule(new StaticIdList(null));
 }
예제 #19
0
 /**
  * {@inheritdoc}
  *
  * @throws \LengthException If not both fields are allowed.
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     // Check if we can filter on anything.
     if (!$this->get('fromfield') && !$this->get('tofield')) {
         $objFilter->addFilterRule(new StaticIdList(null));
         return;
     }
     $attribute = $this->getMetaModel()->getAttributeById($this->get('attr_id'));
     if (!$attribute) {
         $objFilter->addFilterRule(new StaticIdList(null));
         return;
     }
     $value = $this->getParameterValue($arrFilterUrl);
     // No filter values, get out.
     if (empty($value)) {
         $objFilter->addFilterRule(new StaticIdList(null));
         return;
     }
     $rule = $this->buildFromToRule($attribute);
     $objFilter->addFilterRule($rule);
     if (count($value) == 2) {
         // Two values, apply filtering for a value range if both fields are allowed.
         if (!($this->get('fromfield') && $this->get('tofield'))) {
             throw new \LengthException('Only one value is allowed, please configure fromfield and tofield.');
         }
         $rule->setLowerBound($this->formatValue($value[0]), $this->get('moreequal'))->setUpperBound($this->formatValue($value[1]), $this->get('lessequal'));
         return;
     }
     // Only one value, decide which one to use.
     if ($this->get('fromfield')) {
         $rule->setLowerBound($this->formatValue($value[0]), $this->get('moreequal'));
         return;
     }
     $rule->setUpperBound($this->formatValue($value[0]), $this->get('lessequal'));
 }
 /**
  * Fetch filter options from foreign table taking the given flag into account.
  *
  * @param IFilter $filter The filter to which the rules shall be added to.
  *
  * @param array   $idList The list of ids of items for which the rules shall be added.
  *
  * @return void
  */
 public function buildFilterRulesForUsedOnly($filter, $idList = array())
 {
     if (empty($idList)) {
         $query = sprintf('SELECT %2$s FROM %1$s GROUP BY %2$s', $this->getMetaModel()->getTableName(), $this->getColName());
     } else {
         $query = sprintf('SELECT %2$s FROM %1$s WHERE id IN (%3$s) GROUP BY %2$s', $this->getMetaModel()->getTableName(), $this->getColName(), $this->parameterMask($idList));
     }
     $arrUsedValues = $this->getDatabase()->prepare($query)->execute($idList)->fetchEach($this->getColName());
     $arrUsedValues = array_filter($arrUsedValues, function ($value) {
         return !empty($value);
     });
     $filter->addFilterRule(new StaticIdList($arrUsedValues));
 }
예제 #21
0
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IMetaModelFilter $objFilter, $arrFilterUrl)
 {
     $objMetaModel = $this->getMetaModel();
     $objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
     $strParamName = $this->getParamName();
     $strParamValue = $arrFilterUrl[$strParamName];
     $strWhat = $strParamValue . '%';
     if ($objAttribute && $strParamName && $strParamValue) {
         $arrIds = $objAttribute->searchFor($strWhat);
         $objFilter->addFilterRule(new MetaModelFilterRuleStaticIdList($arrIds));
         return;
     }
     $objFilter->addFilterRule(new MetaModelFilterRuleStaticIdList(null));
 }
예제 #22
0
 /**
  * {@inheritdoc}
  */
 public function prepareRules(IFilter $objFilter, $arrFilterUrl)
 {
     // Check if we can filter on anything.
     if (!$this->get('fromfield') && !$this->get('tofield')) {
         $objFilter->addFilterRule(new StaticIdList(null));
         return;
     }
     // Get the attributes.
     $attribute = $this->getMetaModel()->getAttributeById($this->get('attr_id'));
     $attribute2 = $this->getMetaModel()->getAttributeById($this->get('attr_id2'));
     // Check if we have a valid value.
     if (!$attribute) {
         $objFilter->addFilterRule(new StaticIdList(null));
         return;
     }
     // Set the second attribute with the first one if the second one is empty.
     if (!$attribute2) {
         $attribute2 = $attribute;
     }
     $value = $this->getParameterValue($arrFilterUrl);
     // No filter values, get out.
     if (empty($value)) {
         $objFilter->addFilterRule(new StaticIdList(null));
         return;
     }
     $filterOne = $this->getMetaModel()->getEmptyFilter();
     $filterTwo = $this->getMetaModel()->getEmptyFilter();
     $moreEqual = (bool) $this->get('moreequal');
     $lessEqual = (bool) $this->get('lessequal');
     $filterOne->addFilterRule(new LessThan($attribute, $this->formatValue($value[0]), $moreEqual))->addFilterRule(new GreaterThan($attribute2, $this->formatValue($value[0]), $lessEqual));
     $filterTwo->addFilterRule(new LessThan($attribute, $this->formatValue($value[1]), $moreEqual))->addFilterRule(new GreaterThan($attribute2, $this->formatValue($value[1]), $lessEqual));
     $upperMatches = $filterOne->getMatchingIds();
     $lowerMatches = $filterTwo->getMatchingIds();
     $result = array_unique(array_merge($upperMatches, $lowerMatches));
     $objFilter->addFilterRule(new StaticIdList($result));
 }
예제 #23
0
 /**
  * Build an AND or OR query.
  *
  * @param IFilter $filter    The filter to add the operations to.
  *
  * @param array   $operation The operation to convert.
  *
  * @return void
  */
 protected function getAndOrFilter(IFilter $filter, $operation)
 {
     if (!$operation['children']) {
         return;
     }
     if ($operation['operation'] == 'AND') {
         $filterRule = new ConditionAnd();
     } else {
         $filterRule = new ConditionOr();
     }
     $filter->addFilterRule($filterRule);
     $children = $this->optimizedFilter($filterRule, $operation['children'], $operation['operation']);
     foreach ($children as $child) {
         $subFilter = $this->getMetaModel()->getEmptyFilter();
         $filterRule->addChild($subFilter);
         $this->calculateSubfilter($child, $subFilter);
     }
 }