1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\comments\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\filters\AccessControl; |
7
|
|
|
use yii\filters\VerbFilter; |
8
|
|
|
use yii\helpers\Json; |
9
|
|
|
use yii\web\BadRequestHttpException; |
10
|
|
|
use yii\web\Controller; |
11
|
|
|
use yii\web\NotFoundHttpException; |
12
|
|
|
use yii\web\Response; |
13
|
|
|
use yii\widgets\ActiveForm; |
14
|
|
|
use yii2mod\comments\events\CommentEvent; |
15
|
|
|
use yii2mod\comments\models\CommentModel; |
16
|
|
|
use yii2mod\comments\traits\ModuleTrait; |
17
|
|
|
use yii2mod\editable\EditableAction; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class DefaultController |
21
|
|
|
* |
22
|
|
|
* @package yii2mod\comments\controllers |
23
|
|
|
*/ |
24
|
|
|
class DefaultController extends Controller |
25
|
|
|
{ |
26
|
|
|
use ModuleTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Event is triggered before creating a new comment. |
30
|
|
|
* Triggered with yii2mod\comments\events\CommentEvent |
31
|
|
|
*/ |
32
|
|
|
const EVENT_BEFORE_CREATE = 'beforeCreate'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Event is triggered after creating a new comment. |
36
|
|
|
* Triggered with yii2mod\comments\events\CommentEvent |
37
|
|
|
*/ |
38
|
|
|
const EVENT_AFTER_CREATE = 'afterCreate'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Event is triggered before deleting the comment. |
42
|
|
|
* Triggered with yii2mod\comments\events\CommentEvent |
43
|
|
|
*/ |
44
|
|
|
const EVENT_BEFORE_DELETE = 'beforeDelete'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Event is triggered after deleting the comment. |
48
|
|
|
* Triggered with yii2mod\comments\events\CommentEvent |
49
|
|
|
*/ |
50
|
|
|
const EVENT_AFTER_DELETE = 'afterDelete'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function actions() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'quick-edit' => [ |
59
|
|
|
'class' => EditableAction::class, |
60
|
|
|
'modelClass' => CommentModel::class, |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function behaviors() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
'access' => [ |
72
|
|
|
'class' => AccessControl::class, |
73
|
|
|
'only' => ['quick-edit', 'delete'], |
74
|
|
|
'rules' => [ |
75
|
|
|
[ |
76
|
|
|
'allow' => true, |
77
|
|
|
'roles' => ['admin'], |
78
|
|
|
], |
79
|
|
|
], |
80
|
|
|
], |
81
|
|
|
'verbs' => [ |
82
|
|
|
'class' => VerbFilter::class, |
83
|
|
|
'actions' => [ |
84
|
|
|
'create' => ['post'], |
85
|
|
|
'delete' => ['post', 'delete'], |
86
|
|
|
], |
87
|
|
|
], |
88
|
|
|
'contentNegotiator' => [ |
89
|
|
|
'class' => 'yii\filters\ContentNegotiator', |
90
|
|
|
'only' => ['create'], |
91
|
|
|
'formats' => [ |
92
|
|
|
'application/json' => Response::FORMAT_JSON, |
93
|
|
|
], |
94
|
|
|
], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create a comment. |
100
|
|
|
* |
101
|
|
|
* @param $entity string encrypt entity |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function actionCreate($entity) |
106
|
|
|
{ |
107
|
|
|
/* @var $commentModel CommentModel */ |
108
|
|
|
$commentModel = Yii::createObject($this->getModule()->commentModelClass); |
109
|
|
|
$event = Yii::createObject(['class' => CommentEvent::class, 'commentModel' => $commentModel]); |
110
|
|
|
$commentModel->setAttributes($this->getCommentAttributesFromEntity($entity)); |
111
|
|
|
$this->trigger(self::EVENT_BEFORE_CREATE, $event); |
112
|
|
|
if ($commentModel->load(Yii::$app->request->post()) && $commentModel->saveComment()) { |
113
|
|
|
$this->trigger(self::EVENT_AFTER_CREATE, $event); |
114
|
|
|
|
115
|
|
|
return ['status' => 'success']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return [ |
119
|
|
|
'status' => 'error', |
120
|
|
|
'errors' => ActiveForm::validate($commentModel), |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Delete comment. |
126
|
|
|
* |
127
|
|
|
* @param int $id Comment ID |
128
|
|
|
* |
129
|
|
|
* @return string Comment text |
130
|
|
|
*/ |
131
|
|
|
public function actionDelete($id) |
132
|
|
|
{ |
133
|
|
|
$commentModel = $this->findModel($id); |
134
|
|
|
$commentModel->setScenario(CommentModel::SCENARIO_MODERATION); |
135
|
|
|
$event = Yii::createObject(['class' => CommentEvent::class, 'commentModel' => $commentModel]); |
136
|
|
|
$this->trigger(self::EVENT_BEFORE_DELETE, $event); |
137
|
|
|
|
138
|
|
|
if ($commentModel->markRejected()) { |
139
|
|
|
$this->trigger(self::EVENT_AFTER_DELETE, $event); |
140
|
|
|
|
141
|
|
|
return Yii::t('yii2mod.comments', 'Comment has been deleted.'); |
142
|
|
|
} else { |
143
|
|
|
Yii::$app->response->setStatusCode(500); |
144
|
|
|
|
145
|
|
|
return Yii::t('yii2mod.comments', 'Comment has not been deleted. Please try again!'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Find model by ID. |
151
|
|
|
* |
152
|
|
|
* @param int|array $id Comment ID |
153
|
|
|
* |
154
|
|
|
* @return CommentModel |
155
|
|
|
* |
156
|
|
|
* @throws NotFoundHttpException |
157
|
|
|
*/ |
158
|
|
|
protected function findModel($id) |
159
|
|
|
{ |
160
|
|
|
$commentModel = $this->getModule()->commentModelClass; |
161
|
|
|
if (null !== ($model = $commentModel::findOne($id))) { |
162
|
|
|
return $model; |
163
|
|
|
} else { |
164
|
|
|
throw new NotFoundHttpException(Yii::t('yii2mod.comments', 'The requested page does not exist.')); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get list of attributes from encrypted entity |
170
|
|
|
* |
171
|
|
|
* @param $entity string encrypted entity |
172
|
|
|
* |
173
|
|
|
* @return array|mixed |
174
|
|
|
* |
175
|
|
|
* @throws BadRequestHttpException |
176
|
|
|
*/ |
177
|
|
|
protected function getCommentAttributesFromEntity($entity) |
178
|
|
|
{ |
179
|
|
|
$decryptEntity = Yii::$app->getSecurity()->decryptByKey(utf8_decode($entity), $this->getModule()->id); |
180
|
|
|
if (false !== $decryptEntity) { |
181
|
|
|
return Json::decode($decryptEntity); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
throw new BadRequestHttpException(Yii::t('yii2mod.comments', 'Oops, something went wrong. Please try again later.')); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|