Ejemplo n.º 1
0
function thumbnail_uri($asset, $folder_path = null, $type = 'small')
{
    if ($asset->supportsThumbnails()) {
        $uri = '/' . (isset($folder_path) ? $folder_path : $asset->getFolderPath()) . lyMediaThumbnails::getThumbnailFolder() . '/' . $asset->getThumbnailFile($type);
    } else {
        $uri = '/lyMediaManagerPlugin/images/' . $asset->getThumbnailFile($type);
    }
    return $uri;
}
 public function setup()
 {
     parent::setup();
     unset($this['title'], $this['description'], $this['relative_path'], $this['lft'], $this['rgt'], $this['level'], $this['created_at'], $this['updated_at']);
     $this->widgetSchema['parent_id'] = new sfWidgetFormInputHidden();
     $this->validatorSchema['parent_id'] = new sfValidatorInteger();
     $this->widgetSchema['name']->setAttribute('size', 10);
     $this->widgetSchema['name']->setLabel('Add subfolder');
     $this->validatorSchema['name'] = new sfValidatorRegex(array('required' => true, 'must_match' => false, 'pattern' => '#^' . lyMediaThumbnails::getThumbnailFolder() . '$|[^a-z0-9-_]#i'));
     $this->validatorSchema->setPostValidator(new lyMediaValidatorFolder());
 }
 public function setup()
 {
     parent::setup();
     unset($this['relative_path'], $this['lft'], $this['rgt'], $this['level'], $this['created_at'], $this['updated_at']);
     $query = Doctrine_Query::create()->from('lyMediaFolder f');
     if (!$this->isNew()) {
         $query->where('f.lft < ? OR f.rgt > ?', array($this->getObject()->getLft(), $this->getObject()->getRgt()));
     }
     $this->widgetSchema['parent_id'] = new sfWidgetFormDoctrineChoice(array('model' => 'lyMediaManagerFolder', 'order_by' => array('lft', ''), 'method' => 'getIndentName', 'query' => $query));
     if ($this->isNew()) {
         if ($user = $this->getOption('user')) {
             if ($user->getAttribute('view') == 'icons' && $user->getAttribute('folder_id')) {
                 $this->setDefault('parent_id', $user->getAttribute('folder_id'));
             }
         }
     } else {
         $this->widgetSchema['parent_id']->setOption('add_empty', 'Move to ...');
     }
     $this->validatorSchema['parent_id'] = new sfValidatorDoctrineChoice(array('required' => $this->isNew(), 'model' => 'lyMediaFolder'));
     $this->validatorSchema['name'] = new sfValidatorRegex(array('required' => true, 'must_match' => false, 'pattern' => '#^' . lyMediaThumbnails::getThumbnailFolder() . '$|[^a-z0-9-_]#i'));
     $this->validatorSchema->setPostValidator(new lyMediaValidatorFolder());
 }
 /**
  * Deletes a file and related thumbnails.
  *
  * @param string $file path of the file to delete (can be relative to web dir).
  * @param bool $thumbs, if true thubnail files are also deleted.
  */
 public function unlink($file, $thumbs = false)
 {
     $file = $this->makePathAbsolute($file);
     if ($thumbs) {
         $info = pathinfo($file);
         $path = $info['dirname'] . DIRECTORY_SEPARATOR . lyMediaThumbnails::getThumbnailFolder() . DIRECTORY_SEPARATOR;
         foreach ($this->getThumbnailTypes() as $key) {
             $tfile = $path . $key . '_' . $info['basename'];
             if (file_exists($tfile)) {
                 if (!@unlink($tfile)) {
                     throw new lyMediaException('Can\'t delete thumbnail "%file%" (permission denied).', array('%file%' => basename($tfile)));
                 }
             }
         }
     }
     if (file_exists($file)) {
         if (!@unlink($file)) {
             throw new lyMediaException('Can\'t delete file "%file%" (permission denied).', array('%file%' => basename($file)));
         }
     }
 }
 /**
  * Used by synchronize task.
  *
  * @param string $baseFolder
  * @param bool $verbose
  * @param bool $removeOrphanAssets
  * @param bool $removeOrphanFolders
  */
 public function synchronizeWith($baseFolder, $verbose = true, $removeOrphanAssets = false, $removeOrphanFolders = false)
 {
     if (!is_dir($baseFolder)) {
         throw new lyMediaException(sprintf('%s is not a directory', $baseFolder));
     }
     $files = sfFinder::type('file')->maxdepth(0)->ignore_version_control()->in($baseFolder);
     $assets = $this->getAssetsWithFilenames();
     $fs = new lyMediaFileSystem();
     foreach ($files as $file) {
         $basename = basename($file);
         if (!array_key_exists($basename, $assets)) {
             // File exists, asset does not exist: create asset
             $fs->import($file, $this->getRelativePath() . $basename);
             $lyMediaAsset = new lyMediaAsset();
             $lyMediaAsset->setFolderId($this->getId());
             $lyMediaAsset->setFilename($basename);
             $lyMediaAsset->setType(mime_content_type($file));
             $lyMediaAsset->setFilesize(round(filesize($file) / 1024));
             $lyMediaAsset->save();
             if ($verbose) {
                 lyMediaTools::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) {
                 lyMediaTools::log(sprintf("Deleting asset %s", $asset->getPath()), 'yellow');
             }
         } else {
             if ($verbose) {
                 lyMediaTools::log(sprintf("Warning: No file for asset %s", $asset->getPath()), 'red');
             }
         }
     }
     $dirs = sfFinder::type('dir')->maxdepth(0)->discard(lyMediaThumbnails::getThumbnailFolder())->ignore_version_control()->in($baseFolder);
     $folders = $this->getSubfoldersWithFolderNames();
     foreach ($dirs as $dir) {
         list(, $name) = lyMediaTools::splitPath($dir);
         if (!array_key_exists($name, $folders)) {
             // dir exists in filesystem, not in database: create folder in database
             $lyMediaFolder = new lyMediaFolder();
             $lyMediaFolder->setName($name);
             $lyMediaFolder->create($this);
             if ($verbose) {
                 lyMediaTools::log(sprintf("Importing directory %s", $dir), 'green');
             }
         } else {
             // dir exists in filesystem and database: look inside
             $lyMediaFolder = $folders[$name];
             unset($folders[$name]);
         }
         $lyMediaFolder->synchronizeWith($dir, $verbose, $removeOrphanAssets, $removeOrphanFolders);
     }
     foreach ($folders as $name => $folder) {
         if ($removeOrphanFolders) {
             $folder->delete(null, true);
             if ($verbose) {
                 lyMediaTools::log(sprintf("Deleting folder %s", $folder->getRelativePath()), 'yellow');
             }
         } else {
             if ($verbose) {
                 lyMediaTools::log(sprintf("Warning: No directory for folder %s", $folder->getRelativePath()), 'red');
             }
         }
     }
 }