/**
  * Adds conditions, matches and where clauses to the query
  * @param IKalturaDbQuery $query
  */
 public function applyCondition(IKalturaDbQuery $query)
 {
     if (!$query instanceof IKalturaIndexQuery) {
         return;
     }
     $matchText = '"' . KalturaCriteria::escapeString($this->value) . '"';
     if ($this->not) {
         $matchText = '!' . $matchText;
     }
     $query->addMatch("@{$this->field} (" . $matchText . ")");
 }
 public function applyCondition(IKalturaDbQuery $query)
 {
     if (is_null($this->categoriesMatchOr) && is_null($this->categoryIdEqual)) {
         return;
     }
     // Fetch the list of categories
     $categoryEntries = null;
     if ($this->categoriesMatchOr) {
         $categoryEntries = entryFilter::categoryFullNamesToIdsParsed($this->categoriesMatchOr, $this->categoryEntryStatusIn);
     } else {
         $categoryEntries = entryFilter::categoryIdsToSphinxIds($this->categoryIdEqual, $this->categoryEntryStatusIn);
     }
     if ($categoryEntries == '') {
         // Set a non-exiting cat. id. in order to return empty results (instead of throwing an exception)
         $categoryEntries = category::CATEGORY_ID_THAT_DOES_NOT_EXIST;
     }
     $categoryEntries = explode(',', $categoryEntries);
     if ($query instanceof IKalturaIndexQuery) {
         $categoriesStrs = array();
         foreach ($categoryEntries as $categoryId) {
             $categoriesStrs[] = '"' . $categoryId . '"';
         }
         $query->addMatch('@' . entryIndex::getIndexFieldName(entryPeer::CATEGORIES_IDS) . ' (' . implode(' | ', $categoriesStrs) . ')');
     } else {
         $query->addColumnWhere(entryPeer::CATEGORIES_IDS, $categoryEntries, KalturaCriteria::IN_LIKE);
     }
     if ($this->orderBy) {
         $orderByField = substr($this->orderBy, 1);
         $orderBy = $this->orderBy[0] == '+' ? Criteria::ASC : Criteria::DESC;
         if ($orderByField != self::CREATED_AT) {
             throw new kCoreException("Unsupported orderBy criteria [{$orderByField}]");
         }
         $dynAttribCriteriaFieldName = entryIndex::DYNAMIC_ATTRIBUTES . '.' . self::getCategoryCreatedAtDynamicAttributeName($this->categoryIdEqual);
         $query->addNumericOrderBy($dynAttribCriteriaFieldName, $orderBy);
     }
 }
 public function applyCondition(IKalturaDbQuery $query, $xPaths = null)
 {
     if (!$query instanceof IKalturaIndexQuery) {
         return;
     }
     $this->parentQuery = $query;
     if (!$this->condition) {
         if (is_null($xPaths)) {
             $xPaths = array();
             $profileFields = MetadataProfileFieldPeer::retrieveActiveByMetadataProfileId($this->metadataProfileId);
             foreach ($profileFields as $profileField) {
                 $xPaths[$profileField->getXpath()] = $profileField;
             }
         }
         $dataConditions = array();
         $subConditions = array();
         $pluginName = MetadataPlugin::PLUGIN_NAME;
         if (count($this->items)) {
             foreach ($this->items as $item) {
                 $dataCondition = null;
                 if ($item instanceof AdvancedSearchFilterComparableCondition) {
                     /* @var $item AdvancedSearchFilterComparableCondition  */
                     $field = $item->getField();
                     if (!isset($xPaths[$field])) {
                         $this->addCondition('1 <> 1');
                         KalturaLog::ERR("Missing field: {$field} in xpath array: " . print_r($xPaths, true));
                         continue;
                     }
                     switch ($item->getComparison()) {
                         case KalturaSearchConditionComparison::EQUAL:
                             $comparison = ' = ';
                             break;
                         case KalturaSearchConditionComparison::GREATER_THAN:
                             $comparison = ' > ';
                             break;
                         case KalturaSearchConditionComparison::GREATER_THAN_OR_EQUAL:
                             $comparison = ' >= ';
                             break;
                         case KalturaSearchConditionComparison::LESS_THAN:
                             $comparison = " < ";
                             break;
                         case KalturaSearchConditionComparison::LESS_THAN_OR_EQUAL:
                             $comparison = " <= ";
                             break;
                         default:
                             KalturaLog::ERR("Missing comparison type");
                             continue;
                     }
                     $metadataField = $this->getMetadataSearchField($field, $xPaths);
                     if (!$metadataField) {
                         KalturaLog::ERR("Missing metadataField for {$field} in xpath array: " . print_r($xPaths, true));
                         continue;
                     }
                     $value = $item->getValue();
                     if (!is_numeric($value)) {
                         switch ($value) {
                             case Criteria::CURRENT_DATE:
                                 $d = getdate();
                                 $value = mktime(0, 0, 0, $d['mon'], $d['mday'], $d['year']);
                                 break;
                             case Criteria::CURRENT_TIME:
                             case Criteria::CURRENT_TIMESTAMP:
                                 $value = time();
                                 break;
                             default:
                                 if ($xPaths[$field]->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_DATE || $xPaths[$field]->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_INT) {
                                     $this->addCondition('1 <> 1');
                                     KalturaLog::ERR("wrong search value: {$field} is numeric. search value: " . print_r($item->getValue(), true));
                                     continue;
                                 }
                                 $value = SphinxUtils::escapeString($value);
                                 break;
                         }
                     }
                     if ($xPaths[$field]->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_DATE) {
                         $value = kTime::getRelativeTime($value);
                     }
                     $newCondition = $metadataField . $comparison . $value;
                     if ($item->getComparison() != KalturaSearchConditionComparison::EQUAL) {
                         $newCondition = "({$newCondition} AND {$metadataField} <> 0)";
                     }
                     $this->addCondition($newCondition);
                 } elseif ($item instanceof AdvancedSearchFilterCondition) {
                     $field = $item->getField();
                     if (!isset($xPaths[$field])) {
                         $this->addCondition('1 <> 1');
                         KalturaLog::ERR("Missing field: {$field} in xpath array: " . print_r($xPaths, true));
                         continue;
                     }
                     $value = $item->getValue();
                     $value = SphinxUtils::escapeString($value);
                     $fieldId = $xPaths[$field]->getId();
                     // any value in the field
                     if (trim($value) == '*') {
                         $dataCondition = "{$pluginName}_{$fieldId}";
                     } elseif (in_array($xPaths[$field]->getType(), array(self::KMC_FIELD_TYPE_OBJECT, self::KMC_FIELD_TYPE_USER))) {
                         $dataCondition = "\\\"{$pluginName}_{$fieldId} {$value} " . kMetadataManager::SEARCH_TEXT_SUFFIX . "_{$fieldId}" . "\\\"";
                     } else {
                         $dataCondition = "{$pluginName}_{$fieldId} << ( {$value} ) << " . kMetadataManager::SEARCH_TEXT_SUFFIX . "_{$fieldId}";
                     }
                     KalturaLog::debug("add {$dataCondition}");
                     $dataConditions[] = "( {$dataCondition} )";
                 } elseif ($item instanceof MetadataSearchFilter) {
                     $item->applyCondition($this, $xPaths);
                 }
             }
         }
         if (count($dataConditions)) {
             $glue = $this->type == MetadataSearchFilter::SEARCH_AND ? ' ' : ' | ';
             $dataConditions = array_unique($dataConditions);
             $key = '@' . $this->getMetadataSearchField();
             $value = implode($glue, $dataConditions);
             $this->addMatch("{$key} {$value}");
         }
         $matchClause = array_unique($this->matchClause);
         $glue = $this->type == self::SEARCH_AND ? ' ' : ' | ';
         $this->condition = implode($glue, $matchClause);
         if ($this->type == self::SEARCH_OR && $this->condition) {
             $this->condition = "( {$this->condition} )";
         }
     }
     if ($this->condition) {
         $query->addMatch($this->condition);
     }
     if (isset($this->orderBy)) {
         $orderByField = substr($this->orderBy, 1);
         $orderBy = $this->orderBy[0] == '+' ? Criteria::ASC : Criteria::DESC;
         $metadataFieldType = null;
         $metadataField = $this->getMetadataSearchField($orderByField, array(), $metadataFieldType);
         $isIntVal = in_array($metadataFieldType, array(MetadataSearchFilter::KMC_FIELD_TYPE_DATE, MetadataSearchFilter::KMC_FIELD_TYPE_INT));
         if ($metadataField) {
             if ($isIntVal) {
                 $query->addNumericOrderBy($metadataField, $orderBy);
             } else {
                 $query->addOrderBy($metadataField, $orderBy);
             }
         }
     }
 }
 public function applyCondition(IKalturaDbQuery $query)
 {
     if ($query instanceof IKalturaIndexQuery) {
         $condition = $this->getCondition();
         KalturaLog::debug("condition [" . print_r($condition, true) . "]");
         $key = '@' . ContentDistributionSphinxPlugin::getSphinxFieldName(ContentDistributionPlugin::SPHINX_EXPANDER_FIELD_DATA);
         $query->addMatch("({$key} {$condition})");
     }
 }
 public function applyCondition(IKalturaDbQuery $query)
 {
     $this->parentQuery = $query;
     if (!$this->condition) {
         if (count($this->items)) {
             $queryDestination = $this;
             if ($this->type == self::SEARCH_AND) {
                 $queryDestination = $query;
             }
             foreach ($this->items as $item) {
                 KalturaLog::debug("item type: " . get_class($item));
                 if ($item instanceof AdvancedSearchFilterItem) {
                     $item->applyCondition($queryDestination);
                 }
             }
             if ($this->type == self::SEARCH_OR && count($this->matchClause)) {
                 $matchClause = array_unique($this->matchClause);
                 $this->condition = '( ' . implode(' | ', $matchClause) . ' )';
             }
         }
     }
     if ($this->condition && $query instanceof IKalturaIndexQuery) {
         $query->addMatch($this->condition);
     }
 }