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