1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Front; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\FeedbackPost; |
6
|
|
|
use Apps\Model\Front\Feedback\FormAnswerAdd; |
7
|
|
|
use Apps\Model\Front\Feedback\FormFeedbackAdd; |
8
|
|
|
use Extend\Core\Arch\FrontAppController as Controller; |
9
|
|
|
use Ffcms\Core\App; |
10
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
11
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
12
|
|
|
use Ffcms\Core\Helper\Type\Any; |
13
|
|
|
use Ffcms\Core\Helper\Type\Str; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Feedback. Create, read, update and delete app for user feedback |
17
|
|
|
* @package Apps\Controller\Front |
18
|
|
|
*/ |
19
|
|
|
class Feedback extends Controller |
20
|
|
|
{ |
21
|
|
|
const ITEM_PER_PAGE = 10; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This action is not allowed there |
25
|
|
|
* @throws NotFoundException |
26
|
|
|
*/ |
27
|
|
|
public function actionIndex(): ?string |
28
|
|
|
{ |
29
|
|
|
throw new NotFoundException('Nothing there...'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add new feedback message action |
34
|
|
|
* @return string |
35
|
|
|
* @throws ForbiddenException |
36
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
37
|
|
|
*/ |
38
|
|
|
public function actionCreate(): ?string |
39
|
|
|
{ |
40
|
|
|
// get configs |
41
|
|
|
$configs = $this->getConfigs(); |
42
|
|
|
if (!App::$User->isAuth() && !(bool)$configs['guestAdd']) { |
43
|
|
|
throw new ForbiddenException(__('Feedback available only for authorized users')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// initialize model |
47
|
|
|
$model = new FormFeedbackAdd((bool)$configs['useCaptcha']); |
48
|
|
|
if ($model->send()) { |
49
|
|
|
if ($model->validate()) { |
50
|
|
|
// if validation is passed save data to db and get row |
51
|
|
|
$record = $model->make(); |
52
|
|
|
App::$Session->getFlashBag()->add('success', __('Your message was added successful')); |
53
|
|
|
$this->response->redirect('feedback/read/' . $record->id . '/' . $record->hash); |
54
|
|
|
} else { |
55
|
|
|
App::$Session->getFlashBag()->add('error', __('Message is not sended! Please, fix issues in form below')); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// render output view |
60
|
|
|
return $this->view->render('feedback/create', [ |
61
|
|
|
'model' => $model, |
62
|
|
|
'useCaptcha' => (bool)$configs['useCaptcha'] |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Read feedback message and answers and work with add answer model |
69
|
|
|
* @param string $id |
70
|
|
|
* @param string $hash |
71
|
|
|
* @return string |
72
|
|
|
* @throws ForbiddenException |
73
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
74
|
|
|
*/ |
75
|
|
|
public function actionRead(string $id, string $hash): ?string |
76
|
|
|
{ |
77
|
|
|
if (!Any::isInt($id) || Str::length($hash) < 16 || Str::length($hash) > 64) { |
78
|
|
|
throw new ForbiddenException(__('The feedback request is not founded')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// get feedback post record from database |
82
|
|
|
/** @var FeedbackPost $recordPost */ |
83
|
|
|
$recordPost = FeedbackPost::where('id', $id) |
84
|
|
|
->where('hash', $hash) |
85
|
|
|
->first(); |
86
|
|
|
|
87
|
|
|
if (!$recordPost) { |
|
|
|
|
88
|
|
|
throw new ForbiddenException(__('The feedback request is not founded')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$model = null; |
92
|
|
|
// check if feedback post is not closed for answers |
93
|
|
|
if (!(bool)$recordPost->closed) { |
94
|
|
|
// init new answer add model |
95
|
|
|
$model = new FormAnswerAdd($recordPost); |
96
|
|
|
// if answer is sender lets try to make it model |
97
|
|
|
if ($model->send() && $model->validate()) { |
98
|
|
|
$model->make(); |
99
|
|
|
App::$Session->getFlashBag()->add('success', __('Your answer was added')); |
100
|
|
|
$model->clearProperties(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// render output view |
105
|
|
|
return $this->view->render('feedback/read', [ |
106
|
|
|
'model' => $model, |
107
|
|
|
'post' => $recordPost, |
108
|
|
|
'answers' => $recordPost->answers()->get() // get feedback answers |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Close feedback request from new answers. |
114
|
|
|
* @param string $id |
115
|
|
|
* @param string $hash |
116
|
|
|
* @return string |
117
|
|
|
* @throws ForbiddenException |
118
|
|
|
*/ |
119
|
|
|
public function actionClose(string $id, string $hash): ?string |
120
|
|
|
{ |
121
|
|
|
// get feedback post record from database |
122
|
|
|
/** @var FeedbackPost $record */ |
123
|
|
|
$record = FeedbackPost::where('id', '=', $id) |
124
|
|
|
->where('hash', '=', $hash) |
125
|
|
|
->where('closed', '=', 0) |
126
|
|
|
->first(); |
127
|
|
|
|
128
|
|
|
// check does we found it |
129
|
|
|
if (!$record) { |
|
|
|
|
130
|
|
|
throw new ForbiddenException(__('The feedback request is not founded')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// check if action is submited |
134
|
|
|
if ($this->request->request->get('closeRequest', false)) { |
135
|
|
|
// if created by authorized user |
136
|
|
|
if ((int)$record->user_id > 0) { |
137
|
|
|
$user = App::$User->identity(); |
138
|
|
|
// button is pressed not by request creator |
139
|
|
|
if (!$user || $user->getId() !== (int)$record->user_id) { |
140
|
|
|
throw new ForbiddenException(__('This feedback request was created by another user')); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// switch closed to 1 and make sql query |
145
|
|
|
$record->closed = true; |
146
|
|
|
$record->save(); |
147
|
|
|
|
148
|
|
|
// add notification and redirect |
149
|
|
|
App::$Session->getFlashBag()->add('warning', __('Feedback request now is closed!')); |
150
|
|
|
$this->response->redirect('feedback/read/' . $id . '/' . $hash); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->view->render('feedback/close', [ |
154
|
|
|
'id' => (int)$id, |
155
|
|
|
'hash' => $hash |
156
|
|
|
]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* List feedback requests messages from authorized user |
161
|
|
|
* @return string |
162
|
|
|
* @throws ForbiddenException |
163
|
|
|
*/ |
164
|
|
|
public function actionList(): ?string |
165
|
|
|
{ |
166
|
|
|
// set current page and offset |
167
|
|
|
$page = (int)$this->request->query->get('page'); |
168
|
|
|
$offset = $page * self::ITEM_PER_PAGE; |
169
|
|
|
|
170
|
|
|
// check if user is authorized or throw exception |
171
|
|
|
if (!App::$User->isAuth()) { |
172
|
|
|
throw new ForbiddenException(__('Feedback listing available only for authorized users')); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// get current user object |
176
|
|
|
$user = App::$User->identity(); |
177
|
|
|
|
178
|
|
|
// initialize query with major condition |
179
|
|
|
$query = FeedbackPost::where('user_id', '=', $user->getId()); |
180
|
|
|
$totalCount = $query->count(); |
181
|
|
|
|
182
|
|
|
// build records object from prepared query using page offset |
183
|
|
|
$records = $query->orderBy('id', 'desc') |
184
|
|
|
->skip($offset) |
185
|
|
|
->take(self::ITEM_PER_PAGE) |
186
|
|
|
->get(); |
187
|
|
|
|
188
|
|
|
// render viewer with parameters |
189
|
|
|
return $this->view->render('feedback/list', [ |
190
|
|
|
'records' => $records, |
191
|
|
|
'pagination' => [ |
192
|
|
|
'step' => self::ITEM_PER_PAGE, |
193
|
|
|
'total' => $totalCount, |
194
|
|
|
'page' => $page |
195
|
|
|
] |
196
|
|
|
]); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|