Example #1
0
 public function translationsAction()
 {
     $admin = $this->getParam("admin");
     if ($admin) {
         $class = "\\Pimcore\\Model\\Translation\\Admin";
         $this->checkPermission("translations_admin");
     } else {
         $class = "\\Pimcore\\Model\\Translation\\Website";
         $this->checkPermission("translations");
     }
     // clear translation cache
     Translation\Website::clearDependentCache();
     if ($this->getParam("data")) {
         $data = \Zend_Json::decode($this->getParam("data"));
         if ($this->getParam("xaction") == "destroy") {
             $data = \Zend_Json::decode($this->getParam("data"));
             if (\Pimcore\Tool\Admin::isExtJS6()) {
                 $t = $class::getByKey($data["key"]);
             } else {
                 $t = $class::getByKey($data);
             }
             $t->delete();
             $this->_helper->json(array("success" => true, "data" => array()));
         } else {
             if ($this->getParam("xaction") == "update") {
                 $t = $class::getByKey($data["key"]);
                 foreach ($data as $key => $value) {
                     if ($key != "key") {
                         $t->addTranslation($key, $value);
                     }
                 }
                 if ($data["key"]) {
                     $t->setKey($data["key"]);
                 }
                 $t->setModificationDate(time());
                 $t->save();
                 $return = array_merge(array("key" => $t->getKey(), "creationDate" => $t->getCreationDate(), "modificationDate" => $t->getModificationDate()), $t->getTranslations());
                 $this->_helper->json(array("data" => $return, "success" => true));
             } else {
                 if ($this->getParam("xaction") == "create") {
                     try {
                         $t = $class::getByKey($data["key"]);
                     } catch (\Exception $e) {
                         $t = new $class();
                         $t->setKey($data["key"]);
                         $t->setCreationDate(time());
                         $t->setModificationDate(time());
                         foreach (Tool::getValidLanguages() as $lang) {
                             $t->addTranslation($lang, "");
                         }
                         $t->save();
                     }
                     $return = array_merge(array("key" => $t->getKey(), "creationDate" => $t->getCreationDate(), "modificationDate" => $t->getModificationDate()), $t->getTranslations());
                     $this->_helper->json(array("data" => $return, "success" => true));
                 }
             }
         }
     } else {
         // get list of types
         if ($admin) {
             $list = new Translation\Admin\Listing();
         } else {
             $list = new Translation\Website\Listing();
         }
         $list->setOrder("asc");
         $list->setOrderKey("key");
         $sortingSettings = \Pimcore\Admin\Helper\QueryParams::extractSortingSettings($this->getAllParams());
         if ($sortingSettings['orderKey']) {
             $list->setOrderKey($sortingSettings['orderKey']);
         }
         if ($sortingSettings['order']) {
             $list->setOrder($sortingSettings['order']);
         }
         $list->setLimit($this->getParam("limit"));
         $list->setOffset($this->getParam("start"));
         $condition = $this->getGridFilterCondition();
         if ($condition) {
             $list->setCondition($condition);
         }
         $list->load();
         $translations = array();
         foreach ($list->getTranslations() as $t) {
             $translations[] = array_merge($t->getTranslations(), array("key" => $t->getKey(), "creationDate" => $t->getCreationDate(), "modificationDate" => $t->getModificationDate()));
         }
         $this->_helper->json(array("data" => $translations, "success" => true, "total" => $list->getTotalCount()));
     }
 }
 public function translationsAction()
 {
     $admin = $this->getParam("admin");
     if ($admin) {
         $class = "\\Pimcore\\Model\\Translation\\Admin";
         $this->checkPermission("translations_admin");
     } else {
         $class = "\\Pimcore\\Model\\Translation\\Website";
         $this->checkPermission("translations");
     }
     $tableName = call_user_func($class . "\\Dao::getTableName");
     // clear translation cache
     Translation\Website::clearDependentCache();
     if ($this->getParam("data")) {
         $data = \Zend_Json::decode($this->getParam("data"));
         if ($this->getParam("xaction") == "destroy") {
             $data = \Zend_Json::decode($this->getParam("data"));
             if (\Pimcore\Tool\Admin::isExtJS6()) {
                 $t = $class::getByKey($data["key"]);
             } else {
                 $t = $class::getByKey($data);
             }
             $t->delete();
             $this->_helper->json(["success" => true, "data" => []]);
         } elseif ($this->getParam("xaction") == "update") {
             $t = $class::getByKey($data["key"]);
             foreach ($data as $key => $value) {
                 if ($key != "key") {
                     $t->addTranslation($key, $value);
                 }
             }
             if ($data["key"]) {
                 $t->setKey($data["key"]);
             }
             $t->setModificationDate(time());
             $t->save();
             $return = array_merge(["key" => $t->getKey(), "creationDate" => $t->getCreationDate(), "modificationDate" => $t->getModificationDate()], $t->getTranslations());
             $this->_helper->json(["data" => $return, "success" => true]);
         } elseif ($this->getParam("xaction") == "create") {
             try {
                 $t = $class::getByKey($data["key"]);
             } catch (\Exception $e) {
                 $t = new $class();
                 $t->setKey($data["key"]);
                 $t->setCreationDate(time());
                 $t->setModificationDate(time());
                 foreach (Tool::getValidLanguages() as $lang) {
                     $t->addTranslation($lang, "");
                 }
                 $t->save();
             }
             $return = array_merge(["key" => $t->getKey(), "creationDate" => $t->getCreationDate(), "modificationDate" => $t->getModificationDate()], $t->getTranslations());
             $this->_helper->json(["data" => $return, "success" => true]);
         }
     } else {
         // get list of types
         if ($admin) {
             $list = new Translation\Admin\Listing();
         } else {
             $list = new Translation\Website\Listing();
         }
         $validLanguages = $this->getUser()->getAllowedLanguagesForViewingWebsiteTranslations();
         $list->setOrder("asc");
         $list->setOrderKey($tableName . ".key", false);
         $sortingSettings = \Pimcore\Admin\Helper\QueryParams::extractSortingSettings($this->getAllParams());
         $joins = [];
         if ($sortingSettings['orderKey']) {
             if (in_array($sortingSettings['orderKey'], $validLanguages)) {
                 $joins[] = ["language" => $sortingSettings['orderKey']];
                 $list->setOrderKey($sortingSettings['orderKey']);
             } else {
                 $list->setOrderKey($tableName . "." . $sortingSettings['orderKey'], false);
             }
         }
         if ($sortingSettings['order']) {
             $list->setOrder($sortingSettings['order']);
         }
         $list->setLimit($this->getParam("limit"));
         $list->setOffset($this->getParam("start"));
         $condition = $this->getGridFilterCondition($tableName);
         $filters = $this->getGridFilterCondition($tableName, true);
         if ($filters) {
             $joins = array_merge($joins, $filters["joins"]);
         }
         if ($condition) {
             $list->setCondition($condition);
         }
         $this->extendTranslationQuery($joins, $list, $tableName, $filters);
         $list->load();
         $translations = [];
         foreach ($list->getTranslations() as $t) {
             $translations[] = array_merge($t->getTranslations(), ["key" => $t->getKey(), "creationDate" => $t->getCreationDate(), "modificationDate" => $t->getModificationDate()]);
         }
         $this->_helper->json(["data" => $translations, "success" => true, "total" => $list->getTotalCount()]);
     }
 }