Exemplo n.º 1
0
 /**
  *
  * @param string $filterJson
  * @param Object_Class $class
  * @return string
  */
 public static function getFilterCondition($filterJson, $class)
 {
     $systemFields = array("o_path", "o_key", "o_id", "o_published", "o_creationDate", "o_modificationDate");
     // create filter condition
     $conditionPartsFilters = array();
     if ($filterJson) {
         $filters = Zend_Json::decode($filterJson);
         foreach ($filters as $filter) {
             $operator = "=";
             if ($filter["type"] == "string") {
                 $operator = "LIKE";
             } else {
                 if ($filter["type"] == "numeric") {
                     if ($filter["comparison"] == "lt") {
                         $operator = "<";
                     } else {
                         if ($filter["comparison"] == "gt") {
                             $operator = ">";
                         } else {
                             if ($filter["comparison"] == "eq") {
                                 $operator = "=";
                             }
                         }
                     }
                 } else {
                     if ($filter["type"] == "date") {
                         if ($filter["comparison"] == "lt") {
                             $operator = "<";
                         } else {
                             if ($filter["comparison"] == "gt") {
                                 $operator = ">";
                             } else {
                                 if ($filter["comparison"] == "eq") {
                                     $operator = "=";
                                 }
                             }
                         }
                         $filter["value"] = strtotime($filter["value"]);
                     } else {
                         if ($filter["type"] == "list") {
                             $operator = "=";
                         } else {
                             if ($filter["type"] == "boolean") {
                                 $operator = "=";
                                 $filter["value"] = (int) $filter["value"];
                             }
                         }
                     }
                 }
             }
             $field = $class->getFieldDefinition($filter["field"]);
             $brickField = null;
             $brickType = null;
             if (!$field) {
                 // if the definition doesn't exist check for a localized field
                 $localized = $class->getFieldDefinition("localizedfields");
                 if ($localized instanceof Object_Class_Data_Localizedfields) {
                     $field = $localized->getFieldDefinition($filter["field"]);
                 }
                 //if the definition doesn't exist check for object brick
                 $keyParts = explode("~", $filter["field"]);
                 if (count($keyParts) > 1) {
                     $brickType = $keyParts[0];
                     $brickKey = $keyParts[1];
                     $key = self::getFieldForBrickType($class, $brickType);
                     $field = $class->getFieldDefinition($key);
                     $brickClass = Object_Objectbrick_Definition::getByKey($brickType);
                     $brickField = $brickClass->getFieldDefinition($brickKey);
                 }
             }
             if ($field instanceof Object_Class_Data_Objectbricks) {
                 // custom field
                 $db = Pimcore_Resource::get();
                 if (is_array($filter["value"])) {
                     $fieldConditions = array();
                     foreach ($filter["value"] as $filterValue) {
                         $fieldConditions[] = $db->getQuoteIdentifierSymbol() . $brickType . $db->getQuoteIdentifierSymbol() . "." . $brickField->getFilterCondition($filterValue, $operator);
                     }
                     $conditionPartsFilters[] = "(" . implode(" OR ", $fieldConditions) . ")";
                 } else {
                     $conditionPartsFilters[] = $db->getQuoteIdentifierSymbol() . $brickType . $db->getQuoteIdentifierSymbol() . "." . $brickField->getFilterCondition($filter["value"], $operator);
                 }
             } else {
                 if ($field instanceof Object_Class_Data) {
                     // custom field
                     if (is_array($filter["value"])) {
                         $fieldConditions = array();
                         foreach ($filter["value"] as $filterValue) {
                             $fieldConditions[] = $field->getFilterCondition($filterValue, $operator);
                         }
                         $conditionPartsFilters[] = "(" . implode(" OR ", $fieldConditions) . ")";
                     } else {
                         $conditionPartsFilters[] = $field->getFilterCondition($filter["value"], $operator);
                     }
                 } else {
                     if (in_array("o_" . $filter["field"], $systemFields)) {
                         // system field
                         $conditionPartsFilters[] = "`o_" . $filter["field"] . "` " . $operator . " '" . $filter["value"] . "' ";
                     }
                 }
             }
         }
     }
     $conditionFilters = "";
     if (count($conditionPartsFilters) > 0) {
         $conditionFilters = " AND (" . implode(" AND ", $conditionPartsFilters) . ")";
     }
     Logger::log("ObjectController filter condition:" . $conditionFilters);
     return $conditionFilters;
 }