/** * 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 execute(DmMediaFolder $folder, $depth = 99) { if ($depth < 1) { return; } /* * Clear php filesystem cache * This will avoid some problems */ clearstatcache(); //$folder->refresh(true); list($dirs, $files) = $this->getDirContents($folder->getFullPath()); $medias = $folder->getDmMediasByFileName(); $children = $folder->getSubfoldersByName(); $dirty = false; /* * 1. Add new files to the medias */ foreach ($files as $file) { $fileBasename = basename($this->sanitizeFile($file)); if (!array_key_exists($fileBasename, $medias)) { // File exists, media does not exist: create media $this->mediaTable->create(array('dm_media_folder_id' => $folder->get('id'), 'file' => $fileBasename))->save(); $dirty = true; } else { // File exists, media exists: do nothing unset($medias[$fileBasename]); } } /* * 2. Remove medias which have no file */ foreach ($medias as $name => $media) { // File does not exist, media exists: delete media try { $media->delete(); } catch (Doctrine_Connection_Exception $e) { //A record needs this media, but the file has been removed :-/ } $dirty = true; } foreach ($dirs as $dir) { $dirName = basename($this->sanitizeDir($dir)); /* * Exists in fs, not in db */ if (!array_key_exists($dirName, $children)) { $subfolderRelPath = trim(dmOs::join($folder->get('rel_path'), $dirName), '/'); if ($child = $this->folderTable->findOneByRelPath($subfolderRelPath)) { // folder exists in db but is not linked to its parent $child->getNode()->moveAsLastChildOf($folder); $child->refresh(); $dirty = true; } else { // dir exists in filesystem, not in database: create folder in database $child = $this->folderTable->create(array('rel_path' => $subfolderRelPath)); $child->getNode()->insertAsLastChildOf($folder); $child->refresh(); $dirty = true; } } else { // dir exists in filesystem and database: do nothing $child = $children[$dirName]; unset($children[$dirName]); } $this->execute($child, $depth - 1); } /* * Not unsetted folders * don't exist in fs */ foreach ($children as $child) { try { $child->getNode()->delete(); } catch (Doctrine_Connection_Exception $e) { //A record needs a media in this folder, but the folder has been removed :-/ $this->filesystem->mkdir($child->getFullPath()); } $dirty = true; } if ($dirty) { $folder->clearCache()->refresh()->refreshRelated('Medias'); } }