Inheritance: extends AbstractModel
Example #1
0
 /**
  *
  */
 protected function update()
 {
     $oldPath = $this->getDao()->getCurrentFullPath();
     parent::update();
     $config = \Pimcore\Config::getSystemConfig();
     if ($oldPath && $config->documents->createredirectwhenmoved && $oldPath != $this->getRealFullPath()) {
         // create redirect for old path
         $redirect = new Redirect();
         $redirect->setTarget($this->getId());
         $redirect->setSource("@" . $oldPath . "/?@");
         $redirect->setStatusCode(301);
         $redirect->setExpiry(time() + 86400 * 60);
         // this entry is removed automatically after 60 days
         $redirect->save();
     }
 }
Example #2
0
 /**
  * Loads a list of static routes for the specicifies parameters, returns an array of Redirect elements
  *
  * @return array
  */
 public function load()
 {
     $redirectsData = $this->db->fetchCol("SELECT id FROM redirects" . $this->getCondition() . $this->getOrder() . $this->getOffsetLimit(), $this->model->getConditionVariables());
     $redirects = [];
     foreach ($redirectsData as $redirectData) {
         $redirects[] = Model\Redirect::getById($redirectData);
     }
     $this->model->setRedirects($redirects);
     return $redirects;
 }
 public function redirectsAction()
 {
     if ($this->getParam("data")) {
         $this->checkPermission("redirects");
         if ($this->getParam("xaction") == "destroy") {
             $data = \Zend_Json::decode($this->getParam("data"));
             if (\Pimcore\Tool\Admin::isExtJS6()) {
                 $id = $data["id"];
             } else {
                 $id = $data;
             }
             $redirect = Redirect::getById($id);
             $redirect->delete();
             $this->_helper->json(array("success" => true, "data" => array()));
         } else {
             if ($this->getParam("xaction") == "update") {
                 $data = \Zend_Json::decode($this->getParam("data"));
                 // save redirect
                 $redirect = Redirect::getById($data["id"]);
                 if ($data["target"]) {
                     if ($doc = Document::getByPath($data["target"])) {
                         $data["target"] = $doc->getId();
                     }
                 }
                 $redirect->setValues($data);
                 $redirect->save();
                 $redirectTarget = $redirect->getTarget();
                 if (is_numeric($redirectTarget)) {
                     if ($doc = Document::getById(intval($redirectTarget))) {
                         $redirect->setTarget($doc->getFullPath());
                     }
                 }
                 $this->_helper->json(array("data" => $redirect, "success" => true));
             } else {
                 if ($this->getParam("xaction") == "create") {
                     $data = \Zend_Json::decode($this->getParam("data"));
                     unset($data["id"]);
                     // save route
                     $redirect = new Redirect();
                     if ($data["target"]) {
                         if ($doc = Document::getByPath($data["target"])) {
                             $data["target"] = $doc->getId();
                         }
                     }
                     $redirect->setValues($data);
                     $redirect->save();
                     $redirectTarget = $redirect->getTarget();
                     if (is_numeric($redirectTarget)) {
                         if ($doc = Document::getById(intval($redirectTarget))) {
                             $redirect->setTarget($doc->getFullPath());
                         }
                     }
                     $this->_helper->json(array("data" => $redirect, "success" => true));
                 }
             }
         }
     } else {
         // get list of routes
         $list = new Redirect\Listing();
         $list->setLimit($this->getParam("limit"));
         $list->setOffset($this->getParam("start"));
         $sortingSettings = \Pimcore\Admin\Helper\QueryParams::extractSortingSettings($this->getAllParams());
         if ($sortingSettings['orderKey']) {
             $list->setOrderKey($sortingSettings['orderKey']);
             $list->setOrder($sortingSettings['order']);
         }
         if ($this->getParam("filter")) {
             $list->setCondition("`source` LIKE " . $list->quote("%" . $this->getParam("filter") . "%") . " OR `target` LIKE " . $list->quote("%" . $this->getParam("filter") . "%"));
         }
         $list->load();
         $redirects = array();
         foreach ($list->getRedirects() as $redirect) {
             if ($link = $redirect->getTarget()) {
                 if (is_numeric($link)) {
                     if ($doc = Document::getById(intval($link))) {
                         $redirect->setTarget($doc->getFullPath());
                     }
                 }
             }
             $redirects[] = $redirect;
         }
         $this->_helper->json(array("data" => $redirects, "success" => true, "total" => $list->getTotalCount()));
     }
     $this->_helper->json(false);
 }
 public function saveAction()
 {
     if ($this->getParam("id")) {
         $page = Document\Page::getById($this->getParam("id"));
         $page = $this->getLatestVersion($page);
         $page->setUserModification($this->getUser()->getId());
         if ($this->getParam("task") == "unpublish") {
             $page->setPublished(false);
         }
         if ($this->getParam("task") == "publish") {
             $page->setPublished(true);
         }
         $settings = array();
         if ($this->getParam("settings")) {
             $settings = \Zend_Json::decode($this->getParam("settings"));
         }
         // check for redirects
         if ($this->getUser()->isAllowed("redirects") && $this->getParam("settings")) {
             if (is_array($settings)) {
                 $redirectList = new Redirect\Listing();
                 $redirectList->setCondition("target = ?", $page->getId());
                 $existingRedirects = $redirectList->load();
                 $existingRedirectIds = array();
                 foreach ($existingRedirects as $existingRedirect) {
                     $existingRedirectIds[$existingRedirect->getId()] = $existingRedirect->getId();
                 }
                 for ($i = 1; $i < 100; $i++) {
                     if (array_key_exists("redirect_url_" . $i, $settings)) {
                         // check for existing
                         if ($settings["redirect_id_" . $i]) {
                             $redirect = Redirect::getById($settings["redirect_id_" . $i]);
                             unset($existingRedirectIds[$redirect->getId()]);
                         } else {
                             // create new one
                             $redirect = new Redirect();
                         }
                         $redirect->setSource($settings["redirect_url_" . $i]);
                         $redirect->setTarget($page->getId());
                         $redirect->setStatusCode(301);
                         $redirect->save();
                     }
                 }
                 // remove existing redirects which were delete
                 foreach ($existingRedirectIds as $existingRedirectId) {
                     $redirect = Redirect::getById($existingRedirectId);
                     $redirect->delete();
                 }
             }
         }
         // check if settings exist, before saving meta data
         if ($this->getParam("settings") && is_array($settings)) {
             $metaData = array();
             for ($i = 1; $i < 30; $i++) {
                 if (array_key_exists("metadata_idName_" . $i, $settings)) {
                     $metaData[] = array("idName" => $settings["metadata_idName_" . $i], "idValue" => $settings["metadata_idValue_" . $i], "contentName" => $settings["metadata_contentName_" . $i], "contentValue" => $settings["metadata_contentValue_" . $i]);
                 }
             }
             $page->setMetaData($metaData);
         }
         // only save when publish or unpublish
         if ($this->getParam("task") == "publish" && $page->isAllowed("publish") or $this->getParam("task") == "unpublish" && $page->isAllowed("unpublish")) {
             $this->setValuesToDocument($page);
             try {
                 $page->save();
                 $this->saveToSession($page);
                 $this->_helper->json(array("success" => true));
             } catch (\Exception $e) {
                 \Logger::err($e);
                 $this->_helper->json(array("success" => false, "message" => $e->getMessage()));
             }
         } else {
             if ($page->isAllowed("save")) {
                 $this->setValuesToDocument($page);
                 try {
                     $page->saveVersion();
                     $this->saveToSession($page);
                     $this->_helper->json(array("success" => true));
                 } catch (\Exception $e) {
                     \Logger::err($e);
                     $this->_helper->json(array("success" => false, "message" => $e->getMessage()));
                 }
             }
         }
     }
     $this->_helper->json(false);
 }
Example #5
0
 public function saveAction()
 {
     try {
         if ($this->getParam("id")) {
             $page = Document\Page::getById($this->getParam("id"));
             // check if there's a document in session which should be used as data-source
             // see also self::clearEditableDataAction() | this is necessary to reset all fields and to get rid of
             // outdated and unused data elements in this document (eg. entries of area-blocks)
             $pageSession = Session::useSession(function ($session) use($page) {
                 if (isset($session->{"document_" . $page->getId()}) && isset($session->{"document_" . $page->getId() . "_useForSave"})) {
                     if ($session->{"document_" . $page->getId() . "_useForSave"}) {
                         // only use the page from the session once
                         unset($session->{"document_" . $page->getId() . "_useForSave"});
                         return $session->{"document_" . $page->getId()};
                     }
                 }
                 return null;
             }, "pimcore_documents");
             if ($pageSession) {
                 $page = $pageSession;
             } else {
                 $page = $this->getLatestVersion($page);
             }
             $page->setUserModification($this->getUser()->getId());
             if ($this->getParam("task") == "unpublish") {
                 $page->setPublished(false);
             }
             if ($this->getParam("task") == "publish") {
                 $page->setPublished(true);
             }
             $settings = [];
             if ($this->getParam("settings")) {
                 $settings = \Zend_Json::decode($this->getParam("settings"));
             }
             // check for redirects
             if ($this->getUser()->isAllowed("redirects") && $this->getParam("settings")) {
                 if (is_array($settings)) {
                     $redirectList = new Redirect\Listing();
                     $redirectList->setCondition("target = ?", $page->getId());
                     $existingRedirects = $redirectList->load();
                     $existingRedirectIds = [];
                     foreach ($existingRedirects as $existingRedirect) {
                         $existingRedirectIds[$existingRedirect->getId()] = $existingRedirect->getId();
                     }
                     for ($i = 1; $i < 100; $i++) {
                         if (array_key_exists("redirect_url_" . $i, $settings)) {
                             // check for existing
                             if ($settings["redirect_id_" . $i]) {
                                 $redirect = Redirect::getById($settings["redirect_id_" . $i]);
                                 unset($existingRedirectIds[$redirect->getId()]);
                             } else {
                                 // create new one
                                 $redirect = new Redirect();
                             }
                             $redirect->setSource($settings["redirect_url_" . $i]);
                             $redirect->setTarget($page->getId());
                             $redirect->setStatusCode(301);
                             $redirect->save();
                         }
                     }
                     // remove existing redirects which were delete
                     foreach ($existingRedirectIds as $existingRedirectId) {
                         $redirect = Redirect::getById($existingRedirectId);
                         $redirect->delete();
                     }
                 }
             }
             // check if settings exist, before saving meta data
             if ($this->getParam("settings") && is_array($settings)) {
                 $metaData = [];
                 for ($i = 1; $i < 30; $i++) {
                     if (array_key_exists("metadata_" . $i, $settings)) {
                         $metaData[] = $settings["metadata_" . $i];
                     }
                 }
                 $page->setMetaData($metaData);
             }
             // only save when publish or unpublish
             if ($this->getParam("task") == "publish" && $page->isAllowed("publish") or $this->getParam("task") == "unpublish" && $page->isAllowed("unpublish")) {
                 $this->setValuesToDocument($page);
                 try {
                     $page->save();
                     $this->saveToSession($page);
                     $this->_helper->json(["success" => true]);
                 } catch (\Exception $e) {
                     if (\Pimcore\Tool\Admin::isExtJS6() && $e instanceof Element\ValidationException) {
                         throw $e;
                     }
                     Logger::err($e);
                     $this->_helper->json(["success" => false, "message" => $e->getMessage()]);
                 }
             } else {
                 if ($page->isAllowed("save")) {
                     $this->setValuesToDocument($page);
                     try {
                         $page->saveVersion();
                         $this->saveToSession($page);
                         $this->_helper->json(["success" => true]);
                     } catch (\Exception $e) {
                         Logger::err($e);
                         $this->_helper->json(["success" => false, "message" => $e->getMessage()]);
                     }
                 }
             }
         }
     } catch (\Exception $e) {
         Logger::log($e);
         if (\Pimcore\Tool\Admin::isExtJS6() && $e instanceof Element\ValidationException) {
             $this->_helper->json(["success" => false, "type" => "ValidationException", "message" => $e->getMessage(), "stack" => $e->getTraceAsString(), "code" => $e->getCode()]);
         }
         throw $e;
     }
     $this->_helper->json(false);
 }