Proyectos de Subversion LeadersLinked - Services

Rev

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