/**
  * Recursively creates parent folders
  *
  * @param string $path
  * @return sfAssetFolder
  */
 public static function createFromPath($path)
 {
     $path = self::cleanPath($path);
     list($parent_path, $name) = sfAssetsLibraryTools::splitPath($path);
     if (!($parent_folder = self::retrieveByPath($parent_path))) {
         $parent_folder = self::createFromPath($parent_path);
         $parent_folder->save();
     }
     $folder = new sfAssetFolder();
     $folder->setName($name);
     $folder->setRelativePath($path);
     $folder->insertAsLastChildOf($parent_folder);
     $folder->save();
     return $folder;
 }
Exemple #2
0
 /**
  * Performs the work of inserting or updating the row in the database.
  *
  * If the object is new, it inserts it; otherwise an update is performed.
  * All related objects are also updated in this method.
  *
  * @param      PropelPDO $con
  * @return     int The number of rows affected by this insert/update and any referring fk objects' save() operations.
  * @throws     PropelException
  * @see        save()
  */
 protected function doSave(PropelPDO $con)
 {
     $affectedRows = 0;
     // initialize var to track total num of affected rows
     if (!$this->alreadyInSave) {
         $this->alreadyInSave = true;
         // We call the save method on the following object(s) if they
         // were passed to this object by their coresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         if ($this->asfAssetFolder !== null) {
             if ($this->asfAssetFolder->isModified() || $this->asfAssetFolder->isNew()) {
                 $affectedRows += $this->asfAssetFolder->save($con);
             }
             $this->setsfAssetFolder($this->asfAssetFolder);
         }
         if ($this->isNew()) {
             $this->modifiedColumns[] = sfAssetPeer::ID;
         }
         // If this object has been modified, then save it to the database.
         if ($this->isModified()) {
             if ($this->isNew()) {
                 $pk = sfAssetPeer::doInsert($this, $con);
                 $affectedRows += 1;
                 // we are assuming that there is only 1 row per doInsert() which
                 // should always be true here (even though technically
                 // BasePeer::doInsert() can insert multiple rows).
                 $this->setId($pk);
                 //[IMV] update autoincrement primary key
                 $this->setNew(false);
             } else {
                 $affectedRows += sfAssetPeer::doUpdate($this, $con);
             }
             $this->resetModified();
             // [HL] After being saved an object is no longer 'modified'
         }
         $this->alreadyInSave = false;
     }
     return $affectedRows;
 }
 /**
  * Overrrides the parent synchronizeWith() method to add a fix when calling
  * insertAsLastChildOf($this->reload()) because $this->reload() doesn't return
  * $this.
  *
  * @see parent::synchronizeWith()
  */
 public function synchronizeWith($baseFolder, $verbose = true, $removeOrphanAssets = false, $removeOrphanFolders = false)
 {
     if (!is_dir($baseFolder)) {
         throw new sfAssetException(sprintf('%s is not a directory', $baseFolder));
     }
     $files = sfFinder::type('file')->maxdepth(0)->ignore_version_control()->in($baseFolder);
     $assets = $this->getAssetsWithFilenames();
     foreach ($files as $file) {
         if (!array_key_exists(basename($file), $assets)) {
             // File exists, asset does not exist: create asset
             $sfAsset = new sfAsset();
             $sfAsset->setFolderId($this->getId());
             $sfAsset->create($file, false);
             $sfAsset->save();
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Importing file %s", $file), 'green');
             }
         } else {
             // File exists, asset exists: do nothing
             unset($assets[basename($file)]);
         }
     }
     foreach ($assets as $name => $asset) {
         if ($removeOrphanAssets) {
             // File does not exist, asset exists: delete asset
             $asset->delete();
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Deleting asset %s", $asset->getUrl()), 'yellow');
             }
         } else {
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Warning: No file for asset %s", $asset->getUrl()), 'red');
             }
         }
     }
     $dirs = sfFinder::type('dir')->maxdepth(0)->discard(sfConfig::get('app_sfAssetsLibrary_thumbnail_dir', 'thumbnail'))->ignore_version_control()->in($baseFolder);
     $folders = $this->getSubfoldersWithFolderNames();
     foreach ($dirs as $dir) {
         list(, $name) = sfAssetsLibraryTools::splitPath($dir);
         if (!array_key_exists($name, $folders)) {
             $this->reload();
             // dir exists in filesystem, not in database: create folder in database
             $sfAssetFolder = new sfAssetFolder();
             $sfAssetFolder->insertAsLastChildOf($this);
             $sfAssetFolder->setName($name);
             $sfAssetFolder->save();
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Importing directory %s", $dir), 'green');
             }
         } else {
             // dir exists in filesystem and database: look inside
             $sfAssetFolder = $folders[$name];
             unset($folders[$name]);
         }
         $sfAssetFolder->synchronizeWith($dir, $verbose, $removeOrphanAssets, $removeOrphanFolders);
     }
     foreach ($folders as $name => $folder) {
         if ($removeOrphanFolders) {
             $folder->delete(null, true);
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Deleting folder %s", $folder->getRelativePath()), 'yellow');
             }
         } else {
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Warning: No directory for folder %s", $folder->getRelativePath()), 'red');
             }
         }
     }
 }
 $sfAssetFolder->save();
 $t->is($sfAssetFolder->getName(), 'Test_Directory', 'getName() returns the folder name');
 $t->diag('sfAssetFolder::getRelativePath()');
 $t->is($sfAssetFolder->getRelativePath(), $root->getRelativePath() . '/' . $sfAssetFolder->getName(), 'getRelativePath() returns the folder relative path, including its own name');
 # $sfAssetFolder2 is /root/Test_Directory/Test_Sub-directory
 $sfAssetFolder2 = new sfAssetFolder();
 $sfAssetFolder2->setName('Test_Sub-directory');
 $sfAssetFolder2->insertAsFirstChildOf($sfAssetFolder);
 $sfAssetFolder2->save();
 $t->is($sfAssetFolder2->getRelativePath(), $sfAssetFolder->getRelativePath() . '/' . $sfAssetFolder2->getName(), 'getRelativePath() returns the folder relative path, including its parent name');
 $id2 = $sfAssetFolder2->getId();
 # $sfAssetFolder3 is /root/Test_Directory/Test_Sub-directory/Test_Sub-sub-directory
 $sfAssetFolder3 = new sfAssetFolder();
 $sfAssetFolder3->insertAsFirstChildOf($sfAssetFolder2);
 $sfAssetFolder3->setName('Test_Sub-sub-directory');
 $sfAssetFolder3->save();
 $t->is($sfAssetFolder3->getRelativePath(), $sfAssetFolder2->getRelativePath() . '/' . $sfAssetFolder3->getName(), 'getRelativePath() returns the folder relative path, including its ancestors names');
 $id3 = $sfAssetFolder3->getId();
 # $sfAsset is /root/Test_Directory/Test_Sub-directory/raikkonen.jpg
 $assets_path = dirname(__FILE__) . '/../assets/';
 $test_asset = $assets_path . 'raikkonen.jpg';
 $sfAsset = new sfAsset();
 $sfAsset->setFolder($sfAssetFolder2);
 $sfAsset->create($test_asset, false);
 $sfAsset->save();
 $sf_asset_id = $sfAsset->getId();
 # $sfAsset2 is /root/Test_Directory/Test_Sub-directory/Test_Sub-sub-directory/toto
 $sfAsset2 = new sfAsset();
 $sfAsset2->setFolder($sfAssetFolder3);
 $sfAsset2->setFilename('toto');
 $sfAsset2->create($test_asset, false);
 $t->is($sfAsset->getRelativePath(), sfConfig::get('app_sfAssetsLibrary_upload_dir') . DIRECTORY_SEPARATOR . 'filename.jpg', 'getRelativePath() returns the path relative to the media directory');
 $t->diag('sfAsset::getFullPath()');
 $t->is($sfAsset->getFullPath(), sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . sfConfig::get('app_sfAssetsLibrary_upload_dir') . DIRECTORY_SEPARATOR . 'filename.jpg', 'getFullPath() returns the complete asset path on the disk');
 $t->is($sfAsset->getFullPath('small'), sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . sfConfig::get('app_sfAssetsLibrary_upload_dir') . DIRECTORY_SEPARATOR . 'thumbnail' . DIRECTORY_SEPARATOR . 'small_filename.jpg', 'getFullPath(\'small\') returns the complete small thumbnail path on the disk');
 $t->is($sfAsset->getFullPath('large'), sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . sfConfig::get('app_sfAssetsLibrary_upload_dir') . DIRECTORY_SEPARATOR . 'thumbnail' . DIRECTORY_SEPARATOR . 'large_filename.jpg', 'getFullPath(\'large\') returns the complete large thumbnail path on the disk');
 $t->diag('sfAsset::getUrl()');
 $t->is($sfAsset->getUrl(), '/' . sfConfig::get('app_sfAssetsLibrary_upload_dir') . DIRECTORY_SEPARATOR . 'filename.jpg', 'getUrl() returns the asset URL');
 $t->is($sfAsset->getUrl('small'), '/' . sfConfig::get('app_sfAssetsLibrary_upload_dir') . '/thumbnail/small_filename.jpg', 'getUrl(\'small\') returns the small thumbnail url');
 $t->is($sfAsset->getUrl('large'), '/' . sfConfig::get('app_sfAssetsLibrary_upload_dir') . '/thumbnail/large_filename.jpg', 'getUrl(\'large\') returns the large thumbnail url');
 $t->diag('sfAsset::create()');
 $assets_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR;
 $test_asset = $assets_path . 'raikkonen.jpg';
 $folder1 = new sfAssetFolder();
 $folder1->getNode()->insertAsFirstChildOf($root);
 $folder1->setName('Test_Directory');
 $folder1->save();
 $asset1 = new sfAsset();
 $asset1->setFolder($folder1);
 $asset1->create($test_asset, false);
 $asset_file_size = (int) (filesize($test_asset) / 1024);
 $t->is($asset1->getFilename(), 'raikkonen.jpg', 'create() sets the object filename according to the given asset');
 $t->is($asset1->getFilesize(), 18, 'create() sets the object filesize according to the given asset');
 $t->ok($asset1->isImage(), 'create() sets the object type according to the given asset');
 $t->is($asset1->getUrl(), $folder1->getUrl() . '/' . $asset1->getFilename(), 'create() sets the object url according to the given asset and folder object');
 $t->ok(is_file($asset1->getFullPath()), 'create() physically copies asset');
 $t->ok(is_file($asset1->getFullPath('large')), 'create() physically creates thumbnail');
 $t->diag('sfAsset::move()');
 $old_path = $asset1->getFullPath();
 $old_thumb_path = $asset1->getFullPath('large');
 $asset1->move($root, 'raikkonen2.jpg');
 $t->is($asset1->getFilename(), 'raikkonen2.jpg', 'move() changes filename');
 public function executeCreateFolder()
 {
     if ($this->getRequest()->getMethod() == sfRequest::POST) {
         // Handle the form submission
         $parentFolder = sfAssetFolderPeer::retrieveByPath($this->getRequestParameter('parent_folder'));
         $this->forward404Unless($parentFolder);
         $folder = new sfAssetFolder();
         $folder->setName($this->getRequestParameter('name'));
         $folder->insertAsLastChildOf($this->parentFolder);
         $folder->save();
         $this->redirectToPath('sfAsset/list?dir=' . $folder->getRelativePath());
     } else {
         // Display the form
         return sfView::SUCCESS;
     }
 }
 /**
  * create folder
  * @param sfWebRequest $request
  */
 public function executeCreateFolder(sfWebRequest $request)
 {
     $this->form = new sfAssetFolderForm();
     $this->form->bind($request->getParameter($this->form->getName()));
     if ($this->form->isValid()) {
         $parentFolder = sfAssetFolderPeer::retrieveByPK($this->form->getValue('parent_folder'));
         $this->forward404Unless($parentFolder, 'parent folder not found');
         $folder = new sfAssetFolder();
         $folder->setName($this->form->getValue('name'));
         $folder->insertAsLastChildOf($parentFolder);
         $folder->save();
         $this->redirectToPath('@sf_asset_library_dir?dir=' . $folder->getRelativePath());
     }
 }