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);
}