public function addCondition($condition)
 {
     if ($this->parentQuery) {
         $this->parentQuery->addCondition($condition);
     }
 }
 public function apply(IKalturaIndexQuery $query)
 {
     $this->currentQuery = $query;
     $field = $this->getTable() . '.' . $this->getColumn();
     // Can apply criterion
     KalturaLog::debug("Applies criterion [{$field}]");
     $comparison = $this->getComparison();
     if ($comparison == Criteria::CUSTOM || $comparison == Criteria::CUSTOM_EQUAL || $comparison == Criteria::ISNOTNULL) {
         KalturaLog::debug("Skip criterion[{$field}] unhandled comparison [{$comparison}]");
         return false;
     }
     if (!$this->criteria->hasSphinxFieldName($field)) {
         KalturaLog::debug("Skip criterion[{$field}] has no sphinx field");
         return false;
     }
     $value = $this->getValue();
     $sphinxField = $this->criteria->getSphinxFieldName($field);
     $type = $this->criteria->getSphinxFieldType($sphinxField);
     // Update value & comparison in case of id field
     if ($field == $this->criteria->getIdField()) {
         if ($comparison == Criteria::EQUAL) {
             $comparison = Criteria::IN;
         }
         if ($comparison == Criteria::NOT_EQUAL) {
             $comparison = Criteria::NOT_IN;
         }
         if (!is_array($value)) {
             $value = explode(',', $value);
         }
         $ids = array();
         foreach ($value as $val) {
             $ids[$val] = $this->criteria->getTranslateIndexId($val);
         }
         $value = $ids;
         $this->criteria->setIds($comparison, $ids);
     }
     $valStr = print_r($value, true);
     KalturaLog::debug("Attach criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] and comparison[{$comparison}] for value[{$valStr}]");
     // update comparison in case of null
     if (is_null($value)) {
         if ($comparison == Criteria::EQUAL) {
             $comparison = Criteria::ISNULL;
         } elseif ($comparison == Criteria::NOT_EQUAL) {
             $comparison = Criteria::ISNOTNULL;
         }
     }
     // update value in case of Date
     if (is_string($value)) {
         // needed since otherwise the switch statement doesn't work as expected otherwise
         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;
         }
     }
     if ($type == IIndexable::FIELD_TYPE_STRING) {
         $match = $this->getStringMatchClause($sphinxField, $comparison, $value);
         KalturaLog::debug("Add match criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] match [{$match}] line [" . __LINE__ . "]");
         $this->addMatch($match);
     } else {
         $condition = $this->getNonStringClause($sphinxField, $type, $comparison, $value);
         KalturaLog::debug("Add condition criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] condition [{$condition}] line [" . __LINE__ . "]");
         $this->addCondition($condition);
     }
     $clauses = $this->getClauses();
     foreach ($clauses as $index => $clause) {
         if (!$clause instanceof SphinxCriterion) {
             KalturaLog::debug("Clause [" . $clause->getColumn() . "] is not Sphinx criterion");
             return false;
         }
         if (!$clause->apply($this)) {
             KalturaLog::debug("Failed to apply clause [" . $clause->getColumn() . "]");
             return false;
         }
     }
     if (count($this->matchClause)) {
         $matchesClause = implode(' ', $this->matchClause);
         if ($this->hasOr() && count($this->matchClause) > 1) {
             $matchesClause = "({$matchesClause})";
         }
         $match = $this->getSelfMatchOperator() . $matchesClause;
         KalturaLog::debug("Add match criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] match [{$match}] line [" . __LINE__ . "]");
         $query->addMatch($match);
     }
     if (count($this->conditionClause)) {
         $attributesClause = implode($this->getSelfConjunction(), array_unique($this->conditionClause));
         if (!strlen(trim($attributesClause))) {
             return true;
         }
         if (!$this->hasOr()) {
             $where = $attributesClause;
             KalturaLog::debug("Add where criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] where [{$where}] line [" . __LINE__ . "]");
             $query->addWhere($where);
             return true;
         }
         // Reduce null
         $expSimplifications = array("({$sphinxField} <> 0 AND {$sphinxField} <= {$value}) OR ({$sphinxField} = 0)" => "{$sphinxField} <= {$value}", "({$sphinxField} <> 0 AND {$sphinxField} < {$value}) OR ({$sphinxField} = 0)" => "{$sphinxField} < {$value}");
         if (isset($expSimplifications[$attributesClause])) {
             KalturaLog::debug("Simplifying expression [{$attributesClause}] => [{$expSimplifications[$attributesClause]}]");
             // We move it to the 'where' since where is allegedly faster than in the condition.
             $where = $this->getSelfConjunction() . $expSimplifications[$attributesClause];
             KalturaLog::debug("Add where criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] where [{$where}] line [" . __LINE__ . "]");
             $query->addWhere($where);
         } else {
             if ($this->needsBrackets()) {
                 $attributesClause = "({$attributesClause})";
             }
             $condition = $this->getSelfConjunction() . $attributesClause;
             KalturaLog::debug("Add condition criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] condition [{$condition}] line [" . __LINE__ . "]");
             $query->addCondition($condition);
         }
     }
     return true;
 }
 public function addCondition($condition)
 {
     if ($this->parentQuery && $this->parentQuery instanceof IKalturaIndexQuery) {
         $this->parentQuery->addCondition($condition);
     }
 }