예제 #1
0
    public function hasTemplate($template)
    {
        return false;
    }
}
$t->diag('::isTaggable');
$t->ok(TaggableToolkit::isTaggable('ValidModel'), 'valid model name');
$t->ok(TaggableToolkit::isTaggable(new ValidModel('ValidModel')), 'valid model object');
$t->ok(!TaggableToolkit::isTaggable('InValidModel'), 'invalid model name');
$t->ok(!TaggableToolkit::isTaggable(new InValidModel('InValidModel')), 'invalid model object');
try {
    TaggableToolkit::isTaggable('MyClass');
    $t->fail('no exception for no doctrine model name');
} catch (Exception $e) {
    $t->pass('no doctrine model name');
}
class MyClass
{
}
try {
    TaggableToolkit::isTaggable(new MyClass());
    $t->fail('no exception for no doctrine model class');
} catch (Exception $e) {
    $t->pass('no doctrine model class');
}
$t->diag('::normalize');
$t->todo('test normalize');
$t->diag('::triplify');
$tags = array('peter', 'wolf');
array_walk($tags, 'TaggableToolkit::triplify', 'namespace:key');
$t->is($tags, array('namespace:key=peter', 'namespace:key=wolf'), 'simple tags');
 /**
  * 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;
 }
예제 #3
0
 /**
  * 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);
         }
     }
 }
예제 #4
0
 /**
  * Retrieves a tag by his name. If it does not exist, creates it (but does not
  * save it)
  *
  * @param      String      $tagname
  * @return     Tag
  */
 public static function findOrCreateByTagname($tagname)
 {
     // retrieve or create the tag
     $tag = Doctrine::getTable('Tag')->findOneByName($tagname);
     if (!$tag) {
         $tag = new Tag();
         $tag->name = $tagname;
         $triple = TaggableToolkit::extractTriple($tagname);
         list($tagname, $triple_namespace, $triple_key, $triple_value) = $triple;
         $tag->triple_namespace = $triple_namespace;
         $tag->triple_key = $triple_key;
         $tag->triple_value = $triple_value;
         $tag->is_triple = !is_null($triple_namespace);
     }
     return $tag;
 }
예제 #5
0
// 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();
Doctrine_Query::create()->delete()->from(TEST_CLASS_2)->execute();
// these tests check for the application of triple tags
$t->diag('applying triple tagging');
$t->ok(TaggableToolkit::extractTriple('ns:key=value') === array('ns:key=value', 'ns', 'key', 'value'), 'triple extracted successfully.');
$t->ok(TaggableToolkit::extractTriple('ns:key') === array('ns:key', null, null, null), 'ns:key is not a triple.');
$t->ok(TaggableToolkit::extractTriple('ns') === array('ns', null, null, null), 'ns is not a triple.');
$object = _create_object();
$object->addTag('tutu');
$object->save();
$object = _create_object();
$object->addTag('ns:key=value');
$object->addTag('ns:key=tutu');
$object->addTag('ns:key=titi');
$object->addTag('ns:key=toto');
$object->save();
$object_tags = $object->getTags();
$t->ok($object->hasTag('ns:key=value'), 'object has triple tag');
$tag = Doctrine_Core::getTable('Tag')->findOrCreateByTagname('ns:key=value');
$t->ok($tag->getIsTriple(), 'a triple tag created from a string is identified as a triple.');
$tag = Doctrine_Core::getTable('Tag')->findOrCreateByTagname('tutu');
$t->ok(!$tag->getIsTriple(), 'a non tripled tag created from a string is not identified as a triple.');