, (*1)
The Comments plugin will allow you comment every model with the possibility to change the template in your APP., (*2)
This plugin works with a behavior and a helper you need to load to fully works., (*3)
Requirements
-
CakePHP 3.4+
- PHP 5.6+
- AuthComponent
Installation
composer require kareylo/cakephp-comments
load the plugin in your config/bootstrap.php
:, (*4)
Plugin::load('Kareylo/Comments', [
'routes' => true
]);
Add in the ModelTable you wanna be commentable the following behavior :, (*5)
$this->addBehavior('Kareylo/Comments.Commentable');
The behavior can take these options :
* modelClass : Class name of the ModelTable.
* Default : null
* commentClass : Name of your CommentsTable if you have one.
* Default : Kareylo/Comments.Comments
* foreignKey : Name of your custom foreignKey.
* Default : ref_id
* countComments : Put true if you wanna your model count its Comments
* Default : false
* fieldCounter : Name of your counter field
* Default : comments_count
, (*6)
Add the following helper in your src/View/AppView.php
, (*7)
public function initialize()
{
$this->loadHelper('Kareylo/Comments.Comment');
}
The helper can take these options :
* type The HTML tag that will surround your comments
* Default ul
* Accept ul
, ol
, div
* typeClass the CSS class your type need to have
* Default null
* subTypeClass The CSS class your subType need to have
* Default null
* loadJS Put true if you wanna default JS to be loaded
* Default false
, (*8)
Usage
Get all your comments with the comments finder, (*9)
$data = $this->Model->find()->where(['Model.id' => $id])->find('comments')->first();
$this->set(compact('data'));
To display your comments, (*10)
$this->Comment->display($data);
You can also chose to not use display($data)
and use a loop to have the full control of your template, (*11)
// in your view
<div class="row">
<h4>Commentaires</h4>
<ul class="comment-list">
<?php foreach ($model->comments as $comment):
echo $this->Comment->comment($comment);
endforeach; ?>
</ul>
<!-- loadJS and display the comment Form if user is connected -->
<?= $this->Comment->loadFormAndJS($model); ?>
</div>
Templates
To create templates for the comment block (1 comment) and the form block, create the views you want if src/Template/Element/Comments
.
Example :, (*12)
/** src/Template/Element/Comments/comment.ctp
* $connected is used to check is user is connected
*/
// src/Template/Element/Comments/form.ctp
= $this->Form->create($comment, ['id' => 'commentForm', 'url' => ['controller' => 'Comments', 'action' => 'add', 'plugin' => 'Comments']]); ?>
<?= $this->Flash->render('comment'); ?>
<?= $this->Form->control('content', ['label' => __('Commentaire'), 'type' => 'textarea']); ?>
<?= $this->Form->hidden('ref'); ?>
<?= $this->Form->hidden('ref_id'); ?>
<?= $this->Form->unlockField('parent_id'); ?>
<?= $this->Form->hidden('parent_id', ['default' => null]); ?>
<?= $this->Form->button(__('Commenter')) ?>
= $this->Form->end() ?>
To create the template for your flash, just add a element in src/Template/Element/Flash/Comments
and name it comment.ctp
```php
//src/Template/Element/Flash/Comments/comment.ctp
, (*13)
= $message ?>
```
- $params['class']
can have the values success
and error
, (*14)
Support
For bugs and feature requests, please use the issues section of this repository., (*15)
Contribute
Follow this guide to contribute, (*16)
License
Licensed under the MIT License. Redistributions of the source code included in this repository must retain the copyright notice found in each file., (*17)
TODO
- [X] Test cases
- [ ] Improved Test Cases (like test Helper)
- [ ] More features
- [ ] Translation