/** * @param string $text * @param sfAsset $asset * @return string */ function link_to_asset_action($text, $asset) { $user = sfContext::getInstance()->getUser(); if ($user->hasAttribute('popup', 'sf_admin/sf_asset/navigation')) { switch ($user->getAttribute('popup', null, 'sf_admin/sf_asset/navigation')) { case 1: // popup called from a Rich Text Editor (ex: TinyMCE) #return link_to($text, '@sf_asset_library_tiny_config?id=' . $asset->getId(), 'title=' . $asset->getFilename()); throw new sfAssetException('this option should be unused...'); case 2: // popup called from a simple form input (or via input_sf_asset_tag) return link_to_function($text, "setImageField('" . $asset->getUrl() . "','" . $asset->getUrl('small') . "'," . $asset->getId() . ')'); } } else { // case : sf view (i.e. module sfAsset, view list) return link_to($text, '@sf_asset_library_edit?id=' . $asset->getId(), 'title=' . $asset->getFilename()); } }
/** * 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 sfAsset $value A sfAsset object. * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). */ public static function addInstanceToPool(sfAsset $obj, $key = null) { if (Propel::isInstancePoolingEnabled()) { if ($key === null) { $key = (string) $obj->getId(); } // if key === null self::$instances[$key] = $obj; } }
$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(); # So now we have: # root/ # Test_Directory/ sfAssetFolder # Test_Sub-directory/ sfAssetFolder2 # Test_Sub-sub-directory/ sfAssetFolder3 # toto # raikkonen.jpg $t->diag('sfAssetFolder::move()'); # move $sfAssetFolder2 from /root/Test_Directory/Test_Sub-directory to /root/Test_Sub-directory $sfAssetFolder2->move($root); $sfAssetFolder2->save(); # So now we have: # root/ # Test_Directory/ sfAssetFolder # Test_Sub-directory/ sfAssetFolder2
<?php $app = "frontend"; include dirname(__FILE__) . '/../../../../test/bootstrap/functional.php'; $browser = new sfTestBrowser(); $browser->initialize(); $con = Propel::getConnection(); $con->begin(); try { $t = new lime_test(5, new lime_output_color()); $t->diag('sfAssetPeer'); $con->begin(); $t->is(sfAssetPeer::retrieveFromUrl(sfAssetFolderPeer::getRoot()->getRelativePath() . '/filename.jpg'), null, 'sfAssetPeer::retrieveFromUrl() returns null when a URL is not found'); $t->is(sfAssetPeer::exists(sfAssetFolderPeer::getRoot()->getId(), 'filename.jpg'), false, 'sfAssetPeer::exists() returns false when an asset is not found'); $sfAsset = new sfAsset(); $sfAsset->setsfAssetFolder(sfAssetFolderPeer::getRoot()); $sfAsset->setFilename('filename.jpg'); $sfAsset->save($con); $t->is(sfAssetPeer::retrieveFromUrl(sfAssetFolderPeer::getRoot()->getRelativePath() . '/filename.jpg')->getId(), $sfAsset->getId(), 'sfAssetPeer::retrieveFromUrl() finds an asset from its URL'); $t->is(sfAssetPeer::retrieveFromUrl($sfAsset->getUrl())->getId(), $sfAsset->getId(), 'sfAssetPeer::retrieveFromUrl() finds an asset from the result of `getUrl()`'); $t->is(sfAssetPeer::exists(sfAssetFolderPeer::getRoot()->getId(), 'filename.jpg'), true, 'sfAssetPeer::exists() returns true when an asset is found'); } catch (Exception $e) { echo $e->getMessage(); } // reset DB $con->rollback();
$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'); $t->is($asset1->getUrl(), $root->getUrl() . '/' . $asset1->getFilename(), 'move() changes the object url according to the new folder'); $t->ok(is_file($asset1->getFullPath()), 'move() physically moves asset to the new location'); $t->ok(!is_file($old_path), 'move() physically removes asset from the previous location'); $t->ok(is_file($asset1->getFullPath('large')), 'move() physically moves thumbnail to the new location'); $t->ok(!is_file($old_thumb_path), 'move() physically removes thumbnail from the previous location'); $t->diag('sfAsset::delete()'); $old_path = $asset1->getFullPath(); $old_thumb_path = $asset1->getFullPath('large'); $old_id = $asset1->getId(); $asset1->delete(); $t->ok(!is_file($old_path), 'delete() physically removes asset'); $t->ok(!is_file($old_thumb_path), 'delete() physically removes thumbnail'); $null = sfAssetTable::getInstance()->find($old_id); $t->ok(!$null, 'delete() removes asset from DB'); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; echo $e->getTraceAsString() . PHP_EOL; } // reset DB $con->rollBack();
public function executeAddQuick() { $folder = sfAssetFolderPeer::retrieveByPath($this->getRequestParameter('parent_folder')); $this->forward404Unless($folder); try { $asset = new sfAsset(); $asset->setsfAssetFolder($folder); $asset->setDescription($this->getRequest()->getFileName('new_file')); try { $asset->setAuthor($this->getUser()->getUsername()); } catch (sfException $e) { // no getUsername() method in sfUser, all right: do nothing } $asset->setFilename($this->getRequest()->getFileName('new_file')); $asset->create($this->getRequest()->getFilePath('new_file')); $asset->save(); } catch (sfAssetException $e) { $this->setFlash('warning_message', $e->getMessage()); $this->setFlash('warning_params', $e->getMessageParams()); $this->redirectToPath('sfAsset/list?dir=' . $folder->getRelativePath()); } if ($this->getUser()->hasAttribute('popup', 'sf_admin/sf_asset/navigation')) { if ($this->getUser()->getAttribute('popup', null, 'sf_admin/sf_asset/navigation') == 1) { $this->redirect('sfAsset/tinyConfigMedia?id=' . $asset->getId()); } else { $this->redirectToPath('sfAsset/list?dir=' . $folder->getRelativePath()); } } $this->redirect('sfAsset/edit?id=' . $asset->getId()); }
/** * upload asset * @param sfWebRequest $request */ public function executeAddQuick(sfWebRequest $request) { $this->form = new sfAssetForm(); $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName())); if (!$this->form->isValid()) { $this->sf_asset = new sfAsset(); return sfView::SUCCESS; } $folder = sfAssetFolderPeer::retrieveByPK($this->form->getValue('folder_id')); $this->forward404Unless($folder, 'folder not found'); $file = $this->form->getValue('file'); try { $asset = new sfAsset(); $asset->setsfAssetFolder($folder); $asset->setDescription($file->getOriginalName()); try { $asset->setAuthor($this->getUser()->getUsername()); } catch (sfException $e) { // no getUsername() method in sfUser, all right: do nothing } $asset->setFilename($file->getOriginalName()); $asset->create($file->getTempName()); $asset->save(); $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $asset))); } catch (sfAssetException $e) { $this->getUser()->setFlash('warning_message', $e->getMessage()); $this->getUser()->setFlash('warning_params', $e->getMessageParams()); $this->redirectToPath('@sf_asset_library_dir?dir=' . $folder->getRelativePath()); } if ($this->getUser()->hasAttribute('popup', 'sf_admin/sf_asset/navigation')) { if ($this->getUser()->getAttribute('popup', null, 'sf_admin/sf_asset/navigation') == 1) { $this->redirect('@sf_asset_library_tiny_config?id=' . $asset->getId()); } else { $this->redirectToPath('@sf_asset_library_dir?dir=' . $folder->getRelativePath()); } } $this->redirect('@sf_asset_library_edit?id=' . $asset->getId()); }
$test_asset = $assets_path . 'raikkonen.jpg'; $t->ok(is_file($test_asset), 'test asset found'); $sfAsset = new sfAsset(); $sfAsset->setsfAssetFolder(sfAssetFolderPeer::getRoot()); $sfAsset->create($test_asset, false); $t->is($sfAsset->getFilename(), 'raikkonen.jpg', 'create() gives correct filename'); $t->is($sfAsset->getFilesize(), 18, 'create() gives correct size'); $t->ok($sfAsset->isImage(), 'create() gives correct type'); $t->ok(is_file($sfAsset->getFullPath()), 'create() physically copies asset'); $t->ok(is_file($sfAsset->getFullPath('large')), 'create() physically creates thumbnail'); $old_path = $sfAsset->getFullPath(); $old_thumb_path = $sfAsset->getFullPath('large'); $sfAsset->move(sfAssetFolderPeer::getRoot(), 'raikkonen2.jpg'); $t->is($sfAsset->getFilename(), 'raikkonen2.jpg', 'move() changes filename'); $t->ok(is_file($sfAsset->getFullPath()), 'move() physically moves asset'); $t->ok(!is_file($old_path), 'move() physically moves asset'); $t->ok(is_file($sfAsset->getFullPath('large')), 'move() physically moves thumbnail'); $t->ok(!is_file($old_thumb_path), 'move() physically moves thumbnail'); $old_path = $sfAsset->getFullPath(); $old_thumb_path = $sfAsset->getFullPath('large'); $old_id = $sfAsset->getId(); $sfAsset->delete(); $t->ok(!is_file($old_path), 'delete() physically removes asset'); $t->ok(!is_file($old_thumb_path), 'delete() physically removes thumbnail'); $null = sfAssetPeer::retrieveByPk($old_id); $t->ok(!$null, 'delete() removes asset from DB'); } catch (Exception $e) { // do nothing } // reset DB $con->rollback();