Changeset 4083
- Timestamp:
- 05/23/07 01:09:39 (2 years ago)
- Files:
-
- plugins/sfPropelActAsTaggableBehaviorPlugin/README (modified) (6 diffs)
- plugins/sfPropelActAsTaggableBehaviorPlugin/config/config.php (modified) (1 diff)
- plugins/sfPropelActAsTaggableBehaviorPlugin/lib/model/TagPeer.php (modified) (9 diffs)
- plugins/sfPropelActAsTaggableBehaviorPlugin/lib/sfPropelActAsTaggableBehavior.class.php (modified) (10 diffs)
- plugins/sfPropelActAsTaggableBehaviorPlugin/lib/sfPropelActAsTaggableToolkit.class.php (modified) (2 diffs)
- plugins/sfPropelActAsTaggableBehaviorPlugin/test/unit/sfPropelActAsTaggableBehaviorTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelActAsTaggableBehaviorPlugin/README
r4077 r4083 59 59 60 60 === Attaching tags to a taggable object === 61 Consider a Propel "Post" class :61 Consider a Propel "Post" class: 62 62 63 63 {{{ … … 68 68 }}} 69 69 70 When the sfPropelActAsTaggableBehaviorPlugin is applied to the Post class, that class automagically gets taggable :70 When the sfPropelActAsTaggableBehaviorPlugin is applied to the Post class, that class automagically gets taggable: 71 71 {{{ 72 72 <?php … … 79 79 80 80 === Retrieving one object's tags === 81 It is possible to retrieve tags from a taggable object :81 It is possible to retrieve tags from a taggable object: 82 82 {{{ 83 83 <?php … … 91 91 }}} 92 92 93 === Removing one object's tags === 94 Of 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 93 103 === Tags cloud generation === 94 The plugin also proposes methods and helpers for generating tags cloud :104 The plugin also proposes methods and helpers for generating tags cloud: 95 105 {{{ 96 106 <?php … … 122 132 123 133 === 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 :134 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: 125 135 {{{ 126 136 <?php … … 168 178 169 179 == 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 :180 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: 171 181 * install the plugin 172 182 * configure a model for using it, for instance "Post" plugins/sfPropelActAsTaggableBehaviorPlugin/config/config.php
r4076 r4083 29 29 array ( 30 30 'sfPropelActAsTaggableBehavior', 31 'removeAllTags' 32 ), 33 array ( 34 'sfPropelActAsTaggableBehavior', 31 35 'removeTag' 32 36 ), plugins/sfPropelActAsTaggableBehaviorPlugin/lib/model/TagPeer.php
r4076 r4083 25 25 * @param Criteria $c 26 26 * @param array $options 27 * @return array 27 28 */ 28 29 public static function getAll(Criteria $c = null, $options = array()) … … 55 56 * @param Criteria $c 56 57 * @param array $options 58 * @return array 57 59 */ 58 60 public static function getAllWithCount(Criteria $c = null, $options = array()) … … 98 100 * 99 101 * @param mixed $tags 102 * @return array 100 103 */ 101 104 public static function getModelsTaggedWith($tags = array()) … … 157 160 * @param Criteria $c 158 161 * @param array $options 162 * @return array 159 163 */ 160 164 public static function getPopulars($c = null, $options = array()) … … 188 192 * @param mixed $tags 189 193 * @param array $options 194 * @return array 190 195 */ 191 196 public static function getRelatedTags($tags = array(), $options = array()) 192 197 { 198 $tags = sfPropelActAsTaggableToolkit::explodeTagString($tags); 199 193 200 if (is_string($tags)) 194 201 { 195 if (false !== strpos($tags, ',')) 196 { 197 $tags = explode(',', $tags); 198 } 199 else 200 { 201 $tags = array($tags); 202 } 202 $tags = array($tags); 203 203 } 204 204 … … 251 251 * @param mixed $tags 252 252 * @param array $options 253 * @return array 253 254 */ 254 255 public static function getTaggedWith($tags = array(), $options = array()) 255 256 { 256 if (is_string($tags))257 {258 if (false !== strpos($tags, ','))259 {260 $tags = explode(',', $tags);261 }262 else263 {264 $tags = array($tags);265 }266 }267 268 257 $taggings = self::getTaggings($tags, $options); 269 258 $result = array(); … … 292 281 * @param mixed $tags 293 282 * @param array $options 283 * @return array 294 284 */ 295 285 private static function getTaggings($tags = array(), $options = array()) 296 286 { 287 $tags = sfPropelActAsTaggableToolkit::explodeTagString($tags); 288 297 289 if (is_string($tags)) 298 290 { 299 if (false !== strpos($tags, ',')) 300 { 301 $tags = explode(',', $tags); 302 } 303 else 304 { 305 $tags = array($tags); 306 } 291 $tags = array($tags); 307 292 } 308 293 … … 362 347 * 363 348 * @param String $tagname 349 * @return Tag 364 350 */ 365 351 public static function retrieveByTagname($tagname) … … 375 361 * 376 362 * @param String $tagname 363 * @return Tag 377 364 */ 378 365 public static function retrieveOrCreateByTagname($tagname) plugins/sfPropelActAsTaggableBehaviorPlugin/lib/sfPropelActAsTaggableBehavior.class.php
r4076 r4083 60 60 private static function add_tag(BaseObject $object, $tag) 61 61 { 62 $tag = trim(rtrim(str_replace(',', ' ', $tag)));62 $tag = sfPropelActAsTaggableToolkit::cleanTagName($tag); 63 63 64 64 if (strlen($tag) > 0) … … 137 137 public function addTag(BaseObject $object, $tagname) 138 138 { 139 if (is_string($tagname) && false !== strpos($tagname, ',')) 140 { 141 $tagname = explode(',', $tagname); 142 } 139 $tagname = sfPropelActAsTaggableToolkit::explodeTagString($tagname); 143 140 144 141 if (is_array($tagname)) … … 209 206 public function getTags(BaseObject $object) 210 207 { 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)); 213 209 ksort($tags); 214 210 return $tags; … … 231 227 public function hasTag(BaseObject $object, $tag = null) 232 228 { 233 if (is_string($tag) && false !== strpos($tag, ',')) 234 { 235 $tag = explode(',', $tag); 236 } 229 $tag = sfPropelActAsTaggableToolkit::explodeTagString($tag); 237 230 238 231 if (is_array($tag)) … … 251 244 $tags = self::get_tags($object); 252 245 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 259 254 if (isset($tags[$tag])) 260 255 { … … 263 258 else 264 259 { 265 $saved_tags = self::get_saved_tags($object);260 $saved_tags = $this->getSavedTags($object); 266 261 $removed_tags = self::get_removed_tags($object); 267 262 return isset($saved_tags[$tag]) && !isset($removed_tags[$tag]); 268 263 } 264 } 265 else 266 { 267 $msg = sprintf('hasTag() does not support this type of argument : %s.', get_class($tag)); 268 throw new Exception($msg); 269 269 } 270 270 } … … 374 374 } 375 375 376 $tags = array_merge(self::get_tags($object), self::get_saved_tags($object));376 $tags = array_merge(self::get_tags($object), $this->getSavedTags($object)); 377 377 self::set_saved_tags($object, $tags); 378 378 self::clear_tags($object); … … 381 381 382 382 /** 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 /** 383 397 * Removes a tag or a set of tags from the object. As usual, the second 384 398 * parameter might be an array of tags or a comma-separated string. … … 389 403 public function removeTag(BaseObject $object, $tagname) 390 404 { 391 if (is_string($tagname) && false !== strpos($tagname, ',')) 392 { 393 $tagname = explode(',', $tagname); 394 } 405 $tagname = sfPropelActAsTaggableToolkit::explodeTagString($tagname); 395 406 396 407 if (is_array($tagname)) … … 403 414 else 404 415 { 416 $tagname = sfPropelActAsTaggableToolkit::cleanTagName($tagname); 405 417 $tags = self::get_tags($object); 406 418 $saved_tags = $this->getSavedTags($object); plugins/sfPropelActAsTaggableBehaviorPlugin/lib/sfPropelActAsTaggableToolkit.class.php
r4076 r4083 11 11 class sfPropelActAsTaggableToolkit 12 12 { 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 */ 13 51 public static function formatTagString($tags) 14 52 { … … 22 60 23 61 $nb_tags = count($tags); 24 62 25 63 if ($nb_tags > 0) 26 64 { plugins/sfPropelActAsTaggableBehaviorPlugin/test/unit/sfPropelActAsTaggableBehaviorTest.php
r4076 r4083 22 22 23 23 // start tests 24 $t = new lime_test(2 4, new lime_output_color());24 $t = new lime_test(25, new lime_output_color()); 25 25 26 26 … … 75 75 $t->ok($object2_copy->hasTag('clever') && !$object2->hasTag('clever'), 'tags are applied to the object instances independently'); 76 76 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 77 86 unset($object, $object2, $object2_copy); 78 87 … … 88 97 $object_tags = $object->getTags(); 89 98 $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'); 91 100 92 101 $object = _create_object();