Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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