Exemplo n.º 1
0
 /**
  * Returns a query matching media items satisfying the specified parameters, all of which
  * are optional:
  * 
  * tag
  * search
  * type (video, image, etc)
  * user (a username, to determine access rights)
  * aspect-width and aspect-height (returns only images with the specified aspect ratio)
  * minimum-width
  * minimum-height
  * width
  * height
  * ids
  * downloadable
  * embeddable
  * 
  * Parameters are passed safely via wildcards so it should be OK to pass unsanitized
  * external API inputs to this method.
  * 
  * 'ids' is an array of item IDs. If it is present, only items with one of those IDs are
  * potentially returned.
  * 
  * If 'search' is present, results are returned in descending order by match quality.
  * Otherwise, if 'ids' is present, results are returned in that order. Otherwise,
  * results are returned newest first.
  * @param mixed $params
  * @return mixed
  */
 public static function getBrowseQuery($params)
 {
     $query = Doctrine_Query::create();
     // We can't use an alias because that is incompatible with getObjectTaggedWithQuery
     $query->from('aMediaItem');
     if (isset($params['ids'])) {
         $query->select('aMediaItem.*, c.*');
         aDoctrine::orderByList($query, $params['ids']);
         $query->andWhereIn("aMediaItem.id", $params['ids']);
     }
     // New: at least one of the specified tags must be present. This is kind of a pain to check for because
     // tags can be specified as arrays or a comma separated string
     if (isset($params['allowed_tags']) && (strlen($params['allowed_tags']) || is_array($params['allowed_tags']) && count($params['allowed_tags']))) {
         $query = TagTable::getObjectTaggedWithQuery('aMediaItem', $params['allowed_tags'], $query, array('nb_common_tags' => 1));
     } elseif (isset($params['tag'])) {
         $query = TagTable::getObjectTaggedWithQuery('aMediaItem', $params['tag'], $query);
     }
     if (isset($params['type'])) {
         // Supports metatypes like _downloadable
         $types = array();
         $typeInfos = aMediaTools::getTypeInfos($params['type']);
         foreach ($typeInfos as $name => $info) {
             $types[] = $name;
         }
         if (count($types)) {
             $query->andWhereIn("aMediaItem.type", $types);
         } else {
             $query->andWhere("0 <> 0");
         }
     }
     if (isset($params['allowed_categories'])) {
         if (!count($params['allowed_categories'])) {
             $query->andWhere('0 <> 0');
         } else {
             $query->innerJoin('aMediaItem.Categories mc1 WITH mc1.id IN (' . implode(',', aArray::getIds($params['allowed_categories'])) . ')');
         }
     }
     if (isset($params['category'])) {
         $query->innerJoin('aMediaItem.Categories mc2 WITH mc2.slug = ?', array($params['category']));
     }
     if (isset($params['search'])) {
         $query = Doctrine::getTable('aMediaItem')->addSearchQuery($query, $params['search']);
     } elseif (isset($params['ids'])) {
         // orderBy added by aDoctrine::orderByIds
     } else {
         // Reverse chrono order if we're not ordering them by search relevance
         $query->orderBy('aMediaItem.id desc');
     }
     if (!sfContext::getInstance()->getUser()->hasCredential(sfConfig::get('app_a_view_locked_sufficient_credentials', 'view_locked'))) {
         $query->andWhere('aMediaItem.view_is_secure = false');
     }
     if (isset($params['aspect-width']) && isset($params['aspect-height'])) {
         $query->andWhere('(aMediaItem.width * ? / ?) = aMediaItem.height', array($params['aspect-height'] + 0, $params['aspect-width'] + 0));
     }
     if (isset($params['minimum-width'])) {
         $query->andWhere('aMediaItem.width >= ?', array($params['minimum-width'] + 0));
     }
     if (isset($params['minimum-height'])) {
         $query->andWhere('aMediaItem.height >= ?', array($params['minimum-height'] + 0));
     }
     if (isset($params['width'])) {
         $query->andWhere('aMediaItem.width = ?', array($params['width'] + 0));
     }
     if (isset($params['height'])) {
         $query->andWhere('aMediaItem.height = ?', array($params['height'] + 0));
     }
     // No crops in the browser please
     $query->andWhere("aMediaItem.slug NOT LIKE '%.%'");
     $query->leftJoin("aMediaItem.Categories c");
     return $query;
 }
Exemplo n.º 2
0
 /**
  * DOCUMENT ME
  */
 public function configure()
 {
     // This call was missing, preventing easy extension of all media item edit forms at the project level
     parent::configure();
     unset($this['id']);
     unset($this['type']);
     unset($this['service_url']);
     unset($this['slug']);
     unset($this['width']);
     unset($this['height']);
     unset($this['format']);
     unset($this['embed']);
     $this->setWidget('file', new aWidgetFormInputFilePersistent(array()));
     $item = $this->getObject();
     // A safe assumption because we always go through the separate upload form on the first pass.
     // This label is a hint that changing the file is not mandatory
     // The 'Replace File' label is safer than superimposing a file button
     // on something that may or may not be a preview or generally a good thing
     // to try to read a button on top of
     $this->getWidget('file')->setLabel('Select a new file');
     if (!$item->isNew()) {
         $this->getWidget('file')->setOption('default-preview', $item->getOriginalPath());
     }
     $mimeTypes = aMediaTools::getOption('mime_types');
     // It comes back as a mapping of extensions to types, get the types
     $extensions = array_keys($mimeTypes);
     $mimeTypes = array_values($mimeTypes);
     $type = false;
     if (!$this->getObject()->isNew()) {
         // You can't change the major type of an existing media object as
         // this would break slots (a .doc where a .gif should be...)
         $type = $this->getObject()->type;
     }
     // What we are selecting to add to a page
     if (!$type) {
         $type = aMediaTools::getType();
     }
     if (!$type) {
         // What we are filtering for
         $type = aMediaTools::getSearchParameter('type');
     }
     if ($type) {
         // This supports composite types like _downloadable
         $infos = aMediaTools::getTypeInfos($type);
         $extensions = array();
         foreach ($infos as $info) {
             if ($info['embeddable']) {
                 // This widget is actually supplying a thumbnail - allow gif, jpg and png
                 $info['extensions'] = array('gif', 'jpg', 'png');
             }
             foreach ($info['extensions'] as $extension) {
                 $extensions[] = $extension;
             }
         }
         $mimeTypes = array();
         $mimeTypesByExtension = aMediaTools::getOption('mime_types');
         foreach ($extensions as $extension) {
             // Careful, if we are filtering for a particular type then not everything
             // will be on the list
             if (isset($mimeTypesByExtension[$extension])) {
                 $mimeTypes[] = $mimeTypesByExtension[$extension];
             }
         }
     }
     // The file is mandatory if it's new. Otherwise
     // we have a problem when they get the file type wrong
     // for one of two and we have to reject that one,
     // then they resubmit - we can add an affirmative way
     // to remove one item from the annotation form later
     $this->setValidator("file", new aValidatorFilePersistent(array("mime_types" => $mimeTypes, 'validated_file_class' => 'aValidatedFile', "required" => $this->getObject()->isNew() ? true : false), array("mime_types" => "The following file types are accepted: " . implode(', ', $extensions))));
     // Necessary to work around FCK's "id and name cannot differ" problem
     // ... But we do it in the action which knows when that matters
     // $this->widgetSchema->setNameFormat('a_media_item_'.$this->getObject()->getId().'_%s');
     // $this->widgetSchema->setFormFormatterName('aAdmin');
 }
Exemplo n.º 3
0
 /**
  * DOCUMENT ME
  * @return mixed
  */
 public static function getUploadAllowed()
 {
     foreach (aMediaTools::getTypeInfos(aMediaTools::getType()) as $typeInfo) {
         if (count($typeInfo['extensions'])) {
             return true;
         }
     }
     return false;
 }