Proyectos de Subversion LeadersLinked - Services

Rev

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