Proyectos de Subversion LeadersLinked - Backend

Rev

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