/** * Fetch images from pool according to any default tag(s) option specified * in the global schema.yml. * Override added to specify per tag in MooEditable * * @return sfDoctrinePager */ public function getPager($per_page = 12, $page = 1, $tagged_object = null, $tag = null) { $pager = new sfDoctrinePager($this->getClassnameToReturn(), $per_page); $im = new sfImagePoolImage(); if (!$im->option('tagging')) { $tagged_object = $tag = null; } $im->free(true); if (isset($tagged_object) && ($tag = $tagged_object->getTagRestriction())) { $query = TagTable::getObjectTaggedWithQuery($this->getClassnameToReturn(), $tag, $pager->getQuery(), array('nb_common_tags' => 1)); $pager->setQuery($query); } else { if (!empty($tag)) { // If only tags we have no taggable object // So get images tagged with this query and do a whereIn on the ids $tags = explode(',', $tag); $images = TagTable::getObjectTaggedWith($tags, array('model' => 'sfImagePoolImage', 'nb_common_tags' => 1)); $image_ids = array(); foreach ($images as $image) { $image_ids[] = $image->id; } if (!empty($image_ids)) { $pager->getQuery()->whereIn('sfImagePoolImage.id', $image_ids); } else { $pager->getQuery()->where('false'); } // we have no images tagged } } $pager->getQuery()->orderBy('updated_at DESC'); $pager->setPage($page); $pager->init(); return $pager; }
/** * 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; }
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.*'); aDoctrine::orderByList($query, $params['ids']); $query->andWhereIn("aMediaItem.id", $params['ids']); } if (isset($params['tag'])) { $query = TagTable::getObjectTaggedWithQuery('aMediaItem', $params['tag'], $query); } if (isset($params['type'])) { $query->andWhere("aMediaItem.type = ?", array($params['type'])); } if (isset($params['allowed_categories'])) { $query->innerJoin('aMediaItem.MediaCategories mc1 WITH mc1.id IN (' . implode(',', aArray::getIds($params['allowed_categories'])) . ')'); } if (isset($params['category'])) { $query->innerJoin('aMediaItem.MediaCategories 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'); } // This will be more interesting later if (!isset($params['user'])) { $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)); } return $query; }