![]() |
|
sfPropelActAsCommentableBehaviorPlugin - 0.5.0Propel commentable behavior |
|
![]() |
34
users
Sign-in
to change your status |
This behavior permits to attach comments to Propel objects. It provides a module for enabling comments in your application in less than 3 minutes. |
| 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 |
|---|---|---|---|
| 1.2.0stable | MIT | 1.2.0stable | 16/01/2009 |
| Version | License | API | Released |
|---|---|---|---|
| 0.5.0beta | MIT | 0.5.0beta | 11/08/2008 |
| 0.4.0beta | MIT license | 0.4.0beta | 10/12/2007 |
| 0.3.0beta | MIT license | 0.3.0beta | 08/10/2007 |
| 0.2.0beta | MIT license | 0.2.0beta | 26/09/2007 |
| 0.1.0beta | MIT license | 0.1.0beta | 13/09/2007 |
sfPropelActAsCommentableStripper classsfPropelActAsCommentableStripper classsf_comment_$id
(Nicolas Perriault)Initial public release. Features comments attachment to heterogene Propel objects.
This behavior permits to attach comments to Propel objects. It provides a module for enabling comments in your application in less than 3 minutes.

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:
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:
allowed_tags:
a: <a>
blockquote: <blockquote>
code: <code>
em: <em>
i: <i>
p: <p>
strong: <strong>
anonymous:
enabled: true
layout:
name: required
email: required
website: optional
title: optional
comment: required
name: Anonymous User
css: false
count:
enabled: true
method: setSfCommentCount
namespace: frontend
date_format: words
hide_form: true
namespaces:
backend: administrator
use_ajax: true
use_gravatar: true
user:
enabled: true
layout:
title: optional
comment: required
table: sf_guard_user
id: id
class: sfGuardUser
id_method: getId
toString: __toString
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:
You do not have to know the plugin internals in order to get started with the behavior. You simply have to include two components:
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>
<?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:
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":
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.
Consider a Propel "Post" class:
class Post extends BasePost
{
}
sfPropelBehavior::add('Post', array('sfPropelActAsCommentableBehavior'));
When the sfPropelActAsCommentableBehaviorPlugin is applied to the Post class, that class automagically gets commentable:
$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'));
It is possible to retrieve comments from a commentable object:
$post = PostPeer::retrieveByPk(1);
$comments = $post->getComments();
foreach ($comments as $comment)
{
echo '<p>'.$comment->getText().'</p>';
}
Of course, comments can also be removed:
$post = PostPeer::retrieveByPk(1);
$post->removeComment(12);
$post->clearComments();
It is rather easy to retrieve the number of comments attached to one object:
$post = PostPeer::retrieveByPk(1);
$nb_comments = $post->getNbComments();
In order to retrieve all the comments in one specific namespace, simply add a "namespace" parameter:
$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);
The behavior implement the following methods:
The plugin has been deeply unit-tested. The tests are located in test/unit/sfPropelActAsCommentableBehaviorTest.php. If you want to run them:
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
This part is a complete tutorial for using the plugin both in front and back-office.
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
class Post extends BasePost
{
}
sfPropelBehavior::add('Post', array('sfPropelActAsCommentableBehavior'));
rebuild the model:
./symfony propel-build-all
clear the cache:
./symfony cc
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, or in the project's app.yml:
all:
sfPropelActAsCommentableBehaviorPlugin:
allowed_tags:
a: <a>
blockquote: <blockquote>
code: <code>
em: <em>
i: <i>
p: <p>
strong: <strong>
anonymous:
enabled: true
layout:
name: required
email: required
website: optional
comment: required
name: Anonymous User
css: false
count:
enabled: false
method: setSfCommentCount
namespace: frontend
date_format: words
hide_form: true
namespaces:
backend: administrator
use_ajax: true
use_gravatar: true
user:
enabled: false
layout:
title: optional
comment: required
table: sf_guard_user
id: id
class: sfGuardUser
id_method: getId
toString: __toString
tweak these values accordingly to the "Optional settings" paragraph. In this example, please note that only administrators can add comments in the comments "backend" namespace.
Include the sfComment components where the comments and the comment form
should appear:
<h2><?php echo $post->getTitle(); ?></h2>
<p><?php echo $post->getText(); ?></p>
<?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.
Include the sfComment components where the comments and the commentform
should appear:
<?php
include_component('sfComment', 'commentList', array('object' => $post, 'namespace' => 'backend'));
include_component('sfComment', 'commentForm', array('object' => $post, 'namespace' => 'backend'));
?>
app.yml file.sfCommentAdmin module, that uses the
admin-generator for providing a view of all comments.sfComment in your
application, and it will override the plugin's module.the sfPropelActAsCommentableStripper can also be overloaded. Simply
create your own class somewhere in your project. This class must be named
"sfPropelActAsCommentableStripper", and must implement a "clean()"
method. For instance, the following stripper won't strip anything (nor validate
the user's entry):
<?php
class sfPropelActAsCommentableStripper
{
static public function clean($text)
{
return $text;
}
}
This plugin is licensed under the MIT license. You can contact the maintainer at xavier@lacot.org.
sfPropelActAsCommentableStripper classsf_comment_$id (Nicolas Perriault)getComments() more flexible (closes #2312, thanks to FrankStelzer)Initial public release. Features comments attachment to heterogene Propel objects.
