Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15585 | Rev 16768 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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