Пример #1
0
 /**
  * Static helper to get a Document by it's id, only type Document is returned, not Document\Page, ... (see getConcreteById() )
  *
  * @param integer $id
  * @return Document
  */
 public static function getById($id)
 {
     $id = intval($id);
     if ($id < 1) {
         return null;
     }
     $cacheKey = "document_" . $id;
     try {
         $document = \Zend_Registry::get($cacheKey);
         if (!$document) {
             throw new \Exception("Document in registry is null");
         }
     } catch (\Exception $e) {
         try {
             if (!($document = Cache::load($cacheKey))) {
                 $document = new Document();
                 $document->getResource()->getById($id);
                 $mappingClass = "\\Pimcore\\Model\\Document\\" . ucfirst($document->getType());
                 // this is the fallback for custom document types using prefixes
                 // so we need to check if the class exists first
                 if (!\Pimcore\Tool::classExists($mappingClass)) {
                     $oldStyleClass = "Document_" . ucfirst($document->getType());
                     if (\Pimcore\Tool::classExists($oldStyleClass)) {
                         $mappingClass = $oldStyleClass;
                     }
                 }
                 $typeClass = Tool::getModelClassMapping($mappingClass);
                 if (Tool::classExists($typeClass)) {
                     $document = new $typeClass();
                     \Zend_Registry::set($cacheKey, $document);
                     $document->getResource()->getById($id);
                     Cache::save($document, $cacheKey);
                 }
             } else {
                 \Zend_Registry::set($cacheKey, $document);
             }
         } catch (\Exception $e) {
             \Logger::warning($e->getMessage());
             return null;
         }
     }
     if (!$document) {
         return null;
     }
     return $document;
 }
Пример #2
0
 /**
  * @static
  * @param $path
  * @return bool
  */
 public static function pathExists($path, $type = null)
 {
     $path = Element\Service::correctPath($path);
     try {
         $document = new Document();
         // validate path
         if (\Pimcore\Tool::isValidPath($path)) {
             $document->getResource()->getByPath($path);
             return true;
         }
     } catch (\Exception $e) {
     }
     return false;
 }