Proyectos de Subversion LeadersLinked - Services

Rev

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