| 9350 |
eleazar |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Controller;
|
|
|
6 |
|
|
|
7 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
8 |
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
|
|
|
9 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
10 |
use Laminas\Log\LoggerInterface;
|
|
|
11 |
use Laminas\View\Model\ViewModel;
|
|
|
12 |
use Laminas\View\Model\JsonModel;
|
|
|
13 |
use LeadersLinked\Library\Functions;
|
|
|
14 |
use LeadersLinked\Mapper\MyTrainerCategoriesMapper;
|
| 10117 |
eleazar |
15 |
use LeadersLinked\Mapper\TopicMapper;
|
| 9356 |
eleazar |
16 |
use LeadersLinked\Mapper\MyTrainerQuestionsMapper;
|
| 9350 |
eleazar |
17 |
use LeadersLinked\Form\MyTrainerQuestionForm;
|
| 9369 |
eleazar |
18 |
use LeadersLinked\Model\MyTrainerQuestions;
|
| 9350 |
eleazar |
19 |
use LeadersLinked\Mapper\CompanyMapper;
|
|
|
20 |
use LeadersLinked\Model\Company;
|
|
|
21 |
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
|
|
|
22 |
|
|
|
23 |
class MyTrainerQuestionController extends AbstractActionController {
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
*
|
|
|
27 |
* @var AdapterInterface
|
|
|
28 |
*/
|
|
|
29 |
private $adapter;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
*
|
|
|
33 |
* @var AbstractAdapter
|
|
|
34 |
*/
|
|
|
35 |
private $cache;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
*
|
|
|
39 |
* @var LoggerInterface
|
|
|
40 |
*/
|
|
|
41 |
private $logger;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
*
|
|
|
45 |
* @var array
|
|
|
46 |
*/
|
|
|
47 |
private $config;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
*
|
|
|
51 |
* @param AdapterInterface $adapter
|
|
|
52 |
* @param AbstractAdapter $cache
|
|
|
53 |
* @param LoggerInterface $logger
|
|
|
54 |
* @param array $config
|
|
|
55 |
*/
|
|
|
56 |
public function __construct($adapter, $cache, $logger, $config) {
|
|
|
57 |
$this->adapter = $adapter;
|
|
|
58 |
$this->cache = $cache;
|
|
|
59 |
$this->logger = $logger;
|
|
|
60 |
$this->config = $config;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public function indexAction() {
|
|
|
64 |
$request = $this->getRequest();
|
|
|
65 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
66 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
67 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
68 |
|
|
|
69 |
try{
|
|
|
70 |
$request = $this->getRequest();
|
|
|
71 |
if ($request->isGet()) {
|
|
|
72 |
|
|
|
73 |
$headers = $request->getHeaders();
|
|
|
74 |
|
|
|
75 |
$isJson = false;
|
|
|
76 |
if ($headers->has('Accept')) {
|
|
|
77 |
$accept = $headers->get('Accept');
|
|
|
78 |
|
|
|
79 |
$prioritized = $accept->getPrioritized();
|
|
|
80 |
|
|
|
81 |
foreach ($prioritized as $key => $value) {
|
|
|
82 |
$raw = trim($value->getRaw());
|
|
|
83 |
|
|
|
84 |
if (!$isJson) {
|
|
|
85 |
$isJson = strpos($raw, 'json');
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
if ($isJson) {
|
|
|
91 |
$search = $this->params()->fromQuery('search', []);
|
|
|
92 |
$search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
|
|
|
93 |
|
|
|
94 |
$page = intval($this->params()->fromQuery('start', 1), 10);
|
|
|
95 |
$records_x_page = intval($this->params()->fromQuery('length', 10), 10);
|
|
|
96 |
$order = $this->params()->fromQuery('order', []);
|
|
|
97 |
$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
|
|
|
98 |
$order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
|
|
|
99 |
|
|
|
100 |
$fields = ['title'];
|
|
|
101 |
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
|
|
|
102 |
|
|
|
103 |
if (!in_array($order_direction, ['ASC', 'DESC'])) {
|
|
|
104 |
$order_direction = 'ASC';
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
|
|
|
108 |
$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/add');
|
|
|
109 |
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/edit');
|
|
|
110 |
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/delete');
|
|
|
111 |
|
| 9356 |
eleazar |
112 |
$questionsMapper = MyTrainerQuestionsMapper::getInstance($this->adapter);
|
| 9350 |
eleazar |
113 |
$paginator = $questionsMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
|
|
|
114 |
|
|
|
115 |
$items = [];
|
|
|
116 |
$records = $paginator->getCurrentItems();
|
|
|
117 |
|
|
|
118 |
foreach ($records as $record) {
|
|
|
119 |
|
|
|
120 |
$item = [
|
|
|
121 |
'id' => $record->id,
|
|
|
122 |
'title' => $record->title,
|
|
|
123 |
'text' => $record->text,
|
|
|
124 |
'actions' => [
|
|
|
125 |
'link_edit' => $this->url()->fromRoute('my-trainer/question/edit', ['id' => $record->uuid]),
|
|
|
126 |
'link_delete' => $this->url()->fromRoute('my-trainer/question/delete', ['id' => $record->uuid])
|
|
|
127 |
]
|
|
|
128 |
];
|
|
|
129 |
|
|
|
130 |
array_push($items, $item);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
return new JsonModel([
|
|
|
134 |
'success' => true,
|
|
|
135 |
'data' => [
|
|
|
136 |
'items' => $items,
|
|
|
137 |
'total' => $paginator->getTotalItemCount(),
|
|
|
138 |
]
|
|
|
139 |
]);
|
|
|
140 |
} else {
|
|
|
141 |
|
| 10189 |
eleazar |
142 |
$form = new MyTrainerQuestionForm($this->adapter);
|
| 9350 |
eleazar |
143 |
|
|
|
144 |
$this->layout()->setTemplate('layout/layout-backend');
|
|
|
145 |
$viewModel = new ViewModel();
|
|
|
146 |
$viewModel->setTemplate('leaders-linked/my-trainer-question/index.phtml');
|
|
|
147 |
$viewModel->setVariable('form', $form);
|
|
|
148 |
return $viewModel;
|
|
|
149 |
}
|
|
|
150 |
} else {
|
|
|
151 |
return new JsonModel([
|
|
|
152 |
'success' => false,
|
|
|
153 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
154 |
]);
|
|
|
155 |
}
|
|
|
156 |
} catch (\Throwable $e) {
|
|
|
157 |
$e->getMessage();
|
|
|
158 |
return new JsonModel([
|
|
|
159 |
'success' => false,
|
|
|
160 |
'data' => $e
|
|
|
161 |
]);
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
public function addAction() {
|
|
|
166 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
167 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
168 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
169 |
|
| 10180 |
eleazar |
170 |
$request = $this->getRequest();
|
| 10238 |
eleazar |
171 |
try{
|
| 9350 |
eleazar |
172 |
if($request->isPost()) {
|
| 10189 |
eleazar |
173 |
$form = new MyTrainerQuestionForm($this->adapter);
|
| 9365 |
eleazar |
174 |
$dataPost = $request->getPost()->toArray();
|
| 9373 |
eleazar |
175 |
$form->setData($dataPost);
|
| 10212 |
eleazar |
176 |
|
| 9350 |
eleazar |
177 |
if($form->isValid()) {
|
| 10198 |
eleazar |
178 |
|
| 10196 |
eleazar |
179 |
$dataPost = (array) $form->getData();
|
| 9385 |
eleazar |
180 |
$hydrator = new ObjectPropertyHydrator();
|
| 9384 |
eleazar |
181 |
$questions = new MyTrainerQuestions();
|
| 9383 |
eleazar |
182 |
$hydrator->hydrate($dataPost, $questions);
|
|
|
183 |
|
| 9382 |
eleazar |
184 |
$questions->first_name = $currentUser->first_name;
|
| 9381 |
eleazar |
185 |
$questions->last_name = $currentUser->last_name;
|
|
|
186 |
$questions->company_id = $currentCompany->id;
|
| 9377 |
eleazar |
187 |
|
| 10117 |
eleazar |
188 |
$categoriesMapper = TopicMapper::getInstance($this->adapter);
|
| 9363 |
eleazar |
189 |
$category = $categoriesMapper->fetchOneByUuid($dataPost['category_id']);
|
| 10249 |
eleazar |
190 |
$questions->category_id = $category->id;
|
| 9363 |
eleazar |
191 |
|
| 9356 |
eleazar |
192 |
$questionsMapper = MyTrainerQuestionsMapper::getInstance($this->adapter);
|
| 9350 |
eleazar |
193 |
|
|
|
194 |
if($questionsMapper->insert($questions)) {
|
|
|
195 |
$questions = $questionsMapper->fetchOne($questions->id);
|
|
|
196 |
|
|
|
197 |
$this->logger->info('Se agrego la pregunta ' . $questions->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
198 |
|
|
|
199 |
$data = [
|
|
|
200 |
'success' => true,
|
|
|
201 |
'data' => 'LABEL_RECORD_ADDED'
|
|
|
202 |
];
|
|
|
203 |
} else {
|
|
|
204 |
$data = [
|
|
|
205 |
'success' => false,
|
|
|
206 |
'data' => $questionsMapper->getError()
|
|
|
207 |
];
|
|
|
208 |
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
return new JsonModel($data);
|
|
|
212 |
|
|
|
213 |
} else {
|
|
|
214 |
$messages = [];
|
|
|
215 |
$form_messages = (array) $form->getMessages();
|
|
|
216 |
foreach($form_messages as $fieldname => $field_messages)
|
|
|
217 |
{
|
|
|
218 |
|
|
|
219 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
} else {
|
|
|
225 |
$data = [
|
|
|
226 |
'success' => false,
|
|
|
227 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
228 |
];
|
|
|
229 |
|
|
|
230 |
return new JsonModel($data);
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
return new JsonModel($data);
|
| 10238 |
eleazar |
234 |
} catch (\Throwable $e) {
|
| 10247 |
eleazar |
235 |
return $e;
|
| 9350 |
eleazar |
236 |
}
|
| 10238 |
eleazar |
237 |
}
|
| 9350 |
eleazar |
238 |
|
|
|
239 |
public function editAction() {
|
|
|
240 |
$request = $this->getRequest();
|
|
|
241 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
242 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
243 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
244 |
|
|
|
245 |
$request = $this->getRequest();
|
|
|
246 |
$uuid = $this->params()->fromRoute('id');
|
|
|
247 |
try{
|
|
|
248 |
if (!$uuid) {
|
|
|
249 |
$data = [
|
|
|
250 |
'success' => false,
|
|
|
251 |
'data' => 'ERROR_INVALID_PARAMETER'
|
|
|
252 |
];
|
|
|
253 |
|
|
|
254 |
return new JsonModel($data);
|
|
|
255 |
}
|
|
|
256 |
|
| 9356 |
eleazar |
257 |
$questionsMapper = MyTrainerQuestionsMapper::getInstance($this->adapter);
|
| 9350 |
eleazar |
258 |
$questions = $questionsMapper->fetchOneByUuid($uuid);
|
|
|
259 |
|
|
|
260 |
if (!$questions) {
|
|
|
261 |
$data = [
|
|
|
262 |
'success' => false,
|
|
|
263 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
264 |
];
|
|
|
265 |
|
|
|
266 |
return new JsonModel($data);
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
if ($questions->company_id != $currentCompany->id) {
|
|
|
270 |
return new JsonModel([
|
|
|
271 |
'success' => false,
|
|
|
272 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
273 |
]);
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
if ($request->isPost()) {
|
| 10189 |
eleazar |
278 |
$form = new MyTrainerQuestionForm($this->adapter);
|
| 9350 |
eleazar |
279 |
$dataPost = $request->getPost()->toArray();
|
|
|
280 |
|
|
|
281 |
$form->setData($dataPost);
|
|
|
282 |
|
|
|
283 |
if ($form->isValid()) {
|
|
|
284 |
$dataPost = (array) $form->getData();
|
|
|
285 |
|
|
|
286 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
287 |
$hydrator->hydrate($dataPost, $questions);
|
| 10250 |
eleazar |
288 |
$categoriesMapper = TopicMapper::getInstance($this->adapter);
|
|
|
289 |
$category = $categoriesMapper->fetchOneByUuid($dataPost['category_id']);
|
|
|
290 |
$questions->category_id = $category->id;
|
| 9350 |
eleazar |
291 |
|
|
|
292 |
if($questionsMapper->update($questions)) {
|
|
|
293 |
$questions = $questionsMapper->fetchOne($questions->id);
|
|
|
294 |
|
|
|
295 |
$this->logger->info('Se modifico la pregunta ' . $questions->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
296 |
|
|
|
297 |
$data = [
|
|
|
298 |
'success' => true,
|
|
|
299 |
'data' => 'LABEL_RECORD_UPDATED'
|
|
|
300 |
];
|
|
|
301 |
} else {
|
|
|
302 |
$data = [
|
|
|
303 |
'success' => false,
|
|
|
304 |
'data' => $questionsMapper->getError()
|
|
|
305 |
];
|
|
|
306 |
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
return new JsonModel($data);
|
|
|
310 |
} else {
|
|
|
311 |
$messages = [];
|
|
|
312 |
$form_messages = (array) $form->getMessages();
|
|
|
313 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
314 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
return new JsonModel([
|
|
|
318 |
'success' => false,
|
|
|
319 |
'data' => $messages
|
|
|
320 |
]);
|
|
|
321 |
}
|
|
|
322 |
} else if ($request->isGet()) {
|
|
|
323 |
|
|
|
324 |
$hydrator = new ObjectPropertyHydrator();
|
| 10252 |
eleazar |
325 |
$categoriesMapper = TopicMapper::getInstance($this->adapter);
|
|
|
326 |
$category = $categoriesMapper->fetchOne($questions->category_id);
|
| 9350 |
eleazar |
327 |
|
|
|
328 |
$data = [
|
|
|
329 |
'success' => true,
|
|
|
330 |
'data' => [
|
|
|
331 |
'id' => $questions->uuid,
|
| 10252 |
eleazar |
332 |
'category_id' => $category->uuid,
|
| 9350 |
eleazar |
333 |
'title' => $questions->title,
|
|
|
334 |
'text' => $questions->text,
|
|
|
335 |
]
|
|
|
336 |
];
|
|
|
337 |
|
|
|
338 |
return new JsonModel($data);
|
|
|
339 |
} else {
|
|
|
340 |
$data = [
|
|
|
341 |
'success' => false,
|
|
|
342 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
343 |
];
|
|
|
344 |
|
|
|
345 |
return new JsonModel($data);
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
return new JsonModel($data);
|
|
|
349 |
} catch (\Throwable $e) {
|
|
|
350 |
$e->getMessage();
|
|
|
351 |
return new JsonModel([
|
|
|
352 |
'success' => false,
|
|
|
353 |
'data' => $e
|
|
|
354 |
]);
|
|
|
355 |
}
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
public function deleteAction() {
|
|
|
359 |
$request = $this->getRequest();
|
|
|
360 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
361 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
362 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
363 |
|
|
|
364 |
$request = $this->getRequest();
|
|
|
365 |
$uuid = $this->params()->fromRoute('id');
|
|
|
366 |
|
|
|
367 |
if (!$uuid) {
|
|
|
368 |
$data = [
|
|
|
369 |
'success' => false,
|
|
|
370 |
'data' => 'ERROR_INVALID_PARAMETER'
|
|
|
371 |
];
|
|
|
372 |
|
|
|
373 |
return new JsonModel($data);
|
|
|
374 |
}
|
|
|
375 |
|
| 9356 |
eleazar |
376 |
$questionsMapper = MyTrainerQuestionsMapper::getInstance($this->adapter);
|
| 9350 |
eleazar |
377 |
$questions = $questionsMapper->fetchOneByUuid($uuid);
|
|
|
378 |
if (!$questions) {
|
|
|
379 |
$data = [
|
|
|
380 |
'success' => false,
|
|
|
381 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
382 |
];
|
|
|
383 |
|
|
|
384 |
return new JsonModel($data);
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
if ($questions->company_id != $currentCompany->id) {
|
|
|
388 |
return new JsonModel([
|
|
|
389 |
'success' => false,
|
|
|
390 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
391 |
]);
|
|
|
392 |
}
|
|
|
393 |
|
|
|
394 |
if ($request->isPost()) {
|
|
|
395 |
|
|
|
396 |
|
|
|
397 |
$result = $questionsMapper->delete($questions->id);
|
|
|
398 |
if ($result) {
|
|
|
399 |
$this->logger->info('Se borro la pregunta ' . $questions->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
400 |
|
|
|
401 |
$data = [
|
|
|
402 |
'success' => true,
|
|
|
403 |
'data' => 'LABEL_RECORD_DELETED'
|
|
|
404 |
];
|
|
|
405 |
} else {
|
|
|
406 |
|
|
|
407 |
$data = [
|
|
|
408 |
'success' => false,
|
|
|
409 |
'data' => $questionsMapper->getError()
|
|
|
410 |
];
|
|
|
411 |
|
|
|
412 |
return new JsonModel($data);
|
|
|
413 |
}
|
|
|
414 |
} else {
|
|
|
415 |
$data = [
|
|
|
416 |
'success' => false,
|
|
|
417 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
418 |
];
|
|
|
419 |
|
|
|
420 |
return new JsonModel($data);
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
return new JsonModel($data);
|
|
|
424 |
}
|
|
|
425 |
}
|