1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Apps\Controller\Admin; |
5
|
|
|
|
6
|
|
|
use Apps\ActiveRecord\FeedbackAnswer; |
7
|
|
|
use Apps\Model\Admin\Feedback\FormAnswerAdd; |
8
|
|
|
use Apps\Model\Admin\Feedback\FormSettings; |
9
|
|
|
use Apps\Model\Admin\Feedback\FormUpdate; |
10
|
|
|
use Extend\Core\Arch\AdminController as Controller; |
11
|
|
|
use Ffcms\Core\App; |
12
|
|
|
use Apps\ActiveRecord\FeedbackPost; |
13
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
14
|
|
|
use Ffcms\Core\Helper\HTML\SimplePagination; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Feedback. Control and manage feedback request and answers. |
18
|
|
|
* @package Apps\Controller\Admin |
19
|
|
|
*/ |
20
|
|
|
class Feedback extends Controller |
21
|
|
|
{ |
22
|
|
|
const VERSION = '0.1'; |
23
|
|
|
const ITEM_PER_PAGE = 10; |
24
|
|
|
|
25
|
|
|
public $type = 'app'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* List feedback post messages with notifications |
29
|
|
|
* @return string |
30
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
31
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function actionIndex() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
// set current page and offset |
36
|
|
|
$page = (int)$this->request->query->get('page'); |
|
|
|
|
37
|
|
|
$offset = $page * self::ITEM_PER_PAGE; |
38
|
|
|
|
39
|
|
|
// get feedback posts AR table |
40
|
|
|
$query = new FeedbackPost(); |
41
|
|
|
|
42
|
|
|
// build pagination |
43
|
|
|
$pagination = new SimplePagination([ |
44
|
|
|
'url' => ['feedback/index'], |
45
|
|
|
'page' => $page, |
46
|
|
|
'step' => self::ITEM_PER_PAGE, |
47
|
|
|
'total' => $query->count() |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
// build listing objects |
51
|
|
|
$records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); |
52
|
|
|
|
53
|
|
|
// render output |
54
|
|
|
return $this->view->render('index', [ |
|
|
|
|
55
|
|
|
'records' => $records, |
56
|
|
|
'pagination' => $pagination |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Read feedback post and answer and add answer to thread post |
62
|
|
|
* @param int $id |
63
|
|
|
* @return string |
64
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
65
|
|
|
* @throws NotFoundException |
66
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
67
|
|
|
*/ |
68
|
|
|
public function actionRead($id) |
69
|
|
|
{ |
70
|
|
|
// find feedback post by id |
71
|
|
|
$record = FeedbackPost::find($id); |
72
|
|
View Code Duplication |
if ($record === null || $record === false) { |
|
|
|
|
73
|
|
|
throw new NotFoundException(__('The feedback message is not founded')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// initialize model with answer add if thread is not closed |
77
|
|
|
$model = null; |
78
|
|
|
if ((int)$record->closed !== 1) { |
79
|
|
|
$model = new FormAnswerAdd($record, App::$User->identity()->getId()); |
80
|
|
|
if ($model->send()) { |
81
|
|
|
if ($model->validate()) { |
82
|
|
|
$model->make(); |
83
|
|
|
App::$Session->getFlashBag()->add('success', __('New answer is successful add')); |
84
|
|
|
} else { |
85
|
|
|
App::$Session->getFlashBag()->add('error', 'Validation failure'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// render view |
91
|
|
|
return $this->view->render('read', [ |
|
|
|
|
92
|
|
|
'record' => $record, |
93
|
|
|
'model' => $model |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Edit feedback post or answer |
99
|
|
|
* @param string $type |
100
|
|
|
* @param int $id |
101
|
|
|
* @return string |
102
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
103
|
|
|
* @throws NotFoundException |
104
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
105
|
|
|
*/ |
106
|
|
|
public function actionUpdate($type, $id) |
107
|
|
|
{ |
108
|
|
|
// get active record based on type (post or answer for post) |
109
|
|
|
$record = null; |
110
|
|
|
$postId = $id; |
111
|
|
|
switch ($type) { |
112
|
|
|
case 'post': |
113
|
|
|
$record = FeedbackPost::find($id); |
114
|
|
|
break; |
115
|
|
|
case 'answer': |
116
|
|
|
$record = FeedbackAnswer::find($id); |
117
|
|
|
if ($record !== null && $record !== false) { |
118
|
|
|
$postId = (int)$record->getFeedbackPost()->id; |
119
|
|
|
} |
120
|
|
|
break; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// try what we got |
124
|
|
View Code Duplication |
if ($record === null || $record === false) { |
|
|
|
|
125
|
|
|
throw new NotFoundException(__('Feedback item is not founded')); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// initialize model |
129
|
|
|
$model = new FormUpdate($record); |
130
|
|
|
if ($model->send()) { |
131
|
|
|
if ($model->validate()) { |
132
|
|
|
$model->make(); |
133
|
|
|
App::$Session->getFlashBag()->add('success', __('Feedback item are successful changed')); |
134
|
|
|
$this->response->redirect('feedback/read/' . $postId); |
|
|
|
|
135
|
|
|
} else { |
136
|
|
|
App::$Session->getFlashBag()->add('danger', __('Updating is failed')); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// render output view |
141
|
|
|
return $this->view->render('update', [ |
|
|
|
|
142
|
|
|
'model' => $model->filter() |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Turn feedback request post - close, open, readed |
148
|
|
|
* @param string $direction |
149
|
|
|
* @param int $id |
150
|
|
|
* @return null |
151
|
|
|
* @throws NotFoundException |
152
|
|
|
*/ |
153
|
|
|
public function actionTurn($direction, $id) |
154
|
|
|
{ |
155
|
|
|
// try to find record |
156
|
|
|
$record = FeedbackPost::find($id); |
157
|
|
View Code Duplication |
if ($record === null || $record === false) { |
|
|
|
|
158
|
|
|
throw new NotFoundException(__('Feedback request with id %id% is not found', ['id' => $id])); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// switch operation direction to what we must change |
162
|
|
|
switch ($direction) { |
163
|
|
|
case 'open': |
164
|
|
|
$record->closed = 0; |
165
|
|
|
$record->save(); |
166
|
|
|
break; |
167
|
|
|
case 'close': |
168
|
|
|
$record->closed = 1; |
169
|
|
|
$record->save(); |
170
|
|
|
break; |
171
|
|
|
case 'read': |
172
|
|
|
$record->readed = 1; |
173
|
|
|
$record->save(); |
174
|
|
|
break; |
175
|
|
|
default: |
176
|
|
|
throw new NotFoundException(__('Hack attention')); |
177
|
|
|
break; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// add notification of successful changes |
181
|
|
|
App::$Session->getFlashBag()->add('success', __('Feedback request is changed!')); |
182
|
|
|
|
183
|
|
|
// redirect to feedback post read |
184
|
|
|
$this->response->redirect('feedback/read/' . $id); |
|
|
|
|
185
|
|
|
return null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Delete feedback post or answer |
190
|
|
|
* @param string $type |
191
|
|
|
* @param int $id |
192
|
|
|
* @return string |
193
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
194
|
|
|
* @throws NotFoundException |
195
|
|
|
* @throws \Exception |
196
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
197
|
|
|
*/ |
198
|
|
|
public function actionDelete($type, $id) |
199
|
|
|
{ |
200
|
|
|
// try to get active record by type |
201
|
|
|
$record = null; |
202
|
|
|
switch ($type) { |
203
|
|
|
case 'post': |
204
|
|
|
$record = FeedbackPost::find($id); |
205
|
|
|
break; |
206
|
|
|
case 'answer': |
207
|
|
|
$record = FeedbackAnswer::find($id); |
208
|
|
|
break; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// check if we get the row |
212
|
|
View Code Duplication |
if ($record === null || $record === false) { |
|
|
|
|
213
|
|
|
throw new NotFoundException(__('Feedback item is not founded')); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// if delete is submited |
217
|
|
|
if ($this->request->request->get('deleteFeedback')) { |
|
|
|
|
218
|
|
|
// remove all answers |
219
|
|
|
if ($type === 'post') { |
220
|
|
|
FeedbackAnswer::where('feedback_id', '=', $record->id)->delete(); |
221
|
|
|
// remove item |
222
|
|
|
$record->delete(); |
223
|
|
|
App::$Session->getFlashBag()->add('success', __('Feedback record is successful removed')); |
224
|
|
|
$this->response->redirect('feedback/index'); |
|
|
|
|
225
|
|
|
} else { |
226
|
|
|
// its a answer, lets remove it and redirect back in post |
227
|
|
|
$postId = $record->feedback_id; |
228
|
|
|
$record->delete(); |
229
|
|
|
$this->response->redirect('feedback/read/' . $postId); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// render view |
234
|
|
|
return $this->view->render('delete', [ |
|
|
|
|
235
|
|
|
'type' => $type, |
236
|
|
|
'record' => $record |
237
|
|
|
]); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Settings of feedback application |
242
|
|
|
* @return string |
243
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
244
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
245
|
|
|
*/ |
246
|
|
View Code Duplication |
public function actionSettings() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
// initialize model and pass configs |
249
|
|
|
$model = new FormSettings($this->getConfigs()); |
250
|
|
|
|
251
|
|
|
// check if form is submited to save data |
252
|
|
|
if ($model->send()) { |
253
|
|
|
// is validation passed? |
254
|
|
|
if ($model->validate()) { |
255
|
|
|
// save properties |
256
|
|
|
$this->setConfigs($model->getAllProperties()); |
257
|
|
|
App::$Session->getFlashBag()->add('success', __('Settings is successful updated')); |
258
|
|
|
$this->response->redirect('feedback/index'); |
|
|
|
|
259
|
|
|
} else { |
260
|
|
|
App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// render view |
265
|
|
|
return $this->view->render('settings', [ |
|
|
|
|
266
|
|
|
'model' => $model->filter() |
267
|
|
|
]); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
} |
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.