/**
  * Retrieves a tag by his name. If it does not exist, creates it (but does not
  * save it)
  *
  * @param      String      $tagname
  * @return     Tag
  */
 public function findOrCreateByTagname($tagname)
 {
     // retrieve or create the tag
     $tag = Doctrine_Core::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;
 }
Example #2
0
    ');
$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');
$t->is($tag, array('namespace:1_incorrect_key=value', null, null, null), 'incorrect key');
$t->diag('::formatTagString');
$tag = TaggableToolkit::formatTagString('dog');
$t->is($tag, '"dog"', 'single tag string');
$tag = TaggableToolkit::formatTagString('dog,cat,bird');
$t->is($tag, '"bird", "cat" and "dog"', 'multi tag string');
$tag = TaggableToolkit::formatTagString('dog, cat, bird,');
$t->is($tag, '"bird", "cat" and "dog"', 'not cleaned multi tag string');
$tag = TaggableToolkit::formatTagString(array('dog', 'cat', 'bird'));
$t->is($tag, '"bird", "cat" and "dog"', 'simple tag array');
class Doctrine
{
    public static function isValidModelClass($model)
    {
        if (is_object($model)) {
 /**
  * Adds a tag to the object. The "tagname" param can be a string or an array
  * of strings. These 3 code sequences produce an equivalent result :
  *
  * 1- $object->addTag('tag1,tag2,tag3');
  * 2- $object->addTag('tag1');
  *    $object->addTag('tag2');
  *    $object->addTag('tag3');
  * 3- $object->addTag(array('tag1','tag2','tag3'));
  *
  * @param      mixed       $tagname
  */
 public function addTag($tagname, $options = array())
 {
     $tagname = TaggableToolkit::explodeTagString($tagname);
     if (is_array($tagname)) {
         foreach ($tagname as $tag) {
             $this->add_tag($this->getInvoker(), $tag, $options);
         }
     } else {
         $removed_tags = $this->get_removed_tags($this->getInvoker());
         if (isset($removed_tags[$tagname])) {
             unset($removed_tags[$tagname]);
             $this->set_removed_tags($this->getInvoker(), $removed_tags);
             $this->add_saved_tag($this->getInvoker(), $tagname);
         } else {
             $saved_tags = $this->getSavedTags();
             if (sfConfig::get('app_sfDoctrineActAsTaggablePlugin_triple_distinct', false)) {
                 // the binome namespace:key must be unique
                 $triple = TaggableToolkit::extractTriple($tagname);
                 if (!is_null($triple[1]) && !is_null($triple[2])) {
                     $tags = $this->getTags(array('triple' => true, 'return' => 'tag'));
                     $pattern = '/^' . $triple[1] . ':' . $triple[2] . '=(.*)$/';
                     $removed = array();
                     foreach ($tags as $tag) {
                         if (preg_match($pattern, $tag)) {
                             $removed[] = $tag;
                         }
                     }
                     $this->removeTag($removed);
                 }
             }
             if (!isset($saved_tags[$tagname])) {
                 $this->add_tag($this->getInvoker(), $tagname, $options);
             }
         }
     }
 }
// 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.');