Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | | 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;
16817 efrain 14
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
4384 eleazar 15
use LeadersLinked\Mapper\SurveyFormMapper;
16817 efrain 16
use LeadersLinked\Form\Survey\SurveyFormForm;
4384 eleazar 17
use LeadersLinked\Model\SurveyForm;
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);
16817 efrain 119
                $paginator = $surveyMapper->fetchAllDataTableNormalByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
4384 eleazar 120
 
121
                $items = [];
122
                $records = $paginator->getCurrentItems();
4500 eleazar 123
 
4384 eleazar 124
                foreach ($records as $record) {
4503 eleazar 125
 
4384 eleazar 126
                    $item = [
127
                        'id' => $record->id,
128
                        'name' => $record->name,
16817 efrain 129
                        'text' => $record->text,
4384 eleazar 130
                        'status' => $record->status,
131
                        'actions' => [
16817 efrain 132
                            'link_edit' => $this->url()->fromRoute('survey/form/edit', ['id' => $record->uuid]),
133
                            'link_delete' => $this->url()->fromRoute('survey/form/delete', ['id' => $record->uuid])
4384 eleazar 134
                        ]
135
                    ];
136
 
137
                    array_push($items, $item);
138
                }
139
 
140
                return new JsonModel([
141
                    'success' => true,
142
                    'data' => [
143
                        'items' => $items,
144
                        'total' => $paginator->getTotalItemCount(),
145
                    ]
146
                ]);
147
            } else {
148
 
4398 eleazar 149
                $form = new SurveyFormForm($this->adapter, $currentCompany->id);
4384 eleazar 150
 
151
                $this->layout()->setTemplate('layout/layout-backend');
152
                $viewModel = new ViewModel();
4393 eleazar 153
                $viewModel->setTemplate('leaders-linked/survey-form/index.phtml');
4384 eleazar 154
                $viewModel->setVariable('form', $form);
155
                return $viewModel;
156
            }
157
        } else {
158
            return new JsonModel([
159
                'success' => false,
160
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
161
            ]);
162
            ;
163
        }
164
    }
165
 
16817 efrain 166
    public function addAction()
167
    {
4384 eleazar 168
        $request = $this->getRequest();
169
        $currentUserPlugin = $this->plugin('currentUserPlugin');
170
        $currentCompany = $currentUserPlugin->getCompany();
171
        $currentUser = $currentUserPlugin->getUser();
172
 
173
        $request = $this->getRequest();
174
 
175
        if ($request->isPost()) {
16817 efrain 176
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
177
            $id = $this->params()->fromRoute('id');
4492 eleazar 178
 
16817 efrain 179
            if($id) {
180
                $surveyForm = $surveyFormMapper->fetchOneByUuid($id);
181
                if (!$surveyForm) {
182
                    $data = [
183
                        'success' => false,
184
                        'data' => 'ERROR_RECORD_NOT_FOUND'
185
                    ];
186
 
187
                    return new JsonModel($data);
188
                }
189
 
190
                if ($surveyForm ->company_id != $currentCompany->id) {
191
                    return new JsonModel([
192
                        'success' => false,
193
                        'data' => 'ERROR_UNAUTHORIZED'
194
                    ]);
195
                }
196
 
197
 
198
            } else {
199
                $surveyForm = new SurveyForm();
200
            }
201
 
202
 
4495 eleazar 203
            $form = new SurveyFormForm($this->adapter, $currentCompany->id);
4496 eleazar 204
 
4384 eleazar 205
            $dataPost = $request->getPost()->toArray();
206
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
207
 
208
            $form->setData($dataPost);
209
 
210
            if ($form->isValid()) {
16817 efrain 211
 
4384 eleazar 212
                $dataPost = (array) $form->getData();
16817 efrain 213
 
4384 eleazar 214
                $hydrator = new ObjectPropertyHydrator();
16817 efrain 215
                $hydrator->hydrate($dataPost, $surveyForm);
4384 eleazar 216
 
16817 efrain 217
 
218
 
219
 
220
                if($surveyForm->id) {
221
                    $result = $surveyFormMapper->update($surveyForm);
222
                } else {
223
                    $surveyForm->type = SurveyForm::TYPE_NORMAL;
224
                    $surveyForm->company_id = $currentCompany->id;
225
                    $result = $surveyFormMapper->insert($surveyForm);
226
 
4384 eleazar 227
                }
228
 
229
                if ($result) {
16817 efrain 230
 
231
                    if($surveyForm->uuid) {
232
                        $message = 'LABEL_RECORD_UPDATED';
233
 
234
                        $this->logger->info('Se actualizo el formulario' . $surveyForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
4384 eleazar 235
                    } else {
16817 efrain 236
                        $message = 'LABEL_RECORD_ADDED';
237
 
238
 
239
                        $this->logger->info('Se agrego el formulario' . $surveyForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
240
 
241
                        $surveyForm = $surveyFormMapper->fetchOne($surveyForm->id);
242
 
4384 eleazar 243
                    }
16817 efrain 244
 
245
                    $data = [
246
                        'success' => true,
247
                        'data' => [
248
                            'message' => $message,
249
                            'action' => $this->url()->fromRoute('survey/form/add', ['id'=> $surveyForm->uuid]),
250
                        ]
251
                    ];
4384 eleazar 252
                } else {
253
                    $data = [
254
                        'success' => false,
16817 efrain 255
                        'data' => $surveyFormMapper->getError()
4384 eleazar 256
                    ];
257
                }
258
 
259
                return new JsonModel($data);
260
            } else {
261
                $messages = [];
262
                $form_messages = (array) $form->getMessages();
263
                foreach ($form_messages as $fieldname => $field_messages) {
264
 
265
                    $messages[$fieldname] = array_values($field_messages);
266
                }
267
 
268
                return new JsonModel([
269
                    'success' => false,
270
                    'data' => $messages
271
                ]);
272
            }
273
        } else {
274
            $data = [
275
                'success' => false,
276
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
277
            ];
278
 
279
            return new JsonModel($data);
280
        }
281
 
282
        return new JsonModel($data);
283
    }
284
 
16817 efrain 285
    public function editAction()
286
    {
4384 eleazar 287
        $request = $this->getRequest();
288
        $currentUserPlugin = $this->plugin('currentUserPlugin');
289
        $currentCompany = $currentUserPlugin->getCompany();
290
        $currentUser = $currentUserPlugin->getUser();
291
 
292
        $request = $this->getRequest();
293
        $uuid = $this->params()->fromRoute('id');
294
 
295
 
296
        if (!$uuid) {
297
            $data = [
298
                'success' => false,
299
                'data' => 'ERROR_INVALID_PARAMETER'
300
            ];
301
 
302
            return new JsonModel($data);
303
        }
304
 
16817 efrain 305
        $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
306
        $surveyForm = $surveyFormMapper->fetchOneByUuid($uuid);
4506 eleazar 307
 
16817 efrain 308
        if (!$surveyForm) {
4384 eleazar 309
            $data = [
310
                'success' => false,
311
                'data' => 'ERROR_RECORD_NOT_FOUND'
312
            ];
313
 
314
            return new JsonModel($data);
315
        }
316
 
16817 efrain 317
        if ($surveyForm ->company_id != $currentCompany->id) {
4384 eleazar 318
            return new JsonModel([
319
                'success' => false,
320
                'data' => 'ERROR_UNAUTHORIZED'
321
            ]);
322
        }
323
 
324
 
325
        if ($request->isPost()) {
5384 eleazar 326
            $form = new SurveyFormForm($this->adapter, $currentCompany->id);
4384 eleazar 327
            $dataPost = $request->getPost()->toArray();
328
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
329
 
330
            $form->setData($dataPost);
331
 
332
            if ($form->isValid()) {
333
                $dataPost = (array) $form->getData();
334
 
335
                $hydrator = new ObjectPropertyHydrator();
16817 efrain 336
                $hydrator->hydrate($dataPost, $surveyForm);
4384 eleazar 337
 
338
 
16817 efrain 339
                $result = $surveyFormMapper->update($surveyForm);
4384 eleazar 340
 
341
                if ($result) {
16817 efrain 342
                    $this->logger->info('Se edito el formulario : ' . $surveyForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
4384 eleazar 343
                    $data = [
344
                        'success' => true,
16817 efrain 345
                        'data' => [
346
                            'message' => 'LABEL_RECORD_UPDATED',
347
                            'action' => $this->url()->fromRoute('survey/form/add', ['id'=> $surveyForm->uuid]),
348
                        ],
4384 eleazar 349
                    ];
350
                } else {
351
                    $data = [
352
                        'success' => false,
16817 efrain 353
                        'data' => $surveyFormMapper->getError()
4384 eleazar 354
                    ];
355
                }
356
 
357
                return new JsonModel($data);
358
            } else {
359
                $messages = [];
360
                $form_messages = (array) $form->getMessages();
361
                foreach ($form_messages as $fieldname => $field_messages) {
362
                    $messages[$fieldname] = array_values($field_messages);
363
                }
364
 
365
                return new JsonModel([
366
                    'success' => false,
367
                    'data' => $messages
368
                ]);
369
            }
370
        } else if ($request->isGet()) {
371
 
372
            $data = [
373
                'success' => true,
374
                'data' => [
16817 efrain 375
                    'name'          => $surveyForm->name,
376
                    'text'          => $surveyForm->text,
377
                    'status'        => $surveyForm->status,
378
                    'content'       => $surveyForm->content ? json_decode($surveyForm->content, true) : [],
4384 eleazar 379
                ]
380
            ];
381
 
382
            return new JsonModel($data);
383
        } else {
384
            $data = [
385
                'success' => false,
386
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
387
            ];
388
 
389
            return new JsonModel($data);
390
        }
391
 
392
        return new JsonModel($data);
393
    }
394
 
16817 efrain 395
    public function deleteAction()
396
    {
4384 eleazar 397
        $request = $this->getRequest();
398
        $currentUserPlugin = $this->plugin('currentUserPlugin');
399
        $currentCompany = $currentUserPlugin->getCompany();
400
        $currentUser = $currentUserPlugin->getUser();
401
 
402
        $request = $this->getRequest();
403
        $uuid = $this->params()->fromRoute('id');
404
 
405
        if (!$uuid) {
406
            $data = [
407
                'success' => false,
408
                'data' => 'ERROR_INVALID_PARAMETER'
409
            ];
410
 
411
            return new JsonModel($data);
412
        }
413
 
16817 efrain 414
        $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
415
        $surveyForm = $surveyFormMapper->fetchOneByUuid($uuid);
416
        if (!$surveyForm) {
4384 eleazar 417
            $data = [
418
                'success' => false,
419
                'data' => 'ERROR_RECORD_NOT_FOUND'
420
            ];
421
 
422
            return new JsonModel($data);
423
        }
424
 
16817 efrain 425
        if ($surveyForm->company_id != $currentCompany->id) {
4384 eleazar 426
            return new JsonModel([
427
                'success' => false,
428
                'data' => 'ERROR_UNAUTHORIZED'
429
            ]);
430
        }
431
 
432
        if ($request->isPost()) {
433
 
16817 efrain 434
            $result = $surveyFormMapper->delete($surveyForm->id);
4384 eleazar 435
            if ($result) {
16817 efrain 436
                $this->logger->info('Se borro el formulario : ' . $surveyForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
4384 eleazar 437
 
438
                $data = [
439
                    'success' => true,
440
                    'data' => 'LABEL_RECORD_DELETED'
441
                ];
442
            } else {
443
 
444
                $data = [
445
                    'success' => false,
16817 efrain 446
                    'data' => $surveyFormMapper->getError()
4384 eleazar 447
                ];
448
 
449
                return new JsonModel($data);
450
            }
451
        } else {
452
            $data = [
453
                'success' => false,
454
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
455
            ];
456
 
457
            return new JsonModel($data);
458
        }
459
 
460
        return new JsonModel($data);
461
    }
462
 
463
}