/** * 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; }