Proyectos de Subversion LeadersLinked - Services

Rev

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

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