Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
308 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\HabitGoal;
14
use LeadersLinked\Mapper\HabitGoalMapper;
15
use LeadersLinked\Form\Habit\HabitGoalForm;
16
 
17
 
18
class HabitGoalController 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
        if($request->isGet()) {
78
            $currentUserPlugin = $this->plugin('currentUserPlugin');
79
            $currentUser = $currentUserPlugin->getUser();
80
 
81
            $acl = $this->getEvent()->getViewModel()->getVariable('acl');
82
            $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/add');
83
            $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/edit');
84
            $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/delete');
85
 
86
 
87
                $search = $this->params()->fromQuery('search');
88
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
89
 
90
                $page               = intval($this->params()->fromQuery('start', 1), 10);
91
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
92
                $order              =  $this->params()->fromQuery('order', []);
93
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
94
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
95
 
96
                $fields =  ['name'];
97
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
98
 
99
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
100
                    $order_direction = 'ASC';
101
                }
102
 
103
                $habitGoalMapper = HabitGoalMapper::getInstance($this->adapter);
104
                $paginator = $habitGoalMapper->fetchAllDataTable($currentUser->id, $search,  $page, $records_x_page, $order_field, $order_direction);
105
 
106
                $items = [];
107
                $records = $paginator->getCurrentItems();
108
                foreach($records as $record)
109
                {
110
                    $item = [
337 www 111
                        'id' => $record->uuid,
308 www 112
                        'name' => $record->name,
113
                        'description' => $record->description,
114
                        'actions' => [
115
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/goals/edit', ['id' => $record->uuid ], ['force_canonical' => true]) : '',
116
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/goals/delete', ['id' =>$record->uuid ],  ['force_canonical' => true]) : '',
117
                        ]
118
                    ];
119
 
120
                    array_push($items, $item);
121
                }
122
 
123
                return new JsonModel([
124
                    'success' => true,
125
                    'data' => [
126
                        'items' => $items,
127
                        'total' => $paginator->getTotalItemCount(),
128
                        'link_add' => $allowAdd ? $this->url()->fromRoute('habits/goals/add', [], ['force_canonical' => true]) : '',
129
                    ]
130
                ]);
131
 
132
 
133
        } else {
134
            return new JsonModel([
135
                'success' => false,
136
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
137
            ]);;
138
        }
139
    }
140
 
141
 
142
    public function addAction()
143
    {
144
        $request = $this->getRequest();
145
 
146
        if($request->isGet()) {
147
 
148
 
149
            $currentUserPlugin = $this->plugin('currentUserPlugin');
150
            $currentUser = $currentUserPlugin->getUser();
151
 
152
            $skills = [];
153
            $habitSkillMapper = \LeadersLinked\Mapper\HabitSkillMapper::getInstance($this->adapter);
154
            $records = $habitSkillMapper->fetchAllByUserId($currentUser->id);
155
            foreach($records as $record)
156
            {
157
                $skills[ $record->uuid ] = $record->name;
158
            }
159
 
160
            return new JsonModel([
161
                'success'   => true,
162
                'data'   => $skills
163
            ]);
164
 
165
 
166
        } else if($request->isPost()) {
167
            $currentUserPlugin = $this->plugin('currentUserPlugin');
168
            $currentUser = $currentUserPlugin->getUser();
169
 
170
            $form = new  HabitGoalForm($this->adapter, $currentUser->id);
171
            $dataPost = $request->getPost()->toArray();
172
 
173
 
174
            $form->setData($dataPost);
175
 
176
            if($form->isValid()) {
177
 
178
                $dataPost = (array) $form->getData();
179
 
180
 
181
 
182
 
183
                $habitGoal = new HabitGoal();
184
                $habitGoal->network_id  = $currentUser->network_id;
185
                $habitGoal->user_id     = $currentUser->id;
186
                $habitGoal->name        = $dataPost['name'];
187
                $habitGoal->description = $dataPost['description'];
188
                $habitGoal->value       = $dataPost['value'];
189
                $habitGoal->start_date  = $dataPost['start_date'];
190
                $habitGoal->end_date    = $dataPost['end_date'];
191
 
192
 
193
                $habitGoalMapper = \LeadersLinked\Mapper\HabitGoalMapper::getInstance($this->adapter);
194
                $result = $habitGoalMapper->insert($habitGoal );
195
 
196
                if($result) {
197
 
198
                    if(!empty($dataPost['skill_id'])) {
199
                        $habitSkillMapper       = \LeadersLinked\Mapper\HabitSkillMapper::getInstance($this->adapter);
200
                        $habitGoalSkillMapper   = \LeadersLinked\Mapper\HabitGoalSkillMapper::getInstance($this->adapter);
201
 
202
                        foreach($dataPost['skill_id'] as $uuid)
203
                        {
204
                            $habitSkill = $habitSkillMapper->fetchOneByUuid($uuid);
205
                            if($habitSkill->user_id == $currentUser->id) {
206
                                $habitGoalSkill = new \LeadersLinked\Model\HabitGoalSkill();
207
                                $habitGoalSkill->network_id = $currentUser->network_id;
208
                                $habitGoalSkill->user_id    = $currentUser->id;
209
                                $habitGoalSkill->goal_id    = $habitGoal->id;
210
                                $habitGoalSkill->skill_id   = $habitGoal->id;
211
 
212
 
213
 
214
 
215
                                $habitGoalSkillMapper->insert($habitGoalSkill);
216
 
217
                            }
218
                        }
219
                    }
220
 
221
 
222
 
223
 
224
                    $this->logger->info('Se agrego el valor : ' . $habitGoal->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
225
 
309 www 226
                    $habitGoal = $habitGoalMapper->fetchOne($habitGoal->id);
227
 
228
                    $acl            = $this->getEvent()->getViewModel()->getVariable('acl');
229
                    $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/edit');
230
                    $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/delete');
231
 
308 www 232
                    $data = [
233
                        'success'   => true,
309 www 234
                        'message'   => 'LABEL_RECORD_ADDED',
235
                        'data'      => [
310 www 236
                            'id' => $habitGoal->uuid,
309 www 237
                            'name' => $habitGoal->name,
238
                            'description' => $habitGoal->description,
239
 
240
                            'actions' => [
241
                                'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/goals/edit', ['id' => $habitGoal->uuid ], ['force_canonical' => true]) : '',
242
                                'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/goals/delete', ['id' => $habitGoal->uuid ],  ['force_canonical' => true]) : '',
243
                            ]
244
                        ]
308 www 245
                    ];
309 www 246
 
308 www 247
                } else {
248
                    $data = [
249
                        'success'   => false,
250
                        'data'      => $habitGoalMapper->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
 
286
        $request = $this->getRequest();
287
        $id = $this->params()->fromRoute('id');
288
 
289
 
290
        if(!$id) {
291
            return new JsonModel([
292
                'success'   => false,
293
                'data'   => 'ERROR_INVALID_PARAMETER'
294
            ]);
295
 
296
        }
297
 
298
 
299
 
300
        $habitGoalMapper = HabitGoalMapper::getInstance($this->adapter);
301
        $habitGoal = $habitGoalMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
302
        if(!$habitGoal) {
303
            return new JsonModel([
304
                'success'   => false,
305
                'data'   => 'ERROR_RECORD_NOT_FOUND'
306
            ]);
307
 
308
 
309
        }
310
 
311
 
312
        if($currentUser->id != $habitGoal->user_id) {
313
            return new JsonModel([
314
                'success'   => false,
315
                'data'   => 'ERROR_UNAUTHORIZED'
316
            ]);
317
 
318
        }
319
 
320
        if($request->isGet()) {
321
 
322
            $skills = [];
323
            $habitGoalSkillMapper = \LeadersLinked\Mapper\HabitGoalSkillMapper::getInstance($this->adapter);
324
            $habitSkillMapper = \LeadersLinked\Mapper\HabitSkillMapper::getInstance($this->adapter);
325
 
326
            $records =  $habitGoalSkillMapper->fetchAllByGoalId($habitGoal->id);
327
            foreach($records as $record)
328
            {
329
                $habitSkill = $habitSkillMapper->fetchOne($record->skill_id);
330
 
331
                array_push($skills, $habitSkill->uuid);
332
            }
333
 
334
 
335
 
336
            return new JsonModel([
337
                'success'   => true,
338
                'data'   => [
339
                    'name'          => $habitGoal->name,
340
                    'description'   => $habitGoal->description,
341
                    'value'         => $habitGoal->value,
342
                    'start_date'    => $habitGoal->start_date,
343
                    'end_date'      => $habitGoal->end_date,
344
                    'skill_id'      => $skills
345
                ]
346
            ]);
347
        } else if($request->isPost()) {
348
            $form = new  HabitGoalForm($this->adapter, $currentUser->id);
349
            $dataPost = $request->getPost()->toArray();
350
 
351
 
352
 
353
            $form->setData($dataPost);
354
 
355
            if($form->isValid()) {
356
 
357
                $dataPost = (array) $form->getData();
358
 
359
                $habitGoal->name = $dataPost['name'];
360
                $habitGoal->description = $dataPost['description'];
361
                $habitGoal->value       = $dataPost['value'];
362
                $habitGoal->start_date  = $dataPost['start_date'];
363
                $habitGoal->end_date    = $dataPost['end_date'];
364
 
365
 
366
 
367
                $habitGoalMapper = \LeadersLinked\Mapper\HabitGoalMapper::getInstance($this->adapter);
368
 
369
                $result = $habitGoalMapper->update($habitGoal);
370
 
371
                if($result) {
372
                    $habitGoalSkillMapper   = \LeadersLinked\Mapper\HabitGoalSkillMapper::getInstance($this->adapter);
373
                    $habitGoalSkillMapper->deleteAllByGoalId($habitGoal->id);
374
 
375
                    if(!empty($dataPost['skill_id'])) {
376
                        $habitSkillMapper       = \LeadersLinked\Mapper\HabitSkillMapper::getInstance($this->adapter);
377
                        foreach($dataPost['skill_id'] as $uuid)
378
                        {
379
                            $habitSkill = $habitSkillMapper->fetchOneByUuid($uuid);
380
 
381
 
382
 
383
                            if($habitSkill && $habitSkill->user_id == $currentUser->id) {
384
                                $habitGoalSkill = new \LeadersLinked\Model\HabitGoalSkill();
385
                                $habitGoalSkill->network_id = $currentUser->network_id;
386
                                $habitGoalSkill->user_id    = $currentUser->id;
387
                                $habitGoalSkill->goal_id    = $habitGoal->id;
388
                                $habitGoalSkill->skill_id   = $habitSkill->id;
389
 
390
                                $habitGoalSkillMapper->insert($habitGoalSkill);
391
 
392
                            }
393
                        }
394
                    }
395
 
396
                    $this->logger->info('Se edito el valor : ' . $habitGoal->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
397
 
309 www 398
 
399
                    $acl            = $this->getEvent()->getViewModel()->getVariable('acl');
400
                    $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/edit');
401
                    $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'habits/goals/delete');
402
 
308 www 403
                    $data = [
404
                        'success'   => true,
309 www 405
                        'message'   => 'LABEL_RECORD_UPDATED',
406
                        'data'      => [
310 www 407
                            'id' => $habitGoal->uuid,
309 www 408
                            'name' => $habitGoal->name,
409
                            'description' => $habitGoal->description,
410
 
411
                            'actions' => [
412
                                'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/goals/edit', ['id' => $habitGoal->uuid ], ['force_canonical' => true]) : '',
413
                                'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/goals/delete', ['id' => $habitGoal->uuid ],  ['force_canonical' => true]) : '',
414
                            ]
415
                        ]
308 www 416
                    ];
309 www 417
 
308 www 418
                } else {
419
                    $data = [
420
                        'success'   => false,
421
                        'data'      => $habitGoalMapper->getError()
422
                    ];
423
 
424
                }
425
 
426
                return new JsonModel($data);
427
 
428
 
429
            } else {
430
                $messages = [];
431
                $form_messages = (array) $form->getMessages();
432
                foreach($form_messages  as $fieldname => $field_messages)
433
                {
434
 
435
                    $messages[$fieldname] = array_values($field_messages);
436
                }
437
 
438
                return new JsonModel([
439
                    'success'   => false,
440
                    'data'   => $messages
441
                ]);
442
            }
443
 
444
        } else {
445
            return new JsonModel([
446
                'success' => false,
447
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
448
            ]);
449
        }
450
    }
451
 
452
 
453
    public function deleteAction()
454
    {
455
        $currentUserPlugin = $this->plugin('currentUserPlugin');
456
        $currentUser = $currentUserPlugin->getUser();
457
 
458
        $request = $this->getRequest();
459
        $id = $this->params()->fromRoute('id');
460
 
461
        if(!$id) {
462
            return new JsonModel([
463
                'success'   => false,
464
                'data'   => 'ERROR_INVALID_PARAMETER'
465
            ]);
466
        }
467
 
468
 
469
 
470
        $habitGoalMapper = HabitGoalMapper::getInstance($this->adapter);
471
        $habitGoal = $habitGoalMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
472
        if(!$habitGoal) {
473
            return new JsonModel([
474
                'success'   => false,
475
                'data'   => 'ERROR_RECORD_NOT_FOUND'
476
            ]);
477
        }
478
 
479
 
480
        if($currentUser->id != $habitGoal->user_id) {
481
            $response = [
482
                'success' => false,
483
                'data' => 'ERROR_UNAUTHORIZED'
484
            ];
485
 
486
            return new JsonModel($response);
487
        }
488
 
489
 
490
        if($request->isPost()) {
491
            $result = $habitGoalMapper->delete($habitGoal);
492
            if($result) {
493
                $this->logger->info('Se borro el valor : ' .  $habitGoal->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
494
 
495
                return new JsonModel([
496
                    'success' => true,
497
                    'data' => 'LABEL_RECORD_DELETED'
498
                ]);
499
            } else {
500
 
501
                return new JsonModel([
502
                    'success'   => false,
503
                    'data'      => $habitGoalMapper->getError()
504
                ]);
505
 
506
            }
507
 
508
        } else {
509
            return new JsonModel([
510
                'success' => false,
511
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
512
            ]);
513
 
514
        }
515
 
516
 
517
    }
518
}