Proyectos de Subversion LeadersLinked - Services

Rev

Rev 309 | Ir a la última revisión | | 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
 
179
                    $data = [
180
                        'success'   => true,
181
                        'data'   => 'LABEL_RECORD_ADDED'
182
                    ];
183
                } else {
184
                    $data = [
185
                        'success'   => false,
186
                        'data'      => $habitSkillMapper->getError()
187
                    ];
188
 
189
                }
190
 
191
                return new JsonModel($data);
192
 
193
            } else {
194
                $messages = [];
195
                $form_messages = (array) $form->getMessages();
196
                foreach($form_messages  as $fieldname => $field_messages)
197
                {
198
 
199
                    $messages[$fieldname] = array_values($field_messages);
200
                }
201
 
202
                return new JsonModel([
203
                    'success'   => false,
204
                    'data'   => $messages
205
                ]);
206
            }
207
 
208
        } else {
209
            return new JsonModel([
210
                'success' => false,
211
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
212
            ]);
213
        }
214
    }
215
 
216
    public function editAction()
217
    {
218
        $currentUserPlugin = $this->plugin('currentUserPlugin');
219
        $currentUser = $currentUserPlugin->getUser();
220
 
221
 
222
        $request = $this->getRequest();
223
        $id = $this->params()->fromRoute('id');
224
 
225
 
226
        if(!$id) {
227
            return new JsonModel([
228
                'success'   => false,
229
                'data'   => 'ERROR_INVALID_PARAMETER'
230
            ]);
231
 
232
        }
233
 
234
 
235
 
236
        $habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);
237
        $habitSkill = $habitSkillMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
238
        if(!$habitSkill) {
239
            return new JsonModel([
240
                'success'   => false,
241
                'data'   => 'ERROR_RECORD_NOT_FOUND'
242
            ]);
243
 
244
 
245
        }
246
 
247
 
248
        if($currentUser->id != $habitSkill->user_id) {
249
            return new JsonModel([
250
                'success'   => false,
251
                'data'   => 'ERROR_UNAUTHORIZED'
252
            ]);
253
 
254
        }
255
 
256
        if($request->isGet()) {
257
            return new JsonModel([
258
                'success'   => true,
259
                'data'   => [
260
                    'name'                      => $habitSkill->name,
261
                    'description'               => $habitSkill->description,
262
                    'monday_active' 			=> $habitSkill->monday_active,
263
                    'tuesday_active' 			=> $habitSkill->tuesday_active,
264
                    'wednesday_active' 			=> $habitSkill->wednesday_active,
265
                    'thursday_active' 			=> $habitSkill->thursday_active,
266
                    'friday_active' 			=> $habitSkill->friday_active,
267
                    'saturday_active' 			=> $habitSkill->saturday_active,
268
                    'sunday_active' 			=> $habitSkill->sunday_active,
269
                    'monday_time' 				=> $habitSkill->monday_time,
270
                    'tuesday_time' 				=> $habitSkill->tuesday_time,
271
                    'wednesday_time' 			=> $habitSkill->wednesday_time,
272
                    'thursday_time' 			=> $habitSkill->thursday_time,
273
                    'friday_time' 				=> $habitSkill->friday_time,
274
                    'saturday_time' 			=> $habitSkill->saturday_time,
275
                    'sunday_time' 				=> $habitSkill->sunday_time,
276
                    'quantitative_value' 		=> $habitSkill->quantitative_value,
277
                    'qualitative_description' 	=> $habitSkill->qualitative_description,
278
                    'notification_10min_before' => $habitSkill->notification_10min_before,
279
                    'notification_30min_before' => $habitSkill->notification_30min_before,
280
                    'intelligence' 				=> $habitSkill->intelligence,
281
                ]
282
            ]);
283
        } else if($request->isPost()) {
284
            $form = new  HabitSkillForm($this->adapter);
285
            $dataPost = $request->getPost()->toArray();
286
 
287
            $form->setData($dataPost);
288
 
289
            if($form->isValid()) {
290
 
291
                $dataPost = (array) $form->getData();
292
 
293
                $hydrator = new \LeadersLinked\Hydrator\ObjectPropertyHydrator();
294
                $hydrator->hydrate($dataPost, $habitSkill);
295
 
296
 
297
 
298
                $result = $habitSkillMapper->update($habitSkill);
299
 
300
                if($result) {
301
                    $this->logger->info('Se edito el valor : ' . $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
302
 
303
                    $data = [
304
                        'success'   => true,
305
                        'data'   => 'LABEL_RECORD_UPDATED'
306
                    ];
307
                } else {
308
                    $data = [
309
                        'success'   => false,
310
                        'data'      => $habitSkillMapper->getError()
311
                    ];
312
 
313
                }
314
 
315
                return new JsonModel($data);
316
 
317
 
318
            } else {
319
                $messages = [];
320
                $form_messages = (array) $form->getMessages();
321
                foreach($form_messages  as $fieldname => $field_messages)
322
                {
323
 
324
                    $messages[$fieldname] = array_values($field_messages);
325
                }
326
 
327
                return new JsonModel([
328
                    'success'   => false,
329
                    'data'   => $messages
330
                ]);
331
            }
332
 
333
        } else {
334
            return new JsonModel([
335
                'success' => false,
336
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
337
            ]);
338
        }
339
    }
340
 
341
 
342
    public function deleteAction()
343
    {
344
        $currentUserPlugin = $this->plugin('currentUserPlugin');
345
        $currentUser = $currentUserPlugin->getUser();
346
 
347
        $request = $this->getRequest();
348
        $id = $this->params()->fromRoute('id');
349
 
350
        if(!$id) {
351
            return new JsonModel([
352
                'success'   => false,
353
                'data'   => 'ERROR_INVALID_PARAMETER'
354
            ]);
355
        }
356
 
357
 
358
 
359
        $habitSkillMapper = HabitSkillMapper::getInstance($this->adapter);
360
        $habitSkill = $habitSkillMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
361
        if(!$habitSkill) {
362
            return new JsonModel([
363
                'success'   => false,
364
                'data'   => 'ERROR_RECORD_NOT_FOUND'
365
            ]);
366
        }
367
 
368
 
369
        if($currentUser->id != $habitSkill->user_id) {
370
            $response = [
371
                'success' => false,
372
                'data' => 'ERROR_UNAUTHORIZED'
373
            ];
374
 
375
            return new JsonModel($response);
376
        }
377
 
378
 
379
        if($request->isPost()) {
380
            $result = $habitSkillMapper->delete($habitSkill);
381
            if($result) {
382
                $this->logger->info('Se borro el valor : ' .  $habitSkill->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
383
 
384
                return new JsonModel([
385
                    'success' => true,
386
                    'data' => 'LABEL_RECORD_DELETED'
387
                ]);
388
            } else {
389
 
390
                return new JsonModel([
391
                    'success'   => false,
392
                    'data'      => $habitSkillMapper->getError()
393
                ]);
394
 
395
            }
396
 
397
        } else {
398
            return new JsonModel([
399
                'success' => false,
400
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
401
            ]);
402
 
403
        }
404
 
405
 
406
    }
407
}