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 taggable. For instance, for lib/model/Post.php:
[php]
getTitle(); ?>
getText(); ?>
<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 that do not belong to
one namespace. If you want to display comments for the namespace "gerard", then
simply pass this optionnal parameter to the component:
<?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]
<?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]
<?php
$post = PostPeer::retrieveByPk(1);
$post->removeComment(12);
$post->clearComments();
## 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()** - Deletes all the comments attached to the object
* **getComments($options = array())** - Returns the list of the comments
attached to the object. The options array can contain several options
* **getNbComments()** - Returns the number of the comments attached to the
object.
* **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 taggable. For instance, for lib/model/Post.php:
[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: [sfComment](default,)
* activate both the "sfComment" and the "sfCommentAdmin" modules in the settings.yml of the backend application:
enabled_modules: [sfComment, sfCommentAdmin](default,)
* add the following lines in the app.yml of both **applications**:
all:
sfPropelActAsCommentableBehaviorPlugin:
use_ajax: 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](#Optionalsettings). 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:
[php]
<h2><?php echo $post->getTitle(); ?></h2>
getText(); ?>
<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.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.