Proyectos de Subversion LeadersLinked - Services

Rev

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