getById() public static method

public static getById ( integer $id ) : Version
$id integer
return Version
Esempio n. 1
0
 /**
  * Get latest available version, using $force always returns a version no matter if it is the same as the published one
  * @param bool $force
  * @return array
  */
 public function getLatestVersion($force = false)
 {
     $versionData = $this->db->fetchRow("SELECT id,date FROM versions WHERE cid = ? AND ctype='document' ORDER BY `id` DESC LIMIT 1", $this->model->getId());
     if ($versionData["id"] && $versionData["date"] > $this->model->getModificationDate() || $force) {
         $version = Version::getById($versionData["id"]);
         return $version;
     }
     return;
 }
Esempio n. 2
0
 /**
  * Loads a list of thumanils for the specicifies parameters, returns an array of Schedule\Task elements
  *
  * @return array
  */
 public function load()
 {
     $versions = array();
     $data = $this->db->fetchCol("SELECT id FROM versions" . $this->getCondition() . $this->getOrder() . $this->getOffsetLimit(), $this->model->getConditionVariables());
     foreach ($data as $id) {
         $versions[] = Model\Version::getById($id);
     }
     $this->model->setVersions($versions);
     return $versions;
 }
 public function diffVersionsAction()
 {
     Pimcore\Model\Object\AbstractObject::setDoNotRestoreKeyAndPath(true);
     $id1 = intval($this->getParam("from"));
     $id2 = intval($this->getParam("to"));
     $version1 = Model\Version::getById($id1);
     $object1 = $version1->loadData();
     $version2 = Model\Version::getById($id2);
     $object2 = $version2->loadData();
     Pimcore\Model\Object\AbstractObject::setDoNotRestoreKeyAndPath(false);
     if ($object1 && $object2) {
         if ($object1->isAllowed("versions") && $object2->isAllowed("versions")) {
             $this->view->object1 = $object1;
             $this->view->object2 = $object2;
         } else {
             throw new \Exception("Permission denied, version ids [" . $id1 . ", " . $id2 . "]");
         }
     } else {
         throw new \Exception("Version with ids [" . $id1 . ", " . $id2 . "] doesn't exist");
     }
 }
Esempio n. 4
0
 public function deleteVersionAction()
 {
     $version = Model\Version::getById($this->getParam("id"));
     $version->delete();
     $this->_helper->json(["success" => true]);
 }
Esempio n. 5
0
 /**
  * @throws \Zend_Controller_Router_Exception
  */
 public function init()
 {
     // this is only executed once per request (first request)
     if (self::$isInitial) {
         \Pimcore::getEventManager()->trigger("frontend.controller.preInit", $this);
     }
     parent::init();
     // log exceptions if handled by error_handler
     $this->checkForErrors();
     // general definitions
     if (self::$isInitial) {
         \Pimcore::unsetAdminMode();
         Document::setHideUnpublished(true);
         Object\AbstractObject::setHideUnpublished(true);
         Object\AbstractObject::setGetInheritedValues(true);
         Object\Localizedfield::setGetFallbackValues(true);
     }
     // assign variables
     $this->view->controller = $this;
     // init website config
     $config = Config::getWebsiteConfig();
     $this->config = $config;
     $this->view->config = $config;
     $document = $this->getParam("document");
     if (!$document instanceof Document) {
         \Zend_Registry::set("pimcore_editmode", false);
         $this->editmode = false;
         $this->view->editmode = false;
         self::$isInitial = false;
         // check for a locale first, and set it if available
         if ($this->getParam("pimcore_parentDocument")) {
             // this is a special exception for renderlets in editmode (ajax request), because they depend on the locale of the parent document
             // otherwise there'll be notices like:  Notice: 'No translation for the language 'XX' available.'
             if ($parentDocument = Document::getById($this->getParam("pimcore_parentDocument"))) {
                 if ($parentDocument->getProperty("language")) {
                     $this->setLocaleFromDocument($parentDocument->getProperty("language"));
                 }
             }
         }
         // no document available, continue, ...
         return;
     } else {
         $this->setDocument($document);
         // register global locale if the document has the system property "language"
         if ($this->getDocument()->getProperty("language")) {
             $this->setLocaleFromDocument($this->getDocument()->getProperty("language"));
         }
         if (self::$isInitial) {
             // append meta-data to the headMeta() view helper,  if it is a document-request
             if (!Model\Staticroute::getCurrentRoute() && $this->getDocument() instanceof Document\Page) {
                 if (is_array($this->getDocument()->getMetaData())) {
                     foreach ($this->getDocument()->getMetaData() as $meta) {
                         // only name
                         if (!empty($meta["idName"]) && !empty($meta["idValue"]) && !empty($meta["contentValue"])) {
                             $method = "append" . ucfirst($meta["idName"]);
                             $this->view->headMeta()->{$method}($meta["idValue"], $meta["contentValue"]);
                         }
                     }
                 }
             }
         }
     }
     // this is only executed once per request (first request)
     if (self::$isInitial) {
         // contains the logged in user if necessary
         $user = null;
         // default is to set the editmode to false, is enabled later if necessary
         \Zend_Registry::set("pimcore_editmode", false);
         if (Tool::isFrontentRequestByAdmin()) {
             $this->disableBrowserCache();
             // start admin session & get logged in user
             $user = Authentication::authenticateSession();
         }
         if (\Pimcore::inDebugMode()) {
             $this->disableBrowserCache();
         }
         if (!$this->document->isPublished()) {
             if (Tool::isFrontentRequestByAdmin()) {
                 if (!$user) {
                     throw new \Zend_Controller_Router_Exception("access denied for " . $this->document->getFullPath());
                 }
             } else {
                 throw new \Zend_Controller_Router_Exception("access denied for " . $this->document->getFullPath());
             }
         }
         // logged in users only
         if ($user) {
             // set the user to registry so that it is available via \Pimcore\Tool\Admin::getCurrentUser();
             \Zend_Registry::set("pimcore_admin_user", $user);
             // document editmode
             if ($this->getParam("pimcore_editmode")) {
                 \Zend_Registry::set("pimcore_editmode", true);
                 // check if there is the document in the session
                 $docKey = "document_" . $this->getDocument()->getId();
                 $docSession = Session::getReadOnly("pimcore_documents");
                 if ($docSession->{$docKey}) {
                     // if there is a document in the session use it
                     $this->setDocument($docSession->{$docKey});
                 } else {
                     // set the latest available version for editmode if there is no doc in the session
                     $latestVersion = $this->getDocument()->getLatestVersion();
                     if ($latestVersion) {
                         $latestDoc = $latestVersion->loadData();
                         if ($latestDoc instanceof Document\PageSnippet) {
                             $this->setDocument($latestDoc);
                         }
                     }
                 }
                 // register editmode plugin
                 $front = \Zend_Controller_Front::getInstance();
                 $front->registerPlugin(new \Pimcore\Controller\Plugin\Frontend\Editmode($this), 1000);
             }
             // document preview
             if ($this->getParam("pimcore_preview")) {
                 // get document from session
                 $docKey = "document_" . $this->getParam("document")->getId();
                 $docSession = Session::getReadOnly("pimcore_documents");
                 if ($docSession->{$docKey}) {
                     $this->setDocument($docSession->{$docKey});
                 }
             }
             // object preview
             if ($this->getParam("pimcore_object_preview")) {
                 $key = "object_" . $this->getParam("pimcore_object_preview");
                 $session = Session::getReadOnly("pimcore_objects");
                 if ($session->{$key}) {
                     $object = $session->{$key};
                     // add the object to the registry so every call to Object::getById() will return this object instead of the real one
                     \Zend_Registry::set("object_" . $object->getId(), $object);
                 }
             }
             // for version preview
             if ($this->getParam("pimcore_version")) {
                 // only get version data at the first call || because of embedded Snippets ...
                 if (!\Zend_Registry::isRegistered("pimcore_version_active")) {
                     $version = Model\Version::getById($this->getParam("pimcore_version"));
                     $this->setDocument($version->getData());
                     \Zend_Registry::set("pimcore_version_active", true);
                 }
             }
         }
         // for public versions
         if ($this->getParam("v")) {
             try {
                 $version = Model\Version::getById($this->getParam("v"));
                 if ($version->getPublic()) {
                     $this->setDocument($version->getData());
                 }
             } catch (\Exception $e) {
             }
         }
         // check for persona
         if ($this->getDocument() instanceof Document\Page) {
             $this->getDocument()->setUsePersona(null);
             // reset because of preview and editmode (saved in session)
             if ($this->getParam("_ptp") && self::$isInitial) {
                 $this->getDocument()->setUsePersona($this->getParam("_ptp"));
             }
         }
         // check if document is a wrapped hardlink, if this is the case send a rel=canonical header to the source document
         if ($this->getDocument() instanceof Document\Hardlink\Wrapper\WrapperInterface) {
             // get the cononical (source) document
             $hardlinkCanonicalSourceDocument = Document::getById($this->getDocument()->getId());
             $request = $this->getRequest();
             if (\Pimcore\Tool\Frontend::isDocumentInCurrentSite($hardlinkCanonicalSourceDocument)) {
                 $this->getResponse()->setHeader("Link", '<' . $request->getScheme() . "://" . $request->getHttpHost() . $hardlinkCanonicalSourceDocument->getFullPath() . '>; rel="canonical"');
             }
         }
         \Pimcore::getEventManager()->trigger("frontend.controller.postInit", $this);
     }
     // set some parameters
     $this->editmode = \Zend_Registry::get("pimcore_editmode");
     $this->view->editmode = \Zend_Registry::get("pimcore_editmode");
     self::$isInitial = false;
 }
Esempio n. 6
0
 public function versionUpdateAction()
 {
     $data = \Zend_Json::decode($this->getParam("data"));
     $version = Version::getById($data["id"]);
     $version->setPublic($data["public"]);
     $version->setNote($data["note"]);
     $version->save();
     $this->_helper->json(["success" => true]);
 }
Esempio n. 7
0
 /**
  *
  */
 public function maintenanceCleanUp()
 {
     $conf["document"] = Config::getSystemConfig()->documents->versions;
     $conf["asset"] = Config::getSystemConfig()->assets->versions;
     $conf["object"] = Config::getSystemConfig()->objects->versions;
     $elementTypes = array();
     foreach ($conf as $elementType => $tConf) {
         if (intval($tConf->days) > 0) {
             $versioningType = "days";
             $value = intval($tConf->days);
         } else {
             $versioningType = "steps";
             $value = intval($tConf->steps);
         }
         if ($versioningType) {
             $elementTypes[] = array("elementType" => $elementType, $versioningType => $value);
         }
     }
     $ignoredIds = array();
     while (true) {
         $versions = $this->getDao()->maintenanceGetOutdatedVersions($elementTypes, $ignoredIds);
         if (count($versions) == 0) {
             break;
         }
         $counter = 0;
         \Logger::debug("versions to check: " . count($versions));
         if (is_array($versions) && !empty($versions)) {
             $totalCount = count($versions);
             foreach ($versions as $index => $id) {
                 try {
                     $version = Version::getById($id);
                 } catch (\Exception $e) {
                     $ignoredIds[] = $id;
                     \Logger::debug("Version with " . $id . " not found\n");
                     continue;
                 }
                 $counter++;
                 // do not delete public versions
                 if ($version->getPublic()) {
                     $ignoredIds[] = $version->getId();
                     continue;
                 }
                 if ($version->getCtype() == "document") {
                     $element = Document::getById($version->getCid());
                 } elseif ($version->getCtype() == "asset") {
                     $element = Asset::getById($version->getCid());
                 } elseif ($version->getCtype() == "object") {
                     $element = Object::getById($version->getCid());
                 }
                 if ($element instanceof ElementInterface) {
                     \Logger::debug("currently checking Element-ID: " . $element->getId() . " Element-Type: " . Element\Service::getElementType($element) . " in cycle: " . $counter . "/" . $totalCount);
                     if ($element->getModificationDate() >= $version->getDate()) {
                         // delete version if it is outdated
                         \Logger::debug("delete version: " . $version->getId() . " because it is outdated");
                         $version->delete();
                     } else {
                         $ignoredIds[] = $version->getId();
                         \Logger::debug("do not delete version (" . $version->getId() . ") because version's date is newer than the actual modification date of the element. Element-ID: " . $element->getId() . " Element-Type: " . Element\Service::getElementType($element));
                     }
                 } else {
                     // delete version if the corresponding element doesn't exist anymore
                     \Logger::debug("delete version (" . $version->getId() . ") because the corresponding element doesn't exist anymore");
                     $version->delete();
                 }
                 // call the garbage collector if memory consumption is > 100MB
                 if (memory_get_usage() > 100000000) {
                     \Pimcore::collectGarbage();
                 }
             }
         }
     }
 }
Esempio n. 8
0
 /**
  *
  */
 public static function execute()
 {
     $list = new Listing();
     $list->setCondition("active = 1 AND date < ?", time());
     $tasks = $list->load();
     foreach ($tasks as $task) {
         try {
             if ($task->getCtype() == "document") {
                 $document = Document::getById($task->getCid());
                 if ($document instanceof Document) {
                     if ($task->getAction() == "publish-version" && $task->getVersion()) {
                         try {
                             $version = Version::getById($task->getVersion());
                             $document = $version->getData();
                             if ($document instanceof Document) {
                                 $document->setPublished(true);
                                 $document->save();
                             } else {
                                 \Logger::err("Schedule\\Task\\Executor: Could not restore document from version data.");
                             }
                         } catch (\Exception $e) {
                             \Logger::err("Schedule\\Task\\Executor: Version [ " . $task->getVersion() . " ] does not exist.");
                         }
                     } else {
                         if ($task->getAction() == "publish") {
                             $document->setPublished(true);
                             $document->save();
                         } else {
                             if ($task->getAction() == "unpublish") {
                                 $document->setPublished(false);
                                 $document->save();
                             } else {
                                 if ($task->getAction() == "delete") {
                                     $document->delete();
                                 }
                             }
                         }
                     }
                 }
             } else {
                 if ($task->getCtype() == "asset") {
                     $asset = Asset::getById($task->getCid());
                     if ($asset instanceof Asset) {
                         if ($task->getAction() == "publish-version" && $task->getVersion()) {
                             try {
                                 $version = Version::getById($task->getVersion());
                                 $asset = $version->getData();
                                 if ($asset instanceof Asset) {
                                     $asset->save();
                                 } else {
                                     \Logger::err("Schedule\\Task\\Executor: Could not restore asset from version data.");
                                 }
                             } catch (\Exception $e) {
                                 \Logger::err("Schedule\\Task\\Executor: Version [ " . $task->getVersion() . " ] does not exist.");
                             }
                         } else {
                             if ($task->getAction() == "delete") {
                                 $asset->delete();
                             }
                         }
                     }
                 } else {
                     if ($task->getCtype() == "object") {
                         $object = Object::getById($task->getCid());
                         if ($object instanceof Object) {
                             if ($task->getAction() == "publish-version" && $task->getVersion()) {
                                 try {
                                     $version = Version::getById($task->getVersion());
                                     $object = $version->getData();
                                     if ($object instanceof Object\AbstractObject) {
                                         $object->setPublished(true);
                                         $object->save();
                                     } else {
                                         \Logger::err("Schedule\\Task\\Executor: Could not restore object from version data.");
                                     }
                                 } catch (\Exception $e) {
                                     \Logger::err("Schedule\\Task\\Executor: Version [ " . $task->getVersion() . " ] does not exist.");
                                 }
                             } else {
                                 if ($task->getAction() == "publish") {
                                     $object->setPublished(true);
                                     $object->save();
                                 } else {
                                     if ($task->getAction() == "unpublish") {
                                         $object->setPublished(false);
                                         $object->save();
                                     } else {
                                         if ($task->getAction() == "delete") {
                                             $object->delete();
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             $task->setActive(false);
             $task->save();
         } catch (\Exception $e) {
             \Logger::err("There was a problem with the scheduled task ID: " . $task->getId());
             \Logger::err($e);
         }
     }
 }
 public function showVersionAction()
 {
     $id = intval($this->getParam("id"));
     $version = Model\Version::getById($id);
     $asset = $version->loadData();
     if ($asset->isAllowed("versions")) {
         $this->view->asset = $asset;
         $this->render("show-version-" . $asset->getType());
     } else {
         throw new \Exception("Permission denied, version id [" . $id . "]");
     }
 }
Esempio n. 10
0
 public function diffVersionsAction()
 {
     include_once 'DaisyDiff/HTMLDiff.php';
     include_once 'simple_html_dom.php';
     $versionFrom = Version::getById($this->getParam("from"));
     $versionTo = Version::getById($this->getParam("to"));
     $docFrom = $versionFrom->loadData();
     $docTo = $versionTo->loadData();
     // unlock the current session to access the version files
     session_write_close();
     $request = $this->getRequest();
     $fromSourceHtml = Tool::getHttpData($request->getScheme() . "://" . $request->getHttpHost() . $docFrom->getFullPath() . "?pimcore_version=" . $this->getParam("from") . "&pimcore_admin_sid=" . $_COOKIE["pimcore_admin_sid"]);
     $toSourceHtml = Tool::getHttpData($request->getScheme() . "://" . $request->getHttpHost() . $docTo->getFullPath() . "?pimcore_version=" . $this->getParam("to") . "&pimcore_admin_sid=" . $_COOKIE["pimcore_admin_sid"]);
     $fromSource = str_get_html($fromSourceHtml);
     $toSource = str_get_html($toSourceHtml);
     if ($fromSource && $toSource) {
         if ($docFrom instanceof Document\Page) {
             $from = $fromSource->find("body", 0);
             $to = $toSource->find("body", 0);
         } else {
             $from = $fromSource;
             $to = $toSource;
         }
         $diff = new HTMLDiffer();
         $text = $diff->htmlDiff($from, $to);
         if ($docFrom instanceof Document\Page) {
             $fromSource->find("head", 0)->innertext = $fromSource->find("head", 0)->innertext . '<link rel="stylesheet" type="text/css" href="/pimcore/static/css/daisydiff.css" />';
             $fromSource->find("body", 0)->innertext = $text;
             echo $fromSource;
         } else {
             echo '<link rel="stylesheet" type="text/css" href="/pimcore/static/css/daisydiff.css" />';
             echo $text;
         }
     } else {
         echo "Unable to create diff";
     }
     $this->removeViewRenderer();
 }
 public function diffVersionsAction()
 {
     $versionFrom = Version::getById($this->getParam("from"));
     $docFrom = $versionFrom->loadData();
     $request = $this->getRequest();
     $sessionName = Tool\Session::getOption("name");
     $prefix = $request->getScheme() . "://" . $request->getHttpHost() . $docFrom->getFullPath() . "?pimcore_version=";
     $fromUrl = $prefix . $this->getParam("from") . "&" . $sessionName . "=" . $_COOKIE[$sessionName];
     $toUrl = $prefix . $this->getParam("to") . "&" . $sessionName . "=" . $_COOKIE[$sessionName];
     $fromFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/version-diff-tmp-" . uniqid() . ".png";
     $toFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/version-diff-tmp-" . uniqid() . ".png";
     $diffFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/version-diff-tmp-" . uniqid() . ".png";
     if (\Pimcore\Image\HtmlToImage::isSupported() && class_exists("Imagick")) {
         \Pimcore\Image\HtmlToImage::convert($fromUrl, $fromFile);
         \Pimcore\Image\HtmlToImage::convert($toUrl, $toFile);
         $image1 = new Imagick($fromFile);
         $image2 = new Imagick($toFile);
         if ($image1->getImageWidth() == $image2->getImageWidth() && $image1->getImageHeight() == $image2->getImageHeight()) {
             $result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
             $result[0]->setImageFormat("png");
             $result[0]->writeImage($diffFile);
             $result[0]->clear();
             $result[0]->destroy();
             $this->view->image = base64_encode(file_get_contents($diffFile));
             unlink($diffFile);
         } else {
             $this->view->image1 = base64_encode(file_get_contents($fromFile));
             $this->view->image2 = base64_encode(file_get_contents($toFile));
         }
         // cleanup
         $image1->clear();
         $image1->destroy();
         $image2->clear();
         $image2->destroy();
         unlink($fromFile);
         unlink($toFile);
     } else {
         $this->renderScript("document/diff-versions-unsupported.php");
     }
 }