Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 7218 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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