Proyectos de Subversion LeadersLinked - Services

Rev

Rev 309 | Rev 312 | 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()) {
274
            return new JsonModel([
275
                'success'   => true,
276
                'data'   => [
277
                    'name'                      => $habitSkill->name,
278
                    'description'               => $habitSkill->description,
279
                    'monday_active' 			=> $habitSkill->monday_active,
280
                    'tuesday_active' 			=> $habitSkill->tuesday_active,
281
                    'wednesday_active' 			=> $habitSkill->wednesday_active,
282
                    'thursday_active' 			=> $habitSkill->thursday_active,
283
                    'friday_active' 			=> $habitSkill->friday_active,
284
                    'saturday_active' 			=> $habitSkill->saturday_active,
285
                    'sunday_active' 			=> $habitSkill->sunday_active,
286
                    'monday_time' 				=> $habitSkill->monday_time,
287
                    'tuesday_time' 				=> $habitSkill->tuesday_time,
288
                    'wednesday_time' 			=> $habitSkill->wednesday_time,
289
                    'thursday_time' 			=> $habitSkill->thursday_time,
290
                    'friday_time' 				=> $habitSkill->friday_time,
291
                    'saturday_time' 			=> $habitSkill->saturday_time,
292
                    'sunday_time' 				=> $habitSkill->sunday_time,
293
                    'quantitative_value' 		=> $habitSkill->quantitative_value,
294
                    'qualitative_description' 	=> $habitSkill->qualitative_description,
295
                    'notification_10min_before' => $habitSkill->notification_10min_before,
296
                    'notification_30min_before' => $habitSkill->notification_30min_before,
297
                    'intelligence' 				=> $habitSkill->intelligence,
298
                ]
299
            ]);
300
        } else if($request->isPost()) {
301
            $form = new  HabitSkillForm($this->adapter);
302
            $dataPost = $request->getPost()->toArray();
303
 
304
            $form->setData($dataPost);
305
 
306
            if($form->isValid()) {
307
 
308
                $dataPost = (array) $form->getData();
309
 
310
                $hydrator = new \LeadersLinked\Hydrator\ObjectPropertyHydrator();
311
                $hydrator->hydrate($dataPost, $habitSkill);
312
 
313
 
314
 
315
                $result = $habitSkillMapper->update($habitSkill);
316
 
317
                if($result) {
318
                    $this->logger->info('Se edito el valor : ' . $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
319
 
309 www 320
 
321
 
322
                    $acl            = $this->getEvent()->getViewModel()->getVariable('acl');
323
                    $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/edit');
324
                    $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'habits/skills/delete');
325
 
304 www 326
                    $data = [
327
                        'success'   => true,
309 www 328
                        'message'   => 'LABEL_RECORD_UPDATED',
329
                        'data'      => [
310 www 330
                            'id' => $habitSkill->uuid,
309 www 331
                            'name' => $habitSkill->name,
332
                            'description' => $habitSkill->description,
333
 
334
                            'actions' => [
335
                                'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/skills/edit', ['id' => $habitSkill->uuid ], ['force_canonical' => true]) : '',
336
                                'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/skills/delete', ['id' => $habitSkill->uuid ],  ['force_canonical' => true]) : '',
337
                            ]
338
                        ]
304 www 339
                    ];
309 www 340
 
304 www 341
                } else {
342
                    $data = [
343
                        'success'   => false,
344
                        'data'      => $habitSkillMapper->getError()
345
                    ];
346
 
347
                }
348
 
349
                return new JsonModel($data);
350
 
351
 
352
            } else {
353
                $messages = [];
354
                $form_messages = (array) $form->getMessages();
355
                foreach($form_messages  as $fieldname => $field_messages)
356
                {
357
 
358
                    $messages[$fieldname] = array_values($field_messages);
359
                }
360
 
361
                return new JsonModel([
362
                    'success'   => false,
363
                    'data'   => $messages
364
                ]);
365
            }
366
 
367
        } else {
368
            return new JsonModel([
369
                'success' => false,
370
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
371
            ]);
372
        }
373
    }
374
 
375
 
376
    public function deleteAction()
377
    {
378
        $currentUserPlugin = $this->plugin('currentUserPlugin');
379
        $currentUser = $currentUserPlugin->getUser();
380
 
381
        $request = $this->getRequest();
382
        $id = $this->params()->fromRoute('id');
383
 
384
        if(!$id) {
385
            return new JsonModel([
386
                'success'   => false,
387
                'data'   => 'ERROR_INVALID_PARAMETER'
388
            ]);
389
        }
390
 
391
 
392
 
393
        $habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);
394
        $habitSkill = $habitSkillMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
395
        if(!$habitSkill) {
396
            return new JsonModel([
397
                'success'   => false,
398
                'data'   => 'ERROR_RECORD_NOT_FOUND'
399
            ]);
400
        }
401
 
402
 
403
        if($currentUser->id != $habitSkill->user_id) {
404
            $response = [
405
                'success' => false,
406
                'data' => 'ERROR_UNAUTHORIZED'
407
            ];
408
 
409
            return new JsonModel($response);
410
        }
411
 
412
 
413
        if($request->isPost()) {
414
            $result = $habitSkillMapper->delete($habitSkill);
415
            if($result) {
416
                $this->logger->info('Se borro el valor : ' .  $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
417
 
418
                return new JsonModel([
419
                    'success' => true,
420
                    'data' => 'LABEL_RECORD_DELETED'
421
                ]);
422
            } else {
423
 
424
                return new JsonModel([
425
                    'success'   => false,
426
                    'data'      => $habitSkillMapper->getError()
427
                ]);
428
 
429
            }
430
 
431
        } else {
432
            return new JsonModel([
433
                'success' => false,
434
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
435
            ]);
436
 
437
        }
438
 
439
 
440
    }
441
}