/**
  * DOCUMENT ME
  * @return mixed
  */
 protected function getTaggedItems()
 {
     $value = $this->slot->getArrayValue();
     $this->items = array();
     $this->itemIds = array();
     // Not set yet
     if (!count($value)) {
         return;
     }
     if (isset($value['form'])) {
         // Tolerate what my early alphas did to save our devs some grief, but don't
         // respect it
         return;
     }
     // We have getBrowseQuery, so reuse it!
     $params = array();
     if (isset($value['categories_list'])) {
         $params['allowed_categories'] = aCategoryTable::getInstance()->createQuery('c')->whereIn('c.id', $value['categories_list'])->execute();
     }
     if (isset($value['tags_list'])) {
         $params['allowed_tags'] = $value['tags_list'];
     }
     if (isset($this->options['constraints'])) {
         foreach ($this->options['constraints'] as $k => $v) {
             $params[$k] = $v;
         }
     }
     $params['type'] = 'image';
     $q = aMediaItemTable::getBrowseQuery($params);
     $q->andWhere('(aMediaItem.view_is_secure IS NULL OR aMediaItem.view_is_secure IS FALSE)');
     $q->limit($value['count']);
     $q->orderBy('aMediaItem.created_at DESC');
     $this->items = $q->execute();
     // shuffle likes real arrays better
     $a = array();
     foreach ($this->items as $item) {
         $a[] = $item;
     }
     $this->items = $a;
     $this->itemIds = aArray::getIds($this->items);
 }
 public function executeInfo(sfRequest $request)
 {
     $params = array();
     $this->validateAPIKey();
     if ($request->hasParameter('ids')) {
         $ids = $request->getParameter('ids');
         if (!preg_match("/^(\\d+\\,?)*\$/", $ids)) {
             // Malformed request
             $this->jsonResponse('malformed');
         }
         $ids = explode(",", $ids);
         if ($ids === false) {
             $ids = array();
         }
         $params['ids'] = $ids;
     }
     $numbers = array("width", "height", "minimum-width", "minimum-height", "aspect-width", "aspect-height");
     foreach ($numbers as $number) {
         if ($request->hasParameter($number)) {
             $n = $request->getParameter($number) + 0;
             if ($number < 0) {
                 $n = 0;
             }
             $params[$number] = $n;
         }
     }
     $strings = array("tag", "search", "type", "user");
     foreach ($strings as $string) {
         if ($request->hasParameter($string)) {
             $params[$string] = $request->getParameter($string);
         }
     }
     if (isset($params['tag'])) {
         $this->logMessage("ZZZZZ got tag: " . $params['tag'], "info");
     }
     $query = aMediaItemTable::getBrowseQuery($params);
     $countQuery = clone $query;
     $countQuery->offset(0);
     $countQuery->limit(0);
     $result = new StdClass();
     $result->total = $countQuery->count();
     if ($request->hasParameter('offset')) {
         $offset = max($request->getParameter('offset') + 0, 0);
         $query->offset($offset);
     }
     if ($request->hasParameter('limit')) {
         $limit = max($request->getParameter('limit') + 0, 0);
         $query->limit($limit);
     }
     $absolute = !!$request->getParameter('absolute', false);
     $items = $query->execute();
     $nitems = array();
     foreach ($items as $item) {
         $info = array();
         $info['type'] = $item->getType();
         $info['id'] = $item->getId();
         $info['slug'] = $item->getSlug();
         $info['width'] = $item->getWidth();
         $info['height'] = $item->getHeight();
         $info['format'] = $item->getFormat();
         $info['title'] = $item->getTitle();
         $info['description'] = $item->getDescription();
         $info['credit'] = $item->getCredit();
         $info['tags'] = array_keys($item->getTags());
         // The embed HTML we suggest is a template in which they can
         // replace _WIDTH_ and _HEIGHT_ and _c-OR-s_ with
         // whatever they please
         // Absolute URL option
         $info['embed'] = $item->getEmbedCode('_WIDTH_', '_HEIGHT_', '_c-OR-s_', '_FORMAT_', $absolute);
         // The image URL we suggest is a template in which they can
         // replace _WIDTH_, _HEIGHT_, _c-OR-s_ and _FORMAT_ with
         // whatever they please
         $controller = sfContext::getInstance()->getController();
         // Must use keys that will be acceptable as property names, no hyphens!
         // original refers to the original file, if we ever had it
         // (images and PDFs). If you ask for the original of a video, you
         // currently get the media plugin's copy of the best available still.
         $info['original'] = $controller->genUrl("@a_media_original?" . http_build_query(array("slug" => $item->getSlug(), "format" => $item->getFormat()), $absolute));
         $info['image'] = $controller->genUrl("a_media_image?" . http_build_query(array("slug" => $item->getSlug(), "width" => "1000001", "height" => "1000002", "format" => "jpg", "resizeType" => "c")), $absolute);
         $info['image'] = str_replace(array("1000001", "1000002", ".c."), array("_WIDTH_", "_HEIGHT_", "._c-OR-s_."), $info['image']);
         $info['image'] = preg_replace("/\\.jpg\$/", "._FORMAT_", $info['image']);
         if ($info['type'] === 'video') {
             $info['serviceUrl'] = $item->getServiceUrl();
         }
         $nitems[] = $info;
     }
     $result->items = $nitems;
     $this->jsonResponse('ok', $result);
 }
 public function executeIndex(sfRequest $request)
 {
     $params = array();
     $tag = $request->getParameter('tag');
     $type = $request->getParameter('type');
     $category = $request->getParameter('category');
     if (aMediaTools::getType()) {
         $type = aMediaTools::getType();
     }
     $search = $request->getParameter('search');
     if ($request->isMethod('post')) {
         // Give the routing engine a shot at making the URL pretty.
         // We use addParams because it automatically deletes any
         // params with empty values. (To be fair, http_build_query can't
         // do that because some crappy web application might actually
         // use checkboxes with empty values, and that's not
         // technically wrong. We have the luxury of saying "reasonable
         // people who work here don't do that.")
         return $this->redirect(aUrl::addParams("aMedia/index", array("tag" => $tag, "search" => $search, "type" => $type)));
     }
     if (!empty($tag)) {
         $params['tag'] = $tag;
     }
     if (!empty($search)) {
         $params['search'] = $search;
     }
     if (!empty($type)) {
         $params['type'] = $type;
     }
     if (!empty($category)) {
         $params['category'] = $category;
     }
     $user = $this->getUser();
     if ($user->isAuthenticated() && method_exists($user, "getGuardUser")) {
         $params['user'] = $user->getGuardUser()->getUsername();
     }
     // Cheap insurance that these are integers
     $aspectWidth = floor(aMediaTools::getAttribute('aspect-width'));
     $aspectHeight = floor(aMediaTools::getAttribute('aspect-height'));
     // TODO: performance of these is not awesome (it's a linear search).
     // It would be more awesome with the right kind of indexing. For the
     // aspect ratio test to be more efficient we'd have to store the lowest
     // common denominator aspect ratio and index that.
     if ($aspectWidth && $aspectHeight) {
         $params['aspect-width'] = $aspectWidth;
         $params['aspect-height'] = $aspectHeight;
     }
     $minimumWidth = floor(aMediaTools::getAttribute('minimum-width'));
     if ($minimumWidth) {
         $params['minimum-width'] = $minimumWidth;
     }
     $minimumHeight = floor(aMediaTools::getAttribute('minimum-height'));
     if ($minimumHeight) {
         $params['minimum-height'] = $minimumHeight;
     }
     $width = floor(aMediaTools::getAttribute('width'));
     if ($width) {
         $params['width'] = $width;
     }
     $height = floor(aMediaTools::getAttribute('height'));
     if ($height) {
         $params['height'] = $height;
     }
     // The media module is now an engine module. There is always a page, and that
     // page might have a restricted set of categories associated with it
     $mediaCategories = aTools::getCurrentPage()->MediaCategories;
     if (count($mediaCategories)) {
         $params['allowed_categories'] = $mediaCategories;
     }
     $query = aMediaItemTable::getBrowseQuery($params);
     $this->pager = new sfDoctrinePager('aMediaItem', aMediaTools::getOption('per_page'));
     $this->pager->setQuery($query);
     $page = $request->getParameter('page', 1);
     $this->pager->setPage($page);
     $this->pager->init();
     $this->results = $this->pager->getResults();
     aMediaTools::setSearchParameters(array("tag" => $tag, "type" => $type, "search" => $search, "page" => $page, 'category' => $category));
     $this->pagerUrl = "aMedia/index?" . http_build_query($params);
     if (aMediaTools::isSelecting()) {
         $this->selecting = true;
         if (aMediaTools::getAttribute("label")) {
             $this->label = aMediaTools::getAttribute("label");
         }
         $this->limitSizes = false;
         if ($aspectWidth || $aspectHeight || $minimumWidth || $minimumHeight || $width || $height) {
             $this->limitSizes = true;
         }
     }
 }
 /**
  * DOCUMENT ME
  * @param sfWebRequest $request
  * @return mixed
  */
 public function executeIndex(sfWebRequest $request)
 {
     $params = array();
     $tag = $request->getParameter('tag');
     $type = aMediaTools::getType();
     $type = $type ? $type : $request->getParameter('type');
     // It is permissible to filter more narrowly if the overall type is a metatype (_downloadable)
     if (substr($type, 0, 1) === '_') {
         if ($request->getParameter('type')) {
             $type = $request->getParameter('type');
         }
     }
     $this->embedAllowed = aMediaTools::getEmbedAllowed();
     $this->uploadAllowed = aMediaTools::getUploadAllowed();
     $category = $request->getParameter('category');
     $search = $request->getParameter('search');
     if ($request->isMethod('post')) {
         // Give the routing engine a shot at making the URL pretty.
         // We use addParams because it automatically deletes any
         // params with empty values. (To be fair, http_build_query can't
         // do that because some crappy web application might actually
         // use checkboxes with empty values, and that's not
         // technically wrong. We have the luxury of saying "reasonable
         // people who work here don't do that.")
         return $this->redirect(aUrl::addParams("aMedia/index", array("tag" => $tag, "search" => $search, "type" => $type)));
     }
     if (!empty($tag)) {
         $params['tag'] = $tag;
     }
     if (!empty($search)) {
         $params['search'] = $search;
     }
     if (!empty($type)) {
         $params['type'] = $type;
     }
     if (!empty($category)) {
         $params['category'] = $category;
     }
     // Cheap insurance that these are integers
     $aspectWidth = floor(aMediaTools::getAttribute('aspect-width'));
     $aspectHeight = floor(aMediaTools::getAttribute('aspect-height'));
     if ($type === 'image') {
         // Now that we provide cropping tools, width and height should only exclude images
         // that are too small to ever be cropped to that size
         $minimumWidth = floor(aMediaTools::getAttribute('minimum-width'));
         $width = floor(aMediaTools::getAttribute('width'));
         $minimumWidth = max($minimumWidth, $width);
         $minimumHeight = floor(aMediaTools::getAttribute('minimum-height'));
         $height = floor(aMediaTools::getAttribute('height'));
         $minimumHeight = max($minimumHeight, $height);
         // Careful, aspect ratio can impose a bound on the other dimension
         if ($minimumWidth && $aspectWidth) {
             $minimumHeight = max($minimumHeight, $minimumWidth * $aspectHeight / $aspectWidth);
         }
         if ($minimumHeight && $aspectHeight) {
             $minimumWidth = max($minimumWidth, $minimumHeight * $aspectWidth / $aspectHeight);
         }
         // We've updated these with implicit constraints from the aspect ratio, the width and height params, etc.
         aMediaTools::setAttribute('minimum-width', $minimumWidth);
         aMediaTools::setAttribute('minimum-height', $minimumHeight);
         $params['minimum-width'] = $minimumWidth;
         $params['minimum-height'] = $minimumHeight;
     } else {
         // TODO: performance of these is not awesome (it's a linear search).
         // It would be more awesome with the right kind of indexing. For the
         // aspect ratio test to be more efficient we'd have to store the lowest
         // common denominator aspect ratio and index that.
         if ($aspectWidth && $aspectHeight) {
             $params['aspect-width'] = $aspectWidth;
             $params['aspect-height'] = $aspectHeight;
         }
         $minimumWidth = floor(aMediaTools::getAttribute('minimum-width'));
         if ($minimumWidth) {
             $params['minimum-width'] = $minimumWidth;
         }
         $minimumHeight = floor(aMediaTools::getAttribute('minimum-height'));
         if ($minimumHeight) {
             $params['minimum-height'] = $minimumHeight;
         }
         $width = floor(aMediaTools::getAttribute('width'));
         if ($width) {
             $params['width'] = $width;
         }
         $height = floor(aMediaTools::getAttribute('height'));
         if ($height) {
             $params['height'] = $height;
         }
     }
     // The media module is now an engine module. There is always a page, and that
     // page might have a restricted set of categories associated with it
     $mediaCategories = aTools::getCurrentPage()->Categories;
     if (count($mediaCategories)) {
         $params['allowed_categories'] = $mediaCategories;
     }
     $query = aMediaItemTable::getBrowseQuery($params);
     $this->pager = new sfDoctrinePager('aMediaItem', aMediaTools::getOption('per_page'));
     $page = $request->getParameter('page', 1);
     $this->pager->setQuery($query);
     if ($request->hasParameter('max_per_page')) {
         $this->getUser()->setAttribute('max_per_page', $request->getParameter('max_per_page'), 'apostrophe_media_prefs');
     }
     $this->max_per_page = $this->getUser()->getAttribute('max_per_page', 20, 'apostrophe_media_prefs');
     $this->pager->setMaxPerPage($this->max_per_page);
     $this->pager->setPage($page);
     $this->pager->init();
     $this->results = $this->pager->getResults();
     Taggable::preloadTags($this->results);
     // Go to the last page if we are beyond it
     if ($page > 1 && $page > $this->pager->getLastPage()) {
         $page--;
         $params['page'] = $page;
         return $this->redirect('aMedia/index?' . http_build_query($params));
     }
     aMediaTools::setSearchParameters(array("tag" => $tag, "type" => $type, "search" => $search, "page" => $page, 'category' => $category));
     $this->pagerUrl = "aMedia/index?" . http_build_query($params);
     if (aMediaTools::isSelecting()) {
         $this->selecting = true;
         if (aMediaTools::getAttribute("label")) {
             $this->label = aMediaTools::getAttribute("label");
         }
         $this->limitSizes = $minimumWidth || $minimumHeight;
     }
     if ($request->hasParameter('layout')) {
         $this->getUser()->setAttribute('layout', $request->getParameter('layout'), 'apostrophe_media_prefs');
     }
     $this->layout = aMediaTools::getLayout($this->getUser()->getAttribute('layout', 'two-up', 'apostrophe_media_prefs'));
     $this->enabled_layouts = aMediaTools::getEnabledLayouts();
     return $this->pageTemplate;
 }