Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16778 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 8
 
1 www 9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\GroupTypeMapper;
17002 efrain 15
use LeadersLinked\Form\GroupType\GroupTypeForm;
1 www 16
use LeadersLinked\Model\GroupType;
17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
18
 
17002 efrain 19
 
1 www 20
class GroupTypeController extends AbstractActionController
21
{
22
    /**
23
     *
16769 efrain 24
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 25
     */
26
    private $adapter;
27
 
28
    /**
29
     *
16769 efrain 30
     * @var \LeadersLinked\Cache\CacheInterface
1 www 31
     */
16769 efrain 32
    private $cache;
33
 
34
 
35
    /**
36
     *
37
     * @var \Laminas\Log\LoggerInterface
38
     */
1 www 39
    private $logger;
40
 
41
    /**
42
     *
43
     * @var array
44
     */
45
    private $config;
46
 
16769 efrain 47
 
1 www 48
    /**
49
     *
16769 efrain 50
     * @var \Laminas\Mvc\I18n\Translator
51
     */
52
    private $translator;
53
 
54
 
55
    /**
56
     *
57
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
58
     * @param \LeadersLinked\Cache\CacheInterface $cache
59
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
1 www 60
     * @param array $config
16769 efrain 61
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 62
     */
16769 efrain 63
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 64
    {
16769 efrain 65
        $this->adapter      = $adapter;
66
        $this->cache        = $cache;
67
        $this->logger       = $logger;
68
        $this->config       = $config;
69
        $this->translator   = $translator;
1 www 70
    }
71
 
72
    public function indexAction()
73
    {
74
        $request = $this->getRequest();
75
 
76
        $request = $this->getRequest();
77
        if($request->isGet()) {
78
 
79
 
80
            $headers  = $request->getHeaders();
81
 
82
            $isJson = false;
83
            if($headers->has('Accept')) {
84
                $accept = $headers->get('Accept');
85
 
86
                $prioritized = $accept->getPrioritized();
87
 
88
                foreach($prioritized as $key => $value) {
89
                    $raw = trim($value->getRaw());
90
 
91
                    if(!$isJson) {
92
                        $isJson = strpos($raw, 'json');
93
                    }
94
 
95
                }
96
            }
97
 
98
            if($isJson) {
5633 stevensc 99
                $search = $this->params()->fromQuery('search');
16778 efrain 100
                $search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
1 www 101
 
102
                $page               = intval($this->params()->fromQuery('start', 1), 10);
103
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
104
                $order =  $this->params()->fromQuery('order', []);
105
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 106
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
1 www 107
 
108
                $fields =  ['name'];
109
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
110
 
111
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
112
                    $order_direction = 'ASC';
113
                }
114
 
115
 
116
 
117
                $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
118
                $paginator = $groupTypeMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction);
119
 
120
                $items = [];
121
                $records = $paginator->getCurrentItems();
122
                foreach($records as $record)
123
                {
124
                    $item = [
125
                        'name' => $record->name,
126
                        'status' => $record->status,
127
                        'actions' => [
128
                            'link_edit' => $this->url()->fromRoute('settings/group-types/edit', ['id' => $record->uuid ]),
129
                            'link_delete' => $this->url()->fromRoute('settings/group-types/delete', ['id' => $record->uuid ])
130
                        ]
131
                    ];
132
 
133
                    array_push($items, $item);
134
                }
135
 
136
                return new JsonModel([
137
                    'success' => true,
138
                    'data' => [
139
                        'items' => $items,
140
                        'total' => $paginator->getTotalItemCount(),
141
                    ]
142
                ]);
143
 
144
 
145
            } else {
146
                $form = new GroupTypeForm();
147
 
148
                $this->layout()->setTemplate('layout/layout-backend');
149
                $viewModel = new ViewModel();
150
                $viewModel->setTemplate('leaders-linked/group-types/index.phtml');
151
                $viewModel->setVariable('form', $form);
152
                return $viewModel ;
153
            }
154
        } else {
155
            return new JsonModel([
156
                'success' => false,
157
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
158
            ]);;
159
        }
160
    }
161
 
162
 
163
    public function addAction()
164
    {
165
        $currentUserPlugin = $this->plugin('currentUserPlugin');
166
        $currentUser = $currentUserPlugin->getUser();
167
        $request = $this->getRequest();
168
 
169
        if($request->isPost()) {
170
            $form = new  GroupTypeForm();
171
            $dataPost = $request->getPost()->toArray();
172
 
173
            $form->setData($dataPost);
174
 
175
            if($form->isValid()) {
176
                $dataPost = (array) $form->getData();
177
                $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : GroupType::STATUS_INACTIVE;
178
 
179
                $hydrator = new ObjectPropertyHydrator();
180
                $groupType = new GroupType();
181
                $hydrator->hydrate($dataPost, $groupType);
182
 
183
 
184
 
185
                $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
186
                $result = $groupTypeMapper->insert($groupType);
187
 
188
                if($result) {
189
                    $this->logger->info('Se agrego el tipo de grupo ' . $groupType->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
190
 
191
                    $data = [
192
                        'success'   => true,
193
                        'data'   => 'LABEL_RECORD_ADDED'
194
                    ];
195
                } else {
196
                    $data = [
197
                        'success'   => false,
198
                        'data'      => $groupTypeMapper->getError()
199
                    ];
200
 
201
                }
202
 
203
                return new JsonModel($data);
204
 
205
            } else {
206
                $messages = [];
207
                $form_messages = (array) $form->getMessages();
208
                foreach($form_messages  as $fieldname => $field_messages)
209
                {
210
 
211
                    $messages[$fieldname] = array_values($field_messages);
212
                }
213
 
214
                return new JsonModel([
215
                    'success'   => false,
216
                    'data'   => $messages
217
                ]);
218
            }
219
 
220
        } else {
221
            $data = [
222
                'success' => false,
223
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
224
            ];
225
 
226
            return new JsonModel($data);
227
        }
228
 
229
        return new JsonModel($data);
230
    }
231
 
232
    public function editAction()
233
    {
234
        $currentUserPlugin = $this->plugin('currentUserPlugin');
235
        $currentUser = $currentUserPlugin->getUser();
236
 
237
        $request = $this->getRequest();
238
        $uuid = $this->params()->fromRoute('id');
239
 
240
 
241
        if(!$uuid) {
242
            $data = [
243
                'success'   => false,
244
                'data'   => 'ERROR_INVALID_PARAMETER'
245
            ];
246
 
247
            return new JsonModel($data);
248
        }
249
 
250
        $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
251
        $groupType = $groupTypeMapper->fetchOneByUuid($uuid);
252
        if(!$groupType) {
253
            $data = [
254
                'success'   => false,
255
                'data'   => 'ERROR_RECORD_NOT_FOUND'
256
            ];
257
 
258
            return new JsonModel($data);
259
        }
260
 
261
        if($request->isPost()) {
262
            $form = new  GroupTypeForm();
263
            $dataPost = $request->getPost()->toArray();
264
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : GroupType::STATUS_INACTIVE;
265
 
266
            $form->setData($dataPost);
267
 
268
            if($form->isValid()) {
269
                $dataPost = (array) $form->getData();
270
 
271
                $hydrator = new ObjectPropertyHydrator();
272
                $hydrator->hydrate($dataPost, $groupType);
273
                $result = $groupTypeMapper->update($groupType);
274
 
275
                if($result) {
276
                    $this->logger->info('Se actualizo el tipo de grupo ' . $groupType->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
277
 
278
                    $data = [
279
                        'success' => true,
280
                        'data' => 'LABEL_RECORD_UPDATED'
281
                    ];
282
                } else {
283
                    $data = [
284
                        'success'   => false,
285
                        'data'      => $groupTypeMapper->getError()
286
                    ];
287
                }
288
 
289
                return new JsonModel($data);
290
 
291
            } else {
292
                $messages = [];
293
                $form_messages = (array) $form->getMessages();
294
                foreach($form_messages  as $fieldname => $field_messages)
295
                {
296
                    $messages[$fieldname] = array_values($field_messages);
297
                }
298
 
299
                return new JsonModel([
300
                    'success'   => false,
301
                    'data'   => $messages
302
                ]);
303
            }
304
        } else if ($request->isGet()) {
305
            $hydrator = new ObjectPropertyHydrator();
306
 
307
            $data = [
308
                'success' => true,
309
                'data' => $hydrator->extract($groupType)
310
            ];
311
 
312
            return new JsonModel($data);
313
        } else {
314
            $data = [
315
                'success' => false,
316
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
317
            ];
318
 
319
            return new JsonModel($data);
320
        }
321
 
322
        return new JsonModel($data);
323
    }
324
 
325
    public function deleteAction()
326
    {
327
        $currentUserPlugin = $this->plugin('currentUserPlugin');
328
        $currentUser = $currentUserPlugin->getUser();
329
 
330
        $request = $this->getRequest();
331
        $uuid = $this->params()->fromRoute('id');
332
 
333
        if(!$uuid) {
334
            $data = [
335
                'success'   => false,
336
                'data'   => 'ERROR_INVALID_PARAMETER'
337
            ];
338
 
339
            return new JsonModel($data);
340
        }
341
 
342
 
343
 
344
        $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
345
        $groupType = $groupTypeMapper->fetchOneByUuid($uuid);
346
        if(!$groupType) {
347
            $data = [
348
                'success'   => false,
349
                'data'   => 'ERROR_RECORD_NOT_FOUND'
350
            ];
351
 
352
            return new JsonModel($data);
353
        }
354
 
355
        if($request->isPost()) {
356
            $result = $groupTypeMapper->delete($groupType);
357
            if($result) {
358
                $this->logger->info('Se borro el tipo de grupo ' . $groupType->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
359
 
360
                $data = [
361
                    'success' => true,
362
                    'data' => 'LABEL_RECORD_DELETED'
363
                ];
364
            } else {
365
 
366
                $data = [
367
                    'success'   => false,
368
                    'data'      => $groupTypeMapper->getError()
369
                ];
370
 
371
                return new JsonModel($data);
372
            }
373
 
374
        } else {
375
            $data = [
376
                'success' => false,
377
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
378
            ];
379
 
380
            return new JsonModel($data);
381
        }
382
 
383
        return new JsonModel($data);
384
    }
385
}