public function gridProxyAction()
 {
     if ($this->_getParam("language")) {
         $this->setLanguage($this->_getParam("language"));
     }
     if ($this->_getParam("data")) {
         if ($this->_getParam("xaction") == "update") {
             $data = Zend_Json::decode($this->_getParam("data"));
             // save
             $object = Object_Abstract::getById($data["id"]);
             $objectData = array();
             foreach ($data as $key => $value) {
                 $parts = explode("~", $key);
                 if (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 = "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(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_Abstract::getById($this->_getParam("folderId"));
         $class = Object_Class::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);
                 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");
         }
         if ($this->_getParam("sort")) {
             if (array_key_exists($this->_getParam("sort"), $colMappings)) {
                 $orderKey = $colMappings[$this->_getParam("sort")];
             } else {
                 $orderKey = $this->_getParam("sort");
             }
         }
         if ($this->_getParam("dir")) {
             $order = $this->_getParam("dir");
         }
         $listClass = "Object_" . ucfirst($className) . "_List";
         // create filter condition
         if ($this->_getParam("filter")) {
             $conditionFilters = Object_Service::getFilterCondition($this->_getParam("filter"), $class);
         }
         if ($this->_getParam("condition")) {
             $conditionFilters = " AND (" . $this->_getParam("condition") . ")";
         }
         $list = new $listClass();
         if (!empty($bricks)) {
             foreach ($bricks as $b) {
                 $list->addObjectbrick($b);
             }
         }
         if ($this->_getParam("only_direct_children") == "true") {
             $pathCondition = "o_parentId = " . $folder->getId();
         } else {
             $pathCondition = "(o_path = '" . $folder->getFullPath() . "' OR o_path LIKE '" . str_replace("//", "/", $folder->getFullPath() . "/") . "%')";
         }
         $list->setCondition($pathCondition . $conditionFilters);
         $list->setLimit($limit);
         $list->setOffset($start);
         $list->setOrder($order);
         $list->setOrderKey($orderKey);
         $list->setIgnoreLocale(true);
         $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()));
     }
 }
 /**
  * @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"));
     $offset = intval($this->_getParam("start"));
     $limit = intval($this->_getParam("limit"));
     $offset = $offset ? $offset : 0;
     $limit = $limit ? $limit : 50;
     $searcherList = new Search_Backend_Data_List();
     $conditionParts = array();
     $db = Pimcore_Resource::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 (count($parts) > 1) {
                 $bricks[$parts[0]] = $parts[0];
             }
         }
     }
     // filtering for objects
     if ($this->_getParam("filter")) {
         $class = Object_Class::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 1=1 " . $conditionFilters . ") )";
     }
     if (is_array($types) and !empty($types[0])) {
         foreach ($types as $type) {
             $conditionTypeParts[] = $db->quote($type);
         }
         $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")) {
         $searcherList->setOrderKey($this->_getParam("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_Abstract) {
                 $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();
 }
 public function getVariantsAction()
 {
     // get list of variants
     if ($this->_getParam("xaction") == "update") {
         $data = Zend_Json::decode($this->_getParam("data"));
         // save
         $object = Object_Abstract::getById($data["id"]);
         $objectData = array();
         foreach ($data as $key => $value) {
             $parts = explode("~", $key);
             if (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 = "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(array("data" => Object_Service::gridObjectData($object, $this->_getParam("fields")), "success" => true));
         } catch (Exception $e) {
             $this->_helper->json(array("success" => false, "message" => $e->getMessage()));
         }
     } else {
         $parentObject = Object_Concrete::getById($this->_getParam("objectId"));
         if (empty($parentObject)) {
             throw new Exception("No Object found with id " . $this->_getParam("objectId"));
         }
         $class = $parentObject->getO_class();
         $className = $parentObject->getO_class()->getName();
         $start = 0;
         $limit = 15;
         $orderKey = "o_id";
         $order = "ASC";
         $fields = array();
         $bricks = array();
         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");
         }
         if ($this->_getParam("sort")) {
             if ($this->_getParam("sort") == "fullpath") {
                 $orderKey = array("o_path", "o_key");
             } else {
                 if ($this->_getParam("sort") == "id") {
                     $orderKey = "o_id";
                 } else {
                     if ($this->_getParam("sort") == "published") {
                         $orderKey = "o_published";
                     } else {
                         if ($this->_getParam("sort") == "modificationDate") {
                             $orderKey = "o_modificationDate";
                         } else {
                             if ($this->_getParam("sort") == "creationDate") {
                                 $orderKey = "o_creationDate";
                             } else {
                                 $orderKey = $this->_getParam("sort");
                             }
                         }
                     }
                 }
             }
         }
         if ($this->_getParam("dir")) {
             $order = $this->_getParam("dir");
         }
         $listClass = "Object_" . ucfirst($className) . "_List";
         $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 .= " AND (" . $this->_getParam("condition") . ")";
         }
         $list = new $listClass();
         if (!empty($bricks)) {
             foreach ($bricks as $b) {
                 $list->addObjectbrick($b);
             }
         }
         $list->setCondition($conditionFilters);
         $list->setLimit($limit);
         $list->setOffset($start);
         $list->setOrder($order);
         $list->setOrderKey($orderKey);
         $list->setIgnoreLocale(true);
         $list->setObjectTypes(array(Object_Abstract::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 exportCustomAction()
 {
     $folder = Object_Abstract::getById($this->_getParam("folderId"));
     $class = Object_Class::getById($this->_getParam("classId"));
     //get available fields
     $availableFields = $this->gridGetColumnConfig($this->_getParam("classId"), explode(',', $this->_getParam('fields')));
     //create translator from admin translations
     $translator = new Pimcore_Translate_Admin($this->getLanguage());
     //get info about column configuration and process it
     $fields = array();
     $bricks = array();
     $labels = array();
     foreach ($availableFields as $key => $column) {
         $fields[$key] = $column['key'];
         $labels[$column['key']] = $translator->translate($column['label']);
         //are object bricks used?
         $parts = explode("~", $column['key']);
         if (count($parts) > 1) {
             $bricks[$parts[0]] = $parts[0];
         }
     }
     $className = $class->getName();
     $listClass = "Object_" . ucfirst($className) . "_List";
     if ($this->_getParam("filter")) {
         $conditionFilters = Object_Service::getFilterCondition($this->_getParam("filter"), $class);
     }
     if ($this->_getParam("condition")) {
         $conditionFilters = " AND (" . $this->_getParam("condition") . ")";
     }
     $list = new $listClass();
     $list->setCondition("o_path LIKE '" . $folder->getFullPath() . "%'" . $conditionFilters);
     $order = $this->_getParam('order');
     $direction = $this->_getParam('direction');
     if ($order && $direction) {
         $list->setOrderKey($order);
         $list->setOrder($direction);
     } else {
         $list->setOrder("ASC");
         $list->setOrderKey("o_id");
     }
     if ($this->_getParam("objecttype")) {
         $list->setObjectTypes(array($this->_getParam("objecttype")));
     }
     //add bricks
     if (!empty($bricks)) {
         foreach ($bricks as $b) {
             $list->addObjectbrick($b);
         }
     }
     $list->load();
     $objects = array();
     foreach ($list->getObjects() as $object) {
         if ($object instanceof Object_Concrete) {
             $tmp = Object_Service::gridObjectData($object, $fields);
             $o = array();
             //convert values to human readable format
             foreach ($fields as $key => $field) {
                 $value = $tmp[$field];
                 if ($value === null) {
                     $value = '';
                 }
                 if (is_bool($value)) {
                     $value = $value ? $translator->translate('yes') : $translator->translate('no');
                 }
                 $type = isset($availableFields[$key]['layout']) ? $availableFields[$key]['layout'] : null;
                 if ($field == 'creationDate' || $field == 'modificationDate' || $type instanceof Object_Class_Data_Date || $type instanceof Object_Class_Data_Datetime) {
                     $value = date('j.n.Y H:i:s', (int) $value);
                 }
                 $o[$labels[$field]] = $value;
             }
             $objects[] = $o;
         }
     }
     //create csv
     if (!empty($objects)) {
         $columns = array_keys($objects[0]);
         foreach ($columns as $key => $value) {
             $columns[$key] = '"' . $value . '"';
         }
         $csv = implode(";", $columns) . "\r\n";
         foreach ($objects as $o) {
             foreach ($o as $key => $value) {
                 //clean value of evil stuff such as " and linebreaks
                 if (is_string($value)) {
                     $value = strip_tags($value);
                     $value = str_replace('"', '', $value);
                     $value = str_replace("\r", "", $value);
                     $value = str_replace("\n", "", $value);
                     $o[$key] = '"' . $value . '"';
                 }
             }
             $csv .= implode(";", $o) . "\r\n";
         }
     }
     header('Content-Encoding: UTF-8');
     header('Content-type: text/csv; charset=UTF-8');
     header("Content-Disposition: attachment; filename=\"export.csv\"");
     echo "";
     echo $csv;
     exit;
 }