Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
67 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
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\PositionMapper;
15
use LeadersLinked\Model\Position;
16
use LeadersLinked\Form\PositionForm;
17
use LeadersLinked\Mapper\UserMapper;
18
use LeadersLinked\Mapper\JobDescriptionMapper;
19
 
20
 
21
class PositionController extends AbstractActionController
22
{
23
    /**
24
     *
25
     * @var AdapterInterface
26
     */
27
    private $adapter;
28
 
29
 
30
    /**
31
     *
32
     * @var AbstractAdapter
33
     */
34
    private $cache;
35
 
36
    /**
37
     *
38
     * @var  LoggerInterface
39
     */
40
    private $logger;
41
 
42
 
43
    /**
44
     *
45
     * @var array
46
     */
47
    private $config;
48
 
49
 
50
    /**
51
     *
52
     * @param AdapterInterface $adapter
53
     * @param AbstractAdapter $cache
54
     * @param LoggerInterface $logger
55
     * @param array $config
56
     */
57
    public function __construct($adapter, $cache , $logger, $config)
58
    {
59
        $this->adapter      = $adapter;
60
        $this->cache        = $cache;
61
        $this->logger       = $logger;
62
        $this->config       = $config;
63
 
64
    }
65
 
66
    public function indexAction()
67
    {
68
        $currentUserPlugin = $this->plugin('currentUserPlugin');
69
        $currentUser = $currentUserPlugin->getUser();
70
        $currentCompany = $currentUserPlugin->getCompany();
71
 
72
        $request = $this->getRequest();
73
 
74
        $headers  = $request->getHeaders();
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) {
99
                $search = $this->params()->fromQuery('search', []);
100
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
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);
106
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
107
 
108
                $fields =  ['job_description'];
109
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'job_description';
110
 
111
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
112
                    $order_direction = 'ASC';
113
                }
114
 
115
                $positionMapper = PositionMapper::getInstance($this->adapter);
116
                $paginator = $positionMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
117
 
118
                $items = [];
119
                $records = $paginator->getCurrentItems();
120
                foreach($records as $record)
121
                {
122
                    $item = [
123
                        'job_description' => $record['job_description'],
124
                        'user' => trim($record['first_name'] . ' ' . $record['last_name']) . ' (' . $record['email'] . ')',
125
                        'status' => $record['status'],
126
                        'actions' => [
127
                            'link_edit' => $this->url()->fromRoute('settings/positions/edit', ['id' => $record['uuid'] ]),
128
                            'link_delete' => $this->url()->fromRoute('settings/positions/delete', ['id' => $record['uuid'] ])
129
                        ]
130
                    ];
131
 
132
                    array_push($items, $item);
133
                }
134
 
135
                return new JsonModel([
136
                    'success' => true,
137
                    'data' => [
138
                        'items' => $items,
139
                        'total' => $paginator->getTotalItemCount(),
140
                    ]
141
                ]);
142
 
143
 
144
            } else  {
145
                $form = new PositionForm($this->adapter, $currentCompany->id);
146
 
147
                $this->layout()->setTemplate('layout/layout-backend');
148
                $viewModel = new ViewModel();
149
                $viewModel->setTemplate('leaders-linked/positions/index.phtml');
150
                $viewModel->setVariable('form', $form);
151
                return $viewModel ;
152
            }
153
 
154
        } else {
155
            return new JsonModel([
156
                'success' => false,
157
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
158
            ]);;
159
        }
160
    }
161
 
162
    public function addAction()
163
    {
164
        $currentUserPlugin = $this->plugin('currentUserPlugin');
165
        $currentUser = $currentUserPlugin->getUser();
166
        $currentCompany = $currentUserPlugin->getCompany();
167
 
168
        $request = $this->getRequest();
169
 
170
 
171
        if($request->isPost()) {
172
            if($currentCompany) {
173
                $form = new PositionForm($this->adapter, $currentCompany->id);
174
            } else {
175
                $form = new PositionForm($this->adapter);
176
            }
177
            $dataPost = $request->getPost()->toArray();
178
 
179
            $form->setData($dataPost);
180
 
181
            if($form->isValid()) {
182
                $dataPost = (array) $form->getData();
183
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : Position::STATUS_INACTIVE;
184
 
185
                $userMapper = UserMapper::getInstance($this->adapter);
186
                $user = $userMapper->fetchOneByUuid($dataPost['user_id']);
187
                $dataPost['user_id'] = $user->id;
188
 
189
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
190
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
191
                $dataPost['job_description_id'] = $jobDescription->id;
192
 
193
 
194
 
195
                $hydrator = new ObjectPropertyHydrator();
196
                $position = new Position();
197
 
198
                $hydrator->hydrate($dataPost, $position);
199
 
200
                if($currentCompany) {
201
                    $position->company_id = $currentCompany->id;
202
                }
203
 
204
 
205
                $positionMapper = PositionMapper::getInstance($this->adapter);
206
                $result = $positionMapper->insert($position);
207
 
208
 
209
                if($result) {
210
                    $this->logger->info('Se agrego la posicion ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
211
 
212
                    $data = [
213
                        'success'   => true,
214
                        'data'   => 'LABEL_RECORD_ADDED'
215
                    ];
216
                } else {
217
                    $data = [
218
                        'success'   => false,
219
                        'data'      => $positionMapper->getError()
220
                    ];
221
 
222
                }
223
 
224
                return new JsonModel($data);
225
 
226
            } else {
227
                $messages = [];
228
                $form_messages = (array) $form->getMessages();
229
                foreach($form_messages  as $fieldname => $field_messages)
230
                {
231
 
232
                    $messages[$fieldname] = array_values($field_messages);
233
                }
234
 
235
                return new JsonModel([
236
                    'success'   => false,
237
                    'data'   => $messages
238
                ]);
239
            }
240
 
241
        } else {
242
            $data = [
243
                'success' => false,
244
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
245
            ];
246
 
247
            return new JsonModel($data);
248
        }
249
 
250
        return new JsonModel($data);
251
    }
252
 
253
    public function editAction()
254
    {
255
        $currentUserPlugin = $this->plugin('currentUserPlugin');
256
        $currentUser = $currentUserPlugin->getUser();
257
        $currentCompany = $currentUserPlugin->getCompany();
258
 
259
        $request = $this->getRequest();
260
        $uuid = $this->params()->fromRoute('id');
261
 
262
 
263
        if(!$uuid) {
264
            $data = [
265
                'success'   => false,
266
                'data'   => 'ERROR_INVALID_PARAMETER'
267
            ];
268
 
269
            return new JsonModel($data);
270
        }
271
 
272
        $positionMapper = PositionMapper::getInstance($this->adapter);
273
        $position = $positionMapper->fetchOneByUuid($uuid);
274
        if(!$position) {
275
            $data = [
276
                'success'   => false,
277
                'data'   => 'ERROR_RECORD_NOT_FOUND'
278
            ];
279
 
280
            return new JsonModel($data);
281
        }
282
 
283
        if($position->company_id != $currentCompany->id) {
284
            $data = [
285
                'success'   => false,
286
                'data'   => 'ERROR_UNAUTHORIZED'
287
            ];
288
 
289
            return new JsonModel($data);
290
        }
291
 
292
        if($request->isPost()) {
293
            $form = new PositionForm($this->adapter, $currentCompany->id);
294
            $dataPost = $request->getPost()->toArray();
295
 
296
 
297
            $form->setData($dataPost);
298
 
299
            if($form->isValid()) {
300
                $dataPost = (array) $form->getData();
301
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : Position::STATUS_INACTIVE;
302
 
303
                $userMapper = UserMapper::getInstance($this->adapter);
304
                $user = $userMapper->fetchOneByUuid($dataPost['user_id']);
305
                $dataPost['user_id'] = $user->id;
306
 
307
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
308
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
309
                $dataPost['job_description_id'] = $jobDescription->id;
310
 
311
                $hydrator = new ObjectPropertyHydrator();
312
                $hydrator->hydrate($dataPost, $position);
313
 
314
                $result = $positionMapper->update($position);
315
 
316
                if($result) {
317
                    $this->logger->info('Se actualizo la posición ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
318
 
319
                    $data = [
320
                        'success' => true,
321
                        'data' => 'LABEL_RECORD_UPDATED'
322
                    ];
323
                } else {
324
                    $data = [
325
                        'success'   => false,
326
                        'data'      => $positionMapper->getError()
327
                    ];
328
                }
329
 
330
                return new JsonModel($data);
331
 
332
            } else {
333
                $messages = [];
334
                $form_messages = (array) $form->getMessages();
335
                foreach($form_messages  as $fieldname => $field_messages)
336
                {
337
                    $messages[$fieldname] = array_values($field_messages);
338
                }
339
 
340
                return new JsonModel([
341
                    'success'   => false,
342
                    'data'   => $messages
343
                ]);
344
            }
345
        } else if ($request->isGet()) {
346
            $userMapper = UserMapper::getInstance($this->adapter);
347
            $user = $userMapper->fetchOne($position->user_id);
348
 
349
 
350
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
351
            $jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
352
 
353
 
354
            $hydrator = new ObjectPropertyHydrator();
355
 
356
            $data = $hydrator->extract($position);
357
            $data['user_id'] = $user->uuid;
358
            $data['job_description_id'] = $jobDescription->uuid;
359
 
360
            $response = [
361
                'success' => true,
362
                'data' => $data
363
            ];
364
 
365
            return new JsonModel($response);
366
        } else {
367
            $data = [
368
                'success' => false,
369
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
370
            ];
371
 
372
            return new JsonModel($data);
373
        }
374
 
375
        return new JsonModel($data);
376
    }
377
 
378
    public function deleteAction()
379
    {
380
        $currentUserPlugin = $this->plugin('currentUserPlugin');
381
        $currentUser = $currentUserPlugin->getUser();
382
        $currentCompany = $currentUserPlugin->getCompany();
383
 
384
        $request = $this->getRequest();
385
        $uuid = $this->params()->fromRoute('id');
386
 
387
        if(!$uuid) {
388
            $data = [
389
                'success'   => false,
390
                'data'   => 'ERROR_INVALID_PARAMETER'
391
            ];
392
 
393
            return new JsonModel($data);
394
        }
395
 
396
 
397
        $positionMapper = PositionMapper::getInstance($this->adapter);
398
        $position = $positionMapper->fetchOneByUuid($uuid);
399
        if(!$position) {
400
            $data = [
401
                'success'   => false,
402
                'data'   => 'ERROR_RECORD_NOT_FOUND'
403
            ];
404
 
405
            return new JsonModel($data);
406
        }
407
 
408
        if($position->company_id != $currentCompany->id) {
409
            $data = [
410
                'success'   => false,
411
                'data'   => 'ERROR_UNAUTHORIZED'
412
            ];
413
 
414
            return new JsonModel($data);
415
        }
416
 
417
        if($request->isPost()) {
418
            $result = $positionMapper->delete($position);
419
            if($result) {
420
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
421
                $jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
422
 
423
                $this->logger->info('Se borro la posición ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
424
 
425
                $data = [
426
                    'success' => true,
427
                    'data' => 'LABEL_RECORD_DELETED'
428
                ];
429
            } else {
430
 
431
                $data = [
432
                    'success'   => false,
433
                    'data'      => $positionMapper->getError()
434
                ];
435
 
436
                return new JsonModel($data);
437
            }
438
 
439
        } else {
440
            $data = [
441
                'success' => false,
442
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
443
            ];
444
 
445
            return new JsonModel($data);
446
        }
447
 
448
        return new JsonModel($data);
449
    }
450
 
451
 
452
}