/**
  * Returns the taggings associated to one tag or a set of tags.
  *
  * The second optionnal parameter permits to restrict the results with
  * different criterias
  *
  * @param      mixed       $tags      Array of tag strings or string
  * @param      array       $options   Array of options parameters
  * @return     array
  */
 protected static function getTaggings($tags = array(), $options = array())
 {
     $tags = TaggableToolkit::explodeTagString($tags);
     if (is_string($tags)) {
         $tags = array($tags);
     }
     $q = Doctrine_Query::create()->select('DISTINCT t.id')->from('Tag t INDEXBY t.id');
     if (count($tags) > 0) {
         $q->whereIn('t.name', $tags);
     }
     if (isset($options['triple'])) {
         $q->addWhere('t.is_triple = ?', $options['triple']);
     }
     if (isset($options['namespace'])) {
         $q->addWhere('t.triple_namespace = ?', $options['namespace']);
     }
     if (isset($options['key'])) {
         $q->addWhere('t.triple_key = ?', $options['key']);
     }
     if (isset($options['value'])) {
         $q->addWhere('t.triple_value = ?', $options['value']);
     }
     if (!isset($options['nb_common_tags']) || $options['nb_common_tags'] > count($tags)) {
         $options['nb_common_tags'] = count($tags);
     }
     $tag_ids = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
     if (0 == count($tag_ids)) {
         // if not tag has been found, then there will be no tagging
         return array();
     }
     $q = Doctrine_Query::create()->select('tg.taggable_id')->from('Tagging tg')->whereIn('tg.tag_id', array_keys($tag_ids))->groupBy('tg.taggable_id')->having('count(tg.taggable_model) >= ?', $options['nb_common_tags']);
     // Taggable model class option
     if (isset($options['model'])) {
         if (!class_exists($options['model'])) {
             throw new sfDoctrineException(sprintf('The class "%s" does not exist, or it is not a model class.', $options['model']));
         }
         $q->addWhere('tg.taggable_model = ?', $options['model']);
     } else {
         $q->addSelect('tg.taggable_model')->addGroupBy('tg.taggable_model');
     }
     $results = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
     $taggings = array();
     foreach ($results as $rs) {
         if (isset($options['model'])) {
             $model = $options['model'];
         } else {
             $model = $rs['taggable_model'];
         }
         if (!isset($taggings[$model])) {
             $taggings[$model] = array();
         }
         $taggings[$model][] = $rs['taggable_id'];
     }
     return $taggings;
 }
Example #2
0
$t->todo('test optional option parameter');
$t->diag('::explodeTagString');
$tag = TaggableToolkit::explodeTagString('');
$t->is($tag, '', 'empty string');
$tag = TaggableToolkit::explodeTagString('test1');
$t->is($tag, 'test1', 'single tag');
$tag = TaggableToolkit::explodeTagString('test1 test2');
$t->is($tag, 'test1 test2', 'single tag with whitespace');
$tag = TaggableToolkit::explodeTagString('test1,test2');
$t->is($tag, array('test1', 'test2'), 'double tag');
$tag = TaggableToolkit::explodeTagString(' test1  , test2');
$t->is($tag, array('test1', 'test2'), 'double dirty tag');
$tag = TaggableToolkit::explodeTagString(' test1  ,
    test2');
$t->is($tag, array('test1', 'test2'), 'double extra dirty tag');
$tag = TaggableToolkit::explodeTagString(', test1  ,
    ');
$t->is($tag, array('test1'), 'single extra dirty tag');
$t->diag('::extractTriple');
$tag = TaggableToolkit::extractTriple('test1');
$t->is($tag, array('test1', null, null, null), 'no triple');
$tag = TaggableToolkit::extractTriple('namespace:key=value');
$t->is($tag, array('namespace:key=value', 'namespace', 'key', 'value'), 'correct triple');
$tag = TaggableToolkit::extractTriple('namespace:=value');
$t->is($tag, array('namespace:=value', null, null, null), 'empty key');
$tag = TaggableToolkit::extractTriple(':=value');
$t->is($tag, array(':=value', null, null, null), 'empty namespace, key');
$tag = TaggableToolkit::extractTriple(':=');
$t->is($tag, array(':=', null, null, null), 'empty namespace, key, value');
$tag = TaggableToolkit::extractTriple('1_incorrect_namespace:key=value');
$t->is($tag, array('1_incorrect_namespace:key=value', null, null, null), 'incorrect namespace');
$tag = TaggableToolkit::extractTriple('namespace:1_incorrect_key=value');
 /**
  * Removes a tag or a set of tags from the object. The
  * parameter might be an array of tags or a comma-separated string.
  *
  * @param      mixed       $tagname
  */
 public function removeTag($tagname)
 {
     $tagname = TaggableToolkit::explodeTagString($tagname);
     if (is_array($tagname)) {
         foreach ($tagname as $tag) {
             $this->removeTag($tag);
         }
     } else {
         $tagname = TaggableToolkit::cleanTagName($tagname);
         $tags = $this->get_tags($this->getInvoker());
         $saved_tags = $this->getSavedTags();
         if (isset($tags[$tagname])) {
             unset($tags[$tagname]);
             $this->set_tags($this->getInvoker(), $tags);
         }
         if (isset($saved_tags[$tagname])) {
             unset($saved_tags[$tagname]);
             $this->set_saved_tags($this->getInvoker(), $saved_tags);
             $this->add_removed_tag($this->getInvoker(), $tagname);
         }
     }
 }