Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17010 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
17007 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
 
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\View\Model\ViewModel;
10
use Laminas\View\Model\JsonModel;
11
 
12
use LeadersLinked\Library\Functions;
13
use LeadersLinked\Model\HabitSkill;
14
use LeadersLinked\Mapper\HabitSkillMapper;
15
use LeadersLinked\Form\Habit\HabitSkillForm;
16
 
17
 
18
class HabitSkillController extends AbstractActionController
19
{
20
    /**
21
     *
22
     * @var \Laminas\Db\Adapter\AdapterInterface
23
     */
24
    private $adapter;
25
 
26
    /**
27
     *
28
     * @var \LeadersLinked\Cache\CacheInterface
29
     */
30
    private $cache;
31
 
32
 
33
    /**
34
     *
35
     * @var \Laminas\Log\LoggerInterface
36
     */
37
    private $logger;
38
 
39
    /**
40
     *
41
     * @var array
42
     */
43
    private $config;
44
 
45
 
46
    /**
47
     *
48
     * @var \Laminas\Mvc\I18n\Translator
49
     */
50
    private $translator;
51
 
52
 
53
    /**
54
     *
55
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
56
     * @param \LeadersLinked\Cache\CacheInterface $cache
57
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
58
     * @param array $config
59
     * @param \Laminas\Mvc\I18n\Translator $translator
60
     */
61
    public function __construct($adapter, $cache, $logger, $config, $translator)
62
    {
63
        $this->adapter      = $adapter;
64
        $this->cache        = $cache;
65
        $this->logger       = $logger;
66
        $this->config       = $config;
67
        $this->translator   = $translator;
68
    }
69
 
70
    public function indexAction()
71
    {
72
 
73
        $currentUserPlugin = $this->plugin('currentUserPlugin');
74
        $currentUser = $currentUserPlugin->getUser();
75
        $currentCompany = $currentUserPlugin->getCompany();
76
 
77
        $request = $this->getRequest();
78
 
79
        if($request->isGet()) {
80
 
81
 
82
            $headers  = $request->getHeaders();
83
 
84
            $isJson = false;
85
            if($headers->has('Accept')) {
86
                $accept = $headers->get('Accept');
87
 
88
                $prioritized = $accept->getPrioritized();
89
 
90
                foreach($prioritized as $key => $value) {
91
                    $raw = trim($value->getRaw());
92
 
93
                    if(!$isJson) {
94
                        $isJson = strpos($raw, 'json');
95
                    }
96
 
97
                }
98
            }
99
 
100
            if($isJson) {
101
 
102
 
103
 
104
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
105
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/add');
106
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/edit');
107
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/delete');
108
 
109
 
110
                    $search = $this->params()->fromQuery('search');
111
                    $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
112
 
113
                    $page               = intval($this->params()->fromQuery('start', 1), 10);
114
                    $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
115
                    $order              =  $this->params()->fromQuery('order', []);
116
                    $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
117
                    $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
118
 
119
                    $fields =  ['name'];
120
                    $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
121
 
122
                    if(!in_array($order_direction, ['ASC', 'DESC'])) {
123
                        $order_direction = 'ASC';
124
                    }
125
 
126
                    $habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);
127
                    $paginator = $habitSkillMapper->fetchAllDataTableTemplates($currentCompany->id, $search,  $page, $records_x_page, $order_field, $order_direction);
128
 
129
                    $items = [];
130
                    $records = $paginator->getCurrentItems();
131
                    foreach($records as $record)
132
                    {
133
                        $item = [
134
                            'id' => $record->uuid,
135
                            'name' => $record->name,
136
                            'intelligence' => $record->intelligence,
137
                            'actions' => [
138
                                'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/skills/edit', ['id' => $record->uuid ], ['force_canonical' => true]) : '',
139
                                'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/skills/delete', ['id' =>$record->uuid ],  ['force_canonical' => true]) : '',
140
                            ]
141
                        ];
142
 
143
                        array_push($items, $item);
144
                    }
145
 
146
                    return new JsonModel([
147
                        'success' => true,
148
                        'data' => [
149
                            'items' => $items,
150
                            'total' => $paginator->getTotalItemCount(),
151
                            'link_add' => $allowAdd ? $this->url()->fromRoute('habits/skills/add', [], ['force_canonical' => true]) : '',
152
                        ]
153
                    ]);
154
            } else {
155
                $form = new HabitSkillForm();
156
 
157
                $this->layout()->setTemplate('layout/layout-backend');
158
                $viewModel = new ViewModel();
159
                $viewModel->setTemplate('leaders-linked/habits/skills.phtml');
160
                $viewModel->setVariable('form', $form);
161
                return $viewModel ;
162
            }
163
 
164
 
165
        } else {
166
            return new JsonModel([
167
                'success' => false,
168
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
169
            ]);;
170
        }
171
    }
172
 
173
 
174
 
175
    public function addAction()
176
    {
177
        $currentUserPlugin = $this->plugin('currentUserPlugin');
178
        $currentUser = $currentUserPlugin->getUser();
179
        $currentCompany = $currentUserPlugin->getCompany();
180
 
181
        $request = $this->getRequest();
182
 
183
 
184
        if($request->isPost()) {
185
            $form = new  HabitSkillForm($this->adapter);
186
            $dataPost = $request->getPost()->toArray();
187
 
188
            $form->setData($dataPost);
189
 
190
            if($form->isValid()) {
191
 
192
 
193
                $currentUserPlugin = $this->plugin('currentUserPlugin');
194
                $currentUser = $currentUserPlugin->getUser();
195
 
196
                $dataPost = (array) $form->getData();
197
 
198
 
199
                $habitSkill = new HabitSkill();
200
                $hydrator = new \LeadersLinked\Hydrator\ObjectPropertyHydrator();
201
                $hydrator->hydrate($dataPost, $habitSkill);
202
 
203
                $habitSkill->network_id         = $currentUser->network_id;
204
                $habitSkill->company_id         = $currentCompany->id;
205
                $habitSkill->monday_time        = $dataPost['monday_hour']. ':' . $dataPost['monday_minute'];
206
                $habitSkill->tuesday_time       = $dataPost['tuesday_hour']. ':' . $dataPost['tuesday_minute'];
207
                $habitSkill->wednesday_time     = $dataPost['wednesday_hour']. ':' . $dataPost['wednesday_minute'];
208
                $habitSkill->thursday_time      = $dataPost['thursday_hour']. ':' . $dataPost['thursday_minute'];
209
                $habitSkill->friday_time        = $dataPost['friday_hour']. ':' . $dataPost['friday_minute'];
210
                $habitSkill->saturday_time      = $dataPost['saturday_hour']. ':' . $dataPost['saturday_minute'];
17010 efrain 211
                $habitSkill->sunday_time        = $dataPost['monday_hour']. ':' . $dataPost['sunday_minute'];
17007 efrain 212
                $habitSkill->template           = HabitSkill::TEMPLATE_YES;
213
 
214
 
215
                $habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);
216
                $result = $habitSkillMapper->insert($habitSkill );
217
 
218
                if($result) {
219
                    $this->logger->info('Se agrego el valor : ' . $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
220
 
221
 
222
                    $habitSkill = $habitSkillMapper->fetchOne($habitSkill->id);
223
 
224
                    $acl            = $this->getEvent()->getViewModel()->getVariable('acl');
225
                    $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/edit');
226
                    $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/delete');
227
 
228
                    $data = [
229
                        'success'   => true,
230
                        'message'   => 'LABEL_RECORD_ADDED',
231
                        'data'      => [
232
                            'id' => $habitSkill->uuid,
233
                            'name' => $habitSkill->name,
234
                            'description' => $habitSkill->description,
235
 
236
                            'actions' => [
237
                                'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/skills/edit', ['id' => $habitSkill->uuid ], ['force_canonical' => true]) : '',
238
                                'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/skills/delete', ['id' => $habitSkill->uuid ],  ['force_canonical' => true]) : '',
239
                            ]
240
                        ]
241
                    ];
242
                } else {
243
                    $data = [
244
                        'success'   => false,
245
                        'data'      => $habitSkillMapper->getError()
246
                    ];
247
 
248
                }
249
 
250
                return new JsonModel($data);
251
 
252
            } else {
253
                $messages = [];
254
                $form_messages = (array) $form->getMessages();
255
                foreach($form_messages  as $fieldname => $field_messages)
256
                {
257
 
258
                    $messages[$fieldname] = array_values($field_messages);
259
                }
260
 
261
                return new JsonModel([
262
                    'success'   => false,
263
                    'data'   => $messages
264
                ]);
265
            }
266
 
267
        } else {
268
            return new JsonModel([
269
                'success' => false,
270
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
271
            ]);
272
        }
273
    }
274
 
275
    public function editAction()
276
    {
277
        $currentUserPlugin = $this->plugin('currentUserPlugin');
278
        $currentUser = $currentUserPlugin->getUser();
279
        $currentCompany = $currentUserPlugin->getCompany();
280
 
281
 
282
        $request = $this->getRequest();
283
        $id = $this->params()->fromRoute('id');
284
 
285
 
286
        if(!$id) {
287
            return new JsonModel([
288
                'success'   => false,
289
                'data'   => 'ERROR_INVALID_PARAMETER'
290
            ]);
291
 
292
        }
293
 
294
 
295
 
296
        $habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);
297
        $habitSkill = $habitSkillMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
298
        if(!$habitSkill) {
299
            return new JsonModel([
300
                'success'   => false,
301
                'data'   => 'ERROR_RECORD_NOT_FOUND'
302
            ]);
303
 
304
 
305
        }
306
 
307
 
308
        if($currentCompany->id != $habitSkill->company_id) {
309
            return new JsonModel([
310
                'success'   => false,
311
                'data'   => 'ERROR_UNAUTHORIZED'
312
            ]);
313
        }
314
 
315
        if($request->isGet()) {
316
 
317
            list($hour, $minute) = explode(':', $habitSkill->monday_time);
318
            $monday_hour    = $hour;
319
            $monday_minute  = $minute;
320
 
321
            list($hour, $minute) = explode(':', $habitSkill->tuesday_time);
322
            $tuesday_hour    = $hour;
323
            $tuesday_minute  = $minute;
324
 
325
            list($hour, $minute) = explode(':', $habitSkill->wednesday_time);
326
            $wednesday_hour    = $hour;
327
            $wednesday_minute  = $minute;
328
 
329
            list($hour, $minute) = explode(':', $habitSkill->thursday_time);
330
            $thursday_hour    = $hour;
331
            $thursday_minute  = $minute;
332
 
333
            list($hour, $minute) = explode(':', $habitSkill->friday_time);
334
            $friday_hour    = $hour;
335
            $friday_minute  = $minute;
336
 
337
            list($hour, $minute) = explode(':', $habitSkill->saturday_time);
338
            $saturday_hour    = $hour;
339
            $saturday_minute  = $minute;
340
 
341
            list($hour, $minute) = explode(':', $habitSkill->sunday_time);
342
            $sunday_hour    = $hour;
343
            $sunday_minute  = $minute;
344
 
345
            return new JsonModel([
346
 
347
                'success'   => true,
348
                'data'   => [
349
                    'name'                      => $habitSkill->name,
350
                    'description'               => $habitSkill->description,
351
                    'monday_active' 			=> $habitSkill->monday_active,
352
                    'tuesday_active' 			=> $habitSkill->tuesday_active,
353
                    'wednesday_active' 			=> $habitSkill->wednesday_active,
354
                    'thursday_active' 			=> $habitSkill->thursday_active,
355
                    'friday_active' 			=> $habitSkill->friday_active,
356
                    'saturday_active' 			=> $habitSkill->saturday_active,
357
                    'sunday_active' 			=> $habitSkill->sunday_active,
358
                    'monday_hour' 				=> $monday_hour,
359
                    'tuesday_hour' 				=> $tuesday_hour,
360
                    'wednesday_hour' 			=> $wednesday_hour,
361
                    'thursday_hour' 			=> $thursday_hour,
362
                    'friday_hour' 				=> $friday_hour,
363
                    'saturday_hour' 			=> $saturday_hour,
364
                    'sunday_hour' 				=> $sunday_hour,
365
                    'monday_minute' 	        => $monday_minute,
366
                    'tuesday_minute' 			=> $tuesday_minute,
367
                    'wednesday_minute' 			=> $wednesday_minute,
368
                    'thursday_minute' 			=> $thursday_minute,
369
                    'friday_minute' 			=> $friday_minute,
370
                    'saturday_minute' 			=> $saturday_minute,
371
                    'sunday_minute' 			=> $sunday_minute,
372
                    'quantitative_value' 		=> $habitSkill->quantitative_value,
373
                    'qualitative_description' 	=> $habitSkill->qualitative_description,
374
                    'notification_10min_before' => $habitSkill->notification_10min_before,
375
                    'notification_30min_before' => $habitSkill->notification_30min_before,
376
                    'intelligence' 				=> $habitSkill->intelligence,
377
                ]
378
            ]);
379
        } else if($request->isPost()) {
380
            $form = new  HabitSkillForm($this->adapter);
381
            $dataPost = $request->getPost()->toArray();
382
 
383
            $form->setData($dataPost);
384
 
385
            if($form->isValid()) {
386
 
387
                $dataPost = (array) $form->getData();
388
 
389
                $hydrator = new \LeadersLinked\Hydrator\ObjectPropertyHydrator();
390
                $hydrator->hydrate($dataPost, $habitSkill);
391
 
392
                $habitSkill->monday_time        = $dataPost['monday_hour']. ':' . $dataPost['monday_minute'];
393
                $habitSkill->tuesday_time       = $dataPost['tuesday_hour']. ':' . $dataPost['tuesday_minute'];
394
                $habitSkill->wednesday_time     = $dataPost['wednesday_hour']. ':' . $dataPost['wednesday_minute'];
395
                $habitSkill->thursday_time      = $dataPost['thursday_hour']. ':' . $dataPost['thursday_minute'];
396
                $habitSkill->friday_time        = $dataPost['friday_hour']. ':' . $dataPost['friday_minute'];
397
                $habitSkill->saturday_time      = $dataPost['saturday_hour']. ':' . $dataPost['saturday_minute'];
17011 efrain 398
                $habitSkill->sunday_time        = $dataPost['monday_hour']. ':' . $dataPost['sunday_minute'];
17007 efrain 399
 
400
 
401
 
402
                $result = $habitSkillMapper->update($habitSkill);
403
 
404
                if($result) {
405
                    $this->logger->info('Se edito el valor : ' . $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
406
 
407
 
408
 
409
                    $acl            = $this->getEvent()->getViewModel()->getVariable('acl');
410
                    $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/edit');
411
                    $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/delete');
412
 
413
                    $data = [
414
                        'success'   => true,
415
                        'message'   => 'LABEL_RECORD_UPDATED',
416
                        'data'      => [
417
                            'id' => $habitSkill->uuid,
418
                            'name' => $habitSkill->name,
419
                            'description' => $habitSkill->description,
420
 
421
                            'actions' => [
422
                                'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/skills/edit', ['id' => $habitSkill->uuid ], ['force_canonical' => true]) : '',
423
                                'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/skills/delete', ['id' => $habitSkill->uuid ],  ['force_canonical' => true]) : '',
424
                            ]
425
                        ]
426
                    ];
427
 
428
                } else {
429
                    $data = [
430
                        'success'   => false,
431
                        'data'      => $habitSkillMapper->getError()
432
                    ];
433
 
434
                }
435
 
436
                return new JsonModel($data);
437
 
438
 
439
            } else {
440
                $messages = [];
441
                $form_messages = (array) $form->getMessages();
442
                foreach($form_messages  as $fieldname => $field_messages)
443
                {
444
 
445
                    $messages[$fieldname] = array_values($field_messages);
446
                }
447
 
448
                return new JsonModel([
449
                    'success'   => false,
450
                    'data'   => $messages
451
                ]);
452
            }
453
 
454
        } else {
455
            return new JsonModel([
456
                'success' => false,
457
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
458
            ]);
459
        }
460
    }
461
 
462
 
463
    public function deleteAction()
464
    {
465
        $currentUserPlugin = $this->plugin('currentUserPlugin');
466
        $currentUser = $currentUserPlugin->getUser();
467
        $currentCompany = $currentUserPlugin->getCompany();
468
 
469
        $request = $this->getRequest();
470
        $id = $this->params()->fromRoute('id');
471
 
472
        if(!$id) {
473
            return new JsonModel([
474
                'success'   => false,
475
                'data'   => 'ERROR_INVALID_PARAMETER'
476
            ]);
477
        }
478
 
479
 
480
 
481
        $habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);
482
        $habitSkill = $habitSkillMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
483
        if(!$habitSkill) {
484
            return new JsonModel([
485
                'success'   => false,
486
                'data'   => 'ERROR_RECORD_NOT_FOUND'
487
            ]);
488
        }
489
 
490
 
491
        if($currentCompany->id != $habitSkill->company_id) {
492
            return new JsonModel([
493
                'success'   => false,
494
                'data'   => 'ERROR_UNAUTHORIZED'
495
            ]);
496
        }
497
 
498
 
499
        if($request->isPost()) {
500
            $result = $habitSkillMapper->delete($habitSkill);
501
            if($result) {
502
                $this->logger->info('Se borro el valor : ' .  $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
503
 
504
                return new JsonModel([
505
                    'success' => true,
506
                    'data' => 'LABEL_RECORD_DELETED'
507
                ]);
508
            } else {
509
 
510
                return new JsonModel([
511
                    'success'   => false,
512
                    'data'      => $habitSkillMapper->getError()
513
                ]);
514
 
515
            }
516
 
517
        } else {
518
            return new JsonModel([
519
                'success' => false,
520
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
521
            ]);
522
 
523
        }
524
 
525
 
526
    }
527
}