Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16747 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16701 efrain 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;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\FastSurveyMapper;
15
use LeadersLinked\Form\FastSurvey\FastSurveyForm;
16
use LeadersLinked\Model\FastSurvey;
17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
18
use LeadersLinked\Model\Feed;
19
use LeadersLinked\Mapper\CompanyUserMapper;
20
use LeadersLinked\Mapper\FeedMapper;
21
 
22
class FastSurveyController extends AbstractActionController {
23
 
24
    /**
25
     *
26
     * @var AdapterInterface
27
     */
28
    private $adapter;
29
 
30
    /**
31
     *
32
     * @var AbstractAdapter
33
     */
34
    private $cache;
35
 
36
    /**
37
     *
38
     * @var  LoggerInterface
39
     */
40
    private $logger;
41
 
42
    /**
43
     *
44
     * @var array
45
     */
46
    private $config;
47
 
48
    /**
49
     *
50
     * @param AdapterInterface $adapter
51
     * @param AbstractAdapter $cache
52
     * @param LoggerInterface $logger
53
     * @param array $config
54
     */
55
    public function __construct($adapter, $cache, $logger, $config) {
56
        $this->adapter = $adapter;
57
        $this->cache = $cache;
58
        $this->logger = $logger;
59
        $this->config = $config;
60
    }
61
 
62
    public function indexAction()
63
    {
64
        $currentUserPlugin = $this->plugin('currentUserPlugin');
65
        $currentUser = $currentUserPlugin->getUser();
66
        $currentCompany = $currentUserPlugin->getCompany();
67
 
68
        $request = $this->getRequest();
69
 
70
        $request = $this->getRequest();
71
        if($request->isGet()) {
72
 
73
 
74
            $headers  = $request->getHeaders();
75
 
76
            $isJson = false;
77
            if($headers->has('Accept')) {
78
                $accept = $headers->get('Accept');
79
 
80
                $prioritized = $accept->getPrioritized();
81
 
82
                foreach($prioritized as $key => $value) {
83
                    $raw = trim($value->getRaw());
84
 
85
                    if(!$isJson) {
86
                        $isJson = strpos($raw, 'json');
87
                    }
88
 
89
                }
90
            }
91
 
92
 
93
            if($isJson) {
94
 
95
 
96
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
97
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'fast-survey/edit');
98
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'fast-survey/delete');
99
                $allowResult = $acl->isAllowed($currentUser->usertype_id, 'fast-survey/result');
100
                $allowDownload = $acl->isAllowed($currentUser->usertype_id, 'fast-survey/download');
101
 
102
 
103
 
104
                $search = $this->params()->fromQuery('search');
105
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
106
 
107
                $page               = intval($this->params()->fromQuery('start', 1), 10);
108
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
109
                $order =  $this->params()->fromQuery('order', []);
110
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
111
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
112
 
113
                $fields =  ['added_on'];
114
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'added_on';
115
 
116
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
117
                    $order_direction = 'DESC';
118
                }
119
 
120
                $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
121
 
122
                $paginator = $fastSurveyMapper->fetchAllDataTable($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
123
 
124
 
125
                $items = [];
126
                $records = $paginator->getCurrentItems();
127
                foreach($records as $record)
128
                {
129
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
130
                    $added_on = $dt->format('d/m/Y h:i a');
131
 
132
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->expire_on);
133
                    $expire_on = $dt->format('d/m/Y h:i a');
134
 
135
                    $votes = $record->votes1
136
                        + $record->votes2
137
                        + $record->votes3
138
                        + $record->votes4
139
                        + $record->votes5;
140
 
141
                    switch($record->status)
142
                    {
143
                        case FastSurvey::STATUS_ACTIVE :
144
                            $status = 'LABEL_ACTIVE';
145
                            break;
146
 
147
                        default :
148
                            $status = 'LABEL_INACTIVE';
149
                            break;
150
 
151
                    }
152
 
153
 
154
                    $item = [
155
                        'question' => $record->question,
156
                        'details' => [
157
                            'status' => $status,
158
                            'added_on' => $added_on,
159
                            'expire_on' => $expire_on,
160
                        ],
161
                        'votes' => $votes,
162
                        'actions' => [
163
                            'link_edit' => $allowEdit && !$votes ?   $this->url()->fromRoute('fast-survey/edit', ['id' => $record->uuid ]) : '',
164
                            'link_delete' => $allowDelete && !$votes ? $this->url()->fromRoute('fast-survey/delete', ['id' => $record->uuid ]) : '',
165
                            'link_result' => $allowResult ? $this->url()->fromRoute('fast-survey/result', ['id' => $record->uuid ]) : '',
166
                            'link_download' => $allowDownload ? $this->url()->fromRoute('fast-survey/download', ['id' => $record->uuid ]) : '',
167
                        ],
168
 
169
                    ];
170
 
171
                    array_push($items, $item);
172
                }
173
 
174
                return new JsonModel([
175
                    'success' => true,
176
                    'data' => [
177
                        'items' => $items,
178
                        'total' => $paginator->getTotalItemCount(),
179
                    ]
180
                ]);
181
            } else  {
182
                $form = new FastSurveyForm();
183
 
184
                $this->layout()->setTemplate('layout/layout-backend');
185
                $viewModel = new ViewModel();
186
                $viewModel->setTemplate('leaders-linked/fast-survey/index.phtml');
187
                $viewModel->setVariables([
188
                    'form' => $form
189
                ]);
190
                return $viewModel ;
191
            }
192
 
193
        } else {
194
            return new JsonModel([
195
                'success' => false,
196
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
197
            ]);;
198
        }
199
    }
200
 
201
    public function addAction()
202
    {
203
        $currentUserPlugin = $this->plugin('currentUserPlugin');
204
        $currentUser = $currentUserPlugin->getUser();
205
 
206
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
207
        $currentNetwork = $currentNetworkPlugin->getNetwork();
208
 
209
        $currentCompany = $currentUserPlugin->getCompany();
210
 
211
        $request = $this->getRequest();
212
 
213
 
214
        if ($request->isPost()) {
215
 
216
 
217
            $form = new FastSurveyForm();
218
            $dataPost = $request->getPost()->toArray();
219
 
220
            $form->setData($dataPost);
221
            if ($form->isValid()) {
222
 
223
                $dataPost = (array) $form->getData();
224
 
225
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
226
                $companyUser = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
227
 
228
 
229
 
230
 
231
                $hydrator = new ObjectPropertyHydrator();
232
                $fastSurvey = new FastSurvey();
233
                $hydrator->hydrate($dataPost, $fastSurvey);
234
 
235
                $fastSurvey->network_id = $currentNetwork->id;
236
                $fastSurvey->company_id = $currentCompany->id;
237
                $fastSurvey->user_id = $companyUser->id;
238
                $fastSurvey->status = FastSurvey::STATUS_ACTIVE;
239
 
240
                $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
241
                $result = $fastSurveyMapper->insert($fastSurvey);
242
                if ($result) {
243
                    $feed = new Feed();
244
                    $feed->company_id = $currentCompany->id;
245
                    $feed->network_id = $currentNetwork->id;
246
                    $feed->user_id = $companyUser->id;
247
                    $feed->fast_survey_id = $fastSurvey->id;
248
                    $feed->type = Feed::TYPE_COMPANY;
249
                    $feed->file_type = Feed::FILE_TYPE_FAST_SURVEY;
250
                    $feed->posted_or_shared = Feed::POSTED;
251
                    $feed->status = Feed::STATUS_PUBLISHED;
252
                    $feed->title = '';
253
                    $feed->description = '';
254
                    $feed->total_comments   = 0;
255
                    $feed->total_shared     = 0;
256
 
257
                    $feed->shared_with      = Feed::SHARE_WITH_CONNECTIONS;
258
 
259
                    $feedMapper = FeedMapper::getInstance($this->adapter);
260
                    $feedMapper->insert($feed);
261
 
262
                    $response = [
263
                        'success' => true,
264
                        'data' => 'LABEL_RECORD_ADDED',
265
                    ];
266
 
267
                    $this->logger->info('Se agrego la encuesta rápida : ' . $fastSurvey->question, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
268
 
269
                } else {
270
                    $response = [
271
                        'success' => false,
272
                        'data' => $fastSurveyMapper->getError()
273
                    ];
274
                }
275
 
276
                return new JsonModel($response);
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 editAction()
303
    {
304
        $currentUserPlugin = $this->plugin('currentUserPlugin');
305
        $currentUser = $currentUserPlugin->getUser();
306
        $currentCompany = $currentUserPlugin->getCompany();
307
 
308
        $request = $this->getRequest();
309
        $id = $this->params()->fromRoute('id');
310
 
311
 
312
        if (!$id) {
313
            $data = [
314
                'success' => false,
315
                'data' => 'ERROR_INVALID_PARAMETER'
316
            ];
317
 
318
            return new JsonModel($data);
319
        }
320
 
321
        $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
322
        $fastSurvey = $fastSurveyMapper->fetchOneByUuid($id);
323
        if (!$fastSurvey) {
324
            $data = [
325
                'success' => false,
326
                'data' => 'ERROR_RECORD_NOT_FOUND'
327
            ];
328
 
329
            return new JsonModel($data);
330
        }
331
 
332
        if($fastSurvey->company_id != $currentCompany->id) {
333
            $response = [
334
                'success' => false,
335
                'data' =>  'ERROR_UNAUTHORIZED'
336
            ];
337
 
338
            return new JsonModel($response);
339
        }
340
 
341
        if($fastSurvey->votes1 > 0 || $fastSurvey->votes2 > 0|| $fastSurvey->votes3 > 0|| $fastSurvey->votes4 > 0|| $fastSurvey->votes5 > 0) {
342
            $response = [
343
                'success' => false,
344
                'data' =>  'ERROR_FAST_SURVEY_THERE_ARE_VOTES'
345
            ];
346
 
347
            return new JsonModel($response);
348
        }
349
 
350
        if ($request->isPost()) {
351
            $form = new FastSurveyForm();
352
            $dataPost = $request->getPost()->toArray();
353
 
354
            $form->setData($dataPost);
355
 
356
            if ($form->isValid()) {
357
                $dataPost = (array) $form->getData();
358
 
359
                $hydrator = new ObjectPropertyHydrator();
360
                $hydrator->hydrate($dataPost, $fastSurvey);
361
 
362
                $result = $fastSurveyMapper->update($fastSurvey);
363
                if ($result) {
364
                    $this->logger->info('Se actualizo la encuesta : ' . $fastSurvey->question, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
365
 
366
                    $data = [
367
                        'success' => true,
368
                        'data' => 'LABEL_RECORD_UPDATED'
369
                    ];
370
                } else {
371
                    $data = [
372
                        'success' => false,
373
                        'data' => $fastSurveyMapper->getError()
374
                    ];
375
                }
376
 
377
                return new JsonModel($data);
378
            } else {
379
                $messages = [];
380
                $form_messages = (array) $form->getMessages();
381
                foreach ($form_messages as $fieldname => $field_messages) {
382
                    $messages[$fieldname] = array_values($field_messages);
383
                }
384
 
385
                return new JsonModel([
386
                    'success' => false,
387
                    'data' => $messages
388
                ]);
389
            }
390
        } else if ($request->isGet()) {
391
            $hydrator = new ObjectPropertyHydrator();
392
 
393
            $data = [
394
                'success' => true,
395
                'data' => [
396
                    'question' => $fastSurvey->question,
397
                    'number_of_answers' => $fastSurvey->number_of_answers,
398
                    'answer1' => $fastSurvey->answer1,
399
                    'answer2' => $fastSurvey->answer2,
400
                    'answer3' => $fastSurvey->answer3,
401
                    'answer4' => $fastSurvey->answer4,
402
                    'answer5' => $fastSurvey->answer5,
403
                    'duration_days' => $fastSurvey->duration_days,
404
                    'duration_hours' => $fastSurvey->duration_hours,
405
                    'duration_minutes' => $fastSurvey->duration_minutes,
406
                    'status' => $fastSurvey->status,
407
                    'privacy' => $fastSurvey->privacy,
408
                    'result_type' => $fastSurvey->result_type,
409
                ]
410
            ];
411
 
412
            return new JsonModel($data);
413
        } else {
414
            $data = [
415
                'success' => false,
416
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
417
            ];
418
 
419
            return new JsonModel($data);
420
        }
421
 
422
        return new JsonModel($data);
423
    }
424
 
425
    public function deleteAction()
426
    {
427
        $currentUserPlugin = $this->plugin('currentUserPlugin');
428
        $currentUser = $currentUserPlugin->getUser();
429
        $currentCompany = $currentUserPlugin->getCompany();
430
 
431
        $request = $this->getRequest();
432
        if ($request->isPost()) {
433
 
434
            $id = $this->params()->fromRoute('id');
435
            if (!$id) {
436
                $response = [
437
                    'success' => false,
438
                    'data' => 'ERROR_INVALID_PARAMETER'
439
                ];
440
 
441
                return new JsonModel($response);
442
            }
443
 
444
 
445
 
446
            $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
447
            $fastSurvey = $fastSurveyMapper->fetchOneByUuid($id);
448
            if (!$fastSurvey) {
449
                $data = [
450
                    'success' => false,
451
                    'data' => 'ERROR_RECORD_NOT_FOUND'
452
                ];
453
 
454
                return new JsonModel($data);
455
            }
456
 
457
            if($fastSurvey->company_id != $currentCompany->id) {
458
                $response = [
459
                    'success' => false,
460
                    'data' =>  'ERROR_UNAUTHORIZED'
461
                ];
462
 
463
                return new JsonModel($response);
464
            }
465
 
466
            if($fastSurvey->votes1 > 0 || $fastSurvey->votes2 > 0|| $fastSurvey->votes3 > 0|| $fastSurvey->votes4 > 0|| $fastSurvey->votes5 > 0) {
467
                $response = [
468
                    'success' => false,
469
                    'data' =>  'ERROR_FAST_SURVEY_THERE_ARE_VOTES'
470
                ];
471
 
472
                return new JsonModel($response);
473
            }
474
 
475
 
476
            $feedMapper = FeedMapper::getInstance($this->adapter);
477
            $feedMapper->deleteByFastSurvey($fastSurvey);
478
 
479
            $result = $fastSurveyMapper->delete($fastSurvey);
480
            if ($result) {
481
 
482
 
483
 
484
                $this->logger->info('Se borro la encuesta : ' . $fastSurvey->question, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
485
 
486
                $response = [
487
                    'success' => true,
488
                    'data' => 'LABEL_RECORD_DELETED'
489
                ];
490
            } else {
491
 
492
                $response = [
493
                    'success' => false,
494
                    'data' => $fastSurveyMapper->getError()
495
                ];
496
 
497
            }
498
        } else {
499
            $response = [
500
                'success' => false,
501
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
502
            ];
503
 
504
        }
505
 
506
        return new JsonModel($response);
507
    }
508
 
509
 
510
 
511
 
512
 
513
 
514
}