Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16790 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16770 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
 
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
use LeadersLinked\Library\Functions;
13
use LeadersLinked\Form\Organization\PositionForm;
14
use LeadersLinked\Mapper\UserMapper;
15
use LeadersLinked\Mapper\JobDescriptionMapper;
16
use LeadersLinked\Mapper\OrganizationPositionMapper;
17
use LeadersLinked\Model\OrganizationPosition;
18
use LeadersLinked\Mapper\OrganizationPositionSubordinateMapper;
19
use LeadersLinked\Form\Organization\DatatableForm;
20
 
21
 
22
class OrganizationPositionController extends AbstractActionController
23
{
24
    /**
25
     *
26
     * @var \Laminas\Db\Adapter\AdapterInterface
27
     */
28
    private $adapter;
29
 
30
    /**
31
     *
32
     * @var \LeadersLinked\Cache\CacheInterface
33
     */
34
    private $cache;
35
 
36
 
37
    /**
38
     *
39
     * @var \Laminas\Log\LoggerInterface
40
     */
41
    private $logger;
42
 
43
    /**
44
     *
45
     * @var array
46
     */
47
    private $config;
48
 
49
 
50
    /**
51
     *
52
     * @var \Laminas\Mvc\I18n\Translator
53
     */
54
    private $translator;
55
 
56
 
57
    /**
58
     *
59
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
60
     * @param \LeadersLinked\Cache\CacheInterface $cache
61
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
62
     * @param array $config
63
     * @param \Laminas\Mvc\I18n\Translator $translator
64
     */
65
    public function __construct($adapter, $cache, $logger, $config, $translator)
66
    {
67
        $this->adapter      = $adapter;
68
        $this->cache        = $cache;
69
        $this->logger       = $logger;
70
        $this->config       = $config;
71
        $this->translator   = $translator;
72
    }
73
 
74
    public function indexAction()
75
    {
76
        $currentUserPlugin = $this->plugin('currentUserPlugin');
77
        $currentUser = $currentUserPlugin->getUser();
78
        $currentCompany = $currentUserPlugin->getCompany();
79
 
80
        $request = $this->getRequest();
81
 
82
        $headers  = $request->getHeaders();
83
 
84
        $request = $this->getRequest();
85
        if($request->isGet()) {
86
 
87
 
88
            $headers  = $request->getHeaders();
89
 
90
            $isJson = false;
91
            if($headers->has('Accept')) {
92
                $accept = $headers->get('Accept');
93
 
94
                $prioritized = $accept->getPrioritized();
95
 
96
                foreach($prioritized as $key => $value) {
97
                    $raw = trim($value->getRaw());
98
 
99
                    if(!$isJson) {
100
                        $isJson = strpos($raw, 'json');
101
                    }
102
 
103
                }
104
            }
105
 
106
            if($isJson) {
107
                $job_description_id = Functions::sanitizeFilterString($this->params()->fromQuery('job_description_id'));
108
                if(empty($job_description_id)) {
109
 
110
                    return new JsonModel([
111
                        'success' => false,
112
                        'data' => [
113
                            'items' => [],
114
                            'total' => 0,
115
                            'link_add' => ''
116
                        ]
117
                    ]);
118
                }
119
 
120
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
121
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($job_description_id);
122
 
123
                if(!$jobDescription) {
124
 
125
                    return new JsonModel([
126
                        'success' => false,
127
                        'data' => 'ERROR_JOB_DESCRIPTION_NOT_FOUND'
128
                    ]);
129
                }
130
 
131
 
132
                if($jobDescription->company_id != $currentCompany->id) {
133
                    return new JsonModel([
134
                        'success' => false,
135
                        'data' => 'ERROR_UNAUTHORIZED'
136
                    ]);
137
                }
138
 
139
 
140
 
141
 
142
 
143
 
144
                $search = $this->params()->fromQuery('search', []);
145
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
146
 
147
                $page               = intval($this->params()->fromQuery('start', 1), 10);
148
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
149
                $order =  $this->params()->fromQuery('order', []);
150
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
151
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
152
 
153
                $fields =  ['user'];
154
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'user';
155
 
156
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
157
                    $order_direction = 'ASC';
158
                }
159
 
160
                $positionMapper = OrganizationPositionMapper::getInstance($this->adapter);
161
                $paginator = $positionMapper->fetchAllDataTableByCompanyIdAndJobDescriptionId($jobDescription->company_id, $jobDescription->id, $search, $page, $records_x_page, $order_field, $order_direction);
162
 
163
                $items = [];
164
                $records = $paginator->getCurrentItems();
165
                foreach($records as $record)
166
                {
167
                    $item = [
168
                        'user' => $record['user'],
169
                        'status' => $record['status'],
170
                        'actions' => [
171
                            'link_edit' => $this->url()->fromRoute('organization/positions/edit', ['id' => $record['uuid'] ]),
172
                            'link_delete' => $this->url()->fromRoute('organization/positions/delete', ['id' => $record['uuid'] ]),
173
                            'link_subordinates' => $this->url()->fromRoute('organization/positions/subordinates', ['id' => $record['uuid'] ])
174
                        ]
175
                    ];
176
 
177
                    array_push($items, $item);
178
                }
179
 
180
                return new JsonModel([
181
                    'success' => true,
182
                    'data' => [
183
                        'items' => $items,
184
                        'total' => $paginator->getTotalItemCount(),
185
                        'link_add' => $this->url()->fromRoute('organization/positions/add', ['job_description_id' => $jobDescription->uuid ]),
186
                    ]
187
                ]);
188
 
189
 
190
            } else  {
191
 
192
                $formDatatable    = new DatatableForm($this->adapter, $currentCompany->id);
193
                $formPosition = new PositionForm($this->adapter, $currentCompany->id);
194
 
195
                $this->layout()->setTemplate('layout/layout-backend');
196
                $viewModel = new ViewModel();
197
                $viewModel->setTemplate('leaders-linked/organization/positions');
198
                $viewModel->setVariables([
199
                    'formDatatable' => $formDatatable,
200
                    'formPosition' => $formPosition,
201
 
202
 
203
                ]);
204
                return $viewModel ;
205
            }
206
 
207
        } else {
208
            return new JsonModel([
209
                'success' => false,
210
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
211
            ]);;
212
        }
213
    }
214
 
215
    public function addAction()
216
    {
217
        $currentUserPlugin = $this->plugin('currentUserPlugin');
218
        $currentUser = $currentUserPlugin->getUser();
219
        $currentCompany = $currentUserPlugin->getCompany();
220
 
221
        $request = $this->getRequest();
222
 
223
 
224
        if($request->isPost()) {
225
            $job_description_id = $this->params()->fromRoute('job_description_id');
226
 
227
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
228
            $jobDescription = $jobDescriptionMapper->fetchOneByUuid($job_description_id);
229
 
230
            if(!$jobDescription) {
231
                return new JsonModel([
232
                    'success'   => false,
233
                    'data'   => 'JOB_DESCRIPTION_NOT_FOUND'
234
                ]);
235
            }
236
 
237
 
238
            if($jobDescription->company_id != $currentCompany->id) {
239
                return new JsonModel([
240
                    'success'   => false,
241
                    'data'   =>  'ERROR_UNAUTHORIZED'
242
                ]);
243
            }
244
 
245
 
246
            $form = new PositionForm($this->adapter, $currentCompany->id);
247
            $dataPost = $request->getPost()->toArray();
248
 
249
            $form->setData($dataPost);
250
 
251
            if($form->isValid()) {
252
                $dataPost = (array) $form->getData();
253
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : OrganizationPosition::STATUS_INACTIVE;
254
 
255
                $userMapper = UserMapper::getInstance($this->adapter);
256
                $user = $userMapper->fetchOneByUuid($dataPost['user_id']);
257
                $dataPost['user_id'] = $user->id;
258
 
259
                $hydrator = new ObjectPropertyHydrator();
260
                $position = new OrganizationPosition();
261
                $position->company_id = $jobDescription->company_id;
262
                $position->job_description_id = $jobDescription->id;
263
 
264
                $hydrator->hydrate($dataPost, $position);
265
 
266
 
267
                $positionMapper = OrganizationPositionMapper::getInstance($this->adapter);
268
                $result = $positionMapper->insert($position);
269
 
270
 
271
                if($result) {
272
                    $this->logger->info('Se agrego la posicion ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
273
 
274
                    $data = [
275
                        'success'   => true,
276
                        'data'   => 'LABEL_RECORD_ADDED'
277
                    ];
278
                } else {
279
                    $data = [
280
                        'success'   => false,
281
                        'data'      => $positionMapper->getError()
282
                    ];
283
 
284
                }
285
 
286
                return new JsonModel($data);
287
 
288
            } else {
289
                $messages = [];
290
                $form_messages = (array) $form->getMessages();
291
                foreach($form_messages  as $fieldname => $field_messages)
292
                {
293
 
294
                    $messages[$fieldname] = array_values($field_messages);
295
                }
296
 
297
                return new JsonModel([
298
                    'success'   => false,
299
                    'data'   => $messages
300
                ]);
301
            }
302
 
303
        } else {
304
            $data = [
305
                'success' => false,
306
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
307
            ];
308
 
309
            return new JsonModel($data);
310
        }
311
 
312
        return new JsonModel($data);
313
    }
314
 
315
    public function editAction()
316
    {
317
        $currentUserPlugin = $this->plugin('currentUserPlugin');
318
        $currentUser = $currentUserPlugin->getUser();
319
        $currentCompany = $currentUserPlugin->getCompany();
320
 
321
        $request = $this->getRequest();
322
        $uuid = $this->params()->fromRoute('id');
323
 
324
 
325
        if(!$uuid) {
326
            $data = [
327
                'success'   => false,
328
                'data'   => 'ERROR_INVALID_PARAMETER'
329
            ];
330
 
331
            return new JsonModel($data);
332
        }
333
 
334
        $positionMapper = OrganizationPositionMapper::getInstance($this->adapter);
335
        $position = $positionMapper->fetchOneByUuid($uuid);
336
        if(!$position) {
337
            $data = [
338
                'success'   => false,
339
                'data'   => 'ERROR_RECORD_NOT_FOUND'
340
            ];
341
 
342
            return new JsonModel($data);
343
        }
344
 
345
        if($position->company_id != $currentCompany->id) {
346
            $data = [
347
                'success'   => false,
348
                'data'   => 'ERROR_UNAUTHORIZED'
349
            ];
350
 
351
            return new JsonModel($data);
352
        }
353
 
354
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
355
        $jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
356
 
357
        if($request->isPost()) {
358
            $form = new PositionForm($this->adapter, $currentCompany->id);
359
            $dataPost = $request->getPost()->toArray();
360
 
361
 
362
            $form->setData($dataPost);
363
 
364
            if($form->isValid()) {
365
                $dataPost = (array) $form->getData();
366
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : OrganizationPosition::STATUS_INACTIVE;
367
 
368
                $userMapper = UserMapper::getInstance($this->adapter);
369
                $user = $userMapper->fetchOneByUuid($dataPost['user_id']);
370
                $dataPost['user_id'] = $user->id;
371
 
372
                $hydrator = new ObjectPropertyHydrator();
373
                $hydrator->hydrate($dataPost, $position);
374
 
375
                $result = $positionMapper->update($position);
376
 
377
                if($result) {
378
                    $this->logger->info('Se actualizo la posición ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
379
 
380
                    $data = [
381
                        'success' => true,
382
                        'data' => 'LABEL_RECORD_UPDATED'
383
                    ];
384
                } else {
385
                    $data = [
386
                        'success'   => false,
387
                        'data'      => $positionMapper->getError()
388
                    ];
389
                }
390
 
391
                return new JsonModel($data);
392
 
393
            } else {
394
                $messages = [];
395
                $form_messages = (array) $form->getMessages();
396
                foreach($form_messages  as $fieldname => $field_messages)
397
                {
398
                    $messages[$fieldname] = array_values($field_messages);
399
                }
400
 
401
                return new JsonModel([
402
                    'success'   => false,
403
                    'data'   => $messages
404
                ]);
405
            }
406
        } else if ($request->isGet()) {
407
            $userMapper = UserMapper::getInstance($this->adapter);
408
            $user = $userMapper->fetchOne($position->user_id);
409
 
410
 
411
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
412
            $jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
413
 
414
 
415
            $hydrator = new ObjectPropertyHydrator();
416
 
417
            $data = $hydrator->extract($position);
418
            $data['user_id'] = $user->uuid;
419
            $data['job_description_id'] = $jobDescription->uuid;
420
            $data['users'] = [];
421
            $data['subordinates'] = [];
422
 
423
            $ids = [];
424
 
425
            $userMapper = UserMapper::getInstance($this->adapter);
426
 
427
            $positionSubordinateMapper = OrganizationPositionSubordinateMapper::getInstance($this->adapter);
428
            $subordinates = $positionSubordinateMapper->fetchAllByPositionId($position->id);
429
 
430
            foreach($subordinates as $subordinate)
431
            {
432
                $user = $userMapper->fetchOne($subordinate->user_id);
433
                if($user) {
434
                    array_push($data['subordinates'], $user->uuid);
435
                }
436
            }
437
 
438
            /*
439
            $jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
440
            $jobDescriptionSubordinates = $jobDescriptionSubordinateMapper->fetchAllByJobDescriptionIdTopLevel($jobDescription->id);
441
 
442
            foreach($jobDescriptionSubordinates as $jobDescriptionSubordinate)
443
            {
444
                $positions = $positionMapper->fetchAllByJobDescriptionIdAndCompanyId($jobDescriptionSubordinate->job_description_id_low_level, $currentCompany->id);
445
 
446
                foreach($positions as $position)
447
                {
448
                    if(!in_array($position->user_id, $ids)) {
449
                        array_push($ids, $position->user_id);
450
                    }
451
                }
452
 
453
            }*/
454
 
455
 
456
 
457
            if($ids) {
458
                $users = $userMapper->fetchAllByIds($ids);
459
                foreach($users as $user)
460
                {
461
                    array_push($data['users'], [
462
                        'uuid' => $user->uuid,
463
                        'name' => trim(trim($user->first_name) . ' ' . trim($user->last_name)) . ' (' . trim($user->email) . ')',
464
                    ]);
465
 
466
                }
467
            }
468
 
469
 
470
            $response = [
471
                'success' => true,
472
                'data' => $data
473
            ];
474
 
475
            return new JsonModel($response);
476
        } else {
477
            $data = [
478
                'success' => false,
479
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
480
            ];
481
 
482
            return new JsonModel($data);
483
        }
484
 
485
        return new JsonModel($data);
486
    }
487
 
488
    public function deleteAction()
489
    {
490
        $currentUserPlugin = $this->plugin('currentUserPlugin');
491
        $currentUser = $currentUserPlugin->getUser();
492
        $currentCompany = $currentUserPlugin->getCompany();
493
 
494
        $request = $this->getRequest();
495
        $uuid = $this->params()->fromRoute('id');
496
 
497
        if(!$uuid) {
498
            $data = [
499
                'success'   => false,
500
                'data'   => 'ERROR_INVALID_PARAMETER'
501
            ];
502
 
503
            return new JsonModel($data);
504
        }
505
 
506
 
507
        $positionMapper = OrganizationPositionMapper::getInstance($this->adapter);
508
        $position = $positionMapper->fetchOneByUuid($uuid);
509
        if(!$position) {
510
            $data = [
511
                'success'   => false,
512
                'data'   => 'ERROR_RECORD_NOT_FOUND'
513
            ];
514
 
515
            return new JsonModel($data);
516
        }
517
 
518
        if($position->company_id != $currentCompany->id) {
519
            $data = [
520
                'success'   => false,
521
                'data'   => 'ERROR_UNAUTHORIZED'
522
            ];
523
 
524
            return new JsonModel($data);
525
        }
526
 
527
        if($request->isPost()) {
528
            $result = $positionMapper->delete($position);
529
            if($result) {
530
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
531
                $jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
532
 
533
                $this->logger->info('Se borro la posición ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
534
 
535
                $data = [
536
                    'success' => true,
537
                    'data' => 'LABEL_RECORD_DELETED'
538
                ];
539
            } else {
540
 
541
                $data = [
542
                    'success'   => false,
543
                    'data'      => $positionMapper->getError()
544
                ];
545
 
546
                return new JsonModel($data);
547
            }
548
 
549
        } else {
550
            $data = [
551
                'success' => false,
552
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
553
            ];
554
 
555
            return new JsonModel($data);
556
        }
557
 
558
        return new JsonModel($data);
559
    }
560
    /*
561
    public function subordinatesAction()
562
    {
563
        $currentUserPlugin = $this->plugin('currentUserPlugin');
564
        $currentUser = $currentUserPlugin->getUser();
565
        $currentCompany = $currentUserPlugin->getCompany();
566
 
567
        $request = $this->getRequest();
568
        $uuid = $this->params()->fromRoute('job_description_id');
569
 
570
 
571
        if(!$uuid) {
572
            $data = [
573
                'success'   => false,
574
                'data'   => 'ERROR_INVALID_PARAMETER'
575
            ];
576
 
577
            return new JsonModel($data);
578
        }
579
 
580
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
581
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
582
        if(!$jobDescription) {
583
            $data = [
584
                'success'   => false,
585
                'data'   => 'ERROR_RECORD_NOT_FOUND'
586
            ];
587
 
588
            return new JsonModel($data);
589
        }
590
 
591
 
592
 
593
 
594
        if($jobDescription->company_id != $currentCompany->id) {
595
            $data = [
596
                'success'   => false,
597
                'data'   => 'ERROR_UNAUTHORIZED'
598
            ];
599
 
600
            return new JsonModel($data);
601
        }
602
 
603
 
604
        if ($request->isGet()) {
605
            $ids = [];
606
 
607
            $userMapper = UserMapper::getInstance($this->adapter);
608
            $positionMapper = PositionMapper::getInstance($this->adapter);
609
 
610
 
611
            $jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
612
            $jobDescriptionSubordinates = $jobDescriptionSubordinateMapper->fetchAllByJobDescriptionIdTopLevel($jobDescription->id);
613
 
614
            foreach($jobDescriptionSubordinates as $jobDescriptionSubordinate)
615
            {
616
                $positions = $positionMapper->fetchAllByJobDescriptionIdAndCompanyId($jobDescriptionSubordinate->job_description_id_low_level, $currentCompany->id);
617
 
618
                foreach($positions as $position)
619
                {
620
                    if(!in_array($position->user_id, $ids)) {
621
                        array_push($ids, $position->user_id);
622
                    }
623
                }
624
 
625
            }
626
 
627
            $data = [];
628
 
629
            if($ids) {
630
                $users = $userMapper->fetchAllByIds($ids);
631
                foreach($users as $user)
632
                {
633
                    array_push($data, [
634
                        'uuid' => $user->uuid,
635
                        'name' => trim(trim($user->first_name) . ' ' . trim($user->last_name)) . ' (' . trim($user->email) . ')',
636
                    ]);
637
                }
638
            }
639
 
640
 
641
            $response = [
642
                'success' => true,
643
                'data' => $data
644
            ];
645
 
646
            return new JsonModel($response);
647
        } else {
648
            $data = [
649
                'success' => false,
650
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
651
            ];
652
 
653
 
654
        }
655
 
656
        return new JsonModel($data);
657
    }
658
 
659
    public function graphAction()
660
    {
661
        $currentUserPlugin = $this->plugin('currentUserPlugin');
662
        $currentUser = $currentUserPlugin->getUser();
663
        $currentCompany = $currentUserPlugin->getCompany();
664
 
665
        $request = $this->getRequest();
666
        $id = $this->params()->fromRoute('id');
667
 
668
 
669
        if(!$id) {
670
            $data = [
671
                'success'   => false,
672
                'data'   => 'ERROR_INVALID_PARAMETER'
673
            ];
674
 
675
            return new JsonModel($data);
676
        }
677
 
678
 
679
        $positionMapper = PositionMapper::getInstance($this->adapter);
680
        $position = $positionMapper->fetchOneByUuid($id);
681
        if(!$position) {
682
            $data = [
683
                'success'   => false,
684
                'data'   => 'ERROR_RECORD_NOT_FOUND'
685
            ];
686
 
687
            return new JsonModel($data);
688
        }
689
 
690
 
691
 
692
 
693
        if($position->company_id != $currentCompany->id) {
694
            $data = [
695
                'success'   => false,
696
                'data'   => 'ERROR_UNAUTHORIZED'
697
            ];
698
 
699
            return new JsonModel($data);
700
        }
701
 
702
 
703
        if ($request->isGet()) {
704
            $data = [
705
                'success' => false,
706
                'data' => [
707
                    'job_description_id' => $position->job_description_id,
708
                    'user_id' => $position->user_id,
709
                    'status' => $position->status,
710
                ]
711
            ];
712
 
713
 
714
 
715
        } else {
716
            $data = [
717
                'success' => false,
718
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
719
            ];
720
 
721
 
722
        }
723
 
724
        return new JsonModel($data);
725
    }*/
726
 
727
 
728
}