Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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