Skip to content
This repository
Browse code

add update action + events for upd/delete close #4

- added update button and action
- added events for update and delete
- documentation update
  • Loading branch information...
commit 929699dcbdb1dcd9fe97cc85e93dceea79ae1313 1 parent 303285e
Carsten Brandt cebe authored
2  CHANGELOG.md
Source Rendered
@@ -2,6 +2,8 @@
2 2 ### 0.6.0 (to be released)
3 3
4 4 - added getCommentCount() to CommentableBehavior (cebe)
  5 +- added delete button and action (cebe)
  6 +- added update button and action (cebe)
5 7
6 8 ### 0.5.0 (2011-12-19)
7 9
30 CommentModule.php
@@ -17,7 +17,7 @@ class CommentModule extends CWebModule
17 17 * 'modelclass' is a class name of the commentable AR
18 18 * this AR class must have the {@see CommentableBehavior} attached to it
19 19 */
20   - public $commentableModels = array('task');
  20 + public $commentableModels = array();
21 21
22 22 /**
23 23 * @var string name of the user model class to use for comments
@@ -82,4 +82,32 @@ public function onNewComment($comment, $model)
82 82 $event->commentedModel = $model;
83 83 $this->raiseEvent('onNewComment', $event);
84 84 }
  85 +
  86 + /**
  87 + * This event is raised after a comment has been updated
  88 + *
  89 + * @param $comment
  90 + * @param $model currently not available see {@link https://github.com/yiiext/comment-module/issues/10}
  91 + */
  92 + public function onUpdateComment($comment/*, $model*/)
  93 + {
  94 + $event = new CommentEvent();
  95 + $event->comment = $comment;
  96 + //$event->commentedModel = $model;
  97 + $this->raiseEvent('onUpdateComment', $event);
  98 + }
  99 +
  100 + /**
  101 + * This event is raised after a comment got deleted
  102 + *
  103 + * @param $comment
  104 + * @param $model currently not available see {@link https://github.com/yiiext/comment-module/issues/10}
  105 + */
  106 + public function onDeleteComment($comment/*, $model*/)
  107 + {
  108 + $event = new CommentEvent();
  109 + $event->comment = $comment;
  110 + //$event->commentedModel = $model;
  111 + $this->raiseEvent('onDeleteComment', $event);
  112 + }
85 113 }
21 README.md
Source Rendered
@@ -4,9 +4,10 @@ Comment module
4 4 makes every entity of your application commentable.
5 5 Features:
6 6
7   -* Ajax creation of comment
  7 +* Create, Update, Delete comments with ajax
8 8 * Gravatar support
9 9 * define multiple models that can be commented
  10 +* Events raised on new, update, delete
10 11 * more coming soon...
11 12
12 13 If there is something missing here, or you think one step should be described more detailed,
@@ -151,14 +152,24 @@ See [CModule::behaviors](http://www.yiiframework.com/doc/api/1.1/CModule#behavio
151 152 This event is raised when a new comment has been saved.
152 153 The following attributes are available on the `$event` given as the first parameter to the event handler:
153 154
154   -* `$event->comment` is the currently added comment.
  155 +* `$event->comment` is the ActiveRecord instance of the currently added comment.
155 156 * `$event->commentedModel` is the model the comment was added to.
156 157
157 158 Possible use cases:
158 159
159 160 * Send an E-Mail-Notification
160 161
161   -Events coming soon:
  162 +### onUpdateComment
  163 +
  164 +This event is raised when a user edited a comment.
  165 +The following attributes are available on the `$event` given as the first parameter to the event handler:
  166 +
  167 +* `$event->comment` is the ActiveRecord instance of the updated comment.
  168 +
  169 +### onDeleteComment
  170 +
  171 +This event is raised when a user deleted a comment.
  172 +The following attributes are available on the `$event` given as the first parameter to the event handler:
  173 +
  174 +* `$event->comment` is the ActiveRecord instance of the deleted comment.
162 175
163   -### onCommentEdited
164   -### onCommentDelete
20 behaviors/CommentableBehavior.php
@@ -42,6 +42,8 @@
42 42 * }
43 43 * </pre>
44 44 *
  45 + * @property CommentModule $module
  46 + *
45 47 * @author Carsten Brandt <mail@cebe.cc>
46 48 * @package yiiext.modules.comment
47 49 */
@@ -101,8 +103,22 @@ public function getCommentInstance()
101 103 */
102 104 public function getComments()
103 105 {
104   - return Yii::createComponent($this->module->commentModelClass)
105   - ->findAll($this->getCommentCriteria());
  106 + $comments = Yii::createComponent($this->module->commentModelClass)
  107 + ->findAll($this->getCommentCriteria());
  108 + // get model type
  109 + $type = get_class($this->owner);
  110 + foreach($this->module->commentableModels as $scope => $model) {
  111 + if ($type == $model) {
  112 + $type = $scope;
  113 + break;
  114 + }
  115 + }
  116 + foreach($comments as $comment) {
  117 + /** @var Comment $comment */
  118 + $comment->setType($type);
  119 + $comment->setKey($this->owner->primaryKey);
  120 + }
  121 + return $comments;
106 122 }
107 123
108 124 /**
48 controllers/CommentController.php
@@ -105,21 +105,46 @@ public function actionCreate()
105 105 */
106 106 public function actionUpdate($id)
107 107 {
108   - $model=$this->loadModel($id);
  108 + $comment=$this->loadModel($id);
109 109
110   - // Uncomment the following line if AJAX validation is needed
111   - // $this->performAjaxValidation($model);
112   -
113   - if(isset($_POST[$cClass=get_class($model)]))
  110 + if(isset($_POST[$cClass=get_class($comment)]))
114 111 {
115   - $model->attributes=$_POST[$cClass];
116   - if($model->save())
117   - $this->redirect(array('view','id'=>$model->id));
  112 + $comment->attributes = $_POST[$cClass];
  113 +
  114 + if ($comment->save())
  115 + {
  116 + if(Yii::app()->request->isAjaxRequest) {
  117 + // refresh model to replace CDbExpression for timestamp attribute
  118 + $comment->refresh();
  119 +
  120 + // render updated comment
  121 + $this->renderPartial('_view',array(
  122 + 'data'=>$comment,
  123 + ));
  124 + Yii::app()->end();
  125 + } else {
  126 + $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('view','id'=>$comment->id));
  127 + }
  128 + }
118 129 }
119 130
120   - $this->render('update',array(
121   - 'model'=>$model,
122   - ));
  131 + if(Yii::app()->request->isAjaxRequest)
  132 + {
  133 + $output = $this->renderPartial('_form',array(
  134 + 'comment'=>$comment,
  135 + 'ajaxId'=>time(),
  136 + ), true);
  137 + // render javascript functions
  138 + Yii::app()->clientScript->renderBodyEnd($output);
  139 + echo $output;
  140 + Yii::app()->end();
  141 + }
  142 + else
  143 + {
  144 + $this->render('update',array(
  145 + 'model'=>$comment,
  146 + ));
  147 + }
123 148 }
124 149
125 150 /**
@@ -170,6 +195,7 @@ public function actionDelete($id)
170 195 * Returns the data model based on the primary key given in the GET variable.
171 196 * If the data model is not found, an HTTP exception will be raised.
172 197 * @param integer the ID of the model to be loaded
  198 + * @return Comment
173 199 */
174 200 public function loadModel($id)
175 201 {
44 models/Comment.php
@@ -24,6 +24,7 @@ class Comment extends CActiveRecord
24 24 {
25 25 private $_type;
26 26 private $_key;
  27 + private $_new = false;
27 28
28 29 /**
29 30 * @var string set the commentableModels scope from CommentModule
@@ -102,8 +103,8 @@ public function rules()
102 103 {
103 104 return array(
104 105 array('message', 'safe'),
105   - array('type', 'validateType'),
106   - array('key', 'validateKey'),
  106 + array('type', 'validateType', 'on'=>'create'),
  107 + array('key', 'validateKey', 'on'=>'create'),
107 108 // The following rule is used by search().
108 109 // Please remove those attributes that should not be searched.
109 110 array('id, message, userId', 'safe', 'on'=>'search'),
@@ -138,18 +139,41 @@ public function relations()
138 139 );
139 140 }
140 141
  142 + protected function beforeSave()
  143 + {
  144 + $this->_new = $this->isNewRecord;
  145 + return parent::beforeSave();
  146 + }
  147 +
141 148 protected function afterSave()
142 149 {
143   - $commentedModel = CActiveRecord::model($this->module->commentableModels[$this->type]);
144   - $this->getDbConnection()->createCommand(
145   - "INSERT INTO ".$commentedModel->mapTable."(".$commentedModel->mapCommentColumn.", ".$commentedModel->mapRelatedColumn.")
146   - VALUES (:id, :key);"
147   - )->execute(array(':id' => $this->id, ':key' => $this->key));
  150 + if ($this->_new) {
  151 + $commentedModel = CActiveRecord::model($this->module->commentableModels[$this->type]);
  152 + // if comment is new, connect it with commended model
  153 + $this->getDbConnection()->createCommand(
  154 + "INSERT INTO ".$commentedModel->mapTable."(".$commentedModel->mapCommentColumn.", ".$commentedModel->mapRelatedColumn.")
  155 + VALUES (:id, :key);"
  156 + )->execute(array(':id' => $this->id, ':key' => $this->key));
  157 +
  158 + parent::afterSave();
148 159
149   - parent::afterSave();
  160 + // raise new comment event
  161 + $this->module->onNewComment($this, $commentedModel->findByPk($this->key));
  162 + } else {
  163 + parent::afterSave();
  164 + }
  165 + // raise update comment event
  166 + $this->module->onUpdateComment($this/*, $commentedModel->findByPk($this->key)*/);
  167 + }
150 168
151   - // raise new comment event
152   - $this->module->onNewComment($this, $commentedModel->findByPk($this->key));
  169 + protected function afterDelete()
  170 + {
  171 + parent::afterDelete();
  172 + // raise update comment event
  173 + $this->module->onDeleteComment(
  174 + $this/*,
  175 + CActiveRecord::model($this->module->commentableModels[$this->type])->findByPk($this->key)*/
  176 + );
153 177 }
154 178
155 179 /**
36 views/comment/_form.php
... ... @@ -1,16 +1,17 @@
1 1 <?php if (Yii::app()->user->isGuest) {
2   -?><div class="comment-not-loggedin">
  2 +?><div class="ext-comment-not-loggedin">
3 3 Sorry, you have to login to leave a comment.
4 4 </div><?php } else { ?>
5   -<div id="comment-form-ajax" class="form">
  5 +<div id="ext-comment-form-<?php echo $comment->isNewRecord ? 'new' : 'edit-'.$comment->id; ?>" class="form">
6 6
7 7 <?php $form = $this->beginWidget('CActiveForm', array(
8   - 'id'=>'comment-form',
  8 + 'id'=>'ext-comment-form',
9 9 'action'=>array('/comment/comment/create'),
10 10 'enableAjaxValidation'=>false
11 11 )); ?>
12 12
13   - <?php echo $form->error($comment,'userId'); ?>
  13 + <?php /** @var CActiveForm $form */
  14 + echo $form->errorSummary($comment); ?>
14 15
15 16 <div class="row">
16 17 <?php echo $form->labelEx($comment,'message'); ?>
@@ -18,27 +19,38 @@
18 19 <?php echo $form->error($comment,'message'); ?>
19 20 </div>
20 21
21   - <?php echo $form->hiddenField($comment, 'type'); ?>
22   - <?php echo $form->hiddenField($comment, 'key'); ?>
23   -
24 22 <div class="row buttons">
25 23 <?php if ($comment->isNewRecord) {
  24 +
  25 + echo $form->hiddenField($comment, 'type');
  26 + echo $form->hiddenField($comment, 'key');
  27 +
26 28 /* echo CHtml::hiddenField('returnUrl', $this->createUrl(''));}
27 29 echo CHtml::submitButton('Save'); */
28 30 echo CHtml::ajaxSubmitButton('Submit',
29 31 array('/comment/comment/create'),
30 32 array(
31   - 'replace'=>'#comment-form-ajax',
  33 + 'replace'=>'#ext-comment-form-new',
32 34 'error'=>"function(){
33 35 $('#Comment_message').css('border-color', 'red');
34 36 $('#Comment_message').css('background-color', '#fcc');
35 37 }"
36 38 ),
37   - array('id'=>'submit-comment' . (isset($ajaxId) ? $ajaxId : ''))
  39 + array('id'=>'ext-comment-submit' . (isset($ajaxId) ? $ajaxId : ''))
38 40 );
39   - } else {
40   - echo CHtml::submitButton('Save');
41   - }
  41 + } else {
  42 + echo CHtml::ajaxSubmitButton('Update',
  43 + array('/comment/comment/update', 'id'=>$comment->id),
  44 + array(
  45 + 'replace'=>'#ext-comment-form-edit-'.$comment->id,
  46 + 'error'=>"function(){
  47 + $('#Comment_message').css('border-color', 'red');
  48 + $('#Comment_message').css('background-color', '#fcc');
  49 + }"
  50 + ),
  51 + array('id'=>'ext-comment-submit' . (isset($ajaxId) ? $ajaxId : ''))
  52 + );
  53 + }
42 54 ?>
43 55 </div>
44 56
17 views/comment/_view.php
@@ -31,7 +31,8 @@
31 31 }
32 32 ");
33 33 ?>
34   -<div class="ext-comment">
  34 +<div class="ext-comment" id="ext-comment-<?php echo $data->id; ?>">
  35 +
35 36 <span class="ext-comment-head">
36 37 <span class="ext-comment-name"><?php echo CHtml::encode($data->userName); ?></span>
37 38 wrote on
@@ -41,7 +42,9 @@
41 42 ); ?>
42 43 </span>:
43 44 </span>
  45 +
44 46 <hr />
  47 +
45 48 <?php $this->widget('comment.extensions.gravatar.yii-gravatar.YiiGravatar', array(
46 49 'email'=>$data->userEmail,
47 50 'size'=>80,
@@ -54,17 +57,27 @@
54 57 'title'=>CHtml::encode($data->userName)
55 58 )
56 59 )); ?>
  60 +
57 61 <span class="ext-comment-options">
58 62 <?php if (!Yii::app()->user->isGuest && (Yii::app()->user->id == $data->userId)) {
59 63 echo CHtml::ajaxLink('delete', array('/comment/comment/delete', 'id'=>$data->id), array(
60   - 'success'=>'function(){ $("#delete-comment-'.$data->id.'").parent().parent().remove(); }',
  64 + 'success'=>'function(){ $("#ext-comment-'.$data->id.'").remove(); }',
61 65 'type'=>'POST',
62 66 ), array(
63 67 'id'=>'delete-comment-'.$data->id,
64 68 'confirm'=>'Are you sure you want to delete this item?',
65 69 ));
  70 + echo " | ";
  71 + echo CHtml::ajaxLink('edit', array('/comment/comment/update', 'id'=>$data->id), array(
  72 + 'replace'=>'#ext-comment-'.$data->id,
  73 + 'type'=>'GET',
  74 + ), array(
  75 + 'id'=>'ext-comment-edit-'.$data->id,
  76 + ));
66 77 } ?>
67 78 </span>
  79 +
68 80 <p><?php echo nl2br(CHtml::encode($data->message)); ?></p>
  81 +
69 82 <br style="clear: both;"/>
70 83 </div>

0 comments on commit 929699d

Please sign in to comment.
Something went wrong with that request. Please try again.