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); } } } }
/** * returns the number of field set * WARNING - this method's params are different from fillObjectFromRequest due to the structure of the xml * The second parameter $prefix_to_add represents the string to append as prefix to each of the elements names. */ public function fillObjectFromXml(SimpleXMLElement $simple_xml_node, $prefix_to_add, $exclude_params = null) { $set_field_count = 0; $time_params_to_relative = $this->getRelativeTimeFields(); // iterate over all the paramters of the request foreach ($simple_xml_node as $param => $value) { // ignore empty strings in the filter ! if ($value == NULL || strlen($value) == 0) { continue; } if ($exclude_params != NULL && in_array($param, $exclude_params)) { continue; } if (in_array($param, $time_params_to_relative)) { $value = kTime::getRelativeTime($value); } // the field name is the rest of the string coming after the prefix $param_name = $prefix_to_add . $param; // TODO - should add a reg-exp array rather than this hard-coded logic in the base class !! // dont' fill the properties ending with backgroundImage (the avlues are WRONG!) if (kString::endsWith($param_name, "Image")) { continue; } $this->setByName($param_name, (string) html_entity_decode($value)); // cast the SimpleXMLElement to string !! $set_field_count++; } return $set_field_count; }
private function castSimpleType($type, $var) { switch ($type) { case "int": return (int) $var; case "string": return kString::stripUtf8InvalidChars((string) $var); case "bool": if (strtolower($var) === "false") { return false; } else { return (bool) $var; } case "float": return (double) $var; case "bigint": return (double) $var; case "time": if (!$this->disableRelativeTime) { $var = kTime::getRelativeTime($var); } return $var; } return null; }