getFilterCondition() public static method

public static getFilterCondition ( string $filterJson, ClassDefinition $class ) : string
$filterJson string
$class ClassDefinition
return string
 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()));
     }
 }
 public function getBatchJobsAction()
 {
     if ($this->getParam("language")) {
         $this->setLanguage($this->getParam("language"), true);
     }
     $folder = Object::getById($this->getParam("folderId"));
     $class = Object\ClassDefinition::getById($this->getParam("classId"));
     $conditionFilters = array("o_path = ? OR o_path LIKE '" . str_replace("//", "/", $folder->getFullPath() . "/") . "%'");
     if ($this->getParam("filter")) {
         $conditionFilters[] = Object\Service::getFilterCondition($this->getParam("filter"), $class);
     }
     if ($this->getParam("condition")) {
         $conditionFilters[] = " (" . $this->getParam("condition") . ")";
     }
     $className = $class->getName();
     $listClass = "\\Pimcore\\Model\\Object\\" . ucfirst($className) . "\\Listing";
     $list = new $listClass();
     $list->setCondition(implode(" AND ", $conditionFilters), array($folder->getFullPath()));
     $list->setOrder("ASC");
     $list->setOrderKey("o_id");
     if ($this->getParam("objecttype")) {
         $list->setObjectTypes(array($this->getParam("objecttype")));
     }
     $jobs = $list->loadIdList();
     $this->_helper->json(array("success" => true, "jobs" => $jobs));
 }
 /**
  * @return void
  */
 public function findAction()
 {
     $user = $this->getUser();
     $query = $this->getParam("query");
     if ($query == "*") {
         $query = "";
     }
     $query = str_replace("%", "*", $query);
     $types = explode(",", $this->getParam("type"));
     $subtypes = explode(",", $this->getParam("subtype"));
     $classnames = explode(",", $this->getParam("class"));
     if ($this->getParam("type") == "object" && is_array($classnames) && empty($classnames[0])) {
         $subtypes = array("object", "variant", "folder");
     }
     $offset = intval($this->getParam("start"));
     $limit = intval($this->getParam("limit"));
     $offset = $offset ? $offset : 0;
     $limit = $limit ? $limit : 50;
     $searcherList = new Data\Listing();
     $conditionParts = array();
     $db = \Pimcore\Db::get();
     //exclude forbidden assets
     if (in_array("asset", $types)) {
         if (!$user->isAllowed("assets")) {
             $forbiddenConditions[] = " `type` != 'asset' ";
         } else {
             $forbiddenAssetPaths = Element\Service::findForbiddenPaths("asset", $user);
             if (count($forbiddenAssetPaths) > 0) {
                 for ($i = 0; $i < count($forbiddenAssetPaths); $i++) {
                     $forbiddenAssetPaths[$i] = " (maintype = 'asset' AND fullpath not like " . $db->quote($forbiddenAssetPaths[$i] . "%") . ")";
                 }
                 $forbiddenConditions[] = implode(" AND ", $forbiddenAssetPaths);
             }
         }
     }
     //exclude forbidden documents
     if (in_array("document", $types)) {
         if (!$user->isAllowed("documents")) {
             $forbiddenConditions[] = " `type` != 'document' ";
         } else {
             $forbiddenDocumentPaths = Element\Service::findForbiddenPaths("document", $user);
             if (count($forbiddenDocumentPaths) > 0) {
                 for ($i = 0; $i < count($forbiddenDocumentPaths); $i++) {
                     $forbiddenDocumentPaths[$i] = " (maintype = 'document' AND fullpath not like " . $db->quote($forbiddenDocumentPaths[$i] . "%") . ")";
                 }
                 $forbiddenConditions[] = implode(" AND ", $forbiddenDocumentPaths);
             }
         }
     }
     //exclude forbidden objects
     if (in_array("object", $types)) {
         if (!$user->isAllowed("objects")) {
             $forbiddenConditions[] = " `type` != 'object' ";
         } else {
             $forbiddenObjectPaths = Element\Service::findForbiddenPaths("object", $user);
             if (count($forbiddenObjectPaths) > 0) {
                 for ($i = 0; $i < count($forbiddenObjectPaths); $i++) {
                     $forbiddenObjectPaths[$i] = " (maintype = 'object' AND fullpath not like " . $db->quote($forbiddenObjectPaths[$i] . "%") . ")";
                 }
                 $forbiddenConditions[] = implode(" AND ", $forbiddenObjectPaths);
             }
         }
     }
     if ($forbiddenConditions) {
         $conditionParts[] = "(" . implode(" AND ", $forbiddenConditions) . ")";
     }
     if (!empty($query)) {
         $queryCondition = "( MATCH (`data`,`properties`) AGAINST (" . $db->quote($query) . " IN BOOLEAN MODE) )";
         // the following should be done with an exact-search now "ID", because the Element-ID is now in the fulltext index
         // if the query is numeric the user might want to search by id
         //if(is_numeric($query)) {
         //$queryCondition = "(" . $queryCondition . " OR id = " . $db->quote($query) ." )";
         //}
         $conditionParts[] = $queryCondition;
     }
     //For objects - handling of bricks
     $fields = array();
     $bricks = array();
     if ($this->getParam("fields")) {
         $fields = $this->getParam("fields");
         foreach ($fields as $f) {
             $parts = explode("~", $f);
             if (substr($f, 0, 1) == "~") {
                 //                    $type = $parts[1];
                 //                    $field = $parts[2];
                 //                    $keyid = $parts[3];
                 // key value, ignore for now
             } else {
                 if (count($parts) > 1) {
                     $bricks[$parts[0]] = $parts[0];
                 }
             }
         }
     }
     // filtering for objects
     if ($this->getParam("filter") && $this->getParam("class")) {
         $class = Object\ClassDefinition::getByName($this->getParam("class"));
         $conditionFilters = Object\Service::getFilterCondition($this->getParam("filter"), $class);
         $join = "";
         foreach ($bricks as $ob) {
             $join .= " LEFT JOIN object_brick_query_" . $ob . "_" . $class->getId();
             $join .= " `" . $ob . "`";
             $join .= " ON `" . $ob . "`.o_id = `object_" . $class->getId() . "`.o_id";
         }
         $conditionParts[] = "( id IN (SELECT `object_" . $class->getId() . "`.o_id FROM object_" . $class->getId() . $join . " WHERE " . $conditionFilters . ") )";
     }
     if (is_array($types) and !empty($types[0])) {
         foreach ($types as $type) {
             $conditionTypeParts[] = $db->quote($type);
         }
         if (in_array("folder", $subtypes)) {
             $conditionTypeParts[] = $db->quote('folder');
         }
         $conditionParts[] = "( maintype IN (" . implode(",", $conditionTypeParts) . ") )";
     }
     if (is_array($subtypes) and !empty($subtypes[0])) {
         foreach ($subtypes as $subtype) {
             $conditionSubtypeParts[] = $db->quote($subtype);
         }
         $conditionParts[] = "( type IN (" . implode(",", $conditionSubtypeParts) . ") )";
     }
     if (is_array($classnames) and !empty($classnames[0])) {
         if (in_array("folder", $subtypes)) {
             $classnames[] = "folder";
         }
         foreach ($classnames as $classname) {
             $conditionClassnameParts[] = $db->quote($classname);
         }
         $conditionParts[] = "( subtype IN (" . implode(",", $conditionClassnameParts) . ") )";
     }
     if (count($conditionParts) > 0) {
         $condition = implode(" AND ", $conditionParts);
         //echo $condition; die();
         $searcherList->setCondition($condition);
     }
     $searcherList->setOffset($offset);
     $searcherList->setLimit($limit);
     // do not sort per default, it is VERY SLOW
     //$searcherList->setOrder("desc");
     //$searcherList->setOrderKey("modificationdate");
     if ($this->getParam("sort")) {
         // we need a special mapping for classname as this is stored in subtype column
         $sortMapping = ["classname" => "subtype"];
         $sort = $this->getParam("sort");
         if (array_key_exists($this->getParam("sort"), $sortMapping)) {
             $sort = $sortMapping[$this->getParam("sort")];
         }
         $searcherList->setOrderKey($sort);
     }
     if ($this->getParam("dir")) {
         $searcherList->setOrder($this->getParam("dir"));
     }
     $hits = $searcherList->load();
     $elements = array();
     foreach ($hits as $hit) {
         $element = Element\Service::getElementById($hit->getId()->getType(), $hit->getId()->getId());
         if ($element->isAllowed("list")) {
             if ($element instanceof Object\AbstractObject) {
                 $data = Object\Service::gridObjectData($element, $fields);
             } else {
                 if ($element instanceof Document) {
                     $data = Document\Service::gridDocumentData($element);
                 } else {
                     if ($element instanceof Asset) {
                         $data = Asset\Service::gridAssetData($element);
                     }
                 }
             }
             $elements[] = $data;
         } else {
             //TODO: any message that view is blocked?
             //$data = Element\Service::gridElementData($element);
         }
     }
     // only get the real total-count when the limit parameter is given otherwise use the default limit
     if ($this->getParam("limit")) {
         $totalMatches = $searcherList->getTotalCount();
     } else {
         $totalMatches = count($elements);
     }
     $this->_helper->json(array("data" => $elements, "success" => true, "total" => $totalMatches));
     $this->removeViewRenderer();
 }
Example #4
0
 public function getVariantsAction()
 {
     // get list of variants
     if ($this->getParam("language")) {
         $this->setLanguage($this->getParam("language"), true);
     }
     if ($this->getParam("xaction") == "update") {
         $data = \Zend_Json::decode($this->getParam("data"));
         // save
         $object = Object::getById($data["id"]);
         if ($object->isAllowed("publish")) {
             $objectData = [];
             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 {
                     $objectData[$key] = $value;
                 }
             }
             $object->setValues($objectData);
             try {
                 $object->save();
                 $this->_helper->json(["data" => Object\Service::gridObjectData($object, $this->getParam("fields")), "success" => true]);
             } catch (\Exception $e) {
                 $this->_helper->json(["success" => false, "message" => $e->getMessage()]);
             }
         } else {
             throw new \Exception("Permission denied");
         }
     } else {
         $parentObject = Object\Concrete::getById($this->getParam("objectId"));
         if (empty($parentObject)) {
             throw new \Exception("No Object found with id " . $this->getParam("objectId"));
         }
         if ($parentObject->isAllowed("view")) {
             $class = $parentObject->getClass();
             $className = $parentObject->getClass()->getName();
             $start = 0;
             $limit = 15;
             $orderKey = "o_id";
             $order = "ASC";
             $fields = [];
             $bricks = [];
             if ($this->getParam("fields")) {
                 $fields = $this->getParam("fields");
                 foreach ($fields as $f) {
                     $parts = explode("~", $f);
                     if (count($parts) > 1) {
                         $bricks[$parts[0]] = $parts[0];
                     }
                 }
             }
             if ($this->getParam("limit")) {
                 $limit = $this->getParam("limit");
             }
             if ($this->getParam("start")) {
                 $start = $this->getParam("start");
             }
             $orderKey = "o_id";
             $order = "ASC";
             $colMappings = ["filename" => "o_key", "fullpath" => ["o_path", "o_key"], "id" => "o_id", "published" => "o_published", "modificationDate" => "o_modificationDate", "creationDate" => "o_creationDate"];
             $sortingSettings = \Pimcore\Admin\Helper\QueryParams::extractSortingSettings($this->getAllParams());
             if ($sortingSettings['orderKey'] && $sortingSettings['order']) {
                 $orderKey = $sortingSettings['orderKey'];
                 if (array_key_exists($orderKey, $colMappings)) {
                     $orderKey = $colMappings[$orderKey];
                 }
                 $order = $sortingSettings['order'];
             }
             if ($this->getParam("dir")) {
                 $order = $this->getParam("dir");
             }
             $listClass = "\\Pimcore\\Model\\Object\\" . ucfirst($className) . "\\Listing";
             $conditionFilters = ["o_parentId = " . $parentObject->getId()];
             // 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);
             $list->setObjectTypes([Object\AbstractObject::OBJECT_TYPE_VARIANT]);
             $list->load();
             $objects = [];
             foreach ($list->getObjects() as $object) {
                 if ($object->isAllowed("view")) {
                     $o = Object\Service::gridObjectData($object, $fields);
                     $objects[] = $o;
                 }
             }
             $this->_helper->json(["data" => $objects, "success" => true, "total" => $list->getTotalCount()]);
         } else {
             throw new \Exception("Permission denied");
         }
     }
 }
Example #5
0
 public function gridProxyAction()
 {
     $requestedLanguage = $this->getParam("language");
     if ($requestedLanguage) {
         if ($requestedLanguage != "default") {
             $this->setLanguage($requestedLanguage, true);
         }
     } else {
         $requestedLanguage = $this->getLanguage();
     }
     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();
                 $allLanguagesAllowed = false;
                 if (!$user->isAdmin()) {
                     $languagePermissions = $object->getPermissions("lEdit", $user);
                     //sets allowed all languages modification when the lEdit column is empty
                     $allLanguagesAllowed = $languagePermissions["lEdit"] == '';
                     $languagePermissions = explode(",", $languagePermissions["lEdit"]);
                 }
                 $objectData = [];
                 foreach ($data as $key => $value) {
                     $parts = explode("~", $key);
                     if (substr($key, 0, 1) == "~") {
                         $type = $parts[1];
                         $field = $parts[2];
                         $keyid = $parts[3];
                         if ($type == "classificationstore") {
                             $groupKeyId = explode("-", $keyid);
                             $groupId = $groupKeyId[0];
                             $keyid = $groupKeyId[1];
                             $getter = "get" . ucfirst($field);
                             if (method_exists($object, $getter)) {
                                 /** @var  $classificationStoreData Object\Classificationstore */
                                 $classificationStoreData = $object->{$getter}();
                                 $classificationStoreData->setLocalizedKeyValue($groupId, $keyid, $value, $requestedLanguage);
                             }
                         } else {
                             $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 (!$allLanguagesAllowed && !in_array($currentLocale, $languagePermissions)) {
                                             continue;
                                         }
                                     }
                                 }
                             }
                         }
                         $objectData[$key] = $value;
                     }
                 }
                 $object->setValues($objectData);
                 $object->save();
                 $this->_helper->json(["data" => Object\Service::gridObjectData($object, $this->getParam("fields"), $requestedLanguage), "success" => true]);
             } catch (\Exception $e) {
                 $this->_helper->json(["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 = ["filename" => "o_key", "fullpath" => ["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 = [];
         $bricks = [];
         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
                     if ($type == "classificationstore") {
                     }
                 } 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());
         $doNotQuote = false;
         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];
                 } elseif ($class->getFieldDefinition($orderKey) instanceof Object\ClassDefinition\Data\QuantityValue) {
                     $orderKey = "concat(" . $orderKey . "__unit, " . $orderKey . "__value)";
                     $doNotQuote = true;
                 } elseif (strpos($orderKey, "~") !== false) {
                     $orderKeyParts = explode("~", $orderKey);
                     if (count($orderKeyParts) == 2) {
                         $orderKey = $orderKeyParts[1];
                     }
                 }
             }
         }
         $listClass = "\\Pimcore\\Model\\Object\\" . ucfirst($className) . "\\Listing";
         $conditionFilters = [];
         if ($this->getParam("only_direct_children") == "true") {
             $conditionFilters[] = "o_parentId = " . $folder->getId();
         } else {
             $conditionFilters[] = "(o_path = '" . $folder->getRealFullPath() . "' OR o_path LIKE '" . str_replace("//", "/", $folder->getRealFullPath() . "/") . "%')";
         }
         if (!$this->getUser()->isAdmin()) {
             $userIds = $this->getUser()->getRoles();
             $userIds[] = $this->getUser()->getId();
             $conditionFilters[] .= " (\n                                                    (select list from users_workspaces_object where userId in (" . implode(',', $userIds) . ") and LOCATE(CONCAT(o_path,o_key),cpath)=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1\n                                                    OR\n                                                    (select list from users_workspaces_object where userId in (" . implode(',', $userIds) . ") and LOCATE(cpath,CONCAT(o_path,o_key))=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1\n                                                 )";
         }
         $featureJoins = [];
         $featureFilters = false;
         // create filter condition
         if ($this->getParam("filter")) {
             $conditionFilters[] = Object\Service::getFilterCondition($this->getParam("filter"), $class);
             $featureFilters = Object\Service::getFeatureFilters($this->getParam("filter"), $class);
             if ($featureFilters) {
                 $featureJoins = array_merge($featureJoins, $featureFilters["joins"]);
             }
         }
         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);
         if (isset($sortingSettings["isFeature"]) && $sortingSettings["isFeature"]) {
             $orderKey = "cskey_" . $sortingSettings["fieldname"] . "_" . $sortingSettings["groupId"] . "_" . $sortingSettings["keyId"];
             $list->setOrderKey($orderKey);
             $list->setGroupBy("o_id");
             $featureJoins[] = $sortingSettings;
         } else {
             $list->setOrderKey($orderKey, !$doNotQuote);
         }
         $list->setOrder($order);
         if ($class->getShowVariants()) {
             $list->setObjectTypes([Object\AbstractObject::OBJECT_TYPE_OBJECT, Object\AbstractObject::OBJECT_TYPE_VARIANT]);
         }
         Object\Service::addGridFeatureJoins($list, $featureJoins, $class, $featureFilters, $requestedLanguage);
         $list->load();
         $objects = [];
         foreach ($list->getObjects() as $object) {
             $o = Object\Service::gridObjectData($object, $fields, $requestedLanguage);
             $objects[] = $o;
         }
         $this->_helper->json(["data" => $objects, "success" => true, "total" => $list->getTotalCount()]);
     }
 }
Example #6
0
 /**
  * @return void
  */
 public function findAction()
 {
     $user = $this->getUser();
     $query = $this->getParam("query");
     if ($query == "*") {
         $query = "";
     }
     $query = str_replace("%", "*", $query);
     $query = preg_replace("@([^ ])\\-@", "\$1 ", $query);
     $types = explode(",", $this->getParam("type"));
     $subtypes = explode(",", $this->getParam("subtype"));
     $classnames = explode(",", $this->getParam("class"));
     if ($this->getParam("type") == "object" && is_array($classnames) && empty($classnames[0])) {
         $subtypes = ["object", "variant", "folder"];
     }
     $offset = intval($this->getParam("start"));
     $limit = intval($this->getParam("limit"));
     $offset = $offset ? $offset : 0;
     $limit = $limit ? $limit : 50;
     $searcherList = new Data\Listing();
     $conditionParts = [];
     $db = \Pimcore\Db::get();
     //exclude forbidden assets
     if (in_array("asset", $types)) {
         if (!$user->isAllowed("assets")) {
             $forbiddenConditions[] = " `type` != 'asset' ";
         } else {
             $forbiddenAssetPaths = Element\Service::findForbiddenPaths("asset", $user);
             if (count($forbiddenAssetPaths) > 0) {
                 for ($i = 0; $i < count($forbiddenAssetPaths); $i++) {
                     $forbiddenAssetPaths[$i] = " (maintype = 'asset' AND fullpath not like " . $db->quote($forbiddenAssetPaths[$i] . "%") . ")";
                 }
                 $forbiddenConditions[] = implode(" AND ", $forbiddenAssetPaths);
             }
         }
     }
     //exclude forbidden documents
     if (in_array("document", $types)) {
         if (!$user->isAllowed("documents")) {
             $forbiddenConditions[] = " `type` != 'document' ";
         } else {
             $forbiddenDocumentPaths = Element\Service::findForbiddenPaths("document", $user);
             if (count($forbiddenDocumentPaths) > 0) {
                 for ($i = 0; $i < count($forbiddenDocumentPaths); $i++) {
                     $forbiddenDocumentPaths[$i] = " (maintype = 'document' AND fullpath not like " . $db->quote($forbiddenDocumentPaths[$i] . "%") . ")";
                 }
                 $forbiddenConditions[] = implode(" AND ", $forbiddenDocumentPaths);
             }
         }
     }
     //exclude forbidden objects
     if (in_array("object", $types)) {
         if (!$user->isAllowed("objects")) {
             $forbiddenConditions[] = " `type` != 'object' ";
         } else {
             $forbiddenObjectPaths = Element\Service::findForbiddenPaths("object", $user);
             if (count($forbiddenObjectPaths) > 0) {
                 for ($i = 0; $i < count($forbiddenObjectPaths); $i++) {
                     $forbiddenObjectPaths[$i] = " (maintype = 'object' AND fullpath not like " . $db->quote($forbiddenObjectPaths[$i] . "%") . ")";
                 }
                 $forbiddenConditions[] = implode(" AND ", $forbiddenObjectPaths);
             }
         }
     }
     if ($forbiddenConditions) {
         $conditionParts[] = "(" . implode(" AND ", $forbiddenConditions) . ")";
     }
     if (!empty($query)) {
         $queryCondition = "( MATCH (`data`,`properties`) AGAINST (" . $db->quote($query) . " IN BOOLEAN MODE) )";
         // the following should be done with an exact-search now "ID", because the Element-ID is now in the fulltext index
         // if the query is numeric the user might want to search by id
         //if(is_numeric($query)) {
         //$queryCondition = "(" . $queryCondition . " OR id = " . $db->quote($query) ." )";
         //}
         $conditionParts[] = $queryCondition;
     }
     //For objects - handling of bricks
     $fields = [];
     $bricks = [];
     if ($this->getParam("fields")) {
         $fields = $this->getParam("fields");
         foreach ($fields as $f) {
             $parts = explode("~", $f);
             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];
             }
         }
     }
     // filtering for objects
     if ($this->getParam("filter") && $this->getParam("class")) {
         $class = Object\ClassDefinition::getByName($this->getParam("class"));
         // add Localized Fields filtering
         $params = \Zend_Json::decode($this->getParam('filter'));
         $unlocalizedFieldsFilters = [];
         $localizedFieldsFilters = [];
         foreach ($params as $paramConditionObject) {
             //this loop divides filter parameters to localized and unlocalized groups
             $definitionExists = in_array('o_' . $paramConditionObject['property'], Object\Service::getSystemFields()) || $class->getFieldDefinition($paramConditionObject['property']);
             if ($definitionExists) {
                 //TODO: for sure, we can add additional condition like getLocalizedFieldDefinition()->getFieldDefiniton(...
                 $unlocalizedFieldsFilters[] = $paramConditionObject;
             } else {
                 $localizedFieldsFilters[] = $paramConditionObject;
             }
         }
         //get filter condition only when filters array is not empty
         //string statements for divided filters
         $conditionFilters = count($unlocalizedFieldsFilters) ? Object\Service::getFilterCondition(\Zend_Json::encode($unlocalizedFieldsFilters), $class) : null;
         $localizedConditionFilters = count($localizedFieldsFilters) ? Object\Service::getFilterCondition(\Zend_Json::encode($localizedFieldsFilters), $class) : null;
         $join = "";
         foreach ($bricks as $ob) {
             $join .= " LEFT JOIN object_brick_query_" . $ob . "_" . $class->getId();
             $join .= " `" . $ob . "`";
             $join .= " ON `" . $ob . "`.o_id = `object_" . $class->getId() . "`.o_id";
         }
         if (null !== $conditionFilters) {
             //add condition query for non localised fields
             $conditionParts[] = "( id IN (SELECT `object_" . $class->getId() . "`.o_id FROM object_" . $class->getId() . $join . " WHERE " . $conditionFilters . ") )";
         }
         if (null !== $localizedConditionFilters) {
             //add condition query for localised fields
             $conditionParts[] = "( id IN (SELECT `object_localized_data_" . $class->getId() . "`.ooo_id FROM object_localized_data_" . $class->getId() . $join . " WHERE " . $localizedConditionFilters . " GROUP BY ooo_id " . ") )";
         }
     }
     if (is_array($types) and !empty($types[0])) {
         foreach ($types as $type) {
             $conditionTypeParts[] = $db->quote($type);
         }
         if (in_array("folder", $subtypes)) {
             $conditionTypeParts[] = $db->quote('folder');
         }
         $conditionParts[] = "( maintype IN (" . implode(",", $conditionTypeParts) . ") )";
     }
     if (is_array($subtypes) and !empty($subtypes[0])) {
         foreach ($subtypes as $subtype) {
             $conditionSubtypeParts[] = $db->quote($subtype);
         }
         $conditionParts[] = "( type IN (" . implode(",", $conditionSubtypeParts) . ") )";
     }
     if (is_array($classnames) and !empty($classnames[0])) {
         if (in_array("folder", $subtypes)) {
             $classnames[] = "folder";
         }
         foreach ($classnames as $classname) {
             $conditionClassnameParts[] = $db->quote($classname);
         }
         $conditionParts[] = "( subtype IN (" . implode(",", $conditionClassnameParts) . ") )";
     }
     //filtering for tags
     $tagIds = $this->getParam("tagIds");
     if ($tagIds) {
         foreach ($tagIds as $tagId) {
             foreach ($types as $type) {
                 if ($this->getParam("considerChildTags") == "true") {
                     $tag = Pimcore\Model\Element\Tag::getById($tagId);
                     if ($tag) {
                         $tagPath = $tag->getFullIdPath();
                         $conditionParts[] = "id IN (SELECT cId FROM tags_assignment INNER JOIN tags ON tags.id = tags_assignment.tagid WHERE ctype = " . $db->quote($type) . " AND (id = " . intval($tagId) . " OR idPath LIKE " . $db->quote($tagPath . "%") . "))";
                     }
                 } else {
                     $conditionParts[] = "id IN (SELECT cId FROM tags_assignment WHERE ctype = " . $db->quote($type) . " AND tagid = " . intval($tagId) . ")";
                 }
             }
         }
     }
     if (count($conditionParts) > 0) {
         $condition = implode(" AND ", $conditionParts);
         //echo $condition; die();
         $searcherList->setCondition($condition);
     }
     $searcherList->setOffset($offset);
     $searcherList->setLimit($limit);
     // do not sort per default, it is VERY SLOW
     //$searcherList->setOrder("desc");
     //$searcherList->setOrderKey("modificationdate");
     $sortingSettings = \Pimcore\Admin\Helper\QueryParams::extractSortingSettings($this->getAllParams());
     if ($sortingSettings['orderKey']) {
         // we need a special mapping for classname as this is stored in subtype column
         $sortMapping = ["classname" => "subtype"];
         $sort = $sortingSettings['orderKey'];
         if (array_key_exists($sortingSettings['orderKey'], $sortMapping)) {
             $sort = $sortMapping[$sortingSettings['orderKey']];
         }
         $searcherList->setOrderKey($sortingSettings['orderKey']);
     }
     if ($sortingSettings['order']) {
         $searcherList->setOrder($sortingSettings['order']);
     }
     $hits = $searcherList->load();
     $elements = [];
     foreach ($hits as $hit) {
         $element = Element\Service::getElementById($hit->getId()->getType(), $hit->getId()->getId());
         if ($element->isAllowed("list")) {
             if ($element instanceof Object\AbstractObject) {
                 $data = Object\Service::gridObjectData($element, $fields);
             } elseif ($element instanceof Document) {
                 $data = Document\Service::gridDocumentData($element);
             } elseif ($element instanceof Asset) {
                 $data = Asset\Service::gridAssetData($element);
             }
             $elements[] = $data;
         } else {
             //TODO: any message that view is blocked?
             //$data = Element\Service::gridElementData($element);
         }
     }
     // only get the real total-count when the limit parameter is given otherwise use the default limit
     if ($this->getParam("limit")) {
         $totalMatches = $searcherList->getTotalCount();
     } else {
         $totalMatches = count($elements);
     }
     $this->_helper->json(["data" => $elements, "success" => true, "total" => $totalMatches]);
     $this->removeViewRenderer();
 }