Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16768 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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