/** * Move into another folder * * @param DmMediaFolder $folder */ public function move(DmMediaFolder $folder) { if ($folder->id == $this->nodeParentId) { return $this; } if ($folder->getNode()->isDescendantOfOrEqualTo($this)) { throw new dmException('Can not move to a descendant'); } if ($this->getNode()->isRoot()) { throw new dmException('The root folder cannot be moved'); } if (!$this->isWritable()) { throw new dmException(sprintf('The folder %s is not writable.', dmProject::unRootify($this->fullPath))); } if (!$folder->isWritable()) { throw new dmException(sprintf('The folder %s is not writable.', dmProject::unRootify($folder->fullPath))); } if ($folder->hasSubFolder($this->name)) { throw new dmException(sprintf('The selected folder already contains a folder named "%s".', $this->name)); } $oldRelPath = $this->get('rel_path'); $newRelPath = $folder->get('rel_path') . '/' . $this->name; $fs = $this->getService('filesystem'); $oldFullPath = $this->getFullPath(); $newFullPath = dmOs::join($folder->getFullPath(), $this->name); if (!rename($oldFullPath, $newFullPath)) { throw new dmException('Can not move %s to %s', dmProject::unRootify($oldFullPath), dmProject::unRootify($newFullPath)); } $this->set('rel_path', $newRelPath); $this->clearCache(); $this->getNode()->moveAsFirstChildOf($folder); //update descendants if ($descendants = $this->getNode()->getDescendants()) { foreach ($descendants as $folder) { $folder->set('rel_path', dmString::str_replace_once($oldRelPath, $newRelPath, $folder->get('rel_path'))); $folder->save(); } } return $this; }
public function move(DmMediaFolder $folder) { if ($folder->id == $this->dm_media_folder_id) { return $this; } if (!$this->isWritable()) { throw new dmException(sprintf('The file %s is not writable.', dmProject::unRootify($this->fullPath))); } if (!$folder->isWritable()) { throw new dmException(sprintf('The folder %s is not writable.', dmProject::unRootify($folder->fullPath))); } if ($folder->hasSubFolder($this->file)) { throw new dmException(sprintf('The selected folder already contains a folder named "%s".', $this->name)); } if ($folder->hasFile($this->file)) { throw new dmException(sprintf('The selected folder already contains a file named "%s".', $this->name)); } rename($this->fullPath, $folder->fullPath . '/' . $this->file); $this->dm_media_folder_id = $folder->id; $this->Folder = $folder; $this->save(); return $this; }