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