/** * Change asset directory and/or name * * @param sfAssetFolder $newFolder * @param string $newFilename */ public function move(sfAssetFolder $newFolder, $newFilename = null) { if (sfAssetPeer::exists($newFolder->getId(), $newFilename ? $newFilename : $this->getFilename())) { throw new sfAssetException('The target folder "%folder%" already contains an asset named "%name%". The asset has not been moved.', array('%folder%' => $newFolder->getName(), '%name%' => $newFilename ? $newFilename : $this->getFilename())); } $oldFilepaths = $this->getFilepaths(); if ($newFilename) { if (sfAssetsLibraryTools::sanitizeName($newFilename) != $newFilename) { throw new sfAssetException('The filename "%name%" contains incorrect characters. The asset has not be altered.', array('%name%' => $newFilename)); } $this->setFilename($newFilename); } $this->setFolderId($newFolder->getId()); $success = true; foreach ($oldFilepaths as $type => $filepath) { $success = rename($filepath, $this->getFullPath($type)) && $success; } if (!$success) { throw new sfAssetException('Some or all of the file operations failed. It is possible that the moved asset or its thumbnails are missing.'); } }
$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); $sfAsset2->save(); $sf_asset2_id = $sfAsset2->getId();
/** * get a criteria for all folders except one * @param sfAssetFolder $folder folder to exclude * @return Criteria */ public static function getAllPathsButOneCriteria($folder) { $c = new Criteria(); $c->add(self::ID, $folder->getId(), Criteria::NOT_EQUAL); return $c; }
/** * Move under a new parent * * @param sfAssetFolder $new_parent */ public function move(sfAssetFolder $new_parent) { // controls if ($this->getNode()->isRoot()) { throw new sfAssetException('The root folder cannot be moved'); } else { if ($new_parent->hasSubFolder($this->getName())) { throw new sfAssetException('The target folder "%folder%" already contains a folder named "%name%". The folder has not been moved.', array('%folder%' => $new_parent, '%name%' => $this->getName())); } else { if ($new_parent->getNode()->isDescendantOf($this)) { throw new sfAssetException('The target folder cannot be a subfolder of moved folder. The folder has not been moved.'); } else { if ($this->getId() == $new_parent->getId()) { return; } } } } $old_path = $this->getFullPath(); $this->getNode()->moveAsLastChildOf($new_parent); $this->save(); $descendants = $this->getNode()->getChildren(); $descendants = $descendants ? $descendants : array(); // move its assets self::movePhysically($old_path, $this->getFullPath()); foreach ($descendants as $descendant) { // Update relative path $descendant->save(); } // else: nothing to do }
/** * Adds an object to the instance pool. * * Propel keeps cached copies of objects in an instance pool when they are retrieved * from the database. In some cases -- especially when you override doSelect*() * methods in your stub classes -- you may need to explicitly add objects * to the cache in order to ensure that the same objects are always returned by doSelect*() * and retrieveByPK*() calls. * * @param sfAssetFolder $value A sfAssetFolder object. * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). */ public static function addInstanceToPool(sfAssetFolder $obj, $key = null) { if (Propel::isInstancePoolingEnabled()) { if ($key === null) { $key = (string) $obj->getId(); } // if key === null self::$instances[$key] = $obj; } }
/** * Declares an association between this object and a sfAssetFolder object. * * @param sfAssetFolder $v * @return sfAsset The current object (for fluent API support) * @throws PropelException */ public function setsfAssetFolder(sfAssetFolder $v = null) { if ($v === null) { $this->setFolderId(NULL); } else { $this->setFolderId($v->getId()); } $this->asfAssetFolder = $v; // Add binding for other direction of this n:n relationship. // If this object has already been added to the sfAssetFolder object, it will not be re-added. if ($v !== null) { $v->addsfAsset($this); } return $this; }
/** * get a criteria for all folders except one * @param sfAssetFolder $folder folder to exclude * @return Doctrine_Query */ public function getAllPathsButOneCriteria($folder) { $query = $this->createQuery()->where('id != ?', $folder->getId()); return $query; }