Admin_DocumentController::updateAction PHP Method

updateAction() public method

public updateAction ( )
    public function updateAction()
    {
        $success = false;
        $allowUpdate = true;
        $document = Document::getById($this->getParam("id"));
        // this prevents the user from renaming, relocating (actions in the tree) if the newest version isn't the published one
        // the reason is that otherwise the content of the newer not published version will be overwritten
        if ($document instanceof Document\PageSnippet) {
            $latestVersion = $document->getLatestVersion();
            if ($latestVersion && $latestVersion->getData()->getModificationDate() != $document->getModificationDate()) {
                $this->_helper->json(["success" => false, "message" => "You can't relocate if there's a newer not published version"]);
            }
        }
        if ($document->isAllowed("settings")) {
            // if the position is changed the path must be changed || also from the childs
            if ($this->getParam("parentId")) {
                $parentDocument = Document::getById($this->getParam("parentId"));
                //check if parent is changed
                if ($document->getParentId() != $parentDocument->getId()) {
                    if (!$parentDocument->isAllowed("create")) {
                        throw new \Exception("Prevented moving document - no create permission on new parent ");
                    }
                    $intendedPath = $parentDocument->getRealPath();
                    $pKey = $parentDocument->getKey();
                    if (!empty($pKey)) {
                        $intendedPath .= $parentDocument->getKey() . "/";
                    }
                    $documentWithSamePath = Document::getByPath($intendedPath . $document->getKey());
                    if ($documentWithSamePath != null) {
                        $allowUpdate = false;
                    }
                    if ($document->isLocked()) {
                        $allowUpdate = false;
                    }
                }
            }
            if ($allowUpdate) {
                $blockedVars = ["controller", "action", "module"];
                if (!$document->isAllowed("rename") && $this->getParam("key")) {
                    $blockedVars[] = "key";
                    Logger::debug("prevented renaming document because of missing permissions ");
                }
                foreach ($this->getAllParams() as $key => $value) {
                    if (!in_array($key, $blockedVars)) {
                        $document->setValue($key, $value);
                    }
                }
                // if changed the index change also all documents on the same level
                if ($this->getParam("index") !== null) {
                    $list = new Document\Listing();
                    $list->setCondition("parentId = ? AND id != ?", [$this->getParam("parentId"), $document->getId()]);
                    $list->setOrderKey("index");
                    $list->setOrder("asc");
                    $childsList = $list->load();
                    $count = 0;
                    foreach ($childsList as $child) {
                        if ($count == intval($this->getParam("index"))) {
                            $count++;
                        }
                        $child->saveIndex($count);
                        $count++;
                    }
                }
                $document->setUserModification($this->getUser()->getId());
                try {
                    $document->save();
                    $success = true;
                } catch (\Exception $e) {
                    $this->_helper->json(["success" => false, "message" => $e->getMessage()]);
                }
            } else {
                $msg = "Prevented moving document, because document with same path+key already exists or the document is locked. ID: " . $document->getId();
                Logger::debug($msg);
                $this->_helper->json(["success" => false, "message" => $msg]);
            }
        } elseif ($document->isAllowed("rename") && $this->getParam("key")) {
            //just rename
            try {
                $document->setKey($this->getParam("key"));
                $document->setUserModification($this->getUser()->getId());
                $document->save();
                $success = true;
            } catch (\Exception $e) {
                $this->_helper->json(["success" => false, "message" => $e->getMessage()]);
            }
        } else {
            Logger::debug("Prevented update document, because of missing permissions.");
        }
        $this->_helper->json(["success" => $success]);
    }