Pimcore\Model\Object\Service::getFilterCondition PHP Method

getFilterCondition() public static method

public static getFilterCondition ( string $filterJson, ClassDefinition $class ) : string
$filterJson string
$class ClassDefinition
return string
    public static function getFilterCondition($filterJson, $class)
    {
        $systemFields = self::getSystemFields();
        // create filter condition
        $conditionPartsFilters = [];
        if ($filterJson) {
            $db = \Pimcore\Db::get();
            $filters = \Zend_Json::decode($filterJson);
            foreach ($filters as $filter) {
                $operator = "=";
                /**
                 * @extjs
                 */
                $filterField = $filter["field"];
                $filterOperator = $filter["comparison"];
                if (\Pimcore\Tool\Admin::isExtJS6()) {
                    $filterField = $filter["property"];
                    $filterOperator = $filter["operator"];
                }
                if ($filter["type"] == "string") {
                    $operator = "LIKE";
                } elseif ($filter["type"] == "numeric") {
                    if ($filterOperator == "lt") {
                        $operator = "<";
                    } elseif ($filterOperator == "gt") {
                        $operator = ">";
                    } elseif ($filterOperator == "eq") {
                        $operator = "=";
                    }
                } elseif ($filter["type"] == "date") {
                    if ($filterOperator == "lt") {
                        $operator = "<";
                    } elseif ($filterOperator == "gt") {
                        $operator = ">";
                    } elseif ($filterOperator == "eq") {
                        $operator = "=";
                    }
                    $filter["value"] = strtotime($filter["value"]);
                } elseif ($filter["type"] == "list") {
                    $operator = "=";
                } elseif ($filter["type"] == "boolean") {
                    $operator = "=";
                    $filter["value"] = (int) $filter["value"];
                }
                $field = $class->getFieldDefinition($filterField);
                $brickField = null;
                $brickType = null;
                if (!$field) {
                    // if the definition doesn't exist check for a localized field
                    $localized = $class->getFieldDefinition("localizedfields");
                    if ($localized instanceof ClassDefinition\Data\Localizedfields) {
                        $field = $localized->getFieldDefinition($filterField);
                    }
                    //if the definition doesn't exist check for object brick
                    $keyParts = explode("~", $filterField);
                    if (substr($filterField, 0, 1) == "~") {
                        // not needed for now
                        //                            $type = $keyParts[1];
                        //                            $field = $keyParts[2];
                        //                            $keyid = $keyParts[3];
                    } elseif (count($keyParts) > 1) {
                        $brickType = $keyParts[0];
                        $brickKey = $keyParts[1];
                        $key = self::getFieldForBrickType($class, $brickType);
                        $field = $class->getFieldDefinition($key);
                        $brickClass = Objectbrick\Definition::getByKey($brickType);
                        $brickField = $brickClass->getFieldDefinition($brickKey);
                    }
                }
                if ($field instanceof ClassDefinition\Data\Objectbricks) {
                    // custom field
                    $db = \Pimcore\Db::get();
                    if (is_array($filter["value"])) {
                        $fieldConditions = [];
                        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);
                    }
                } elseif ($field instanceof ClassDefinition\Data) {
                    // custom field
                    if (is_array($filter["value"])) {
                        $fieldConditions = [];
                        foreach ($filter["value"] as $filterValue) {
                            $fieldConditions[] = $field->getFilterCondition($filterValue, $operator);
                        }
                        $conditionPartsFilters[] = "(" . implode(" OR ", $fieldConditions) . ")";
                    } else {
                        $conditionPartsFilters[] = $field->getFilterCondition($filter["value"], $operator);
                    }
                } elseif (in_array("o_" . $filterField, $systemFields)) {
                    // system field
                    if ($filterField == "fullpath") {
                        $conditionPartsFilters[] = "concat(o_path, o_key) " . $operator . " " . $db->quote("%" . $filter["value"] . "%");
                    } else {
                        if ($filter['type'] == 'date' && $operator == '=') {
                            //if the equal operator is chosen with the date type, condition has to be changed
                            $maxTime = $filter['value'] + (86400 - 1);
                            //specifies the top point of the range used in the condition
                            $conditionPartsFilters[] = "`o_" . $filterField . "` BETWEEN " . $db->quote($filter["value"]) . " AND " . $db->quote($maxTime);
                        } else {
                            $conditionPartsFilters[] = "`o_" . $filterField . "` " . $operator . " " . $db->quote($filter["value"]);
                        }
                    }
                }
            }
        }
        $conditionFilters = "1 = 1";
        if (count($conditionPartsFilters) > 0) {
            $conditionFilters = "(" . implode(" AND ", $conditionPartsFilters) . ")";
        }
        Logger::log("ObjectController filter condition:" . $conditionFilters);
        return $conditionFilters;
    }

Usage Example

 public function gridProxyAction()
 {
     if ($this->getParam("language")) {
         $this->setLanguage($this->getParam("language"), true);
     }
     if ($this->getParam("data")) {
         if ($this->getParam("xaction") == "update") {
             try {
                 $data = \Zend_Json::decode($this->getParam("data"));
                 // save
                 $object = Object::getById($data["id"]);
                 /** @var Object\ClassDefinition $class */
                 $class = $object->getClass();
                 if (!$object->isAllowed("publish")) {
                     throw new \Exception("Permission denied. You don't have the rights to save this object.");
                 }
                 $user = Tool\Admin::getCurrentUser();
                 if (!$user->isAdmin()) {
                     $languagePermissions = $object->getPermissions("lEdit", $user);
                     $languagePermissions = explode(",", $languagePermissions["lEdit"]);
                 }
                 $objectData = array();
                 foreach ($data as $key => $value) {
                     $parts = explode("~", $key);
                     if (substr($key, 0, 1) == "~") {
                         $type = $parts[1];
                         $field = $parts[2];
                         $keyid = $parts[3];
                         $getter = "get" . ucfirst($field);
                         $setter = "set" . ucfirst($field);
                         $keyValuePairs = $object->{$getter}();
                         if (!$keyValuePairs) {
                             $keyValuePairs = new Object\Data\KeyValue();
                             $keyValuePairs->setObjectId($object->getId());
                             $keyValuePairs->setClass($object->getClass());
                         }
                         $keyValuePairs->setPropertyWithId($keyid, $value, true);
                         $object->{$setter}($keyValuePairs);
                     } elseif (count($parts) > 1) {
                         $brickType = $parts[0];
                         $brickKey = $parts[1];
                         $brickField = Object\Service::getFieldForBrickType($object->getClass(), $brickType);
                         $fieldGetter = "get" . ucfirst($brickField);
                         $brickGetter = "get" . ucfirst($brickType);
                         $valueSetter = "set" . ucfirst($brickKey);
                         $brick = $object->{$fieldGetter}()->{$brickGetter}();
                         if (empty($brick)) {
                             $classname = "\\Pimcore\\Model\\Object\\Objectbrick\\Data\\" . ucfirst($brickType);
                             $brickSetter = "set" . ucfirst($brickType);
                             $brick = new $classname($object);
                             $object->{$fieldGetter}()->{$brickSetter}($brick);
                         }
                         $brick->{$valueSetter}($value);
                     } else {
                         if (!$user->isAdmin() && $languagePermissions) {
                             $fd = $class->getFieldDefinition($key);
                             if (!$fd) {
                                 // try to get via localized fields
                                 $localized = $class->getFieldDefinition("localizedfields");
                                 if ($localized instanceof Object\ClassDefinition\Data\Localizedfields) {
                                     $field = $localized->getFieldDefinition($key);
                                     if ($field) {
                                         $currentLocale = (string) \Zend_Registry::get("Zend_Locale");
                                         if (!in_array($currentLocale, $languagePermissions)) {
                                             continue;
                                         }
                                     }
                                 }
                             }
                         }
                         $objectData[$key] = $value;
                     }
                 }
                 $object->setValues($objectData);
                 $object->save();
                 $this->_helper->json(array("data" => Object\Service::gridObjectData($object, $this->getParam("fields")), "success" => true));
             } catch (\Exception $e) {
                 $this->_helper->json(array("success" => false, "message" => $e->getMessage()));
             }
         }
     } else {
         // get list of objects
         $folder = Object::getById($this->getParam("folderId"));
         $class = Object\ClassDefinition::getById($this->getParam("classId"));
         $className = $class->getName();
         $colMappings = array("filename" => "o_key", "fullpath" => array("o_path", "o_key"), "id" => "o_id", "published" => "o_published", "modificationDate" => "o_modificationDate", "creationDate" => "o_creationDate");
         $start = 0;
         $limit = 20;
         $orderKey = "o_id";
         $order = "ASC";
         $fields = array();
         $bricks = array();
         if ($this->getParam("fields")) {
             $fields = $this->getParam("fields");
             foreach ($fields as $f) {
                 $parts = explode("~", $f);
                 $sub = substr($f, 0, 1);
                 if (substr($f, 0, 1) == "~") {
                     //                        $type = $parts[1];
                     //                        $field = $parts[2];
                     //                        $keyid = $parts[3];
                     // key value, ignore for now
                 } elseif (count($parts) > 1) {
                     $bricks[$parts[0]] = $parts[0];
                 }
             }
         }
         if ($this->getParam("limit")) {
             $limit = $this->getParam("limit");
         }
         if ($this->getParam("start")) {
             $start = $this->getParam("start");
         }
         $sortingSettings = \Pimcore\Admin\Helper\QueryParams::extractSortingSettings($this->getAllParams());
         if ($sortingSettings['order']) {
             $order = $sortingSettings['order'];
         }
         if (strlen($sortingSettings['orderKey']) > 0) {
             $orderKey = $sortingSettings['orderKey'];
             if (!(substr($orderKey, 0, 1) == "~")) {
                 if (array_key_exists($orderKey, $colMappings)) {
                     $orderKey = $colMappings[$orderKey];
                 }
             }
         }
         $listClass = "\\Pimcore\\Model\\Object\\" . ucfirst($className) . "\\Listing";
         $conditionFilters = array();
         if ($this->getParam("only_direct_children") == "true") {
             $conditionFilters[] = "o_parentId = " . $folder->getId();
         } else {
             $conditionFilters[] = "(o_path = '" . $folder->getFullPath() . "' OR o_path LIKE '" . str_replace("//", "/", $folder->getFullPath() . "/") . "%')";
         }
         // create filter condition
         if ($this->getParam("filter")) {
             $conditionFilters[] = Object\Service::getFilterCondition($this->getParam("filter"), $class);
         }
         if ($this->getParam("condition")) {
             $conditionFilters[] = "(" . $this->getParam("condition") . ")";
         }
         $list = new $listClass();
         if (!empty($bricks)) {
             foreach ($bricks as $b) {
                 $list->addObjectbrick($b);
             }
         }
         $list->setCondition(implode(" AND ", $conditionFilters));
         $list->setLimit($limit);
         $list->setOffset($start);
         $list->setOrder($order);
         $list->setOrderKey($orderKey);
         if ($class->getShowVariants()) {
             $list->setObjectTypes([Object\AbstractObject::OBJECT_TYPE_OBJECT, Object\AbstractObject::OBJECT_TYPE_VARIANT]);
         }
         $list->load();
         $objects = array();
         foreach ($list->getObjects() as $object) {
             $o = Object\Service::gridObjectData($object, $fields);
             $objects[] = $o;
         }
         $this->_helper->json(array("data" => $objects, "success" => true, "total" => $list->getTotalCount()));
     }
 }
All Usage Examples Of Pimcore\Model\Object\Service::getFilterCondition