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;
1320 efrain 19
use LeadersLinked\Mapper\JobDescriptionSubordinateMapper;
20
use LeadersLinked\Mapper\PositionSubordinateMapper;
67 efrain 21
 
22
 
23
class PositionController extends AbstractActionController
24
{
25
    /**
26
     *
27
     * @var AdapterInterface
28
     */
29
    private $adapter;
30
 
31
 
32
    /**
33
     *
34
     * @var AbstractAdapter
35
     */
36
    private $cache;
37
 
38
    /**
39
     *
40
     * @var  LoggerInterface
41
     */
42
    private $logger;
43
 
44
 
45
    /**
46
     *
47
     * @var array
48
     */
49
    private $config;
50
 
51
 
52
    /**
53
     *
54
     * @param AdapterInterface $adapter
55
     * @param AbstractAdapter $cache
56
     * @param LoggerInterface $logger
57
     * @param array $config
58
     */
59
    public function __construct($adapter, $cache , $logger, $config)
60
    {
61
        $this->adapter      = $adapter;
62
        $this->cache        = $cache;
63
        $this->logger       = $logger;
64
        $this->config       = $config;
65
 
66
    }
67
 
68
    public function indexAction()
69
    {
70
        $currentUserPlugin = $this->plugin('currentUserPlugin');
71
        $currentUser = $currentUserPlugin->getUser();
72
        $currentCompany = $currentUserPlugin->getCompany();
73
 
74
        $request = $this->getRequest();
75
 
76
        $headers  = $request->getHeaders();
77
 
78
        $request = $this->getRequest();
79
        if($request->isGet()) {
80
 
81
 
82
            $headers  = $request->getHeaders();
83
 
84
            $isJson = false;
85
            if($headers->has('Accept')) {
86
                $accept = $headers->get('Accept');
87
 
88
                $prioritized = $accept->getPrioritized();
89
 
90
                foreach($prioritized as $key => $value) {
91
                    $raw = trim($value->getRaw());
92
 
93
                    if(!$isJson) {
94
                        $isJson = strpos($raw, 'json');
95
                    }
96
 
97
                }
98
            }
99
 
100
            if($isJson) {
101
                $search = $this->params()->fromQuery('search', []);
102
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
103
 
104
                $page               = intval($this->params()->fromQuery('start', 1), 10);
105
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
106
                $order =  $this->params()->fromQuery('order', []);
107
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
108
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
109
 
110
                $fields =  ['job_description'];
111
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'job_description';
112
 
113
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
114
                    $order_direction = 'ASC';
115
                }
116
 
117
                $positionMapper = PositionMapper::getInstance($this->adapter);
118
                $paginator = $positionMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
119
 
120
                $items = [];
121
                $records = $paginator->getCurrentItems();
122
                foreach($records as $record)
123
                {
124
                    $item = [
125
                        'job_description' => $record['job_description'],
126
                        'user' => trim($record['first_name'] . ' ' . $record['last_name']) . ' (' . $record['email'] . ')',
127
                        'status' => $record['status'],
128
                        'actions' => [
15444 efrain 129
                            'link_edit' => $this->url()->fromRoute('jobs-description/positions/edit', ['id' => $record['uuid'] ]),
130
                            'link_delete' => $this->url()->fromRoute('jobs-description/positions/delete', ['id' => $record['uuid'] ])
67 efrain 131
                        ]
132
                    ];
133
 
134
                    array_push($items, $item);
135
                }
136
 
137
                return new JsonModel([
138
                    'success' => true,
139
                    'data' => [
140
                        'items' => $items,
141
                        'total' => $paginator->getTotalItemCount(),
142
                    ]
143
                ]);
144
 
145
 
146
            } else  {
147
                $form = new PositionForm($this->adapter, $currentCompany->id);
148
 
149
                $this->layout()->setTemplate('layout/layout-backend');
150
                $viewModel = new ViewModel();
151
                $viewModel->setTemplate('leaders-linked/positions/index.phtml');
152
                $viewModel->setVariable('form', $form);
153
                return $viewModel ;
154
            }
155
 
156
        } else {
157
            return new JsonModel([
158
                'success' => false,
159
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
160
            ]);;
161
        }
162
    }
163
 
164
    public function addAction()
165
    {
166
        $currentUserPlugin = $this->plugin('currentUserPlugin');
167
        $currentUser = $currentUserPlugin->getUser();
168
        $currentCompany = $currentUserPlugin->getCompany();
169
 
170
        $request = $this->getRequest();
171
 
172
 
173
        if($request->isPost()) {
174
            if($currentCompany) {
175
                $form = new PositionForm($this->adapter, $currentCompany->id);
176
            } else {
177
                $form = new PositionForm($this->adapter);
178
            }
179
            $dataPost = $request->getPost()->toArray();
180
 
181
            $form->setData($dataPost);
182
 
183
            if($form->isValid()) {
184
                $dataPost = (array) $form->getData();
185
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : Position::STATUS_INACTIVE;
186
 
187
                $userMapper = UserMapper::getInstance($this->adapter);
188
                $user = $userMapper->fetchOneByUuid($dataPost['user_id']);
189
                $dataPost['user_id'] = $user->id;
190
 
191
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
192
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
193
                $dataPost['job_description_id'] = $jobDescription->id;
194
 
195
 
196
 
197
                $hydrator = new ObjectPropertyHydrator();
198
                $position = new Position();
199
 
200
                $hydrator->hydrate($dataPost, $position);
201
 
202
                if($currentCompany) {
203
                    $position->company_id = $currentCompany->id;
204
                }
205
 
206
 
207
                $positionMapper = PositionMapper::getInstance($this->adapter);
208
                $result = $positionMapper->insert($position);
209
 
210
 
211
                if($result) {
212
                    $this->logger->info('Se agrego la posicion ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
213
 
214
                    $data = [
215
                        'success'   => true,
216
                        'data'   => 'LABEL_RECORD_ADDED'
217
                    ];
218
                } else {
219
                    $data = [
220
                        'success'   => false,
221
                        'data'      => $positionMapper->getError()
222
                    ];
223
 
224
                }
225
 
226
                return new JsonModel($data);
227
 
228
            } else {
229
                $messages = [];
230
                $form_messages = (array) $form->getMessages();
231
                foreach($form_messages  as $fieldname => $field_messages)
232
                {
233
 
234
                    $messages[$fieldname] = array_values($field_messages);
235
                }
236
 
237
                return new JsonModel([
238
                    'success'   => false,
239
                    'data'   => $messages
240
                ]);
241
            }
242
 
243
        } else {
244
            $data = [
245
                'success' => false,
246
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
247
            ];
248
 
249
            return new JsonModel($data);
250
        }
251
 
252
        return new JsonModel($data);
253
    }
254
 
255
    public function editAction()
256
    {
257
        $currentUserPlugin = $this->plugin('currentUserPlugin');
258
        $currentUser = $currentUserPlugin->getUser();
259
        $currentCompany = $currentUserPlugin->getCompany();
260
 
261
        $request = $this->getRequest();
262
        $uuid = $this->params()->fromRoute('id');
263
 
264
 
265
        if(!$uuid) {
266
            $data = [
267
                'success'   => false,
268
                'data'   => 'ERROR_INVALID_PARAMETER'
269
            ];
270
 
271
            return new JsonModel($data);
272
        }
273
 
274
        $positionMapper = PositionMapper::getInstance($this->adapter);
275
        $position = $positionMapper->fetchOneByUuid($uuid);
276
        if(!$position) {
277
            $data = [
278
                'success'   => false,
279
                'data'   => 'ERROR_RECORD_NOT_FOUND'
280
            ];
281
 
282
            return new JsonModel($data);
283
        }
284
 
285
        if($position->company_id != $currentCompany->id) {
286
            $data = [
287
                'success'   => false,
288
                'data'   => 'ERROR_UNAUTHORIZED'
289
            ];
290
 
291
            return new JsonModel($data);
292
        }
293
 
294
        if($request->isPost()) {
295
            $form = new PositionForm($this->adapter, $currentCompany->id);
296
            $dataPost = $request->getPost()->toArray();
297
 
298
 
299
            $form->setData($dataPost);
300
 
301
            if($form->isValid()) {
302
                $dataPost = (array) $form->getData();
303
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : Position::STATUS_INACTIVE;
304
 
305
                $userMapper = UserMapper::getInstance($this->adapter);
306
                $user = $userMapper->fetchOneByUuid($dataPost['user_id']);
307
                $dataPost['user_id'] = $user->id;
308
 
309
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
310
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
311
                $dataPost['job_description_id'] = $jobDescription->id;
312
 
313
                $hydrator = new ObjectPropertyHydrator();
314
                $hydrator->hydrate($dataPost, $position);
315
 
316
                $result = $positionMapper->update($position);
317
 
318
                if($result) {
319
                    $this->logger->info('Se actualizo la posición ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
320
 
321
                    $data = [
322
                        'success' => true,
323
                        'data' => 'LABEL_RECORD_UPDATED'
324
                    ];
325
                } else {
326
                    $data = [
327
                        'success'   => false,
328
                        'data'      => $positionMapper->getError()
329
                    ];
330
                }
331
 
332
                return new JsonModel($data);
333
 
334
            } else {
335
                $messages = [];
336
                $form_messages = (array) $form->getMessages();
337
                foreach($form_messages  as $fieldname => $field_messages)
338
                {
339
                    $messages[$fieldname] = array_values($field_messages);
340
                }
341
 
342
                return new JsonModel([
343
                    'success'   => false,
344
                    'data'   => $messages
345
                ]);
346
            }
347
        } else if ($request->isGet()) {
348
            $userMapper = UserMapper::getInstance($this->adapter);
349
            $user = $userMapper->fetchOne($position->user_id);
350
 
351
 
352
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
353
            $jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
354
 
355
 
356
            $hydrator = new ObjectPropertyHydrator();
357
 
358
            $data = $hydrator->extract($position);
359
            $data['user_id'] = $user->uuid;
360
            $data['job_description_id'] = $jobDescription->uuid;
1320 efrain 361
            $data['users'] = [];
362
            $data['subordinates'] = [];
67 efrain 363
 
1320 efrain 364
            $ids = [];
365
 
366
            $userMapper = UserMapper::getInstance($this->adapter);
367
 
368
            $positionSubordinateMapper = PositionSubordinateMapper::getInstance($this->adapter);
369
            $subordinates = $positionSubordinateMapper->fetchAllByPositionId($position->id);
370
 
371
            foreach($subordinates as $subordinate)
372
            {
373
                $user = $userMapper->fetchOne($subordinate->user_id);
374
                if($user) {
375
                    array_push($data['subordinates'], $user->uuid);
376
                }
377
            }
378
 
379
 
380
            $jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
381
            $jobDescriptionSubordinates = $jobDescriptionSubordinateMapper->fetchAllByJobDescriptionIdTopLevel($jobDescription->id);
382
 
383
            foreach($jobDescriptionSubordinates as $jobDescriptionSubordinate)
384
            {
385
                $positions = $positionMapper->fetchAllByJobDescriptionIdAndCompanyId($jobDescriptionSubordinate->job_description_id_low_level, $currentCompany->id);
386
 
387
                foreach($positions as $position)
388
                {
389
                    if(!in_array($position->user_id, $ids)) {
390
                        array_push($ids, $position->user_id);
391
                    }
392
                }
393
 
394
            }
395
 
396
 
397
 
398
            if($ids) {
399
                $users = $userMapper->fetchAllByIds($ids);
400
                foreach($users as $user)
401
                {
402
                    array_push($data['users'], [
403
                        'uuid' => $user->uuid,
404
                        'name' => trim(trim($user->first_name) . ' ' . trim($user->last_name)) . ' (' . trim($user->email) . ')',
405
                    ]);
406
 
407
                }
408
            }
409
 
410
 
67 efrain 411
            $response = [
412
                'success' => true,
413
                'data' => $data
414
            ];
415
 
416
            return new JsonModel($response);
417
        } else {
418
            $data = [
419
                'success' => false,
420
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
421
            ];
422
 
423
            return new JsonModel($data);
424
        }
425
 
426
        return new JsonModel($data);
427
    }
428
 
429
    public function deleteAction()
430
    {
431
        $currentUserPlugin = $this->plugin('currentUserPlugin');
432
        $currentUser = $currentUserPlugin->getUser();
433
        $currentCompany = $currentUserPlugin->getCompany();
434
 
435
        $request = $this->getRequest();
436
        $uuid = $this->params()->fromRoute('id');
437
 
438
        if(!$uuid) {
439
            $data = [
440
                'success'   => false,
441
                'data'   => 'ERROR_INVALID_PARAMETER'
442
            ];
443
 
444
            return new JsonModel($data);
445
        }
446
 
447
 
448
        $positionMapper = PositionMapper::getInstance($this->adapter);
449
        $position = $positionMapper->fetchOneByUuid($uuid);
450
        if(!$position) {
451
            $data = [
452
                'success'   => false,
453
                'data'   => 'ERROR_RECORD_NOT_FOUND'
454
            ];
455
 
456
            return new JsonModel($data);
457
        }
458
 
459
        if($position->company_id != $currentCompany->id) {
460
            $data = [
461
                'success'   => false,
462
                'data'   => 'ERROR_UNAUTHORIZED'
463
            ];
464
 
465
            return new JsonModel($data);
466
        }
467
 
468
        if($request->isPost()) {
469
            $result = $positionMapper->delete($position);
470
            if($result) {
471
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
472
                $jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
473
 
474
                $this->logger->info('Se borro la posición ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
475
 
476
                $data = [
477
                    'success' => true,
478
                    'data' => 'LABEL_RECORD_DELETED'
479
                ];
480
            } else {
481
 
482
                $data = [
483
                    'success'   => false,
484
                    'data'      => $positionMapper->getError()
485
                ];
486
 
487
                return new JsonModel($data);
488
            }
489
 
490
        } else {
491
            $data = [
492
                'success' => false,
493
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
494
            ];
495
 
496
            return new JsonModel($data);
497
        }
498
 
499
        return new JsonModel($data);
500
    }
501
 
1320 efrain 502
    public function subordinatesAction()
503
    {
504
        $currentUserPlugin = $this->plugin('currentUserPlugin');
505
        $currentUser = $currentUserPlugin->getUser();
506
        $currentCompany = $currentUserPlugin->getCompany();
507
 
508
        $request = $this->getRequest();
509
        $uuid = $this->params()->fromRoute('job_description_id');
510
 
511
 
512
        if(!$uuid) {
513
            $data = [
514
                'success'   => false,
515
                'data'   => 'ERROR_INVALID_PARAMETER'
516
            ];
517
 
518
            return new JsonModel($data);
519
        }
520
 
521
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
522
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
523
        if(!$jobDescription) {
524
            $data = [
525
                'success'   => false,
526
                'data'   => 'ERROR_RECORD_NOT_FOUND'
527
            ];
528
 
529
            return new JsonModel($data);
530
        }
531
 
532
 
533
 
534
 
535
        if($jobDescription->company_id != $currentCompany->id) {
536
            $data = [
537
                'success'   => false,
538
                'data'   => 'ERROR_UNAUTHORIZED'
539
            ];
540
 
541
            return new JsonModel($data);
542
        }
543
 
544
 
545
        if ($request->isGet()) {
546
            $ids = [];
547
 
548
            $userMapper = UserMapper::getInstance($this->adapter);
549
            $positionMapper = PositionMapper::getInstance($this->adapter);
550
 
551
 
552
            $jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
553
            $jobDescriptionSubordinates = $jobDescriptionSubordinateMapper->fetchAllByJobDescriptionIdTopLevel($jobDescription->id);
554
 
555
            foreach($jobDescriptionSubordinates as $jobDescriptionSubordinate)
556
            {
557
                $positions = $positionMapper->fetchAllByJobDescriptionIdAndCompanyId($jobDescriptionSubordinate->job_description_id_low_level, $currentCompany->id);
558
 
559
                foreach($positions as $position)
560
                {
561
                    if(!in_array($position->user_id, $ids)) {
562
                        array_push($ids, $position->user_id);
563
                    }
564
                }
565
 
566
            }
567
 
568
            $data = [];
569
 
570
            if($ids) {
571
                $users = $userMapper->fetchAllByIds($ids);
572
                foreach($users as $user)
573
                {
574
                    array_push($data, [
575
                        'uuid' => $user->uuid,
576
                        'name' => trim(trim($user->first_name) . ' ' . trim($user->last_name)) . ' (' . trim($user->email) . ')',
577
                    ]);
578
                }
579
            }
580
 
581
 
582
            $response = [
583
                'success' => true,
584
                'data' => $data
585
            ];
586
 
587
            return new JsonModel($response);
588
        } else {
589
            $data = [
590
                'success' => false,
591
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
592
            ];
593
 
594
 
595
        }
596
 
597
        return new JsonModel($data);
598
    }
67 efrain 599
 
1320 efrain 600
 
67 efrain 601
}