sfPropelActAsCommentableBehaviorPlugin
Introduction
This behavior permits to attach comments to Propel objects. It provides a module for enabling comments in your application in less than 3 minutes.
Features
- add/remove comment(s) on an object
- unit-tested
- comment module, with ajax support and layout customization
- comment namespaces (separate comments for the front-office and the back-office, for instance)
- comment admin-module
Screenshots
Philosophy of the stuff
- commentable objects must have a primary key
- comments can only be attached on objects that have already been saved
- comments are saved when applied
Get it installed
- go to your project's root
- Install the plugin:
./symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsCommentableBehaviorPlugin
- if not already done, enabled behaviors in config/propel.ini:
propel.builder.addBehaviors = true
- edit the classes that you want to make commentable. For instance, for lib/model/Post.php:
<?php class Post extends BasePost { } sfPropelBehavior::add('Post', array('sfPropelActAsCommentableBehavior'));
- rebuild the model:
./symfony propel-build-all
- clear the cache:
./symfony cc
If you want to take profit of the included comment module, you should also complete the following steps:
- activate the "sfComment" module in your app's settings.yml:
enabled_modules: [default, sfComment]
- add the following lines in your app.yml:
all: sfPropelActAsCommentableBehaviorPlugin: use_ajax: true css: true anonymous: enabled: true layout: name: required email: required title: optional comment: required name: Anonymous User user: enabled: true layout: title: optional comment: required table: sf_guard_user id: id class: sfGuardUser id_method: getId toString: __toString namespaces: backend: administrator count: enabled: true method: setSfCommentCount namespace: frontend
Optional settings
The plugin has been designed to allow comments from authenticated users, as well as anonymous users. But in general, you will want to adapt the layout of the form, depending on whether the user is authenticated or not. In the app.yml file, you can tweak the default setup:
- the use_ajax rule indicates whether or not the comment system must use ajax.
- the css rule indicates whether or not to load the supplied styles.
- the anonymous rules will tweak the way the plugin handles anonymous
comments:
- enabled : enables or disables anonymous comments.
- layout : defines the layout of the comment form. Required fields will get a "required class" and will get validated. If you want a field not to appear (for instance, the "title" field), simply remove the associated line.
- name : the default name of the user, in case the anonymous comment form does not as for an author name.
- the user rules will tweak the way the plugin handles comments from
authenticated users:
- enabled : enables or disables comments from authenticated users.
- layout : defines the layout of the comment form. Required fields will get a "required class" and will get validated. If you want a field not to appear (for instance, the "title" field), simply remove the associated line.
- table : name of the table that stores the users data.
- id : name of the primary key of a user in the users table.
- class : class associated to the users.
- id_method : name of a method of your user's class, that permits to get the authenticated user id. Usually, you will have to define this method in the myUser.class.php file.
- toString : name of a method that outputs the name of a user (an instance of the class defined two lines before)
- the namespaces parameter lists the namespaces for which a security
check must be made:
- when a namespace is listed under the namespaces parameter, its value represents the required credentials for writing in it.
- If you use namespaces in your comments, please make sure that you fill this parameter accordingly to your needs. For instance, if your application provide back-office internal comments, you won't want a normal front-office user being able to add comments on the back-office.
- You can of course use your own namespaces names (ie., "backend" and "frontend" are not compulsory names).
- the count rules are useful for optimizing objects sorting on their comments count. See the paragraph Retrieving one object's comments number for more details.
Usage
How to use the comments module
You do not have to know the plugin internals in order to get started with the behavior. You simply have to include two components:
- one for displaying the comments associated to an object
- an other for displaying the comment form
For instance, when displaying a blog post, add in the view PHP file:
<h2><?php echo $post->getTitle(); ?></h2> <p><?php echo $post->getText(); ?></p> <h3>Comments</h3> <?php include_component('sfComment', 'commentList', array('object' => $post)); include_component('sfComment', 'commentForm', array('object' => $post)); ?>
By default, the comment list displays all the comments, whatever their namespace. If you want to display comments for the namespace "gerard", then simply pass this optionnal parameter to the component. Comment namespaces are particularly useful when you want to use the comments system for both front-office and back-office environnements, with separate comment lists:
<?php include_component('sfComment', 'commentList', array('object' => $post, 'namespace' => 'gerard')); ?>
This also works for the comment-form component. This way, the following form will add the comment to the namespace "gerard":
<?php include_component('sfComment', 'commentList', array('object' => $post, 'namespace' => 'gerard')); ?>
You can protect namespaces from being accessed when the current user does not have some credentials ; have a look at the configuration file for further informations.
Attaching comments to a commentable object
Consider a Propel "Post" class:
<?php class Post extends BasePost { } sfPropelBehavior::add('Post', array('sfPropelActAsCommentableBehavior'));
When the sfPropelActAsCommentableBehaviorPlugin is applied to the Post class, that class automagically gets commentable:
<?php $post = new Post(); // blah $post->save(); $post->addComment('This is a cool comment.'); $post->addComment(array('title' => 'this is a cool title', 'text' => 'this is a cool comment', 'author_id' => sfContext::getInstance()->getUser()->getUserId())); $post->addComment(array('This is a cool comment.', 'this is one other comment'));
Retrieving one object's comments
It is possible to retrieve comments from a commentable object:
<?php $post = PostPeer::retrieveByPk(1); $comments = $post->getComments(); foreach ($comments as $comment) { echo '<p>'.$comment->getText().'</p>'; }
Removing one object's comment
Of course, comments can also be removed:
<?php $post = PostPeer::retrieveByPk(1); $post->removeComment(12); $post->clearComments();
Retrieving one object's comments number
It is rather easy to retrieve the number of comments attached to one object:
<?php $post = PostPeer::retrieveByPk(1); $nb_comments = $post->getNbComments();
In order to retrieve all the comments in one specific namespace, simply add a "namespace" parameter:
<?php $post = PostPeer::retrieveByPk(1); $nb_comments = $post->getNbComments(array('namespace' => 'frontend'));
One common problem is about sorting objects by their number of comments. For the moment, the plugin does not propose any immediate solution, so you will have to join with the comments table:
SELECT `post.title`,
`post.text`,
COUNT(`sf_comment.id`) as `count`
FROM `post`, `sf_comment`
WHERE `sf_comment.commentable_id`=`post.id`
AND `sf_comment.commentable_model`='post'
GROUP BY (`sf_comment.commentable_id`)
SORT BY `count` DESC;
However, a trick is available in the plugin: if you create a column named "sf_comment_count" (or something else, depending on your app.yml configuration) in the commentable model, its value will be updated each time a new comment is added using the addComment() method.
Several app.yml parameters are involved in this trick:
count:
enabled: true # whether or not the method must be called for updating the comments count
method: setSfCommentCount # name of the method to call in order to update the comments count. If you call the comments count column "gerard", simply put "setGerard" on this line
namespace: frontend # namespaces of the comments that have to be counted (usefull for frontend counts). If you don't use namespaces, don't fill this line.
With this trick, sorting objects by their comment numbers is rather straightforward:
$c = new Criteria(); $c->addDescendingOrderByColumn(PostPeer::SF_COMMENT_COUNT); $posts = PostPeer::doSelect($c);
API
The behavior implement the following methods:
- addComment($comment) - Adds a comment to the object. The "comment" param can be an associative array (in which each element represents one of the comment properties), or an array of associative arrays. In this case, it adds all the comments to the object.
- clearComments($namespace = null) - Deletes all the comments attached to the object
- getComments($options = array(), Criteria $criteria = null) - Returns the list of the comments attached to the object. The options array may contain several restriction options: namespace, order. The Criteria may be used to programmatically restrict the results.
- getNbComments($options = array(), Criteria $criteria = null) - Returns the number of the comments attached to the object.The options array may contain several restriction options: namespace, order. The Criteria may be used to programmatically restrict the results.
- removeComment($comment_id) - Removes one comment from the object.
Unit testing
The plugin has been deeply unit-tested. The tests are located in test/unit/sfPropelActAsCommentableBehaviorTest.php. If you want to run them:
- install the plugin
- configure a model for using it, for instance "Post"
- edit this file and, if required, modify the application name and the TEST_CLASS constant, line 3:
define('TEST_CLASS', 'Post'); - run the tests:
php plugins/sfPropelActAsCommentableBehaviorPlugin/test/unit/sfPropelActAsCommentableBehaviorTest.php
In-depth usage tutorial
This part is a complete tutorial for using the plugin both in front and back-office.
Install the plugin
- go to your project's root
- Install the plugin:
./symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsCommentableBehaviorPlugin
- if not already done, enabled behaviors in config/propel.ini:
propel.builder.addBehaviors = true
- edit the classes that you want to make commentable. For instance, for lib/model/Post.php:
<?php class Post extends BasePost { } sfPropelBehavior::add('Post', array('sfPropelActAsCommentableBehavior'));
- rebuild the model:
./symfony propel-build-all
- clear the cache:
./symfony cc
Set up the plugin
- activate the "sfComment" module in the settings.yml of the frontend application:
enabled_modules: [default, sfComment]
- activate both the "sfComment" and the "sfCommentAdmin" modules in the settings.yml of the backend application:
enabled_modules: [default, sfComment, sfCommentAdmin]
- add the following lines in the app.yml of both applications:
all: sfPropelActAsCommentableBehaviorPlugin: use_ajax: true css: true anonymous: enabled: true layout: name: required email: required title: optional comment: required name: Anonymous User user: enabled: true layout: title: optional comment: required table: sf_guard_user id: id class: sfGuardUser id_method: getId toString: __toString namespaces: backend: administrator
- tweak these values accordingly to the previously explained settings. Please note that only administrator can add comments in the comments "backend" namespace.
Add comments in front-office
Include the sfComment components where the comments and the commentform should appear:
<h2><?php echo $post->getTitle(); ?></h2> <p><?php echo $post->getText(); ?></p> <h3>Comments</h3> <?php include_component('sfComment', 'commentList', array('object' => $post, 'namespace' => 'frontend')); include_component('sfComment', 'commentForm', array('object' => $post, 'namespace' => 'frontend')); ?>
The use of a "namespace" is not required in this case; but it is advised, as it makes it easier to find the comments back.
Add comments in back-office
Include the sfComment components where the comments and the commentform shoumd appear:
<?php include_component('sfComment', 'commentList', array('object' => $post, 'namespace' => 'backend')); include_component('sfComment', 'commentForm', array('object' => $post, 'namespace' => 'backend')); ?>
- you're done! Only users with "administrator" credential are able to add comments to objects in the back-office, while everyone can add comments in the front-office. You can tweak the required credentials by modifying the app.yml file.
Comments administration
- optionnaly, have a look at the sfCommentAdmin module, that uses the admin-generator for providing a view of all comments.
License and credits
This plugin is licensed under the MIT license. You can contact the maintainer at xavier@lacot.org
Changelog
Version 0.4 - 2007-12-10
- compatibility with escaping=both mode
- Added unique DOM id for each comment, in the form sf_comment_$id (Nicolas Perriault)
- use a session token instead of passing the object id and model in the request (Nicolas Perriault)
- bugfixes, based on a patch from Michael Nolan (closes #2595):
- fix anonymous posting (use the token)
- pre and post addition hooks
version 0.3 - 2007-10-08
- added namespaces support
- fixed bad index names (thanks to francois)
- made getComments() more flexible (closes #2312, thanks to FrankStelzer?)
version 0.2 - 2007-09-26
- added a Symfony module for posting and displaying comments
- ajax support
- authenticated users support
- form customization
- added an administration module
version 0.1 - 2007-09-13
Initial public release. Features comments attachment to heterogene Propel objects.
Roadmap / Wishlist
- have custom configurations for specific comment-forms (and not only app-wide configurations)
- make use of a captcha plugin, when a clean one will be available.
- handle other custom fields in comments.
Active tickets
- #2900
- Allow some formatting/HTML in comments
- #3086
- sfComment component of the plugins doesn't save title
- #3258
- Duplication of DOM id when updating sf_comment_list with _commentList partial
- #3544
- namespace is a reserved word in Propel13
- #3745
- [PATCH] sfPropelActAsCommentableBehaviorPlugin plugin is XSS vulnerable in title and author_name fields
- #4024
- Upgrade to Symfony 1.1
Closed tickets
- #2312
- make getComments more flexible
- #2399
- sfPropelActAsCommentableBehavior not working with CRUD
- #2454
- sfPropelActAsCommentableBehavior error in form action for authenticated users
- #2595
- [PATCH] Mixins for pre and post comment addition and fix anonymous posting
- #2616
- Missed helpers
- #2629
- I18N helper call missing in template partials
- #2717
- i18n helper not declared
- #2718
- [PATCH] correct spelling/copy/paste error in sfPropelActAsCommentableBehaviorPlugin
- #2739
- `symfony propel-build-all` should be removed from plugin-docu
- #2899
- CSS should be optional
- #2966
- Still references to actastaggable in actascommentable plugin documentation
see also : SymfonyPlugins
Attachments
- sfPropelActAsCommentableBehaviorPlugin-0.1.0.tgz (5.8 kB) -
sfpropelActAsCommentableBehaviorPlugin - initial release
, added by xavier on 09/13/07 16:55:39. - sfPropelActAsCommentableBehaviorPlugin_comment_1.png (30.7 kB) - added by xavier on 09/26/07 17:18:59.
- sfPropelActAsCommentableBehaviorPlugin_comment_2.png (32.5 kB) - added by xavier on 09/26/07 17:19:20.
- sfPropelActAsCommentableBehaviorPlugin_comment_3.png (35.7 kB) - added by xavier on 09/26/07 17:19:36.
- sfPropelActAsCommentableBehaviorPlugin-0.2.0.tgz (15.0 kB) - added by xavier on 09/26/07 17:29:47.
- sfPropelActAsCommentableBehaviorPlugin-0.3.0.tgz (16.8 kB) -
sfPropelActAsCommentableBehaviorPlugin - releasing 0.3
, added by xavier on 10/08/07 21:06:24. - sfPropelActAsCommentableBehaviorPlugin-0.4.0.tgz (17.9 kB) -
sfPropelActAsCommentableBehaviorPlugin : releasing 0.4
, added by xavier on 12/10/07 12:46:14.


