Controller.php
6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace common\modules\comment;
class Controller extends \yii\web\Controller
{
public $enableCsrfValidation = false;
public function behaviors()
{
return [
'verbs' => [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
'*' => [ 'post' ],
],
],
];
}
public function actionDelete()
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$post = \Yii::$app->request->post('Comment');
$get = \Yii::$app->request->get();
if(empty( $post[ 'comment_id' ] ) && !empty( $get[ 'comment_id' ] )) {
$post[ 'comment_id' ] = $get[ 'comment_id' ];
}
if(!empty( $post[ 'comment_id' ] )) {
if($model = \common\modules\comment\models\Comment::findOne($post[ 'comment_id' ])) {
/**
* @var \common\modules\comment\models\Comment $model
*/
$model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\Comment::SCENARIO_USER : \common\modules\comment\models\Comment::SCENARIO_GUEST;
if($model->deleteComment()) {
\Yii::$app->response->data = [ 'text' => 'Comment marked as deleted and will be check by administrators' ];
} else {
\Yii::$app->response->data = [ 'error' => $model->hasErrors('comment_id') ? $model->getFirstError('comment_id') : 'Cannot delete message' ];
}
} else {
\Yii::$app->response->data = [ 'error' => 'Comment not found' ];
};
} else {
\Yii::$app->response->data = [ 'error' => 'Missing comment_id' ];
}
\Yii::$app->response->send();
}
public function actionUpdate()
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$post = \Yii::$app->request->post();
if(!empty( $post[ 'Comment' ][ 'comment_id' ] )) {
if($model = \common\modules\comment\models\Comment::findOne($post[ 'Comment' ][ 'comment_id' ])) {
/**
* @var \common\modules\comment\models\Comment $model
*/
$model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\Comment::SCENARIO_USER : \common\modules\comment\models\Comment::SCENARIO_GUEST;
$model->load($post);
if(empty( $post[ 'Comment' ][ 'comment_pid' ] )) {
$model->comment_pid = NULL;
}
if($model->updateComment()) {
$model->rating->load($post);
if($model->rating->save()) {
return [
'result' => [
'text' => 'Comment successfully updated',
'html' => $this->renderAjax('@common/modules/comment/widgets/views/_review_comment_view', [ 'model' => $model ]),
],
];
} else {
return [
'error' => $model->hasErrors() ? $model->getFirstErrors() : 'Cannot update message',
'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-review', [
'model' => $model,
]),
];
}
} else {
return [
'error' => $model->hasErrors() ? $model->getFirstErrors() : 'Cannot update message',
'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-review', [
'model' => $model,
]),
];
}
} else {
return [ 'error' => 'Comment not found' ];
}
} else {
return [ 'error' => 'Missing comment_id' ];
}
}
public function actionForm()
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$post = \Yii::$app->request->post('Comment');
if(!empty( $post[ 'comment_id' ] )) {
$model = \common\modules\comment\models\Comment::find()
->where([ 'comment_id' => $post[ 'comment_id' ] ])
->with('parent', 'author')
->one();
if($model) {
/**
* @var \common\modules\comment\models\Comment $model
*/
$model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\Comment::SCENARIO_USER : \common\modules\comment\models\Comment::SCENARIO_GUEST;
if($model->checkUpdate()) {
return [
'result' => [
'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-review', [
'model' => $model,
]),
],
];
} else {
return [ 'error' => 'You are not able to update this comment' ];
}
} else {
return [ 'error' => 'Comment not found' ];
}
} else {
return [ 'error' => 'Missing comment_id' ];
}
}
}