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