public function correctPath()
{
// set path
if ($this->getId() != 1) {
// not for the root node
// check for a valid key, home has no key, so omit the check
if (!Element\Service::isValidKey($this->getKey(), "document")) {
throw new \Exception("invalid key for document with id [ " . $this->getId() . " ] key is: [" . $this->getKey() . "]");
}
if ($this->getParentId() == $this->getId()) {
throw new \Exception("ParentID and ID is identical, an element can't be the parent of itself.");
}
$parent = Document::getById($this->getParentId());
if ($parent) {
// use the parent's path from the database here (getCurrentFullPath), to ensure the path really exists and does not rely on the path
// that is currently in the parent object (in memory), because this might have changed but wasn't not saved
$this->setPath(str_replace("//", "/", $parent->getCurrentFullPath() . "/"));
} else {
// parent document doesn't exist anymore, set the parent to to root
$this->setParentId(1);
$this->setPath("/");
}
if (strlen($this->getKey()) < 1) {
throw new \Exception("Document requires key, generated key automatically");
}
} elseif ($this->getId() == 1) {
// some data in root node should always be the same
$this->setParentId(0);
$this->setPath("/");
$this->setKey("");
$this->setType("page");
}
if (Document\Service::pathExists($this->getRealFullPath())) {
$duplicate = Document::getByPath($this->getRealFullPath());
if ($duplicate instanceof Document and $duplicate->getId() != $this->getId()) {
throw new \Exception("Duplicate full path [ " . $this->getRealFullPath() . " ] - cannot save document");
}
}
if (strlen($this->getRealFullPath()) > 765) {
throw new \Exception("Full path is limited to 765 characters, reduce the length of your parent's path");
}
}