public function clearImageCache($deleteOriginals = false) { if (!$this->getId()) { return; } $cached = glob(aMediaItemTable::getDirectory() . DIRECTORY_SEPARATOR . $this->getSlug() . ".*"); foreach ($cached as $file) { if (!$deleteOriginals) { if (strpos($file, ".original.") !== false) { continue; } } unlink($file); } }
public function executeBrowser($request) { // We don't use a single integrated form for this anymore. What we wanted was // individual-link behavior, and yet we overlaid a form on that, // which had some interesting aspects but was ultimately confusing // and a problem for search engine indexing etc // ... But we do now use a simple form for the search form $this->current = "aMedia/index"; $params = array(); $type = aMediaTools::getSearchParameter('type'); if (strlen($type)) { $this->type = $type; $params['type'] = $type; } $tag = aMediaTools::getSearchParameter('tag'); if (strlen($tag)) { $this->selectedTag = $tag; $params['tag'] = $tag; } $categorySlug = aMediaTools::getSearchParameter('category'); if (strlen($categorySlug)) { $this->selectedCategory = Doctrine::getTable('aMediaCategory')->findOneBySlug($categorySlug); $params['category'] = $categorySlug; } $search = aMediaTools::getSearchParameter('search'); if (strlen($search)) { $this->search = $search; $params['search'] = $search; } $this->searchForm = new aMediaSearchForm(); $this->searchForm->bind(array('search' => $request->getParameter('search'))); $this->current .= "?" . http_build_query($params); $this->allTags = aMediaItemTable::getAllTagNameForUserWithCount(); $tagsByPopularity = $this->allTags; arsort($tagsByPopularity); $this->popularTags = array(); $n = 0; $max = aMediaTools::getOption('popular_tags'); foreach ($tagsByPopularity as $tag => $count) { if ($n == $max) { break; } $this->popularTags[$tag] = $count; $n++; } }
/** * 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); }
/** * These are used internally. See aMediaSelect for the methods you probably want * @param mixed $after * @param mixed $multiple * @param mixed $selection * @param mixed $options */ public static function setSelecting($after, $multiple, $selection, $options = array()) { $items = aMediaItemTable::retrieveByIds($selection); $ids = array(); $imageInfo = array(); $selection = array(); foreach ($items as $item) { $croppingInfo = array(); if ($item->isCrop()) { $croppingInfo = $item->getCroppingInfo(); $item = $item->getCropOriginal(); } $id = $item->id; $selection[] = $id; $info = array('width' => $item->width, 'height' => $item->height); $info = array_merge($info, $croppingInfo); $imageInfo[$item->id] = $info; } $cropping = isset($options['cropping']) && $options['cropping']; aMediaTools::clearSelecting(); aMediaTools::setAttribute("selecting", true); aMediaTools::setAttribute("after", $after); aMediaTools::setAttribute("multiple", $multiple); aMediaTools::setAttribute("cropping", $cropping); aMediaTools::setAttribute("selection", $selection); aMediaTools::setAttribute("imageInfo", $imageInfo); foreach ($options as $key => $val) { aMediaTools::setAttribute($key, $val); } $type = aMediaTools::getType(); if (substr($type, 0, 1) === '_') { // We need to let people filter more narrowly, but also // be able to remember what the metatype was originally aMediaTools::setAttribute('metatype', $type); } }
protected function getDirectory() { return aMediaItemTable::getDirectory(); }
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; }