![]() |
|
sfPropelActAsTaggableBehaviorPlugin - 0.2.0Propel taggable behavior |
|
![]() |
66
users
Sign-in
to change your status |
This behavior permits to attach tags to Propel objects. It includes tag-clouds generation and helpers to display these clouds. |
| Name | Status | |
|---|---|---|
|
|
lead | gro.tocal <<ta>> reivax |
Copyright (c) 2007 Xavier Lacot
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
| Version | License | API | Released |
|---|---|---|---|
| 0.9.0beta | MIT license | 0.9.0beta | 22/12/2008 |
| Version | License | API | Released |
|---|---|---|---|
| 0.9.0beta | MIT license | 0.9.0beta | 22/12/2008 |
| 0.8.1beta | MIT license | 0.8.1beta | 22/09/2008 |
| 0.7.0beta | MIT license | 0.7.0beta | 16/07/2008 |
| 0.6.0beta | MIT license | 0.6.0beta | 10/05/2008 |
| 0.5.0beta | MIT license | 0.5.0beta | 21/03/2008 |
| Version | License | API | Released |
|---|---|---|---|
| 0.9.0beta | MIT license | 0.9.0beta | 22/12/2008 |
| 0.8.1beta | MIT license | 0.8.1beta | 22/09/2008 |
| 0.7.0beta | MIT license | 0.7.0beta | 16/07/2008 |
| 0.6.0beta | MIT license | 0.6.0beta | 10/05/2008 |
| 0.5.0beta | MIT license | 0.5.0beta | 21/03/2008 |
| 0.4.0beta | MIT license | 0.4.0beta | 11/12/2007 |
| 0.3.0beta | MIT license | 0.3.0beta | 02/07/2007 |
| 0.2.0beta | MIT license | 0.2.0beta | 27/06/2007 |
| 0.1.0beta | MIT license | 0.1.0beta | 21/05/2007 |
tag_list() helperremoveAllTags() or setTags() twice before saving an object, the tags were not removed. A big thank to Tom BoutellTagPeer::getPopulars() is now defined with app_sfPropelActAsTaggableBehaviorPlugin_limit (previously with app_tags_limit)Initial public release * add/remove tag(s) on an object * multi-tags object search * multi-models selection * tag cloud generation * related tags handling * unit-tested
This behavior permits to attach tags to Propel objects. It includes tag-clouds generation and helpers to display these clouds.
go to your project's root
Install the plugin:
./symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsTaggableBehaviorPlugin
if not already done, enabled behaviors in config/propel.ini:
propel.builder.addBehaviors = true
edit the classes that you want to make taggable. For instance, for lib/model/Post.php:
[php]
addTag('toto');
$post->addTag('tata, tutu');
$post->addTag(array('Titi', 'Gros Minet'));
$post->save();
### Retrieving one object's tags
It is possible to retrieve tags from a taggable object:
[php]
getTags();
foreach ($tags as $tag)
{
echo $tag.'
';
}
Of course, tags can also be removed:
<?php $post = PostPeer::retrieveByPk(1); $post->removeTag('toto'); $post->removeTag('toto, tutu'); $post->removeAllTags();
The plugin also proposes methods and helpers for generating tags cloud:
<?php // gets the popular tags $tags = TagPeer::getPopulars(); // display the tags cloud. The tags will use the route name "@tag" which tags // the request parameter "tags" echo tag_cloud($tags, '@tag?tags');
The default size of the tag cloud is 100 items, but this value might be tweaked in app.yml:
all:
tags:
limit: 50
When you click on a tag in a tag cloud, you will want to get a list of objects that have been tagged with that tag. But sometimes, it happens that this tag is so popular that you can not find the resource you were searching for. Related-tags clouds are helpful for refining your request, as they provide a way to add an other tag to the request:
<?php // get the tags related to "toto" and "tutu", for the model "Post" only $tags = TagPeer::getRelatedTags('toto,tutu', array('model' => 'Post')); // displays the related tags cloud, using the route "@post_tags" with the // request parameter "tags" echo related_tag_cloud($tags, '@post_tags?tags=', 'toto,tutu');
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:
<?php $c = new Criteria(); $c->addJoin(PostPeer::ID, TaggingPeer::TAGGABLE_ID); $c->add(PostPeer::CREATED_AT, '2007-03%', Criteria::LIKE); $tags = TagPeer::getPopulars($c, array('model' => 'Post')); echo tag_cloud($tags, '@tag?tags=');
In case you want to display a long list of taggable objects with their associated tags, you might want first to preload these objects's tags: it avoids to load tags per object, and gets all tags in a few requests.
<?php $posts = TagPeer::getTaggedWith('toto,tutu', array('model' => 'Post')); sfPropelActAsTaggableBehavior::preloadTags($posts); foreach ($posts as $post) { echo $post-getTitle(); // won't require one request at each loop, as tags have been preloaded. var_dump($post-getTags()); }
The plugin associates a parameterHolder to Propel objects, with 3 disjoin namespaces: * tags: tags that have been attached to the object, but not yet saved. Contract: tags are disjoin of (saved_tags union removed_tags) * saved_tags: tags that are presently saved in the database. Contract: removed_tags are disjoin of (tags union saved_tags) * removed_tags: tags that are presently saved in the database, but which will be removed at the next save(). Contract: removed_tags are disjoin of (tags union saved_tags)
When required, the saved_tags namespace is filled with the tags previously present in the database. The tagging methods have an action on these three namespaces, which are serialized in the database after the Propel object gets saved.
The behavior implement the following methods: * addTag($tagname) - Adds one or several tags to an object * getTags() - Returns the list of the tags attached to the object * hasTag($tag = null) - Returns true if the object has a tag. If a tag ar an array of tags is passed in second parameter, checks if these tags are attached to the object * removeTag($tagname) - Removes a tag or a set of tags from the object. * replaceTag($tagname, $replacement = null) - Replaces a tag with an other one.
The behavior class also implement the following method, which is a facility for preloading all the tags for a set of taggable objects * preloadTags($objects) - Preload tags for a set of objects
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: * install the plugin * configure a model for using it, for instance "Post" * copy the test file to /path/to/your/project/test/unit/sfPropelActAsTaggableBehaviorTest.php * edit this file and modify line 3:
define('TEST_CLASS', 'Post');
run the tests:
./symfony test-unit sfPropelActAsTaggableBehavior
This plugin has been developed by Xavier Lacot and is licensed under the MIT license. Thanks to Tristan Rivoallan for the improvement suggestions.
