Proyectos de Subversion LeadersLinked - Services

Rev

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