getByPath() публичный статический Метод

Static helper to get an asset by the passed path
public static getByPath ( string $path ) : Asset | Archive | Audio | Document | Folder | Image | Text | Unknown | Video
$path string
Результат Asset | Archive | Audio | Document | Folder | Image | Text | Unknown | Video
Пример #1
0
 /**
  * @param string $name
  * @return DAV\INode|void
  * @throws DAV\Exception\NotFound
  */
 function getChild($name)
 {
     $nameParts = explode("/", $name);
     $name = File::getValidFilename($nameParts[count($nameParts) - 1]);
     //$name = implode("/",$nameParts);
     if (is_string($name)) {
         $parentPath = $this->asset->getFullPath();
         if ($parentPath == "/") {
             $parentPath = "";
         }
         if (!($asset = Asset::getByPath($parentPath . "/" . $name))) {
             throw new DAV\Exception\NotFound('File not found: ' . $name);
         }
     } else {
         if ($name instanceof Asset) {
             $asset = $name;
         }
     }
     if ($asset instanceof Asset) {
         if ($asset->getType() == "folder") {
             return new Asset\WebDAV\Folder($asset);
         } else {
             return new Asset\WebDAV\File($asset);
         }
     }
     throw new DAV\Exception\NotFound('File not found: ' . $name);
 }
Пример #2
0
 protected function setValuesToDocument(Document\Link $link)
 {
     // data
     if ($this->getParam("data")) {
         $data = \Zend_Json::decode($this->getParam("data"));
         if (!empty($data["path"])) {
             if ($document = Document::getByPath($data["path"])) {
                 $data["linktype"] = "internal";
                 $data["internalType"] = "document";
                 $data["internal"] = $document->getId();
             } elseif ($asset = Asset::getByPath($data["path"])) {
                 $data["linktype"] = "internal";
                 $data["internalType"] = "asset";
                 $data["internal"] = $asset->getId();
             } else {
                 $data["linktype"] = "direct";
                 $data["direct"] = $data["path"];
             }
         } else {
             // clear content of link
             $data["linktype"] = "internal";
             $data["direct"] = "";
             $data["internalType"] = null;
             $data["internal"] = null;
         }
         unset($data["path"]);
         $link->setValues($data);
     }
     $this->addPropertiesToDocument($link);
 }
Пример #3
0
 /**
  * Moves a file/directory
  *
  * @param string $sourcePath
  * @param string $destinationPath
  * @return void
  */
 public function move($sourcePath, $destinationPath)
 {
     $nameParts = explode("/", $sourcePath);
     $nameParts[count($nameParts) - 1] = File::getValidFilename($nameParts[count($nameParts) - 1]);
     $sourcePath = implode("/", $nameParts);
     $nameParts = explode("/", $destinationPath);
     $nameParts[count($nameParts) - 1] = File::getValidFilename($nameParts[count($nameParts) - 1]);
     $destinationPath = implode("/", $nameParts);
     try {
         if (dirname($sourcePath) == dirname($destinationPath)) {
             $asset = null;
             if ($asset = Asset::getByPath("/" . $destinationPath)) {
                 // If we got here, this means the destination exists, and needs to be overwritten
                 $sourceAsset = Asset::getByPath("/" . $sourcePath);
                 $asset->setData($sourceAsset->getData());
                 $sourceAsset->delete();
             }
             // see: Asset\WebDAV\File::delete() why this is necessary
             $log = Asset\WebDAV\Service::getDeleteLog();
             if (!$asset && array_key_exists("/" . $destinationPath, $log)) {
                 $asset = \Pimcore\Tool\Serialize::unserialize($log["/" . $destinationPath]["data"]);
                 if ($asset) {
                     $sourceAsset = Asset::getByPath("/" . $sourcePath);
                     $asset->setData($sourceAsset->getData());
                     $sourceAsset->delete();
                 }
             }
             if (!$asset) {
                 $asset = Asset::getByPath("/" . $sourcePath);
             }
             $asset->setFilename(basename($destinationPath));
         } else {
             $asset = Asset::getByPath("/" . $sourcePath);
             $parent = Asset::getByPath("/" . dirname($destinationPath));
             $asset->setPath($parent->getFullPath() . "/");
             $asset->setParentId($parent->getId());
         }
         $user = \Pimcore\Tool\Admin::getCurrentUser();
         $asset->setUserModification($user->getId());
         $asset->save();
     } catch (\Exception $e) {
         \Logger::error($e);
     }
 }
Пример #4
0
 /**
  * @see Model\Object\ClassDefinition\Data::getDataFromEditmode
  * @param integer $data
  * @param null|Model\Object\AbstractObject $object
  * @return Asset
  */
 public function getDataFromEditmode($data, $object = null)
 {
     $video = null;
     if ($data["type"] == "asset") {
         if ($asset = Asset::getByPath($data["data"])) {
             $data["data"] = $asset;
         } else {
             $data["data"] = null;
         }
     }
     if ($data["poster"]) {
         if ($poster = Asset::getByPath($data["poster"])) {
             $data["poster"] = $poster;
         } else {
             $data["poster"] = null;
         }
     }
     if (!empty($data["data"])) {
         $video = new Object\Data\Video();
         $video->setData($data["data"]);
         $video->setType($data["type"]);
         $video->setPoster($data["poster"]);
         $video->setTitle($data["title"]);
         $video->setDescription($data["description"]);
     }
     return $video;
 }
Пример #5
0
 /**
  * @param $importValue
  * @return mixed|null|Asset
  */
 public function getFromCsvImport($importValue)
 {
     $value = null;
     if ($el = Asset::getByPath($importValue)) {
         $value = $el;
     } else {
         $value = null;
     }
     return $value;
 }
Пример #6
0
 /**
  * @throws \Exception
  */
 public function correctPath()
 {
     // set path
     if ($this->getId() != 1) {
         // not for the root node
         if ($this->getParentId() == $this->getId()) {
             throw new \Exception("ParentID and ID is identical, an element can't be the parent of itself.");
         }
         $parent = Asset::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, so delete this document
             //$this->delete();
             // parent document doesn't exist anymore, set the parent to to root
             $this->setParentId(1);
             $this->setPath("/");
         }
     } else {
         if ($this->getId() == 1) {
             // some data in root node should always be the same
             $this->setParentId(0);
             $this->setPath("/");
             $this->setFilename("");
             $this->setType("folder");
         }
     }
     // do not allow PHP and .htaccess files
     if (preg_match("@\\.ph(p[345]?|t|tml|ps)\$@i", $this->getFilename()) || $this->getFilename() == ".htaccess") {
         $this->setFilename($this->getFilename() . ".txt");
     }
     if (Asset\Service::pathExists($this->getFullPath())) {
         $duplicate = Asset::getByPath($this->getFullPath());
         if ($duplicate instanceof Asset and $duplicate->getId() != $this->getId()) {
             throw new \Exception("Duplicate full path [ " . $this->getFullPath() . " ] - cannot save asset");
         }
     }
     if (strlen($this->getFullPath()) > 765) {
         throw new \Exception("Full path is limited to 765 characters, reduce the length of your parent's path");
     }
 }
Пример #7
0
 /**
  * fills object field data values from CSV Import String
  * @abstract
  * @param string $importValue
  * @param null|Model\Object\AbstractObject $object
  * @param mixed $params
  * @return Object\ClassDefinition\Data
  */
 public function getFromCsvImport($importValue, $object = null, $params = [])
 {
     $values = explode(",", $importValue);
     $value = [];
     foreach ($values as $element) {
         $tokens = explode(":", $element);
         if (count($tokens) == 2) {
             $type = $tokens[0];
             $path = $tokens[1];
             $value[] = Element\Service::getElementByPath($type, $path);
         } else {
             //fallback for old export files
             if ($el = Asset::getByPath($element)) {
                 $value[] = $el;
             } elseif ($el = Document::getByPath($element)) {
                 $value[] = $el;
             } elseif ($el = Object::getByPath($element)) {
                 $value[] = $el;
             }
         }
     }
     return $value;
 }
Пример #8
0
 /**
  *
  */
 public function restore($user = null)
 {
     $raw = file_get_contents($this->getStoreageFile());
     $element = Serialize::unserialize($raw);
     // check for element with the same name
     if ($element instanceof Document) {
         $indentElement = Document::getByPath($element->getRealFullPath());
         if ($indentElement) {
             $element->setKey($element->getKey() . "_restore");
         }
     } elseif ($element instanceof Asset) {
         $indentElement = Asset::getByPath($element->getRealFullPath());
         if ($indentElement) {
             $element->setFilename($element->getFilename() . "_restore");
         }
     } elseif ($element instanceof Object\AbstractObject) {
         $indentElement = Object::getByPath($element->getRealFullPath());
         if ($indentElement) {
             $element->setKey($element->getKey() . "_restore");
         }
     }
     if (\Pimcore\Tool\Admin::getCurrentUser()) {
         $parent = $element->getParent();
         if (!$parent->isAllowed("publish")) {
             throw new \Exception("Not sufficient permissions");
         }
     }
     $this->restoreChilds($element);
     $this->delete();
 }
Пример #9
0
 /**
  * @param $element
  * @param $key
  * @param $path
  * @return string
  */
 protected function getSaveCopyName($element, $key, $path)
 {
     if ($element instanceof Object\AbstractObject) {
         $equal = Object\AbstractObject::getByPath($path . "/" . $key);
     } else {
         if ($element instanceof Document) {
             $equal = Document::getByPath($path . "/" . $key);
         } else {
             if ($element instanceof Asset) {
                 $equal = Asset::getByPath($path . "/" . $key);
             }
         }
     }
     if ($equal) {
         $key .= "_WScopy";
         return $this->getSaveCopyName($element, $key, $path);
     }
     return $key;
 }
Пример #10
0
 /**
  * @static
  * @param  string $type
  * @param  string $path
  * @return ElementInterface
  */
 public static function getElementByPath($type, $path)
 {
     if ($type == "asset") {
         $element = Asset::getByPath($path);
     } else {
         if ($type == "object") {
             $element = Object::getByPath($path);
         } else {
             if ($type == "document") {
                 $element = Document::getByPath($path);
             }
         }
     }
     return $element;
 }
Пример #11
0
 /**
  * @param $importValue
  * @param null|Model\Object\AbstractObject $object
  * @param mixed $params
  * @return mixed|null|Asset
  */
 public function getFromCsvImport($importValue, $object = null, $params = [])
 {
     $value = null;
     if ($el = Asset::getByPath($importValue)) {
         $value = $el;
     } else {
         $value = null;
     }
     return $value;
 }
Пример #12
0
 /**
  * @see Document\Tag\TagInterface::setDataFromEditmode
  * @param mixed $data
  * @return void
  */
 public function setDataFromEditmode($data)
 {
     if (!is_array($data)) {
         $data = array();
     }
     if ($doc = Document::getByPath($data['path'])) {
         if ($doc instanceof Document) {
             $data['internal'] = true;
             $data['internalId'] = $doc->getId();
             $data['internalType'] = 'document';
         }
         //its an object?
     } else {
         if (strpos($data['path'], '::') !== FALSE) {
             $data['internal'] = true;
             $data['internalType'] = 'object';
         }
     }
     if (!$data['internal']) {
         if ($asset = Asset::getByPath($data['path'])) {
             if ($asset instanceof Asset) {
                 $data['internal'] = true;
                 $data['internalId'] = $asset->getId();
                 $data['internalType'] = 'asset';
             }
         }
     }
     $this->data = $data;
     return $this;
 }
Пример #13
0
 /**
  * @see Document\Tag\TagInterface::setDataFromEditmode
  * @param mixed $data
  * @return void
  */
 public function setDataFromEditmode($data)
 {
     if (!is_array($data)) {
         $data = array();
     }
     if ($doc = Document::getByPath($data["path"])) {
         if ($doc instanceof Document) {
             $data["internal"] = true;
             $data["internalId"] = $doc->getId();
             $data["internalType"] = "document";
         }
     }
     if (!$data["internal"]) {
         if ($asset = Asset::getByPath($data["path"])) {
             if ($asset instanceof Asset) {
                 $data["internal"] = true;
                 $data["internalId"] = $asset->getId();
                 $data["internalType"] = "asset";
             }
         }
     }
     $this->data = $data;
     return $this;
 }
Пример #14
0
 /**
  * @param $path
  * @return $this
  */
 public function setPath($path)
 {
     if (!empty($path)) {
         if ($document = Document::getByPath($path)) {
             $this->linktype = "internal";
             $this->internalType = "document";
             $this->internal = $document->getId();
         } else {
             if ($asset = Asset::getByPath($path)) {
                 $this->linktype = "internal";
                 $this->internalType = "asset";
                 $this->internal = $asset->getId();
             } else {
                 $this->linktype = "direct";
                 $this->direct = $path;
             }
         }
     }
     return $this;
 }
 public function copyAction()
 {
     $success = false;
     $sourceId = intval($this->getParam("sourceId"));
     $source = Asset::getById($sourceId);
     $session = Tool\Session::get("pimcore_copy");
     $targetId = intval($this->getParam("targetId"));
     if ($this->getParam("targetParentId")) {
         $sourceParent = Asset::getById($this->getParam("sourceParentId"));
         // this is because the key can get the prefix "_copy" if the target does already exists
         if ($session->{$this->getParam("transactionId")}["parentId"]) {
             $targetParent = Asset::getById($session->{$this->getParam("transactionId")}["parentId"]);
         } else {
             $targetParent = Asset::getById($this->getParam("targetParentId"));
         }
         $targetPath = preg_replace("@^" . $sourceParent->getFullPath() . "@", $targetParent . "/", $source->getPath());
         $target = Asset::getByPath($targetPath);
     } else {
         $target = Asset::getById($targetId);
     }
     if ($target->isAllowed("create")) {
         $source = Asset::getById($sourceId);
         if ($source != null) {
             if ($this->getParam("type") == "child") {
                 $newAsset = $this->_assetService->copyAsChild($target, $source);
                 // this is because the key can get the prefix "_copy" if the target does already exists
                 if ($this->getParam("saveParentId")) {
                     $session->{$this->getParam("transactionId")}["parentId"] = $newAsset->getId();
                 }
             } else {
                 if ($this->getParam("type") == "replace") {
                     $this->_assetService->copyContents($target, $source);
                 }
             }
             $success = true;
         } else {
             \Logger::debug("prevended copy/paste because asset with same path+key already exists in this location");
         }
     } else {
         \Logger::error("could not execute copy/paste because of missing permissions on target [ " . $targetId . " ]");
         $this->_helper->json(array("error" => false, "message" => "missing_permission"));
     }
     Tool\Session::writeClose();
     $this->_helper->json(array("success" => $success));
 }
Пример #16
0
 /**
  * @param $importValue
  * @return mixed|null|Asset|Document|Element\ElementInterface
  */
 public function getFromCsvImport($importValue)
 {
     $value = null;
     $values = explode(":", $importValue);
     if (count($values) == 2) {
         $type = $values[0];
         $path = $values[1];
         $value = Element\Service::getElementByPath($type, $path);
     } else {
         //fallback for old export files
         if ($el = Asset::getByPath($importValue)) {
             $value = $el;
         } else {
             if ($el = Document::getByPath($importValue)) {
                 $value = $el;
             } else {
                 if ($el = Object::getByPath($importValue)) {
                     $value = $el;
                 }
             }
         }
     }
     return $value;
 }
Пример #17
0
 /**
  *
  */
 public function restore()
 {
     $raw = file_get_contents($this->getStoreageFile());
     $element = Serialize::unserialize($raw);
     // check for element with the same name
     if ($element instanceof Document) {
         $indentElement = Document::getByPath($element->getFullpath());
         if ($indentElement) {
             $element->setKey($element->getKey() . "_restore");
         }
     } else {
         if ($element instanceof Asset) {
             $indentElement = Asset::getByPath($element->getFullpath());
             if ($indentElement) {
                 $element->setFilename($element->getFilename() . "_restore");
             }
         } else {
             if ($element instanceof Object\AbstractObject) {
                 $indentElement = Object::getByPath($element->getFullpath());
                 if ($indentElement) {
                     $element->setKey($element->getKey() . "_restore");
                 }
             }
         }
     }
     $this->restoreChilds($element);
     $this->delete();
 }
Пример #18
0
 /**
  * @see Document\Tag\TagInterface::setDataFromEditmode
  * @param mixed $data
  * @return void
  */
 public function setDataFromEditmode($data)
 {
     if ($data["type"]) {
         $this->type = $data["type"];
     }
     if ($data["title"]) {
         $this->title = $data["title"];
     }
     if ($data["description"]) {
         $this->description = $data["description"];
     }
     // this is to be backward compatible to <= v 1.4.7
     if ($data["id"]) {
         $data["path"] = $data["id"];
     }
     $video = Asset::getByPath($data["path"]);
     if ($video instanceof Asset\Video) {
         $this->id = $video->getId();
     } else {
         $this->id = $data["path"];
     }
     $this->poster = null;
     $poster = Asset::getByPath($data["poster"]);
     if ($poster instanceof Asset\Image) {
         $this->poster = $poster->getId();
     }
     return $this;
 }
 /**
  * Because Assets are not language restricted, we need to get them from db
  * @param string $url
  *
  * @return string|bool
  */
 protected function getLanguageFromAsset($url = '')
 {
     $urlData = parse_url($url);
     $language = 'all';
     if (empty($urlData['path'])) {
         return $language;
     }
     $asset = \Pimcore\Model\Asset::getByPath($urlData['path']);
     if ($asset instanceof \Pimcore\Model\Asset) {
         $languageProperty = $asset->getProperty('assigned_language');
         if (!is_null($languageProperty)) {
             return $languageProperty;
         }
         return $language;
     }
     return $language;
 }