$object_2_1 = _create_object_2(); $object_2_1->addTag('tag1,tag3,tag7'); $object_2_1->save(); $object_2_2 = _create_object_2(); $object_2_2->addTag('tag2,tag7'); $object_2_2->save(); $tagged_with_tag4 = Doctrine_Core::getTable('Tag')->getObjectTaggedWith('tag4'); $t->ok(count($tagged_with_tag4) == 2, 'getObjectTaggedWith() returns objects tagged with one specific tag.'); $tagged_with_tag7 = Doctrine_Core::getTable('Tag')->getObjectTaggedWith('tag7'); $t->ok(count($tagged_with_tag7) == 5, 'getObjectTaggedWith() can return several object types.'); $tagged_with_tag17 = Doctrine_Core::getTable('Tag')->getObjectTaggedWith(array('tag1', 'tag7')); $t->ok(count($tagged_with_tag17) == 3, 'getObjectTaggedWith() returns objects tagged with several specific tags.'); $tagged_with_tag127 = Doctrine_Core::getTable('Tag')->getObjectTaggedWith('tag1, tag2, tag7', array('nb_common_tags' => 2)); $t->ok(count($tagged_with_tag127) == 6, 'the "nb_common_tags" option of getObjectTaggedWith() returns objects tagged with a certain number of tags within a set of specific tags.'); // these tests check the preloadTags() method Taggable::preloadTags($tagged_with_tag17); $nb_tags = 0; foreach ($tagged_with_tag17 as $tmp_object) { $nb_tags += count($tmp_object->getTags()); } $t->ok($nb_tags === 10, 'preloadTags() preloads the tags of the objects.'); // these tests check the isTaggable() method $t->diag('detecting if a model is taggable or not'); $t->ok(TaggableToolkit::isTaggable(TEST_CLASS) === true, 'it is possible to tell if a model is taggable from its name.'); $object = _create_object(); $t->ok(TaggableToolkit::isTaggable($object) === true, 'it is possible to tell if a model is taggable from one of its instances.'); $t->ok(TaggableToolkit::isTaggable(TEST_NON_TAGGABLE_CLASS) === false, TEST_NON_TAGGABLE_CLASS . ' is not taggable, and that is fine.'); // clean the database Doctrine_Query::create()->delete()->from('Tag')->execute(); Doctrine_Query::create()->delete()->from('Tagging')->execute(); Doctrine_Query::create()->delete()->from(TEST_CLASS)->execute();
/** * 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; }