Proyectos de Subversion LeadersLinked - Services

Rev

Rev 303 | Ir a la última revisión | | 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 = [
113
                        'id' => $record->id,
114
                        'name' => $record->name,
115
 
116
                        'actions' => [
117
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/values/edit', ['id' => $record->uuid ], ['force_canonical' => true]) : '',
118
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/values/delete', ['id' =>$record->uuid ],  ['force_canonical' => true]) : '',
119
                        ]
120
                    ];
121
 
122
                    array_push($items, $item);
123
                }
124
 
125
                return new JsonModel([
126
                    'success' => true,
127
                    'data' => [
128
                        'items' => $items,
129
                        'total' => $paginator->getTotalItemCount(),
130
                        'link_add' => $allowAdd ? $this->url()->fromRoute('habits/values/add', [], ['force_canonical' => true]) : '',
131
                    ]
132
                ]);
133
 
134
 
135
        } else {
136
            return new JsonModel([
137
                'success' => false,
138
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
139
            ]);;
140
        }
141
    }
142
 
143
 
144
    public function addAction()
145
    {
146
        $request = $this->getRequest();
147
 
148
 
149
        if($request->isPost()) {
150
            $form = new  HabitValueForm($this->adapter);
151
            $dataPost = $request->getPost()->toArray();
152
 
153
            $form->setData($dataPost);
154
 
155
            if($form->isValid()) {
156
 
157
 
158
                $currentUserPlugin = $this->plugin('currentUserPlugin');
159
                $currentUser = $currentUserPlugin->getUser();
160
 
161
                $dataPost = (array) $form->getData();
162
 
163
 
164
                $habitValue = new HabitValue();
165
                $habitValue->network_id = $currentUser->network_id;
166
                $habitValue->name       = $dataPost['name'];
167
                $habitValue->user_id    = $currentUser->id;
168
 
169
                $habitValueMapper = HabitValueMapper::getInstance($this->adapter);
170
                $result = $habitValueMapper->insert($habitValue );
171
 
172
                if($result) {
173
                    $this->logger->info('Se agrego el valor : ' . $habitValue->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
174
 
175
                    $data = [
176
                        'success'   => true,
177
                        'data'   => 'LABEL_RECORD_ADDED'
178
                    ];
179
                } else {
180
                    $data = [
181
                        'success'   => false,
182
                        'data'      => $habitValueMapper->getError()
183
                    ];
184
 
185
                }
186
 
187
                return new JsonModel($data);
188
 
189
            } else {
190
                $messages = [];
191
                $form_messages = (array) $form->getMessages();
192
                foreach($form_messages  as $fieldname => $field_messages)
193
                {
194
 
195
                    $messages[$fieldname] = array_values($field_messages);
196
                }
197
 
198
                return new JsonModel([
199
                    'success'   => false,
200
                    'data'   => $messages
201
                ]);
202
            }
203
 
204
        } else {
205
            return new JsonModel([
206
                'success' => false,
207
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
208
            ]);
209
        }
210
    }
211
 
212
    public function editAction()
213
    {
214
        $currentUserPlugin = $this->plugin('currentUserPlugin');
215
        $currentUser = $currentUserPlugin->getUser();
216
 
217
 
218
        $request = $this->getRequest();
219
        $id = $this->params()->fromRoute('id');
220
 
221
 
222
        if(!$id) {
223
            return new JsonModel([
224
                'success'   => false,
225
                'data'   => 'ERROR_INVALID_PARAMETER'
226
            ]);
227
 
228
        }
229
 
230
 
231
 
232
        $habitValueMapper = HabitValueMapper::getInstance($this->adapter);
233
        $habitValue = $habitValueMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
234
        if(!$habitValue) {
235
            return new JsonModel([
236
                'success'   => false,
237
                'data'   => 'ERROR_RECORD_NOT_FOUND'
238
            ]);
239
 
240
 
241
        }
242
 
243
 
244
        if($currentUser->id != $habitValue->user_id) {
245
            return new JsonModel([
246
                'success'   => false,
247
                'data'   => 'ERROR_UNAUTHORIZED'
248
            ]);
249
 
250
        }
251
 
252
        if($request->isGet()) {
253
            return new JsonModel([
254
                'success'   => true,
255
                'data'   => [
256
                    'name' => $habitValue->name,
257
                ]
258
            ]);
259
        } else if($request->isPost()) {
260
            $form = new  HabitValueForm($this->adapter);
261
            $dataPost = $request->getPost()->toArray();
262
 
263
            $form->setData($dataPost);
264
 
265
            if($form->isValid()) {
266
 
267
                $dataPost = (array) $form->getData();
268
 
269
                $habitValue->name = $dataPost['name'];
270
 
271
                $result = $habitValueMapper->update($habitValue);
272
 
273
                if($result) {
274
                    $this->logger->info('Se edito el valor : ' . $habitValue->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
275
 
276
                    $data = [
277
                        'success'   => true,
278
                        'data'   => 'LABEL_RECORD_UPDATED'
279
                    ];
280
                } else {
281
                    $data = [
282
                        'success'   => false,
283
                        'data'      => $habitValueMapper->getError()
284
                    ];
285
 
286
                }
287
 
288
                return new JsonModel($data);
289
 
290
 
291
            } else {
292
                $messages = [];
293
                $form_messages = (array) $form->getMessages();
294
                foreach($form_messages  as $fieldname => $field_messages)
295
                {
296
 
297
                    $messages[$fieldname] = array_values($field_messages);
298
                }
299
 
300
                return new JsonModel([
301
                    'success'   => false,
302
                    'data'   => $messages
303
                ]);
304
            }
305
 
306
        } else {
307
            return new JsonModel([
308
                'success' => false,
309
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
310
            ]);
311
        }
312
    }
313
 
314
 
315
    public function deleteAction()
316
    {
317
        $currentUserPlugin = $this->plugin('currentUserPlugin');
318
        $currentUser = $currentUserPlugin->getUser();
319
 
320
        $request = $this->getRequest();
321
        $id = $this->params()->fromRoute('id');
322
 
323
        if(!$id) {
324
            return new JsonModel([
325
                'success'   => false,
326
                'data'   => 'ERROR_INVALID_PARAMETER'
327
            ]);
328
        }
329
 
330
 
331
 
332
        $habitValueMapper = HabitValueMapper::getInstance($this->adapter);
333
        $habitValue = $habitValueMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
334
        if(!$habitValue) {
335
            return new JsonModel([
336
                'success'   => false,
337
                'data'   => 'ERROR_RECORD_NOT_FOUND'
338
            ]);
339
        }
340
 
341
 
342
        if($currentUser->id != $habitValue->user_id) {
343
            $response = [
344
                'success' => false,
345
                'data' => 'ERROR_UNAUTHORIZED'
346
            ];
347
 
348
            return new JsonModel($response);
349
        }
350
 
351
 
352
        if($request->isPost()) {
353
            $result = $habitValueMapper->delete($habitValue);
354
            if($result) {
355
                $this->logger->info('Se borro el valor : ' .  $habitValue->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
356
 
357
                return new JsonModel([
358
                    'success' => true,
359
                    'data' => 'LABEL_RECORD_DELETED'
360
                ]);
361
            } else {
362
 
363
                return new JsonModel([
364
                    'success'   => false,
365
                    'data'      => $habitValueMapper->getError()
366
                ]);
367
 
368
            }
369
 
370
        } else {
371
            return new JsonModel([
372
                'success' => false,
373
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
374
            ]);
375
 
376
        }
377
 
378
 
379
    }
380
}