Skip to content

Commit 1942fc9

Browse files
author
Igor Chepurnoy
committed
add new comment events
1 parent 3549da9 commit 1942fc9

File tree

3 files changed

+126
-22
lines changed

3 files changed

+126
-22
lines changed

README.md

+35-13
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,7 @@ To access the module, you need to add the following code to your application con
4646
'modules' => [
4747
'comment' => [
4848
'class' => 'yii2mod\comments\Module',
49-
// also you can use the `afterCreate` event as follows
50-
'controllerMap' => [
51-
'default' => [
52-
'class' => 'yii2mod\comments\controllers\DefaultController',
53-
'on afterCreate' => function ($event) {
54-
// get saved comment model
55-
$event->getCommentModel();
56-
// your custom code
57-
}
58-
]
59-
]
60-
]
49+
],
6150
]
6251
```
6352

@@ -161,7 +150,7 @@ $model = Post::find()->where(['title' => 'some post title'])->one();
161150
<?php echo \yii2mod\comments\widgets\Comment::widget([
162151
'model' => $model,
163152
'relatedTo' => 'User ' . \Yii::$app->user->identity->username . ' commented on the page ' . \yii\helpers\Url::current(),
164-
'maxLevel' => 2,
153+
'level' => 2,
165154
// set `pageSize` with custom sorting
166155
'dataProviderConfig' => [
167156
'sort' => [
@@ -179,6 +168,39 @@ $model = Post::find()->where(['title' => 'some post title'])->one();
179168
]); ?>
180169
```
181170

171+
## Using Events
172+
173+
You may use the following events:
174+
175+
```php
176+
'modules' => [
177+
'comment' => [
178+
'class' => 'yii2mod\comments\Module',
179+
'controllerMap' => [
180+
'default' => [
181+
'class' => 'yii2mod\comments\controllers\DefaultController',
182+
'on beforeCreate' => function ($event) {
183+
$event->getCommentModel();
184+
// your custom code
185+
},
186+
'on afterCreate' => function ($event) {
187+
$event->getCommentModel();
188+
// your custom code
189+
},
190+
'on beforeDelete' => function ($event) {
191+
$event->getCommentModel();
192+
// your custom code
193+
},
194+
'on afterDelete' => function ($event) {
195+
$event->getCommentModel();
196+
// your custom code
197+
},
198+
]
199+
]
200+
]
201+
]
202+
```
203+
182204
## Internationalization
183205

184206
All text and messages introduced in this extension are translatable under category 'yii2mod.comments'.

controllers/DefaultController.php

+32-7
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,30 @@
2222
*/
2323
class DefaultController extends Controller
2424
{
25+
/**
26+
* Event is triggered before creating a new comment.
27+
* Triggered with yii2mod\comments\events\CommentEvent
28+
*/
29+
const EVENT_BEFORE_CREATE = 'beforeCreate';
30+
2531
/**
2632
* Event is triggered after creating a new comment.
2733
* Triggered with yii2mod\comments\events\CommentEvent
2834
*/
2935
const EVENT_AFTER_CREATE = 'afterCreate';
3036

37+
/**
38+
* Event is triggered before deleting the comment.
39+
* Triggered with yii2mod\comments\events\CommentEvent
40+
*/
41+
const EVENT_BEFORE_DELETE = 'beforeDelete';
42+
43+
/**
44+
* Event is triggered after deleting the comment.
45+
* Triggered with yii2mod\comments\events\CommentEvent
46+
*/
47+
const EVENT_AFTER_DELETE = 'afterDelete';
48+
3149
/**
3250
* @inheritdoc
3351
*/
@@ -52,7 +70,7 @@ public function behaviors()
5270
}
5371

5472
/**
55-
* Create comment.
73+
* Create a comment.
5674
*
5775
* @param $entity string encrypt entity
5876
*
@@ -62,9 +80,10 @@ public function actionCreate($entity)
6280
{
6381
/* @var $commentModel CommentModel */
6482
$commentModel = Yii::createObject(Yii::$app->getModule(Module::$name)->commentModelClass);
83+
$event = Yii::createObject(['class' => CommentEvent::class, 'commentModel' => $commentModel]);
6584
$commentModel->setAttributes($this->getCommentAttributesFromEntity($entity));
85+
$this->trigger(self::EVENT_BEFORE_CREATE, $event);
6686
if ($commentModel->load(Yii::$app->request->post()) && $commentModel->saveComment()) {
67-
$event = Yii::createObject(['class' => CommentEvent::class, 'commentModel' => $commentModel]);
6887
$this->trigger(self::EVENT_AFTER_CREATE, $event);
6988

7089
return ['status' => 'success'];
@@ -85,7 +104,13 @@ public function actionCreate($entity)
85104
*/
86105
public function actionDelete($id)
87106
{
88-
if ($this->findModel($id)->markRejected()) {
107+
$commentModel = $this->findModel($id);
108+
$event = Yii::createObject(['class' => CommentEvent::class, 'commentModel' => $commentModel]);
109+
$this->trigger(self::EVENT_BEFORE_DELETE, $event);
110+
111+
if ($commentModel->markRejected()) {
112+
$this->trigger(self::EVENT_AFTER_DELETE, $event);
113+
89114
return Yii::t('yii2mod.comments', 'Comment has been deleted.');
90115
} else {
91116
Yii::$app->response->setStatusCode(500);
@@ -99,9 +124,9 @@ public function actionDelete($id)
99124
*
100125
* @param int|array $id Comment ID
101126
*
102-
* @throws NotFoundHttpException
103-
*
104127
* @return null|CommentModel|ModerationBehavior
128+
*
129+
* @throws NotFoundHttpException
105130
*/
106131
protected function findModel($id)
107132
{
@@ -119,9 +144,9 @@ protected function findModel($id)
119144
*
120145
* @param $entity string encrypted entity
121146
*
122-
* @throws BadRequestHttpException
123-
*
124147
* @return array|mixed
148+
*
149+
* @throws BadRequestHttpException
125150
*/
126151
protected function getCommentAttributesFromEntity($entity)
127152
{

tests/CommentTest.php

+59-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use yii2mod\comments\tests\data\DefaultController;
1111
use yii2mod\comments\tests\data\PostModel;
1212
use yii2mod\comments\tests\data\User;
13+
use yii2mod\moderation\enums\Status;
1314

1415
/**
1516
* Class CommentTest
@@ -31,6 +32,28 @@ public function testAddComment()
3132
$this->assertEquals('success', $response['status'], 'Unable to add a comment!');
3233
}
3334

35+
public function testBeforeCreateEvent()
36+
{
37+
$commentContent = 'test comment';
38+
Yii::$app->user->login(User::find()->one());
39+
Yii::$app->request->bodyParams = [
40+
'CommentModel' => [
41+
'content' => $commentContent,
42+
],
43+
];
44+
Event::on(
45+
DefaultController::class,
46+
DefaultController::EVENT_BEFORE_CREATE,
47+
function ($event) use ($commentContent) {
48+
$this->assertEquals(1, $event->getCommentModel()->entityId);
49+
$this->assertInstanceOf(CommentModel::class, $event->getCommentModel());
50+
}
51+
);
52+
$response = Yii::$app->runAction('comment/default/create', ['entity' => $this->generateEntity()]);
53+
54+
$this->assertEquals('success', $response['status'], 'Unable to add a comment!');
55+
}
56+
3457
public function testAfterCreateEvent()
3558
{
3659
$commentContent = 'test comment';
@@ -41,11 +64,11 @@ public function testAfterCreateEvent()
4164
],
4265
];
4366
Event::on(
44-
DefaultController::className(),
67+
DefaultController::class,
4568
DefaultController::EVENT_AFTER_CREATE,
4669
function ($event) use ($commentContent) {
4770
$this->assertEquals($commentContent, $event->getCommentModel()->content);
48-
$this->assertInstanceOf(CommentModel::className(), $event->getCommentModel());
71+
$this->assertInstanceOf(CommentModel::class, $event->getCommentModel());
4972
}
5073
);
5174
$response = Yii::$app->runAction('comment/default/create', ['entity' => $this->generateEntity()]);
@@ -78,6 +101,40 @@ public function testDeleteComment()
78101
$this->assertEquals('Comment has been deleted.', $response, 'Unable to delete a comment!');
79102
}
80103

104+
public function testBeforeDeleteEvent()
105+
{
106+
Event::on(
107+
DefaultController::class,
108+
DefaultController::EVENT_BEFORE_DELETE,
109+
function ($event) {
110+
$this->assertEquals(Status::APPROVED, $event->getCommentModel()->status);
111+
$this->assertInstanceOf(CommentModel::class, $event->getCommentModel());
112+
}
113+
);
114+
115+
Yii::$app->user->login(User::find()->one());
116+
$response = Yii::$app->runAction('comment/default/delete', ['id' => 1]);
117+
118+
$this->assertEquals('Comment has been deleted.', $response, 'Unable to delete a comment!');
119+
}
120+
121+
public function testAfterDeleteEvent()
122+
{
123+
Event::on(
124+
DefaultController::class,
125+
DefaultController::EVENT_AFTER_DELETE,
126+
function ($event) {
127+
$this->assertEquals(Status::REJECTED, $event->getCommentModel()->status);
128+
$this->assertInstanceOf(CommentModel::class, $event->getCommentModel());
129+
}
130+
);
131+
132+
Yii::$app->user->login(User::find()->one());
133+
$response = Yii::$app->runAction('comment/default/delete', ['id' => 1]);
134+
135+
$this->assertEquals('Comment has been deleted.', $response, 'Unable to delete a comment!');
136+
}
137+
81138
/**
82139
* @expectedException \yii\web\BadRequestHttpException
83140
*/

0 commit comments

Comments
 (0)