1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Admin; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Apps\ActiveRecord\CommentAnswer; |
7
|
|
|
use Apps\ActiveRecord\CommentPost; |
8
|
|
|
use Apps\Model\Admin\Comments\FormSettings; |
9
|
|
|
use Extend\Core\Arch\AdminController; |
10
|
|
|
use Ffcms\Core\App; |
11
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
12
|
|
|
use Ffcms\Core\Helper\HTML\SimplePagination; |
13
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
14
|
|
|
use Apps\Model\Admin\Comments\FormCommentUpdate; |
15
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
16
|
|
|
use Apps\Model\Admin\Comments\FormCommentDelete; |
17
|
|
|
use Apps\Model\Admin\Comments\FormCommentModerate; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Comments. Admin controller for management user comments. |
21
|
|
|
* This class provide general admin implementation of control for user comments and its settings. |
22
|
|
|
* @package Apps\Controller\Admin |
23
|
|
|
*/ |
24
|
|
|
class Comments extends AdminController |
25
|
|
|
{ |
26
|
|
|
const VERSION = 0.1; |
27
|
|
|
const ITEM_PER_PAGE = 10; |
28
|
|
|
|
29
|
|
|
const TYPE_COMMENT = 'comment'; |
30
|
|
|
const TYPE_ANSWER = 'answer'; |
31
|
|
|
|
32
|
|
|
public $type = 'widget'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* List user comments with pagination |
36
|
|
|
* @return string |
37
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
38
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function actionIndex() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
// set current page and offset |
43
|
|
|
$page = (int)$this->request->query->get('page'); |
|
|
|
|
44
|
|
|
$offset = $page * self::ITEM_PER_PAGE; |
45
|
|
|
|
46
|
|
|
// initialize active record model |
47
|
|
|
$query = new CommentPost(); |
48
|
|
|
|
49
|
|
|
// make pagination |
50
|
|
|
$pagination = new SimplePagination([ |
51
|
|
|
'url' => ['comments/index'], |
52
|
|
|
'page' => $page, |
53
|
|
|
'step' => self::ITEM_PER_PAGE, |
54
|
|
|
'total' => $query->count() |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
// get result as active records object with offset |
58
|
|
|
$records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); |
59
|
|
|
|
60
|
|
|
// render output view |
61
|
|
|
return $this->view->render('index', [ |
|
|
|
|
62
|
|
|
'records' => $records, |
63
|
|
|
'pagination' => $pagination |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* List comment - read comment and list answers |
69
|
|
|
* @param int $id |
70
|
|
|
* @return string |
71
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
72
|
|
|
* @throws NotFoundException |
73
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
74
|
|
|
*/ |
75
|
|
|
public function actionRead($id) |
76
|
|
|
{ |
77
|
|
|
// find object in active record model |
78
|
|
|
$record = CommentPost::find($id); |
79
|
|
|
if ($record === null || $record === false) { |
80
|
|
|
throw new NotFoundException(__('Comment is not founded')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// render response |
84
|
|
|
return $this->view->render('comment_read', [ |
|
|
|
|
85
|
|
|
'record' => $record |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Commentaries and answers edit action |
91
|
|
|
* @param string $type |
92
|
|
|
* @param int $id |
93
|
|
|
* @throws NotFoundException |
94
|
|
|
* @return string |
95
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
96
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
97
|
|
|
*/ |
98
|
|
|
public function actionEdit($type, $id) |
99
|
|
|
{ |
100
|
|
|
// get active record by type and id from active records |
101
|
|
|
$record = null; |
102
|
|
|
switch ($type) { |
103
|
|
|
case static::TYPE_COMMENT: |
104
|
|
|
$record = CommentPost::find($id); |
105
|
|
|
break; |
106
|
|
|
case static::TYPE_ANSWER: |
107
|
|
|
$record = CommentAnswer::find($id); |
108
|
|
|
break; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// check if response is not empty |
112
|
|
|
if ($record === null || $record === false) { |
113
|
|
|
throw new NotFoundException(__('Comment is not founded')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// init edit model |
117
|
|
|
$model = new FormCommentUpdate($record, $type); |
|
|
|
|
118
|
|
|
|
119
|
|
|
// check if data is submited and validated |
120
|
|
|
if ($model->send() && $model->validate()) { |
121
|
|
|
$model->make(); |
122
|
|
|
App::$Session->getFlashBag()->add('success', __('Comment or answer is successful updated')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// render view |
126
|
|
|
return $this->view->render('edit', [ |
|
|
|
|
127
|
|
|
'model' => $model->filter() |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Delete comments and answers single or multiply items |
133
|
|
|
* @param string $type |
134
|
|
|
* @param int $id |
135
|
|
|
* @return string |
136
|
|
|
* @throws NotFoundException |
137
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
138
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
139
|
|
|
*/ |
140
|
|
|
public function actionDelete($type, $id = 0) |
141
|
|
|
{ |
142
|
|
|
// sounds like a multiply delete definition |
143
|
|
View Code Duplication |
if ($id === 0 || (int)$id < 1) { |
|
|
|
|
144
|
|
|
$ids = $this->request->query->get('selected'); |
|
|
|
|
145
|
|
|
if (Obj::isArray($ids) && Arr::onlyNumericValues($ids)) { |
146
|
|
|
$id = $ids; |
147
|
|
|
} else { |
148
|
|
|
throw new NotFoundException('Bad conditions'); |
149
|
|
|
} |
150
|
|
|
} else { |
151
|
|
|
$id = [$id]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// prepare query to db |
155
|
|
|
$query = null; |
156
|
|
|
switch ($type) { |
157
|
|
|
case self::TYPE_COMMENT: |
158
|
|
|
$query = CommentPost::whereIn('id', $id); |
159
|
|
|
break; |
160
|
|
|
case self::TYPE_ANSWER: |
161
|
|
|
$query = CommentAnswer::whereIn('id', $id); |
162
|
|
|
break; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// check if result is not empty |
166
|
|
View Code Duplication |
if ($query === null || $query->count() < 1) { |
|
|
|
|
167
|
|
|
throw new NotFoundException(__('No comments found for this condition')); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// initialize model |
171
|
|
|
$model = new FormCommentDelete($query, $type); |
172
|
|
|
|
173
|
|
|
// check if delete is submited |
174
|
|
View Code Duplication |
if ($model->send() && $model->validate()) { |
|
|
|
|
175
|
|
|
$model->make(); |
176
|
|
|
App::$Session->getFlashBag()->add('success', __('Comments or answers are successful deleted!')); |
177
|
|
|
$this->response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index')); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// render view |
181
|
|
|
return $this->view->render('delete', [ |
|
|
|
|
182
|
|
|
'model' => $model |
183
|
|
|
]); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Moderate guest comments and answer - make it publish |
188
|
|
|
* @param string $type |
189
|
|
|
* @param int $id |
190
|
|
|
* @return string |
191
|
|
|
* @throws NotFoundException |
192
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
193
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
194
|
|
|
*/ |
195
|
|
|
public function actionPublish($type, $id = 0) |
196
|
|
|
{ |
197
|
|
|
// check if it multiple accept ids |
198
|
|
View Code Duplication |
if ($id === 0 || (int)$id < 1) { |
|
|
|
|
199
|
|
|
$ids = $this->request->query->get('selected'); |
|
|
|
|
200
|
|
|
if (Obj::isArray($ids) && Arr::onlyNumericValues($ids)) { |
201
|
|
|
$id = $ids; |
202
|
|
|
} else { |
203
|
|
|
throw new NotFoundException('Bad conditions'); |
204
|
|
|
} |
205
|
|
|
} else { |
206
|
|
|
$id = [$id]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// build query |
210
|
|
|
$query = null; |
211
|
|
|
switch ($type) { |
212
|
|
|
case static::TYPE_COMMENT: |
213
|
|
|
$query = CommentPost::whereIn('id', $id)->where('moderate', '=', 1); |
214
|
|
|
break; |
215
|
|
|
case static::TYPE_ANSWER: |
216
|
|
|
$query = CommentAnswer::whereIn('id', $id)->where('moderate', '=', 1); |
217
|
|
|
break; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// check if result is not empty |
221
|
|
View Code Duplication |
if ($query === null || $query->count() < 1) { |
|
|
|
|
222
|
|
|
throw new NotFoundException(__('No comments found for this condition')); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// initialize moderation model |
226
|
|
|
$model = new FormCommentModerate($query, $type); |
227
|
|
|
|
228
|
|
|
// check if form is submited |
229
|
|
View Code Duplication |
if ($model->send()) { |
|
|
|
|
230
|
|
|
$model->make(); |
231
|
|
|
App::$Session->getFlashBag()->add('success', __('Comments or answers are successful published')); |
232
|
|
|
$this->response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index')); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $this->view->render('publish', [ |
|
|
|
|
236
|
|
|
'model' => $model |
237
|
|
|
]); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* List answers |
242
|
|
|
* @return string |
243
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
244
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
245
|
|
|
*/ |
246
|
|
View Code Duplication |
public function actionAnswerlist() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
// set current page and offset |
249
|
|
|
$page = (int)$this->request->query->get('page'); |
|
|
|
|
250
|
|
|
$offset = $page * self::ITEM_PER_PAGE; |
251
|
|
|
|
252
|
|
|
// initialize ar answers model |
253
|
|
|
$query = new CommentAnswer(); |
254
|
|
|
|
255
|
|
|
// build pagination list |
256
|
|
|
$pagination = new SimplePagination([ |
257
|
|
|
'url' => ['comments/answerlist'], |
258
|
|
|
'page' => $page, |
259
|
|
|
'step' => self::ITEM_PER_PAGE, |
260
|
|
|
'total' => $query->count() |
261
|
|
|
]); |
262
|
|
|
|
263
|
|
|
// get result as active records object with offset |
264
|
|
|
$records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); |
265
|
|
|
|
266
|
|
|
// render output view |
267
|
|
|
return $this->view->render('answer_list', [ |
|
|
|
|
268
|
|
|
'records' => $records, |
269
|
|
|
'pagination' => $pagination |
270
|
|
|
]); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Comment widget global settings |
275
|
|
|
* @return string |
276
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
277
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
278
|
|
|
*/ |
279
|
|
View Code Duplication |
public function actionSettings() |
|
|
|
|
280
|
|
|
{ |
281
|
|
|
// initialize settings model |
282
|
|
|
$model = new FormSettings($this->getConfigs()); |
283
|
|
|
|
284
|
|
|
// check if form is send |
285
|
|
|
if ($model->send()) { |
286
|
|
|
if ($model->validate()) { |
287
|
|
|
$this->setConfigs($model->getAllProperties()); |
288
|
|
|
App::$Session->getFlashBag()->add('success', __('Settings is successful updated')); |
289
|
|
|
$this->response->redirect('comments/index'); |
|
|
|
|
290
|
|
|
} else { |
291
|
|
|
App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
// render view |
296
|
|
|
return $this->view->render('settings', [ |
|
|
|
|
297
|
|
|
'model' => $model->filter() |
298
|
|
|
]); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
|
303
|
|
|
|
304
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.