Пример #1
0
 /**
  * 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;
 }
Пример #2
0
 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;
 }
Пример #3
0
$helper = new dmMediaUnitTestHelper();
$helper->boot();
$t = new lime_test(58);
$mediaTable = dmDb::table('DmMedia');
$folderTable = dmDb::table('DmMediaFolder');
$t->diag('Media tests');
$folderTable->checkRoot();
$root = $folderTable->getTree()->fetchRoot();
$t->diag('syncing root');
$root->sync();
$helper->checkTreeIntegrity($t);
$helper->testFolderCorrelations($t);
$t->isa_ok($root, 'DmMediaFolder', 'root is a media folder');
$t->is($root->fullPath, dmOs::normalize(sfConfig::get('sf_upload_dir')), 'root full path is ' . $root->fullPath);
$t->diag('add a folder in root');
$folder = new DmMediaFolder();
$folder->relPath = 'test_' . dmString::random(8);
$folder->getNode()->insertAsLastChildOf($root);
$helper->checkTreeIntegrity($t);
$t->ok($folder->exists(), 'folder ' . $folder->name . ' has been created');
$t->is($folder->getNode()->getParent(), $root, 'folder\'s parent is root');
$t->is($folder->fullPath, dmOs::join(sfConfig::get('sf_upload_dir'), $folder->name), 'folder\'s full path is ' . $folder->fullPath);
$t->ok(is_dir($folder->fullPath), 'folder exists in filesystem');
$t->diag('add a file in folder');
$fileName = dmString::random(8) . '_' . basename(__FILE__);
$filePath = dmOs::join($folder->fullPath, $fileName);
copy(__FILE__, $filePath);
$media = $mediaTable->create(array('file' => basename($filePath), 'author' => 'Thibault D.', 'legend' => 'dmMedia test cases', 'dm_media_folder_id' => $folder->id))->saveGet();
$t->ok($media->exists(), 'media has been saved');
$t->is($media->fullPath, $folder->fullPath . '/' . $media->file, 'Media full path is ' . $media->fullPath);
$t->is($media->relPath, $folder->relPath . '/' . $media->file, 'Media rel path is ' . $media->relPath);
Пример #4
0
 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');
     }
 }