Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 4990 | Rev 4993 | 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;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
4579 eleazar 11
use Laminas\View\Model\ViewModel;
4384 eleazar 12
use Laminas\View\Model\JsonModel;
4579 eleazar 13
use LeadersLinked\Mapper\SurveyMapper;
4678 eleazar 14
use LeadersLinked\Mapper\SurveyFormMapper;
4883 eleazar 15
use LeadersLinked\Mapper\SurveyJobDescriptionMapper;
16
use LeadersLinked\Mapper\SurveyIndustryMapper;
17
use LeadersLinked\Mapper\SurveyJobCategoryMapper;
18
use LeadersLinked\Mapper\SurveyLocationMapper;
19
use LeadersLinked\Mapper\SurveyServiceMapper;
4855 eleazar 20
use LeadersLinked\Model\Location;
21
use LeadersLinked\Mapper\LocationMapper;
22
use LeadersLinked\Mapper\IndustryMapper;
23
use LeadersLinked\Mapper\JobDescriptionMapper;
24
use LeadersLinked\Mapper\BehaviorMapper;
25
use LeadersLinked\Mapper\JobDescriptionBehaviorCompetencyMapper;
26
use LeadersLinked\Mapper\CompanyMapper;
27
use LeadersLinked\Mapper\ServiceMapper;
28
use LeadersLinked\Model\Company;
29
use LeadersLinked\Mapper\JobCategoryMapper;
4865 eleazar 30
use LeadersLinked\Form\SurveySegmentedForm;
4579 eleazar 31
use LeadersLinked\Form\SurveyForm;
32
use LeadersLinked\Model\Survey;
33
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
4384 eleazar 34
 
35
class SurveyController extends AbstractActionController {
36
 
37
    /**
38
     *
39
     * @var AdapterInterface
40
     */
41
    private $adapter;
42
 
43
    /**
44
     *
45
     * @var AbstractAdapter
46
     */
47
    private $cache;
48
 
49
    /**
50
     *
51
     * @var  LoggerInterface
52
     */
53
    private $logger;
54
 
55
    /**
56
     *
57
     * @var array
58
     */
59
    private $config;
60
 
61
    /**
62
     *
63
     * @param AdapterInterface $adapter
64
     * @param AbstractAdapter $cache
65
     * @param LoggerInterface $logger
66
     * @param array $config
67
     */
68
    public function __construct($adapter, $cache, $logger, $config) {
69
        $this->adapter = $adapter;
70
        $this->cache = $cache;
71
        $this->logger = $logger;
72
        $this->config = $config;
73
    }
74
 
75
    public function indexAction() {
4579 eleazar 76
        $request = $this->getRequest();
77
        $currentUserPlugin = $this->plugin('currentUserPlugin');
78
        $currentCompany = $currentUserPlugin->getCompany();
79
        $currentUser = $currentUserPlugin->getUser();
4384 eleazar 80
 
4579 eleazar 81
 
82
        $request = $this->getRequest();
83
        if ($request->isGet()) {
84
 
85
            $headers = $request->getHeaders();
86
 
87
            $isJson = false;
88
            if ($headers->has('Accept')) {
89
                $accept = $headers->get('Accept');
90
 
91
                $prioritized = $accept->getPrioritized();
92
 
93
                foreach ($prioritized as $key => $value) {
94
                    $raw = trim($value->getRaw());
95
 
96
                    if (!$isJson) {
97
                        $isJson = strpos($raw, 'json');
98
                    }
99
                }
100
            }
101
 
102
            if ($isJson) {
103
                $search = $this->params()->fromQuery('search', []);
104
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
105
 
106
                $page = intval($this->params()->fromQuery('start', 1), 10);
107
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
108
                $order = $this->params()->fromQuery('order', []);
109
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
110
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
111
 
112
                $fields = ['name'];
113
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
114
 
115
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
116
                    $order_direction = 'ASC';
117
                }
118
 
4661 eleazar 119
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
120
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'survey/add');
121
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'survey/edit');
122
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'survey/delete');
123
                $allowSegment = $acl->isAllowed($currentUser->usertype_id, 'survey/segment');
124
 
4579 eleazar 125
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
126
                $paginator = $surveyMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
127
 
128
                $items = [];
129
                $records = $paginator->getCurrentItems();
130
 
131
                foreach ($records as $record) {
4754 eleazar 132
                    $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
133
                    $surveyForm = $surveyFormMapper->fetchOne($record->form_id);
134
 
4579 eleazar 135
                    $item = [
136
                        'id' => $record->id,
137
                        'name' => $record->name,
4754 eleazar 138
                        'form' => $surveyForm->name,
4579 eleazar 139
                        'status' => $record->status,
140
                        'actions' => [
141
                            'link_edit' => $this->url()->fromRoute('survey/edit', ['id' => $record->uuid]),
142
                            'link_delete' => $this->url()->fromRoute('survey/delete', ['id' => $record->uuid]),
143
                            'link_segment' => $this->url()->fromRoute('survey/segment', ['id' => $record->uuid])
144
                        ]
145
                    ];
146
 
147
                    array_push($items, $item);
148
                }
149
 
150
                return new JsonModel([
151
                    'success' => true,
152
                    'data' => [
153
                        'items' => $items,
154
                        'total' => $paginator->getTotalItemCount(),
155
                    ]
156
                ]);
157
            } else {
158
 
159
                $form = new SurveyForm($this->adapter, $currentCompany->id);
4873 eleazar 160
                $formSegmented = new SurveySegmentedForm($this->adapter, $currentCompany->id);
4579 eleazar 161
 
162
                $this->layout()->setTemplate('layout/layout-backend');
163
                $viewModel = new ViewModel();
164
                $viewModel->setTemplate('leaders-linked/survey/index.phtml');
165
                $viewModel->setVariable('form', $form);
4873 eleazar 166
                $viewModel->setVariable('formSegmented', $formSegmented);
4579 eleazar 167
                return $viewModel;
168
            }
169
        } else {
170
            return new JsonModel([
171
                'success' => false,
172
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
173
            ]);
174
        }
4384 eleazar 175
    }
176
 
4579 eleazar 177
    public function addAction() {
178
        $request = $this->getRequest();
179
        $currentUserPlugin = $this->plugin('currentUserPlugin');
180
        $currentCompany = $currentUserPlugin->getCompany();
181
        $currentUser = $currentUserPlugin->getUser();
182
 
183
        $request = $this->getRequest();
184
 
185
        if ($request->isPost()) {
186
 
187
            $form = new SurveyForm($this->adapter, $currentCompany->id);
4672 eleazar 188
 
4579 eleazar 189
            $dataPost = $request->getPost()->toArray();
4661 eleazar 190
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
4579 eleazar 191
 
192
            $form->setData($dataPost);
193
 
194
            if ($form->isValid()) {
195
                $dataPost = (array) $form->getData();
196
 
197
                $hydrator = new ObjectPropertyHydrator();
198
                $survey = new Survey();
199
                $hydrator->hydrate($dataPost, $survey);
200
 
201
                if (!$survey->status) {
202
                    $survey->status = Survey::STATUS_INACTIVE;
203
                }
204
                $survey->company_id = $currentCompany->id;
205
 
4677 eleazar 206
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
207
                $surveyForm = $surveyFormMapper->fetchOneByUuid($dataPost['form_id']);
208
                $survey->form_id = $surveyForm->id;
4579 eleazar 209
 
4724 eleazar 210
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
211
                $result = $surveyMapper->insert($survey);
4730 eleazar 212
 
4579 eleazar 213
                if ($result) {
4734 eleazar 214
 
4728 eleazar 215
                    if ($result) {
4579 eleazar 216
 
217
                        $data = [
218
                            'success' => true,
219
                            'data' => 'LABEL_RECORD_ADDED'
220
                        ];
221
                    } else {
222
 
223
                        $data = [
224
                            'success' => false,
225
                            'data' => 'ERROR_RECORD_NOT_FOUND'
226
                        ];
227
                    }
228
                } else {
229
                    $data = [
230
                        'success' => false,
231
                        'data' => $surveyMapper->getError()
232
                    ];
233
                }
234
 
235
                return new JsonModel($data);
236
            } else {
237
                $messages = [];
238
                $form_messages = (array) $form->getMessages();
239
                foreach ($form_messages as $fieldname => $field_messages) {
240
 
241
                    $messages[$fieldname] = array_values($field_messages);
242
                }
243
 
244
                return new JsonModel([
245
                    'success' => false,
246
                    'data' => $messages
247
                ]);
248
            }
249
        } else {
250
            $data = [
251
                'success' => false,
252
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
253
            ];
254
 
255
            return new JsonModel($data);
256
        }
257
 
258
        return new JsonModel($data);
259
    }
260
 
4856 eleazar 261
    public function segmentAction() {
4855 eleazar 262
        $request = $this->getRequest();
263
        $currentUserPlugin = $this->plugin('currentUserPlugin');
264
        $currentCompany = $currentUserPlugin->getCompany();
265
        $currentUser = $currentUserPlugin->getUser();
266
 
267
        $request = $this->getRequest();
268
        $uuid = $this->params()->fromRoute('id');
269
 
270
 
271
        if (!$uuid) {
272
            $data = [
273
                'success' => false,
274
                'data' => 'ERROR_INVALID_PARAMETER'
275
            ];
276
 
277
            return new JsonModel($data);
278
        }
279
 
280
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
281
        $survey = $surveyMapper->fetchOneByUuid($uuid);
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
 
4863 eleazar 298
        if($request->isPost()){
4865 eleazar 299
            $form = new SurveySegmentedForm($this->adapter, $currentCompany->id);
4855 eleazar 300
            $dataPost = $request->getPost()->toArray();
4991 eleazar 301
            return new JsonModel([
302
                'success'   => false,
303
                'data' => $dataPost
304
            ]);
4855 eleazar 305
            $form->setData($dataPost);
306
 
307
            if ($form->isValid()) {
308
                $dataPost = (array) $form->getData();
309
 
310
                $hydrator = new ObjectPropertyHydrator();
311
 
312
                $location = new Location();
313
                $hydrator->hydrate($dataPost, $location);
314
 
315
                $locationMapper= LocationMapper::getInstance($this->adapter);
316
                $resultLocation = $locationMapper->insert($location);
317
 
318
                if (!$resultLocation) {
319
                    return new JsonModel([
320
                        'success'   => false,
321
                        'data' => 'ERROR_THERE_WAS_AN_ERROR'
322
                    ]);
323
                }
4991 eleazar 324
 
4855 eleazar 325
                $jobDescription = new SurveyJobDescription();
326
                $jobCategory = new SurveyJobCategory();
327
                $industry = new SurveyIndustry();
328
                $service = new SurveyService();
4963 eleazar 329
 
4883 eleazar 330
                if($dataPost['job_description_id']){
4985 eleazar 331
                    foreach($dataPost['job_description_id'] as $jobDescriptionId) {
332
                        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
333
                        $JobDescriptionSurveyId = $jobDescriptionMapper->fetchOneByUuid($jobDescriptionId);
334
                        $hydrator->hydrate($dataPost, $JobDescriptionSurveyId);
335
                        $survey_id = $survey->id;
336
                        $surveyJobDescriptionMapper = SurveyJobDescriptionMapper::getInstance($this->adapter);
337
                        $record = [
338
                            'survey_id' => $survey_id,
339
                            'job_description_id' => $JobDescriptionSurveyId,
340
                        ];
341
                        $record = $surveyJobDescriptionMapper->insert($JobDescriptionSurveyId, $survey_id);
4916 eleazar 342
 
4985 eleazar 343
                        if($record){
344
                            $data = [
345
                                'success' => true,
346
                                'data' => 'LABEL_RECORD_ADDED'
347
                            ];
348
                        }
4916 eleazar 349
                    }
4883 eleazar 350
                }
4855 eleazar 351
 
4916 eleazar 352
                if($dataPost['job_category_id']){
4985 eleazar 353
                    foreach($dataPost['job_category_id'] as $jobCategoryId) {
354
                        $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
355
                        $JobCategorySurveyId = $jobCategoryMapper->fetchOneByUuid($jobCategoryId);
356
                        $hydrator->hydrate($dataPost, $JobCategorySurveyId);
357
                        $survey_id = $survey->id;
358
                        $surveyJobCategoryMapper = SurveyJobCategoryMapper::getInstance($this->adapter);
359
                        $record = [
360
                            'survey_id' => $survey_id,
361
                            'job_category_id' => $JobCategorySurveyId,
362
                        ];
363
                        $record = $surveyJobCategoryMapper->insert($JobCategorySurveyId, $survey_id);
4855 eleazar 364
 
4985 eleazar 365
                        if($record){
366
                            $data = [
367
                                'success' => true,
368
                                'data' => 'LABEL_RECORD_ADDED'
369
                            ];
370
                        }
4916 eleazar 371
                    }
372
                }
4855 eleazar 373
 
4916 eleazar 374
                if($dataPost['industry_id']){
4985 eleazar 375
                    foreach($dataPost['industry_id'] as $industry_id) {
376
                        $industryMapper = IndustryMapper::getInstance($this->adapter);
377
                        $industrySurveyId = $industryMapper->fetchOneByUuid($industry_id);
378
                        $hydrator->hydrate($dataPost, $industrySurveyId);
379
                        $survey_id = $survey->id;
380
                        $surveyIndustryMapper = SurveyIndustryMapper::getInstance($this->adapter);
381
                        $record = [
382
                            'survey_id' => $survey_id,
383
                            'industry_id' => $industrySurveyId,
384
                        ];
385
                        $record = $surveyIndustryMapper->insert($record);
4855 eleazar 386
 
4985 eleazar 387
                        if($record){
388
                            $data = [
389
                                'success' => true,
390
                                'data' => 'LABEL_RECORD_ADDED'
391
                            ];
392
                        }
4916 eleazar 393
                    }
4963 eleazar 394
                }
4916 eleazar 395
 
396
                if($dataPost['service_id']){
4985 eleazar 397
                    foreach($dataPost['service_id'] as $service_id) {
398
                        $hydrator->hydrate($dataPost, $service_id);
399
                        $survey_id = $survey->id;
400
                        $surveyServiceMapper = SurveyServiceMapper::getInstance($this->adapter);
401
                        $record = [
402
                            'survey_id' => $survey_id,
403
                            'service_id' => $service_id,
404
                        ];
405
                        $record = $surveyServiceMapper->insert($record);
4855 eleazar 406
 
4985 eleazar 407
                        if($record){
408
                            $data = [
409
                                'success' => true,
410
                                'data' => 'LABEL_RECORD_ADDED'
411
                            ];
412
                        }
4916 eleazar 413
                    }
4855 eleazar 414
                }
415
 
416
                return new JsonModel($data);
417
            } else {
418
                $messages = [];
419
                $form_messages = (array) $form->getMessages();
420
                foreach ($form_messages as $fieldname => $field_messages) {
421
 
422
                    $messages[$fieldname] = array_values($field_messages);
423
                }
424
 
425
                return new JsonModel([
426
                    'success' => false,
427
                    'data' => $messages
428
                ]);
429
            }
430
        } else {
431
            $data = [
432
                'success' => false,
433
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
434
            ];
435
 
436
            return new JsonModel($data);
437
        }
438
 
439
        return new JsonModel($data);
440
 
441
    }
442
 
4579 eleazar 443
    public function editAction() {
444
        $request = $this->getRequest();
445
        $currentUserPlugin = $this->plugin('currentUserPlugin');
446
        $currentCompany = $currentUserPlugin->getCompany();
447
        $currentUser = $currentUserPlugin->getUser();
448
 
449
        $request = $this->getRequest();
450
        $uuid = $this->params()->fromRoute('id');
451
 
452
 
453
        if (!$uuid) {
454
            $data = [
455
                'success' => false,
456
                'data' => 'ERROR_INVALID_PARAMETER'
457
            ];
458
 
459
            return new JsonModel($data);
460
        }
461
 
462
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
463
        $survey = $surveyMapper->fetchOneByUuid($uuid);
464
 
465
        if (!$survey) {
466
            $data = [
467
                'success' => false,
468
                'data' => 'ERROR_RECORD_NOT_FOUND'
469
            ];
470
 
471
            return new JsonModel($data);
472
        }
473
 
474
        if ($survey->company_id != $currentCompany->id) {
475
            return new JsonModel([
476
                'success' => false,
477
                'data' => 'ERROR_UNAUTHORIZED'
478
            ]);
479
        }
480
 
481
 
482
        if ($request->isPost()) {
483
            $form = new SurveyForm();
484
            $dataPost = $request->getPost()->toArray();
485
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
486
 
487
            $form->setData($dataPost);
488
 
489
            if ($form->isValid()) {
490
                $dataPost = (array) $form->getData();
491
 
492
                $hydrator = new ObjectPropertyHydrator();
493
                $hydrator->hydrate($dataPost, $survey);
494
 
495
                if (!$survey->status) {
496
                    $survey->status = Survey::STATUS_INACTIVE;
497
                }
4749 eleazar 498
 
499
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
500
                $surveyForm = $surveyFormMapper->fetchOneByUuid($dataPost['form_id']);
501
                $survey->form_id = $surveyForm->id;
502
 
4579 eleazar 503
                $result = $surveyMapper->update($survey);
504
 
505
                if ($result) {
506
                    $this->logger->info('Se edito la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
507
                    $data = [
508
                        'success' => true,
509
                        'id' => $survey->id,
510
                        'action_edit' => $this->url()->fromRoute('survey/edit', ['id' => $survey->uuid]),
511
                        'data' => 'LABEL_RECORD_UPDATED'
512
                    ];
513
                } else {
514
                    $data = [
515
                        'success' => false,
516
                        'data' => $surveyMapper->getError()
517
                    ];
518
                }
519
 
520
                return new JsonModel($data);
521
            } else {
522
                $messages = [];
523
                $form_messages = (array) $form->getMessages();
524
                foreach ($form_messages as $fieldname => $field_messages) {
525
                    $messages[$fieldname] = array_values($field_messages);
526
                }
527
 
528
                return new JsonModel([
529
                    'success' => false,
530
                    'data' => $messages
531
                ]);
532
            }
533
        } else if ($request->isGet()) {
534
            $hydrator = new ObjectPropertyHydrator();
535
 
4749 eleazar 536
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
537
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
538
 
4579 eleazar 539
            $data = [
540
                'success' => true,
541
                'data' => [
4653 eleazar 542
                    'name' => $survey->name,
4749 eleazar 543
                    'form_id' => $surveyForm->uuid,
4653 eleazar 544
                    'target' => $survey->target,
545
                    'identity' => $survey->identity,
546
                    'since_date' => $survey->since_date,
547
                    'last_date' => $survey->last_date,
548
                    'status' => $survey->status,
4579 eleazar 549
                ]
550
            ];
551
 
552
            return new JsonModel($data);
553
        } else {
554
            $data = [
555
                'success' => false,
556
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
557
            ];
558
 
559
            return new JsonModel($data);
560
        }
561
 
562
        return new JsonModel($data);
563
    }
564
 
565
    public function deleteAction() {
566
        $request = $this->getRequest();
567
        $currentUserPlugin = $this->plugin('currentUserPlugin');
568
        $currentCompany = $currentUserPlugin->getCompany();
569
        $currentUser = $currentUserPlugin->getUser();
570
 
571
        $request = $this->getRequest();
572
        $uuid = $this->params()->fromRoute('id');
573
 
574
        if (!$uuid) {
575
            $data = [
576
                'success' => false,
577
                'data' => 'ERROR_INVALID_PARAMETER'
578
            ];
579
 
580
            return new JsonModel($data);
581
        }
582
 
583
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
584
        $survey = $surveyMapper->fetchOneByUuid($uuid);
585
        if (!$survey) {
586
            $data = [
587
                'success' => false,
588
                'data' => 'ERROR_RECORD_NOT_FOUND'
589
            ];
590
 
591
            return new JsonModel($data);
592
        }
593
 
594
        if ($survey->company_id != $currentCompany->id) {
595
            return new JsonModel([
596
                'success' => false,
597
                'data' => 'ERROR_UNAUTHORIZED'
598
            ]);
599
        }
600
 
601
        if ($request->isPost()) {
602
 
603
            $result = $surveyMapper->delete($survey->id);
604
            if ($result) {
4735 eleazar 605
                //$this->logger->info('Se borro la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
4579 eleazar 606
 
607
                $data = [
608
                    'success' => true,
609
                    'data' => 'LABEL_RECORD_DELETED'
610
                ];
611
            } else {
612
 
613
                $data = [
614
                    'success' => false,
615
                    'data' => $surveyMapper->getError()
616
                ];
617
 
618
                return new JsonModel($data);
619
            }
620
        } else {
621
            $data = [
622
                'success' => false,
623
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
624
            ];
625
 
626
            return new JsonModel($data);
627
        }
628
 
629
        return new JsonModel($data);
630
    }
631
 
4384 eleazar 632
}