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