Development

Changeset 4083

You must first sign up to be able to contribute.

Changeset 4083

Show
Ignore:
Timestamp:
05/23/07 01:09:39 (2 years ago)
Author:
xavier
Message:

some refactor + added removeAllTags + tests + doc

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelActAsTaggableBehaviorPlugin/README

    r4077 r4083  
    5959 
    6060=== Attaching tags to a taggable object === 
    61 Consider a Propel "Post" class
     61Consider a Propel "Post" class
    6262 
    6363{{{ 
     
    6868}}} 
    6969 
    70 When the sfPropelActAsTaggableBehaviorPlugin is applied to the Post class, that class automagically gets taggable
     70When the sfPropelActAsTaggableBehaviorPlugin is applied to the Post class, that class automagically gets taggable
    7171{{{ 
    7272<?php 
     
    7979 
    8080=== Retrieving one object's tags === 
    81 It is possible to retrieve tags from a taggable object
     81It is possible to retrieve tags from a taggable object
    8282{{{ 
    8383<?php 
     
    9191}}} 
    9292 
     93=== Removing one object's tags === 
     94Of course, tags can also be removed: 
     95{{{ 
     96<?php 
     97$post = PostPeer::retrieveByPk(1); 
     98$post->removeTag('toto'); 
     99$post->removeTag('toto, tutu'); 
     100$post->removeAllTags(); 
     101}}} 
     102 
    93103=== Tags cloud generation === 
    94 The plugin also proposes methods and helpers for generating tags cloud
     104The plugin also proposes methods and helpers for generating tags cloud
    95105{{{ 
    96106<?php 
     
    122132 
    123133=== Specialize your tag clouds === 
    124 The tag retrival mecanism is fully based on Criterias, so it is easy to pass several restrictions. For instance, for retrieving popular tags over posts created in March 2007
     134The tag retrival mecanism is fully based on Criterias, so it is easy to pass several restrictions. For instance, for retrieving popular tags over posts created in March 2007
    125135{{{ 
    126136<?php 
     
    168178 
    169179== Unit testing == 
    170 The plugin has been deeply unit-tested, if not fully. The tests are located in test/unit/sfPropelActAsTaggableBehaviorTest.php. If you want to run them
     180The plugin has been deeply unit-tested, if not fully. The tests are located in test/unit/sfPropelActAsTaggableBehaviorTest.php. If you want to run them
    171181 * install the plugin 
    172182 * configure a model for using it, for instance "Post" 
  • plugins/sfPropelActAsTaggableBehaviorPlugin/config/config.php

    r4076 r4083  
    2929  array ( 
    3030    'sfPropelActAsTaggableBehavior', 
     31    'removeAllTags' 
     32  ), 
     33  array ( 
     34    'sfPropelActAsTaggableBehavior', 
    3135    'removeTag' 
    3236  ), 
  • plugins/sfPropelActAsTaggableBehaviorPlugin/lib/model/TagPeer.php

    r4076 r4083  
    2525   * @param      Criteria    $c 
    2626   * @param      array       $options 
     27   * @return     array 
    2728   */ 
    2829  public static function getAll(Criteria $c = null, $options = array()) 
     
    5556   * @param      Criteria    $c 
    5657   * @param      array       $options 
     58   * @return     array 
    5759   */ 
    5860  public static function getAllWithCount(Criteria $c = null, $options = array()) 
     
    98100   *  
    99101   * @param      mixed       $tags 
     102   * @return     array 
    100103   */ 
    101104  public static function getModelsTaggedWith($tags = array()) 
     
    157160   * @param      Criteria    $c 
    158161   * @param      array       $options 
     162   * @return     array 
    159163   */ 
    160164  public static function getPopulars($c = null, $options = array()) 
     
    188192   * @param      mixed       $tags 
    189193   * @param      array       $options 
     194   * @return     array 
    190195   */ 
    191196  public static function getRelatedTags($tags = array(), $options = array()) 
    192197  { 
     198    $tags = sfPropelActAsTaggableToolkit::explodeTagString($tags); 
     199 
    193200    if (is_string($tags)) 
    194201    { 
    195       if (false !== strpos($tags, ',')) 
    196       { 
    197         $tags = explode(',', $tags); 
    198       } 
    199       else 
    200       { 
    201         $tags = array($tags); 
    202       } 
     202      $tags = array($tags); 
    203203    } 
    204204 
     
    251251   * @param      mixed       $tags 
    252252   * @param      array       $options 
     253   * @return     array 
    253254   */ 
    254255  public static function getTaggedWith($tags = array(), $options = array()) 
    255256  { 
    256     if (is_string($tags)) 
    257     { 
    258       if (false !== strpos($tags, ',')) 
    259       { 
    260         $tags = explode(',', $tags); 
    261       } 
    262       else 
    263       { 
    264         $tags = array($tags); 
    265       } 
    266     } 
    267  
    268257    $taggings = self::getTaggings($tags, $options); 
    269258    $result = array(); 
     
    292281   * @param      mixed       $tags 
    293282   * @param      array       $options 
     283   * @return     array 
    294284   */ 
    295285  private static function getTaggings($tags = array(), $options = array()) 
    296286  { 
     287    $tags = sfPropelActAsTaggableToolkit::explodeTagString($tags); 
     288 
    297289    if (is_string($tags)) 
    298290    { 
    299       if (false !== strpos($tags, ',')) 
    300       { 
    301         $tags = explode(',', $tags); 
    302       } 
    303       else 
    304       { 
    305         $tags = array($tags); 
    306       } 
     291      $tags = array($tags); 
    307292    } 
    308293 
     
    362347   *  
    363348   * @param      String      $tagname 
     349   * @return     Tag 
    364350   */ 
    365351  public static function retrieveByTagname($tagname) 
     
    375361   *  
    376362   * @param      String      $tagname 
     363   * @return     Tag 
    377364   */ 
    378365  public static function retrieveOrCreateByTagname($tagname) 
  • plugins/sfPropelActAsTaggableBehaviorPlugin/lib/sfPropelActAsTaggableBehavior.class.php

    r4076 r4083  
    6060  private static function add_tag(BaseObject $object, $tag) 
    6161  { 
    62     $tag = trim(rtrim(str_replace(',', ' ', $tag))); 
     62    $tag = sfPropelActAsTaggableToolkit::cleanTagName($tag); 
    6363 
    6464    if (strlen($tag) > 0) 
     
    137137  public function addTag(BaseObject $object, $tagname) 
    138138  { 
    139     if (is_string($tagname) && false !== strpos($tagname, ',')) 
    140     { 
    141       $tagname = explode(',', $tagname); 
    142     } 
     139    $tagname = sfPropelActAsTaggableToolkit::explodeTagString($tagname); 
    143140 
    144141    if (is_array($tagname)) 
     
    209206  public function getTags(BaseObject $object) 
    210207  { 
    211     $this->getSavedTags($object); 
    212     $tags = array_merge(self::get_tags($object), self::get_saved_tags($object)); 
     208    $tags = array_merge(self::get_tags($object), $this->getSavedTags($object)); 
    213209    ksort($tags); 
    214210    return $tags; 
     
    231227  public function hasTag(BaseObject $object, $tag = null) 
    232228  { 
    233     if (is_string($tag) && false !== strpos($tag, ',')) 
    234     { 
    235       $tag = explode(',', $tag); 
    236     } 
     229    $tag = sfPropelActAsTaggableToolkit::explodeTagString($tag); 
    237230 
    238231    if (is_array($tag)) 
     
    251244      $tags = self::get_tags($object); 
    252245 
    253       if ($tag == null) 
    254       { 
    255         return (count($tags) > 0) || (count(self::get_saved_tags($object)) > 0);  
    256       } 
    257       else 
    258       { 
     246      if ($tag === null) 
     247      { 
     248        return (count($tags) > 0) || (count($this->getSavedTags($object)) > 0);  
     249      } 
     250      elseif (is_string($tag)) 
     251      { 
     252        $tag = sfPropelActAsTaggableToolkit::cleanTagName($tag); 
     253 
    259254        if (isset($tags[$tag])) 
    260255        { 
     
    263258        else 
    264259        { 
    265           $saved_tags = self::get_saved_tags($object); 
     260          $saved_tags = $this->getSavedTags($object); 
    266261          $removed_tags = self::get_removed_tags($object); 
    267262          return isset($saved_tags[$tag]) && !isset($removed_tags[$tag]); 
    268263        } 
     264      } 
     265      else 
     266      { 
     267        $msg = sprintf('hasTag() does not support this type of argument : %s.', get_class($tag)); 
     268        throw new Exception($msg); 
    269269      } 
    270270    } 
     
    374374    } 
    375375 
    376     $tags = array_merge(self::get_tags($object), self::get_saved_tags($object)); 
     376    $tags = array_merge(self::get_tags($object), $this->getSavedTags($object)); 
    377377    self::set_saved_tags($object, $tags); 
    378378    self::clear_tags($object); 
     
    381381 
    382382  /** 
     383   * Removes all the tags associated to the object. 
     384   *  
     385   * @param      BaseObject  $object 
     386   */ 
     387  public function removeAllTags(BaseObject $object) 
     388  { 
     389    $saved_tags = self::getSavedTags($object); 
     390 
     391    self::set_saved_tags($object, array()); 
     392    self::set_tags($object, array()); 
     393    self::set_removed_tags($object, $saved_tags); 
     394  } 
     395 
     396  /** 
    383397   * Removes a tag or a set of tags from the object. As usual, the second  
    384398   * parameter might be an array of tags or a comma-separated string. 
     
    389403  public function removeTag(BaseObject $object, $tagname) 
    390404  { 
    391     if (is_string($tagname) && false !== strpos($tagname, ',')) 
    392     { 
    393       $tagname = explode(',', $tagname); 
    394     } 
     405    $tagname = sfPropelActAsTaggableToolkit::explodeTagString($tagname); 
    395406 
    396407    if (is_array($tagname)) 
     
    403414    else 
    404415    { 
     416      $tagname = sfPropelActAsTaggableToolkit::cleanTagName($tagname); 
    405417      $tags = self::get_tags($object); 
    406418      $saved_tags = $this->getSavedTags($object); 
  • plugins/sfPropelActAsTaggableBehaviorPlugin/lib/sfPropelActAsTaggableToolkit.class.php

    r4076 r4083  
    1111class sfPropelActAsTaggableToolkit 
    1212{ 
     13  /** 
     14   * "Cleans" a string in order it to be used as a tag. Intended for strings  
     15   * representing a single tag 
     16   *  
     17   * @param      String    $tag 
     18   * @return     bool 
     19   */ 
     20  public static function cleanTagName($tag) 
     21  { 
     22    return trim(rtrim(str_replace(',', ' ', $tag))); 
     23  } 
     24 
     25  /** 
     26   * "Cleans" a string in order it to be used as a tag 
     27   * Intended for strings representing a single tag 
     28   *  
     29   * @param      mixed     $tag 
     30   * @return     mixed 
     31   */ 
     32  public static function explodeTagString($tag) 
     33  { 
     34    if (is_string($tag) && false !== strpos($tag, ',')) 
     35    { 
     36      $tag = explode(',', $tag); 
     37      array_walk($tag, 'trim'); 
     38      array_walk($tag, 'rtrim'); 
     39    } 
     40 
     41    return $tag; 
     42  } 
     43 
     44  /** 
     45   * Formats a tag string/array in a pretty string. For instance, will convert  
     46   * tag3,tag1,tag2 into the following string : "tag1", "tag2" and "tag3" 
     47   *  
     48   * @param      array    $tags 
     49   * @return     String 
     50   */ 
    1351  public static function formatTagString($tags) 
    1452  { 
     
    2260 
    2361    $nb_tags = count($tags); 
    24      
     62 
    2563    if ($nb_tags > 0) 
    2664    { 
  • plugins/sfPropelActAsTaggableBehaviorPlugin/test/unit/sfPropelActAsTaggableBehaviorTest.php

    r4076 r4083  
    2222 
    2323// start tests 
    24 $t = new lime_test(24, new lime_output_color()); 
     24$t = new lime_test(25, new lime_output_color()); 
    2525 
    2626 
     
    7575$t->ok($object2_copy->hasTag('clever') && !$object2->hasTag('clever'), 'tags are applied to the object instances independently'); 
    7676 
     77$object = _create_object(); 
     78$object->addTag('tutu'); 
     79$object->addTag('titi'); 
     80$object->save(); 
     81$object->addTag('tata'); 
     82$object->removeAllTags(); 
     83$t->ok(!$object->hasTag(), 'tags can all be removed at once'); 
     84 
     85 
    7786unset($object, $object2, $object2_copy); 
    7887 
     
    8897$object_tags = $object->getTags(); 
    8998$t->ok((count($object_tags) == 3) && $object->hasTag('tutu') && $object->hasTag('titi'), 'tags can be added with a comma-separated string'); 
    90 $t->ok($object->hasTag('titi,tutu'), 'comma-separated strings are divided into several tags'); 
     99$t->ok($object->hasTag('titi, tutu'), 'comma-separated strings are divided into several tags'); 
    91100 
    92101$object = _create_object();