Proyectos de Subversion LeadersLinked - Services

Rev

Rev 309 | Rev 311 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
302 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\HabitValue;
14
use LeadersLinked\Mapper\HabitValueMapper;
15
use LeadersLinked\Form\Habit\HabitValueForm;
16
 
17
 
18
class HabitValueController 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/values/add');
85
            $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/values/edit');
86
            $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/values/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
                $habitValueMapper = HabitValueMapper::getInstance($this->adapter);
106
                $paginator = $habitValueMapper->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 = [
310 www 113
                        'id' => $record->uuid,
302 www 114
                        'name' => $record->name,
303 www 115
                        'description' => $record->description,
302 www 116
 
117
                        'actions' => [
118
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/values/edit', ['id' => $record->uuid ], ['force_canonical' => true]) : '',
119
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/values/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/values/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
    public function addAction()
146
    {
147
        $request = $this->getRequest();
148
 
149
 
150
        if($request->isPost()) {
151
            $form = new  HabitValueForm($this->adapter);
152
            $dataPost = $request->getPost()->toArray();
153
 
154
            $form->setData($dataPost);
155
 
156
            if($form->isValid()) {
157
 
158
 
159
                $currentUserPlugin = $this->plugin('currentUserPlugin');
160
                $currentUser = $currentUserPlugin->getUser();
161
 
162
                $dataPost = (array) $form->getData();
163
 
164
 
165
                $habitValue = new HabitValue();
166
                $habitValue->network_id = $currentUser->network_id;
167
                $habitValue->name       = $dataPost['name'];
304 www 168
                $habitValue->description = $dataPost['description'];
302 www 169
                $habitValue->user_id    = $currentUser->id;
170
 
171
                $habitValueMapper = HabitValueMapper::getInstance($this->adapter);
172
                $result = $habitValueMapper->insert($habitValue );
173
 
174
                if($result) {
175
                    $this->logger->info('Se agrego el valor : ' . $habitValue->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
176
 
310 www 177
                    $habitValue = $habitValueMapper->fetchOne($habitValue->id);
309 www 178
 
179
                    $acl            = $this->getEvent()->getViewModel()->getVariable('acl');
180
                    $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'habits/values/edit');
181
                    $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'habits/values/delete');
182
 
302 www 183
                    $data = [
184
                        'success'   => true,
309 www 185
                        'message'   => 'LABEL_RECORD_ADDED',
186
                        'data'      => [
310 www 187
                            'id' => $habitValue->uuid,
309 www 188
                            'name' => $habitValue->name,
189
                            'description' => $habitValue->description,
190
 
191
                            'actions' => [
192
                                'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/values/edit', ['id' => $habitValue->uuid ], ['force_canonical' => true]) : '',
193
                                'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/values/delete', ['id' => $habitValue->uuid ],  ['force_canonical' => true]) : '',
194
                            ]
195
                        ]
302 www 196
                    ];
197
                } else {
198
                    $data = [
199
                        'success'   => false,
200
                        'data'      => $habitValueMapper->getError()
201
                    ];
202
 
203
                }
204
 
205
                return new JsonModel($data);
206
 
207
            } else {
208
                $messages = [];
209
                $form_messages = (array) $form->getMessages();
210
                foreach($form_messages  as $fieldname => $field_messages)
211
                {
212
 
213
                    $messages[$fieldname] = array_values($field_messages);
214
                }
215
 
216
                return new JsonModel([
217
                    'success'   => false,
218
                    'data'   => $messages
219
                ]);
220
            }
221
 
222
        } else {
223
            return new JsonModel([
224
                'success' => false,
225
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
226
            ]);
227
        }
228
    }
229
 
230
    public function editAction()
231
    {
232
        $currentUserPlugin = $this->plugin('currentUserPlugin');
233
        $currentUser = $currentUserPlugin->getUser();
234
 
235
 
236
        $request = $this->getRequest();
237
        $id = $this->params()->fromRoute('id');
238
 
239
 
240
        if(!$id) {
241
            return new JsonModel([
242
                'success'   => false,
243
                'data'   => 'ERROR_INVALID_PARAMETER'
244
            ]);
245
 
246
        }
247
 
248
 
249
 
250
        $habitValueMapper = HabitValueMapper::getInstance($this->adapter);
251
        $habitValue = $habitValueMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
252
        if(!$habitValue) {
253
            return new JsonModel([
254
                'success'   => false,
255
                'data'   => 'ERROR_RECORD_NOT_FOUND'
256
            ]);
257
 
258
 
259
        }
260
 
261
 
262
        if($currentUser->id != $habitValue->user_id) {
263
            return new JsonModel([
264
                'success'   => false,
265
                'data'   => 'ERROR_UNAUTHORIZED'
266
            ]);
267
 
268
        }
269
 
270
        if($request->isGet()) {
271
            return new JsonModel([
272
                'success'   => true,
273
                'data'   => [
274
                    'name' => $habitValue->name,
303 www 275
                    'description' => $habitValue->description
302 www 276
                ]
277
            ]);
278
        } else if($request->isPost()) {
279
            $form = new  HabitValueForm($this->adapter);
280
            $dataPost = $request->getPost()->toArray();
281
 
282
            $form->setData($dataPost);
283
 
284
            if($form->isValid()) {
285
 
286
                $dataPost = (array) $form->getData();
287
 
288
                $habitValue->name = $dataPost['name'];
304 www 289
                $habitValue->description = $dataPost['description'];
302 www 290
 
291
                $result = $habitValueMapper->update($habitValue);
292
 
293
                if($result) {
294
                    $this->logger->info('Se edito el valor : ' . $habitValue->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
295
 
309 www 296
 
297
 
298
                    $acl            = $this->getEvent()->getViewModel()->getVariable('acl');
299
                    $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'habits/values/edit');
300
                    $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'habits/values/delete');
301
 
302 www 302
                    $data = [
303
                        'success'   => true,
309 www 304
                        'message'   => 'LABEL_RECORD_UPDATED',
305
                        'data'      => [
310 www 306
                            'id' => $habitValue->uuid,
309 www 307
                            'name' => $habitValue->name,
308
                            'description' => $habitValue->description,
309
 
310
                            'actions' => [
311
                                'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/values/edit', ['id' => $habitValue->uuid ], ['force_canonical' => true]) : '',
312
                                'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/values/delete', ['id' => $habitValue->uuid ],  ['force_canonical' => true]) : '',
313
                            ]
314
                        ]
302 www 315
                    ];
316
                } else {
317
                    $data = [
318
                        'success'   => false,
319
                        'data'      => $habitValueMapper->getError()
320
                    ];
321
 
322
                }
323
 
324
                return new JsonModel($data);
325
 
326
 
327
            } else {
328
                $messages = [];
329
                $form_messages = (array) $form->getMessages();
330
                foreach($form_messages  as $fieldname => $field_messages)
331
                {
332
 
333
                    $messages[$fieldname] = array_values($field_messages);
334
                }
335
 
336
                return new JsonModel([
337
                    'success'   => false,
338
                    'data'   => $messages
339
                ]);
340
            }
341
 
342
        } else {
343
            return new JsonModel([
344
                'success' => false,
345
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
346
            ]);
347
        }
348
    }
349
 
350
 
351
    public function deleteAction()
352
    {
353
        $currentUserPlugin = $this->plugin('currentUserPlugin');
354
        $currentUser = $currentUserPlugin->getUser();
355
 
356
        $request = $this->getRequest();
357
        $id = $this->params()->fromRoute('id');
358
 
359
        if(!$id) {
360
            return new JsonModel([
361
                'success'   => false,
362
                'data'   => 'ERROR_INVALID_PARAMETER'
363
            ]);
364
        }
365
 
366
 
367
 
368
        $habitValueMapper = HabitValueMapper::getInstance($this->adapter);
369
        $habitValue = $habitValueMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
370
        if(!$habitValue) {
371
            return new JsonModel([
372
                'success'   => false,
373
                'data'   => 'ERROR_RECORD_NOT_FOUND'
374
            ]);
375
        }
376
 
377
 
378
        if($currentUser->id != $habitValue->user_id) {
379
            $response = [
380
                'success' => false,
381
                'data' => 'ERROR_UNAUTHORIZED'
382
            ];
383
 
384
            return new JsonModel($response);
385
        }
386
 
387
 
388
        if($request->isPost()) {
389
            $result = $habitValueMapper->delete($habitValue);
390
            if($result) {
391
                $this->logger->info('Se borro el valor : ' .  $habitValue->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
392
 
393
                return new JsonModel([
394
                    'success' => true,
395
                    'data' => 'LABEL_RECORD_DELETED'
396
                ]);
397
            } else {
398
 
399
                return new JsonModel([
400
                    'success'   => false,
401
                    'data'      => $habitValueMapper->getError()
402
                ]);
403
 
404
            }
405
 
406
        } else {
407
            return new JsonModel([
408
                'success' => false,
409
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
410
            ]);
411
 
412
        }
413
 
414
 
415
    }
416
}