Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7290 eleazar 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
16768 efrain 7
 
7290 eleazar 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
use LeadersLinked\Library\Functions;
13
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
14
use LeadersLinked\Mapper\UserMapper;
15
use LeadersLinked\Mapper\CompanyMapper;
16
use LeadersLinked\Model\Company;
17
use Laminas\Hydrator\ArraySerializableHydrator;
18
use Laminas\Db\ResultSet\HydratingResultSet;
19
use LeadersLinked\Mapper\QueryMapper;
20
use Laminas\Paginator\Adapter\DbSelect;
21
use Laminas\Paginator\Paginator;
22
use Laminas\Db\Adapter\AdapterInterface;
23
use LeadersLinked\Mapper\OrganizationalClimateTestMapper;
24
use LeadersLinked\Mapper\OrganizationalClimateMapper;
25
use LeadersLinked\Mapper\OrganizationalClimateFormMapper;
26
use LeadersLinked\Form\OrganizationalClimateTestForm;
27
use LeadersLinked\Model\OrganizationalClimateTest;
7533 eleazar 28
use LeadersLinked\Library\UniqueSurveyReport;
7290 eleazar 29
 
30
class OrganizationalClimateTestController extends AbstractActionController {
31
 
32
    /**
33
     *
16769 efrain 34
     * @var \Laminas\Db\Adapter\AdapterInterface
7290 eleazar 35
     */
36
    private $adapter;
16769 efrain 37
 
7290 eleazar 38
    /**
39
     *
16769 efrain 40
     * @var \LeadersLinked\Cache\CacheInterface
7290 eleazar 41
     */
16769 efrain 42
    private $cache;
43
 
44
 
45
    /**
46
     *
47
     * @var \Laminas\Log\LoggerInterface
48
     */
7290 eleazar 49
    private $logger;
16769 efrain 50
 
7290 eleazar 51
    /**
52
     *
53
     * @var array
54
     */
55
    private $config;
16769 efrain 56
 
57
 
7290 eleazar 58
    /**
59
     *
16769 efrain 60
     * @var \Laminas\Mvc\I18n\Translator
61
     */
62
    private $translator;
63
 
64
 
65
    /**
66
     *
67
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
68
     * @param \LeadersLinked\Cache\CacheInterface $cache
69
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
7290 eleazar 70
     * @param array $config
16769 efrain 71
     * @param \Laminas\Mvc\I18n\Translator $translator
7290 eleazar 72
     */
16769 efrain 73
    public function __construct($adapter, $cache, $logger, $config, $translator)
16768 efrain 74
    {
16769 efrain 75
        $this->adapter      = $adapter;
76
        $this->cache        = $cache;
77
        $this->logger       = $logger;
78
        $this->config       = $config;
79
        $this->translator   = $translator;
7290 eleazar 80
    }
81
 
82
    public function indexAction() {
83
 
84
        $request = $this->getRequest();
85
        $currentUserPlugin = $this->plugin('currentUserPlugin');
86
        $currentCompany = $currentUserPlugin->getCompany();
87
        $currentUser = $currentUserPlugin->getUser();
88
 
7395 eleazar 89
 
7290 eleazar 90
        $request = $this->getRequest();
91
        if ($request->isGet()) {
92
 
93
            $headers = $request->getHeaders();
94
 
95
            $isJson = false;
96
            if ($headers->has('Accept')) {
97
                $accept = $headers->get('Accept');
98
 
99
                $prioritized = $accept->getPrioritized();
100
 
101
                foreach ($prioritized as $key => $value) {
102
                    $raw = trim($value->getRaw());
103
 
104
                    if (!$isJson) {
105
                        $isJson = strpos($raw, 'json');
106
                    }
107
                }
108
            }
109
 
110
            if ($isJson) {
12997 efrain 111
                $organizationalClimate_uuid = $this->params()->fromRoute('organizational_climate_id');
7290 eleazar 112
 
14613 efrain 113
                $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
114
                $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($organizationalClimate_uuid);
115
 
116
                if(!$organizationalClimate) {
7290 eleazar 117
                    return new JsonModel([
14613 efrain 118
                        'success' => false,
119
                        'data' =>  'ERROR_RECORD_NOT_FOUND'
7290 eleazar 120
                    ]);
121
                }
14613 efrain 122
 
123
 
124
                if($currentUser->company_id != $organizationalClimate->company_id) {
125
                    return new JsonModel([
126
                        'success' => false,
127
                        'data' =>  'ERROR_UNAUTHORIZED'
128
                    ]);
129
                }
130
 
7290 eleazar 131
 
14613 efrain 132
 
7290 eleazar 133
                $search = $this->params()->fromQuery('search', []);
16766 efrain 134
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
7290 eleazar 135
 
15371 efrain 136
                $start = intval($this->params()->fromQuery('start', 0), 10);
7290 eleazar 137
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
15371 efrain 138
                $page =  intval($start / $records_x_page);
139
                $page++;
140
 
7290 eleazar 141
                $order = $this->params()->fromQuery('order', []);
142
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
16766 efrain 143
                $order_direction = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
7290 eleazar 144
 
145
                $fields = ['first_name'];
146
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
147
 
148
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
149
                    $order_direction = 'ASC';
150
                }
151
 
152
                $organizationalClimateTestMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
7394 eleazar 153
                $paginator = $organizationalClimateTestMapper->fetchAllDataTableBySurveyId($organizationalClimate->id, $search, $page, $records_x_page, $order_field, $order_direction);
7290 eleazar 154
 
155
                $items = [];
156
                $records = $paginator->getCurrentItems();
157
 
158
                foreach ($records as $record) {
159
 
160
                    $item = [
161
                        'id' => $record->id,
162
                        'first_name' => $record->first_name,
11234 eleazar 163
                        'last_name' => $record->last_name,
7290 eleazar 164
                        'date' => $record->added_on,
165
 
166
                        'actions' => [
12997 efrain 167
                            'link_delete' => $this->url()->fromRoute('organizational-climate/test/delete', ['id' => $record->id, 'organizational_climate_id' => $organizationalClimate->uuid]),
168
                            'link_report' => $this->url()->fromRoute('organizational-climate/test/report', ['uuid' => $record->uuid, 'organizational_climate_id' => $organizationalClimate->uuid])
7290 eleazar 169
                        ]
170
                    ];
171
 
172
                    array_push($items, $item);
173
                }
174
 
175
                return new JsonModel([
176
                    'success' => true,
177
                    'data' => [
178
                        'items' => $items,
179
                        'total' => $paginator->getTotalItemCount(),
14613 efrain 180
                        'link_add' => $this->url()->fromRoute('organizational-climate/test/add', ['organizationalClimate_id' => $organizationalClimate->uuid]),
7290 eleazar 181
                    ]
182
                ]);
183
            } else {
184
                $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
14627 efrain 185
                $records = $organizationalClimateMapper->fetchAllByCompanyId($currentCompany->id);
7290 eleazar 186
 
14627 efrain 187
                $organizational_climates = [];
188
 
189
 
7290 eleazar 190
                $form = new OrganizationalClimateTestForm($this->adapter, $currentCompany->id);
191
 
192
                $this->layout()->setTemplate('layout/layout-backend');
193
                $viewModel = new ViewModel();
194
                $viewModel->setTemplate('leaders-linked/organizational-climate-test/index.phtml');
195
                $viewModel->setVariables([
196
                    'form'      => $form,
197
                    'organizationalClimate' => $organizationalClimate
198
                ]);
199
                return $viewModel;
200
            }
201
        } else {
202
            return new JsonModel([
203
                'success' => false,
204
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
205
            ]);
206
 
207
        }
7395 eleazar 208
 
7290 eleazar 209
    }
210
 
211
    public function addAction() {
212
        $request = $this->getRequest();
213
        $currentUserPlugin = $this->plugin('currentUserPlugin');
214
        $currentCompany = $currentUserPlugin->getCompany();
215
        $currentUser = $currentUserPlugin->getUser();
216
 
217
        $request = $this->getRequest();
218
 
12997 efrain 219
        $organizational_climate_id = $this->params()->fromRoute('organizational_climate_id');
7290 eleazar 220
 
221
        if ($request->isGet()) {
222
            $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
12997 efrain 223
            $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($organizational_climate_id);
7290 eleazar 224
 
225
            $organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
226
            $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);
227
 
228
            $data = [
229
                'organizationalClimate' => [
230
                    'id' => $organizationalClimate->uuid,
231
                    'content' => $organizationalClimateForm->content ? json_decode($organizationalClimateForm->content) : [],
232
                    'description' => $organizationalClimateForm->description,
233
                    'text' => $organizationalClimateForm->text,
234
                ],
235
            ];
236
 
237
        } else if ($request->isPost()) {
238
 
239
            $form = new OrganizationalClimateTestForm($this->adapter, $currentCompany->id);
240
 
241
            $dataPost = $request->getPost()->toArray();
242
 
243
            $form->setData($dataPost);
244
 
245
            if ($form->isValid()) {
246
                $dataPost = (array) $form->getData();
247
 
248
                $hydrator = new ObjectPropertyHydrator();
249
                $organizationalClimate = new OrganizationalClimateTest();
250
                $hydrator->hydrate($dataPost, $organizationalClimate);
251
 
252
                $organizationalClimateDoneMapper = OrganizationalClimateMapper::getInstance($this->adapter);
12997 efrain 253
                $organizationalClimateDone = $organizationalClimateDoneMapper->fetchOneByUuid($organizational_climate_id);
7290 eleazar 254
 
255
                $organizationalClimate->company_id = $currentCompany->id;
12997 efrain 256
                $organizationalClimate->organizational_climate_id = $organizationalClimateDone->id;
7290 eleazar 257
 
258
                $organizationalClimateMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
259
                $result = $organizationalClimateMapper->insert($organizationalClimate);
260
 
261
                if ($result) {
262
                    $this->logger->info('Se agrego el formulario' . $organizationalClimate->first_name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
263
 
264
                    // Get record by id
265
                    $record = $organizationalClimateMapper->fetchOne($organizationalClimate->id);
266
 
267
                    if ($record) {
268
 
269
                        $data = [
270
                            'success' => true,
271
                            'id' => $record->id,
272
                            'data' => 'LABEL_RECORD_ADDED'
273
                        ];
274
                    } else {
275
 
276
                        $data = [
277
                            'success' => false,
278
                            'data' => 'ERROR_RECORD_NOT_FOUND'
279
                        ];
280
                    }
281
                } else {
282
                    $data = [
283
                        'success' => false,
284
                        'data' => $organizationalClimateMapper->getError()
285
                    ];
286
                }
287
 
288
                return new JsonModel($data);
289
            } else {
290
                $messages = [];
291
                $form_messages = (array) $form->getMessages();
292
                foreach ($form_messages as $fieldname => $field_messages) {
293
 
294
                    $messages[$fieldname] = array_values($field_messages);
295
                }
296
 
297
                return new JsonModel([
298
                    'success' => false,
299
                    'data' => $messages
300
                ]);
301
            }
302
        } else {
303
            $data = [
304
                'success' => false,
305
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
306
            ];
307
 
308
            return new JsonModel($data);
309
        }
310
 
311
        return new JsonModel($data);
312
    }
313
 
314
    public function deleteAction() {
315
        $request = $this->getRequest();
316
        $currentUserPlugin = $this->plugin('currentUserPlugin');
317
        $currentCompany = $currentUserPlugin->getCompany();
318
        $currentUser = $currentUserPlugin->getUser();
319
        try{
320
        $request = $this->getRequest();
321
        $id = $this->params()->fromRoute('id');
322
 
323
        if (!$id) {
324
            $data = [
325
                'success' => false,
326
                'data' => 'ERROR_INVALID_PARAMETER'
327
            ];
328
 
329
            return new JsonModel($data);
330
        }
331
 
332
        $organizationalClimateTestMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
333
        $organizationalClimate = $organizationalClimateTestMapper->fetchOne($id);
334
        if (!$organizationalClimate) {
335
            $data = [
336
                'success' => false,
337
                'data' => 'ERROR_RECORD_NOT_FOUND'
338
            ];
339
 
340
            return new JsonModel($data);
341
        }
342
 
343
        if ($organizationalClimate->company_id != $currentCompany->id) {
344
            return new JsonModel([
345
                'success' => false,
346
                'data' => 'ERROR_UNAUTHORIZED'
347
            ]);
348
        }
349
 
350
        if ($request->isPost()) {
351
 
352
            $result = $organizationalClimateTestMapper->delete($organizationalClimate->id);
353
            if ($result) {
354
               // $this->logger->info('Se borro el formulario ' . $organizationalClimate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
355
 
356
                $data = [
357
                    'success' => true,
358
                    'data' => 'LABEL_RECORD_DELETED'
359
                ];
360
            } else {
361
 
362
                $data = [
363
                    'success' => false,
364
                    'data' => $organizationalClimateTestMapper->getError()
365
                ];
366
 
367
                return new JsonModel($data);
368
            }
369
        } else {
370
            $data = [
371
                'success' => false,
372
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
373
            ];
374
 
375
            return new JsonModel($data);
376
        }
377
 
378
        return new JsonModel($data);
379
        } catch (\Throwable $e) {
380
            $e->getMessage();
381
            return new JsonModel([
382
                'success' => false,
383
                'data' => $e
384
            ]);
385
        }
386
 
387
    }
388
 
389
    public function reportAction() {
390
        $request = $this->getRequest();
391
        $currentUserPlugin = $this->plugin('currentUserPlugin');
392
        $currentCompany = $currentUserPlugin->getCompany();
393
        $currentUser = $currentUserPlugin->getUser();
394
 
395
        $request = $this->getRequest();
396
        $uuid = $this->params()->fromRoute('uuid');
397
 
398
 
399
 
400
        if (!$uuid) {
401
            $data = [
402
                'success' => false,
403
                'data' => 'ERROR_INVALID_PARAMETER'
404
            ];
405
 
406
            return new JsonModel($data);
407
        }
408
 
409
        $organizationalClimateTestMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
410
        $organizationalClimateTest = $organizationalClimateTestMapper->fetchOneByuuid($uuid);
411
 
412
 
413
        $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
12997 efrain 414
        $organizationalClimate = $organizationalClimateMapper->fetchOne($organizationalClimateTest->organizational_climate_id);
7290 eleazar 415
 
416
        if (!$organizationalClimateTest) {
417
            $data = [
418
                'success' => false,
419
                'data' => 'ERROR_RECORD_NOT_FOUND'
420
            ];
421
 
422
            return new JsonModel($data);
423
        }
424
 
425
 
426
        if ($request->isGet()) {
427
            $hydrator = new ObjectPropertyHydrator();
428
 
429
            //get form data
430
            $organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
431
            $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);
432
 
433
            if ($organizationalClimateForm) {
434
 
435
                return $this->renderPDF($organizationalClimateForm, $organizationalClimateTest, $organizationalClimate);
436
            } else {
437
 
438
                $data = [
439
                    'success' => false,
440
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
441
                ];
442
 
443
                return new JsonModel($data);
444
            }
445
        } else {
446
            $data = [
447
                'success' => false,
448
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
449
            ];
450
 
451
            return new JsonModel($data);
452
        }
453
 
454
        return new JsonModel($data);
455
    }
456
 
457
    public function renderPDF($organizationalClimateForm, $organizationalClimateTest, $organizationalClimate) {
458
 
459
        $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $organizationalClimate->uuid;
460
 
461
 
462
        if(file_exists($target_path)) {
463
            Functions::deleteFiles($target_path);
464
        } else {
465
            @mkdir($target_path, 0755, true);
466
        }
467
 
468
        // Set Data
16768 efrain 469
        $headerFormName = Functions::utf8_decode($organizationalClimateForm->name);
470
        $headerOrganizationalClimateName = Functions::utf8_decode('Informe de Encuesta: ' . trim($organizationalClimate->name) . ' al ' . date("m-d-Y H:i:s", strtotime($organizationalClimate->added_on)));
7290 eleazar 471
        $sections = json_decode($organizationalClimateForm->content, true);
472
 
473
        $test = json_decode($organizationalClimateTest->content, true);
474
 
475
        //Generate New PDF
476
        $pdf = new UniqueSurveyReport();
477
 
478
        $pdf->AliasNbPages();
479
        $pdf->AddPage();
480
 
481
        // Set header secundary
482
        $pdf->customHeader($headerFormName, $headerOrganizationalClimateName);
483
 
484
        $countSection = 0;
485
 
486
        for ($i = 0; $i < count($sections); $i++) {
487
            if ($countSection > 1) {
488
                $countSection = 0;
489
                $pdf->AddPage();
490
                $pdf->customHeader($headerFormName);
491
            }
492
            $section = $sections[$i];
493
 
494
            foreach ($section['questions'] as $question) {
495
 
496
                    switch ($question['type']) {
497
                        case 'simple':
498
                            $options = [];
499
 
500
                            foreach($question['options'] as $option) {
501
                                $options []= [
502
                                    'text' => strip_tags($option['text']),
503
                                    'selected' => $option['slug_option'] == $test[$question['slug_question']],
504
                                ];
505
                            }
506
 
507
                            $pdf->optionTable(strip_tags($question['text']), $options);
508
 
509
                            break;
510
 
511
                        case 'multiple':
512
                            $options = [];
513
 
514
                            foreach($question['options'] as $option) {
515
                                $options []= [
516
                                    'text' => strip_tags($option['text']),
517
                                    'selected' => in_array($option['slug_option'], $test[$question['slug_question']]),
518
                                ];
519
                            }
520
 
521
                            $pdf->optionTable(strip_tags($question['text']), $options);
522
 
523
                            break;
524
                        case 'open':
525
                            $pdf->borderTable(strip_tags($question['text']), [['content' => $test[$question['slug_question']]]]);
526
 
527
 
528
                            break;
529
                    }
530
                $countSection++;
531
            }
532
 
533
 
534
        }
535
 
536
        return $pdf->Output();
537
    }
538
 
539
 
540
}