Proyectos de Subversion LeadersLinked - Services

Rev

Rev 312 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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