Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16770 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16770 efrain 1
<?php
16790 efrain 2
 
16770 efrain 3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
 
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\View\Model\ViewModel;
10
use Laminas\View\Model\JsonModel;
16790 efrain 11
use LeadersLinked\Form\OrganizationPosition\OrganizationPositionForm;
12
use LeadersLinked\Model\Company;
13
use LeadersLinked\Model\OrganizationPosition;
14
use LeadersLinked\Model\OrganizationPositionCompetency;
15
use LeadersLinked\Model\OrganizationPositionCompetencyBehavior;
16
use LeadersLinked\Mapper\BehaviorMapper;
17
use LeadersLinked\Mapper\CompetencyMapper;
18
use LeadersLinked\Mapper\CompetencyBehaviorMapper;
19
use LeadersLinked\Mapper\CompetencyTypeMapper;
20
use LeadersLinked\Mapper\OrganizationPositionMapper;
21
use LeadersLinked\Mapper\OrganizationPositionCompetencyBehaviorMapper;
22
use LeadersLinked\Mapper\OrganizationPositionCompetencyMapper;
16770 efrain 23
use LeadersLinked\Library\Functions;
16790 efrain 24
use LeadersLinked\Library\OrganizationPositionPdf;
25
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
26
use LeadersLinked\Model\User;
27
use Laminas\Http\Response;
16770 efrain 28
use LeadersLinked\Mapper\JobDescriptionMapper;
29
 
30
 
31
class OrganizationPositionController extends AbstractActionController
32
{
33
    /**
34
     *
35
     * @var \Laminas\Db\Adapter\AdapterInterface
36
     */
37
    private $adapter;
38
 
39
    /**
40
     *
41
     * @var \LeadersLinked\Cache\CacheInterface
42
     */
43
    private $cache;
44
 
45
 
46
    /**
47
     *
48
     * @var \Laminas\Log\LoggerInterface
49
     */
50
    private $logger;
51
 
52
    /**
53
     *
54
     * @var array
55
     */
56
    private $config;
57
 
58
 
59
    /**
60
     *
61
     * @var \Laminas\Mvc\I18n\Translator
62
     */
63
    private $translator;
64
 
65
 
66
    /**
67
     *
68
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
69
     * @param \LeadersLinked\Cache\CacheInterface $cache
70
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
71
     * @param array $config
72
     * @param \Laminas\Mvc\I18n\Translator $translator
73
     */
74
    public function __construct($adapter, $cache, $logger, $config, $translator)
75
    {
76
        $this->adapter      = $adapter;
77
        $this->cache        = $cache;
78
        $this->logger       = $logger;
79
        $this->config       = $config;
80
        $this->translator   = $translator;
81
    }
16790 efrain 82
 
16770 efrain 83
    public function indexAction()
84
    {
85
        $currentUserPlugin = $this->plugin('currentUserPlugin');
86
        $currentUser = $currentUserPlugin->getUser();
87
        $currentCompany = $currentUserPlugin->getCompany();
16790 efrain 88
 
16770 efrain 89
 
90
        $request = $this->getRequest();
16790 efrain 91
        $headers = $request->getHeaders();
92
 
16770 efrain 93
 
16790 efrain 94
        if ($request->isPost()) {
16770 efrain 95
 
16790 efrain 96
            $data = $this->params()->fromPost('data');
97
 
98
            if(!empty($data)) {
99
 
100
                $json = json_decode($data, true);
101
                if(is_array($json)) {
102
 
103
                    $jobDescriptionIdBoss = 0;
104
                    $jobDescriptionMapper = OrganizationPositionMapper::getInstance($this->adapter);
105
 
106
 
107
 
108
                    $position = 0;
109
                    foreach($json as $item)
110
                    {
111
 
112
                        $this->recursiveSave($currentCompany, $jobDescriptionMapper, $jobDescriptionIdBoss, $item, $position);
113
                        $position++;
114
                    }
115
 
116
                }
117
            }
118
            return new JsonModel([
119
                'success' => true,
120
            ]);
16770 efrain 121
 
122
 
16790 efrain 123
        } else if ($request->isGet()) {
124
 
125
 
126
            $headers = $request->getHeaders();
127
 
16770 efrain 128
            $isJson = false;
16790 efrain 129
            if ($headers->has('Accept')) {
16770 efrain 130
                $accept = $headers->get('Accept');
16790 efrain 131
 
16770 efrain 132
                $prioritized = $accept->getPrioritized();
16790 efrain 133
 
134
                foreach ($prioritized as $key => $value) {
16770 efrain 135
                    $raw = trim($value->getRaw());
16790 efrain 136
 
137
                    if (!$isJson) {
16770 efrain 138
                        $isJson = strpos($raw, 'json');
139
                    }
140
                }
141
            }
16790 efrain 142
 
143
            if ($isJson) {
16770 efrain 144
 
145
 
146
 
16790 efrain 147
                $items = [];
16770 efrain 148
 
16790 efrain 149
                $jobDescriptionIdBoss = 0;
150
                $jobDescriptionMapper = OrganizationPositionMapper::getInstance($this->adapter);
16770 efrain 151
 
152
 
153
 
16790 efrain 154
                $items = $this->recursiveLoad($currentUser, $currentCompany, $jobDescriptionMapper, $jobDescriptionIdBoss);
155
 
16770 efrain 156
 
157
 
16790 efrain 158
 
16770 efrain 159
                return new JsonModel([
160
                    'success' => true,
161
                    'data' => [
162
                        'items' => $items,
16790 efrain 163
 
16770 efrain 164
                    ]
165
                ]);
16790 efrain 166
 
167
            } else {
16770 efrain 168
 
169
 
170
 
171
 
16790 efrain 172
                $form = new OrganizationPositionForm($this->adapter, $currentCompany ? $currentCompany->id : null);
173
 
16770 efrain 174
                $this->layout()->setTemplate('layout/layout-backend');
175
                $viewModel = new ViewModel();
16790 efrain 176
                $viewModel->setTemplate('leaders-linked/jobs-description/index.phtml');
16770 efrain 177
                $viewModel->setVariables([
16790 efrain 178
                    'form' => $form,
179
                    'company_name' => $currentCompany->name,
180
 
16770 efrain 181
 
182
                ]);
16790 efrain 183
                return $viewModel;
184
            }
16770 efrain 185
        } else {
186
            return new JsonModel([
187
                'success' => false,
188
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
16790 efrain 189
            ]);
16770 efrain 190
        }
191
    }
192
 
16790 efrain 193
 
194
    /**
195
     *
196
     * @param Company $currentCompany
197
     * @param OrganizationPositionMapper $jobDescriptionMapper
198
     * @param int $jobDescriptionIdBoss
199
     * @param int $position
200
     * @param array $item
201
     */
202
    private function recursiveSave($currentCompany, $jobDescriptionMapper, $jobDescriptionIdBoss, $item, $position)
203
    {
204
 
205
 
206
        if(!empty($item) && is_array($item)) {
207
            foreach($item as $subitem)
208
            {
209
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($subitem['id']);
210
                if($jobDescriptionMapper) {
211
                    if($jobDescriptionIdBoss) {
212
 
213
 
214
                        $jobDescriptionMapper->setOrganizationPositionIdBossByJobDescripcionId($jobDescription->id,  $jobDescriptionIdBoss, $position);
215
                    } else {
216
                        $jobDescriptionMapper->removeOrganizationPositionIdBossByOrganizationPositionId($jobDescription->id, $position);
217
                    }
218
 
219
                    $position++;
220
 
221
                    if(!empty($subitem['children']) && is_array($subitem['children'])) {
222
 
223
                        $position2 = 0;
224
                        foreach($subitem['children'] as $subitem2)
225
                        {
226
                            $this->recursiveSave($currentCompany, $jobDescriptionMapper, $jobDescription->id,  $subitem2, $position2);
227
                            $position2++;
228
                        }
229
 
230
                    }
231
 
232
 
233
                }
234
            }
235
        }
236
    }
237
 
238
 
239
    /**
240
     *
241
     * @param User $currentUser
242
     * @param Company $currentCompany
243
     * @param OrganizationPositionMapper $jobDescriptionMapper
244
     * @param int $jobDescriptionIdBoss
245
     * @return array
246
     */
247
    private function recursiveLoad($currentUser, $currentCompany, $jobDescriptionMapper, $jobDescriptionIdBoss)
248
    {
249
        $acl            = $this->getEvent()->getViewModel()->getVariable('acl');
250
        $allowAdd       = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/add') ? 1 : 0;
251
        $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/edit') ? 1 : 0;
252
        $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/delete') ? 1 : 0;
253
        $allowReport    = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/report') ? 1 : 0;
254
 
255
 
256
        $items = [];
257
 
258
 
259
        if ($currentCompany) {
260
            $records = $jobDescriptionMapper->fetchAllByCompanyIdAndOrganizationPositionIdBoss($currentCompany->id, $jobDescriptionIdBoss);
261
        } else {
262
            $records = $jobDescriptionMapper->fetchAllDefaultAndOrganizationPositionIdBoss($jobDescriptionIdBoss);
263
        }
264
 
265
 
266
        foreach($records as $record)
267
        {
268
 
269
 
270
 
271
            array_push($items, [
272
                'uuid' => $record->uuid,
273
                'name' => $record->name,
274
                'children' => $this->recursiveLoad($currentUser, $currentCompany, $jobDescriptionMapper, $record->id),
275
                'status' => $record->status,
276
                'link_report' => $allowReport ? $this->url()->fromRoute('jobs-description/report', ['id' => $record->uuid]) : '',
277
                'link_edit' => $allowEdit ? $this->url()->fromRoute('jobs-description/edit', ['id' => $record->uuid]) : '',
278
                'link_delete' => $allowDelete ? $this->url()->fromRoute('jobs-description/delete', ['id' => $record->uuid]) : '',
279
                'link_add' => $allowAdd ? $this->url()->fromRoute('jobs-description/add', ['id' => $record->uuid]) : '',
280
            ]);
281
        }
282
 
283
        return $items;
284
    }
285
 
16770 efrain 286
    public function addAction()
287
    {
16790 efrain 288
 
289
 
16770 efrain 290
        $currentUserPlugin = $this->plugin('currentUserPlugin');
291
        $currentUser = $currentUserPlugin->getUser();
292
        $currentCompany = $currentUserPlugin->getCompany();
16790 efrain 293
 
16770 efrain 294
        $request = $this->getRequest();
16790 efrain 295
 
296
 
297
        if ($request->isGet()) {
298
            $behaviors = [];
299
            $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
16770 efrain 300
 
301
 
16790 efrain 302
            if($currentCompany) {
303
                $records = $behaviorMapper->fetchAllActiveByCompanyId($currentCompany->id);
304
            } else {
305
                $records = $behaviorMapper->fetchAllActiveByDefault();
306
            }
307
 
308
            $behaviorIds = [];
309
            foreach($records as $record)
310
            {
311
 
312
                $behaviorIds[ $record->id ] = $record->uuid;
313
 
314
                array_push($behaviors, [
315
                    'uuid' => $record->uuid,
316
                    'description' => $record->description
16770 efrain 317
                ]);
318
            }
319
 
16790 efrain 320
 
321
            $competencyTypes = [];
322
 
323
 
324
            $competencyTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
325
 
326
            if($currentCompany) {
327
                $records = $competencyTypeMapper->fetchAllActiveByCompanyId($currentCompany->id);
328
            } else {
329
                $records = $competencyTypeMapper->fetchAllActiveByDefault();
330
            }
331
 
332
 
333
            $competencyTypeIds = [];
334
            foreach($records as $record)
335
            {
336
                $competencyTypeIds[ $record->id ] =  $record->uuid;
337
 
338
 
339
                array_push($competencyTypes, [
340
                    'uuid' => $record->uuid,
341
                    'name' => $record->name,
16770 efrain 342
                ]);
343
            }
344
 
16790 efrain 345
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
346
            if($currentCompany) {
347
                $records = $competencyMapper->fetchAllActiveByCompanyId($currentCompany->id);
348
            } else {
349
                $records = $competencyMapper->fetchAllActiveByDefault();
350
            }
16770 efrain 351
 
16790 efrain 352
            $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
16770 efrain 353
 
354
 
16790 efrain 355
 
356
            $competencies = [];
357
 
358
            foreach($records as $record)
359
            {
360
                if(!isset($competencyTypeIds[ $record->competency_type_id ])) {
361
                    continue;
362
                }
16770 efrain 363
 
16790 efrain 364
                $behaviors_by_competency = [];
16770 efrain 365
 
16790 efrain 366
                $competencyBehaviors = $competencyBehaviorMapper->fetchAllByCompetencyId($record->id);
367
                foreach($competencyBehaviors as $competencyBehavior)
368
                {
369
                    if(!isset($behaviorIds[ $competencyBehavior->behavior_id ])) {
370
                        continue;
371
                    }
372
 
373
                    array_push($behaviors_by_competency, $behaviorIds[ $competencyBehavior->behavior_id ]);
374
                }
16770 efrain 375
 
16790 efrain 376
 
377
                if($behaviors_by_competency) {
378
                    array_push($competencies, [
379
                        'competency_type_uuid' => $competencyTypeIds[ $record->competency_type_id ],
380
                        'uuid' => $record->uuid,
381
                        'name' => $record->name,
382
                        'behaviors' => $behaviors_by_competency,
383
                    ]);
384
                }
385
 
386
 
387
            }
388
 
389
 
390
 
391
            $jobDescriptionMapper = OrganizationPositionMapper::getInstance($this->adapter);
392
            $records = $jobDescriptionMapper->fetchAllByCompanyId($currentCompany->id);
393
 
394
            $jobsDescription = [];
395
 
396
            foreach ($records as $record)
397
            {
398
                $jobsDescription[ $record->uuid ] = $record->name;
399
            }
16770 efrain 400
 
401
 
16790 efrain 402
            $data = [
403
                'success' => true,
404
                'data' => [
405
                    'jobs_description' => $jobsDescription,
406
                    'behaviors' => $behaviors,
407
                    'competency_types' => $competencyTypes,
408
                    'competencies' => $competencies,
409
                ]
410
            ];
411
 
412
 
413
            return new JsonModel($data);
414
        } else if ($request->isPost()) {
415
            $form = new OrganizationPositionForm($this->adapter, $currentCompany ? $currentCompany->id : null);
416
            $dataPost = $request->getPost()->toArray();
417
 
418
 
419
            $form->setData($dataPost);
420
 
421
            if ($form->isValid()) {
422
                $jobDescriptionMapper = OrganizationPositionMapper::getInstance($this->adapter);
16770 efrain 423
 
16790 efrain 424
                $dataPost = (array) $form->getData();
425
 
426
                if(!empty($dataPost['job_description_id_boss'])) {
427
 
428
                    $jobDescriptionBoos = $jobDescriptionMapper->fetchOneByUuid(Functions::sanitizeFilterString($dataPost['job_description_id_boss']));
429
                    if($jobDescriptionBoos && $jobDescriptionBoos->company_id == $currentCompany->id) {
430
                        $dataPost['job_description_id_boss'] = $jobDescriptionBoos->id;
431
                    } else {
432
                        $dataPost['job_description_id_boss'] = 0;
433
                    }
434
 
435
 
436
                }
16770 efrain 437
 
16790 efrain 438
 
439
 
440
                $dataPost['status'] = empty($dataPost['status']) ? OrganizationPosition::STATUS_INACTIVE : $dataPost['status'];
441
 
442
                $hydrator = new ObjectPropertyHydrator();
443
                $jobDescription = new OrganizationPosition();
444
 
445
 
446
                if ($currentCompany) {
447
                    $jobDescription->company_id = $currentCompany ? $currentCompany->id : null;
448
                }
449
 
450
                $hydrator->hydrate($dataPost, $jobDescription);
451
 
452
                $result = $jobDescriptionMapper->insert($jobDescription);
453
 
454
                if ($result) {
455
                    $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
456
                    $competencyMapper = CompetencyMapper::getInstance($this->adapter);
457
                    $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
16770 efrain 458
 
16790 efrain 459
 
460
                    $jobDescriptionCompetencyBehaviorMapper = OrganizationPositionCompetencyBehaviorMapper::getInstance($this->adapter);
461
                    $jobDescriptionCompetencyMapper = OrganizationPositionCompetencyMapper::getInstance($this->adapter);
462
 
463
                    $competencies_selected = isset($dataPost['competencies_selected']) ? $dataPost['competencies_selected'] : [];
464
                    foreach($competencies_selected as $competency_selected)
465
                    {
466
 
467
                        $competency_uuid = isset($competency_selected['competency_uuid']) ? $competency_selected['competency_uuid'] : '';
468
                        $competency = $competencyMapper->fetchOneByUuid($competency_uuid);
469
 
470
 
471
                        if($competency) {
472
                            if($currentCompany) {
473
                                $ok = $competency->company_id == $currentCompany->id;
474
                            } else {
475
                                $ok = empty($competency->company_id);
476
                            }
477
                        } else {
478
                            $ok = false;
479
                        }
480
 
481
                        if(!$ok) {
482
                            continue;
483
                        }
484
 
485
 
486
                        $behavior_uuid = isset($competency_selected['behavior_uuid']) ? $competency_selected['behavior_uuid'] : '';
487
                        $behavior = $behaviorMapper->fetchOneByUuid($behavior_uuid);
488
                        if($behavior) {
489
                            if($currentCompany) {
490
                                $ok = $behavior->company_id == $currentCompany->id;
491
                            } else {
492
                                $ok = empty($behavior->company_id);
493
                            }
494
 
495
 
496
                        } else {
497
                            $ok = false;
498
                        }
499
 
500
                        if($competency->company_id != $behavior->company_id) {
501
                            continue;
502
                        }
503
 
504
                        $competencyBehavior = $competencyBehaviorMapper->fetchOneByBehaviorIdAndCompetencyId($behavior->id, $competency->id);
505
                        if(!$competencyBehavior) {
506
                            continue;
507
                        }
508
 
509
                        $level = isset($competency_selected['level']) ? $competency_selected['level'] : 0;
510
                        $level = intval($level, 10);
511
 
512
 
513
                        $jobDescriptionCompetency =  $jobDescriptionCompetencyMapper->fetchOneByOrganizationPositionIdAndCompetencyId($jobDescription->id, $competency->id);
514
                        if(!$jobDescriptionCompetency) {
515
                            $jobDescriptionCompetency = new OrganizationPositionCompetency();
516
                            $jobDescriptionCompetency->competency_id = $competency->id;
517
                            $jobDescriptionCompetency->job_description_id = $jobDescription->id;
518
                            $jobDescriptionCompetencyMapper->insert($jobDescriptionCompetency);
519
 
520
                        }
521
 
522
 
523
 
524
                        $jobDescriptionCompetencyBehavior = new OrganizationPositionCompetencyBehavior();
525
                        $jobDescriptionCompetencyBehavior->job_description_id = $jobDescription->id;
526
                        $jobDescriptionCompetencyBehavior->competency_id = $competency->id;
527
                        $jobDescriptionCompetencyBehavior->behavior_id = $behavior->id;
528
                        $jobDescriptionCompetencyBehavior->level = $level;
529
                        $jobDescriptionCompetencyBehaviorMapper->insert($jobDescriptionCompetencyBehavior);
530
                    }
531
 
532
 
533
                    $this->logger->info('Se agrego el cargo ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
534
 
16770 efrain 535
                    $data = [
16790 efrain 536
                        'success' => true,
537
                        'data' => 'LABEL_RECORD_ADDED'
16770 efrain 538
                    ];
539
                } else {
540
                    $data = [
16790 efrain 541
                        'success' => false,
542
                        'data' => $jobDescriptionMapper->getError()
16770 efrain 543
                    ];
544
                }
16790 efrain 545
 
16770 efrain 546
                return new JsonModel($data);
547
            } else {
548
                $messages = [];
549
                $form_messages = (array) $form->getMessages();
16790 efrain 550
                foreach ($form_messages as $fieldname => $field_messages) {
551
 
16770 efrain 552
                    $messages[$fieldname] = array_values($field_messages);
553
                }
16790 efrain 554
 
16770 efrain 555
                return new JsonModel([
16790 efrain 556
                    'success' => false,
557
                    'data' => $messages
16770 efrain 558
                ]);
559
            }
560
        } else {
561
            $data = [
562
                'success' => false,
563
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
564
            ];
16790 efrain 565
 
16770 efrain 566
            return new JsonModel($data);
567
        }
16790 efrain 568
 
16770 efrain 569
        return new JsonModel($data);
570
    }
571
 
572
    public function editAction()
573
    {
574
        $currentUserPlugin = $this->plugin('currentUserPlugin');
575
        $currentUser = $currentUserPlugin->getUser();
576
        $currentCompany = $currentUserPlugin->getCompany();
16790 efrain 577
 
16770 efrain 578
        $request = $this->getRequest();
579
        $uuid = $this->params()->fromRoute('id');
580
 
16790 efrain 581
 
582
        if (!$uuid) {
16770 efrain 583
            $data = [
16790 efrain 584
                'success' => false,
585
                'data' => 'ERROR_INVALID_PARAMETER'
16770 efrain 586
            ];
16790 efrain 587
 
16770 efrain 588
            return new JsonModel($data);
589
        }
590
 
16790 efrain 591
        $jobDescriptionMapper = OrganizationPositionMapper::getInstance($this->adapter);
592
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
593
        if (!$jobDescription) {
16770 efrain 594
            $data = [
16790 efrain 595
                'success' => false,
596
                'data' => 'ERROR_RECORD_NOT_FOUND'
16770 efrain 597
            ];
16790 efrain 598
 
16770 efrain 599
            return new JsonModel($data);
600
        }
16790 efrain 601
 
602
        if ($currentCompany && $jobDescription->company_id != $currentCompany->id) {
16770 efrain 603
            $data = [
16790 efrain 604
                'success' => false,
605
                'data' => 'ERROR_UNAUTHORIZED'
16770 efrain 606
            ];
16790 efrain 607
 
16770 efrain 608
            return new JsonModel($data);
609
        }
16790 efrain 610
 
611
 
612
        if ($request->isPost()) {
613
            $form = new OrganizationPositionForm($this->adapter, $currentCompany ? $currentCompany->id : null, $jobDescription->id);
16770 efrain 614
            $dataPost = $request->getPost()->toArray();
16790 efrain 615
 
16770 efrain 616
            $form->setData($dataPost);
16790 efrain 617
 
618
            if ($form->isValid()) {
16770 efrain 619
                $dataPost = (array) $form->getData();
16790 efrain 620
                if(!empty($dataPost['job_description_id_boss'])) {
621
 
622
                    $jobDescriptionBoos = $jobDescriptionMapper->fetchOneByUuid(Functions::sanitizeFilterString($dataPost['job_description_id_boss']));
623
                    if($jobDescriptionBoos && $jobDescriptionBoos->company_id == $currentCompany->id) {
624
                        $dataPost['job_description_id_boss'] = $jobDescriptionBoos->id;
625
                    } else {
626
                        $dataPost['job_description_id_boss'] = 0;
627
                    }
628
                }
629
 
630
                $hydrator = new ObjectPropertyHydrator();
631
                $hydrator->hydrate($dataPost, $jobDescription);
632
 
16770 efrain 633
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : OrganizationPosition::STATUS_INACTIVE;
634
 
16790 efrain 635
                $jobDescriptionMapper = OrganizationPositionMapper::getInstance($this->adapter);
636
 
637
 
638
                $result = $jobDescriptionMapper->update($jobDescription);
16770 efrain 639
 
16790 efrain 640
                if ($result) {
641
                    $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
642
                    $competencyMapper = CompetencyMapper::getInstance($this->adapter);
643
                    $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
644
 
645
 
646
                    $jobDescriptionCompetencyBehaviorMapper = OrganizationPositionCompetencyBehaviorMapper::getInstance($this->adapter);
647
                    $jobDescriptionCompetencyBehaviorMapper->deleteAllBOrganizationPositionId($jobDescription->id);
648
 
649
                    $jobDescriptionCompetencyMapper = OrganizationPositionCompetencyMapper::getInstance($this->adapter);
650
                    $jobDescriptionCompetencyMapper->deleteAllBOrganizationPositionId($jobDescription->id);
651
 
652
                    $competencies_selected = isset($dataPost['competencies_selected']) ? $dataPost['competencies_selected'] : [];
653
                    foreach($competencies_selected as $competency_selected)
654
                    {
655
                        $competency_uuid = isset($competency_selected['competency_uuid']) ? $competency_selected['competency_uuid'] : '';
656
                        $competency = $competencyMapper->fetchOneByUuid($competency_uuid);
657
 
658
 
659
                        if($competency) {
660
                            if($currentCompany) {
661
                                $ok = $competency->company_id == $currentCompany->id;
662
                            } else {
663
                                $ok = empty($competency->company_id);
664
                            }
665
                        } else {
666
                            $ok = false;
667
                        }
668
 
669
                        if(!$ok) {
670
                            continue;
671
                        }
672
 
673
 
674
                        $behavior_uuid = isset($competency_selected['behavior_uuid']) ? $competency_selected['behavior_uuid'] : '';
675
                        $behavior = $behaviorMapper->fetchOneByUuid($behavior_uuid);
676
                        if($behavior) {
677
                            if($currentCompany) {
678
                                $ok = $behavior->company_id == $currentCompany->id;
679
                            } else {
680
                                $ok = empty($behavior->company_id);
681
                            }
682
 
683
 
684
                        } else {
685
                            $ok = false;
686
                        }
687
 
688
                        if($competency->company_id != $behavior->company_id) {
689
                            continue;
690
                        }
691
 
692
                        $competencyBehavior = $competencyBehaviorMapper->fetchOneByBehaviorIdAndCompetencyId($behavior->id, $competency->id);
693
                        if(!$competencyBehavior) {
694
                            continue;
695
                        }
696
 
697
                        $level = isset($competency_selected['level']) ? $competency_selected['level'] : 0;
698
                        $level = intval($level, 10);
699
 
700
 
701
                        $jobDescriptionCompetency =  $jobDescriptionCompetencyMapper->fetchOneByOrganizationPositionIdAndCompetencyId($jobDescription->id, $competency->id);
702
                        if(!$jobDescriptionCompetency) {
703
                            $jobDescriptionCompetency = new OrganizationPositionCompetency();
704
                            $jobDescriptionCompetency->competency_id = $competency->id;
705
                            $jobDescriptionCompetency->job_description_id = $jobDescription->id;
706
                            $jobDescriptionCompetencyMapper->insert($jobDescriptionCompetency);
707
 
708
                        }
709
 
710
 
711
 
712
                        $jobDescriptionCompetencyBehavior = new OrganizationPositionCompetencyBehavior();
713
                        $jobDescriptionCompetencyBehavior->job_description_id = $jobDescription->id;
714
                        $jobDescriptionCompetencyBehavior->competency_id = $competency->id;
715
                        $jobDescriptionCompetencyBehavior->behavior_id = $behavior->id;
716
                        $jobDescriptionCompetencyBehavior->level = $level;
717
                        $jobDescriptionCompetencyBehaviorMapper->insert($jobDescriptionCompetencyBehavior);
718
                    }
719
 
720
 
721
 
722
                    $this->logger->info('Se actualizo el cargo ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
16770 efrain 723
 
724
                    $data = [
725
                        'success' => true,
726
                        'data' => 'LABEL_RECORD_UPDATED'
727
                    ];
728
                } else {
729
                    $data = [
16790 efrain 730
                        'success' => false,
731
                        'data' => $jobDescriptionMapper->getError()
16770 efrain 732
                    ];
733
                }
16790 efrain 734
 
16770 efrain 735
                return new JsonModel($data);
736
            } else {
737
                $messages = [];
738
                $form_messages = (array) $form->getMessages();
16790 efrain 739
                foreach ($form_messages as $fieldname => $field_messages) {
16770 efrain 740
                    $messages[$fieldname] = array_values($field_messages);
741
                }
16790 efrain 742
 
16770 efrain 743
                return new JsonModel([
16790 efrain 744
                    'success' => false,
745
                    'data' => $messages
16770 efrain 746
                ]);
747
            }
748
        } else if ($request->isGet()) {
16790 efrain 749
            $behaviors = [];
750
            $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
16770 efrain 751
 
752
 
16790 efrain 753
            if($currentCompany) {
754
                $records = $behaviorMapper->fetchAllActiveByCompanyId($currentCompany->id);
755
            } else {
756
                $records = $behaviorMapper->fetchAllActiveByDefault();
757
            }
16770 efrain 758
 
16790 efrain 759
            $behaviorIds = [];
760
            foreach($records as $record)
761
            {
762
 
763
                $behaviorIds[ $record->id ] = $record->uuid;
764
 
765
                array_push($behaviors, [
766
                    'uuid' => $record->uuid,
767
                    'description' => $record->description
768
                ]);
769
            }
16770 efrain 770
 
771
 
16790 efrain 772
            $competencyTypes = [];
16770 efrain 773
 
774
 
16790 efrain 775
            $competencyTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
776
 
777
            if($currentCompany) {
778
                $records = $competencyTypeMapper->fetchAllActiveByCompanyId($currentCompany->id);
779
            } else {
780
                $records = $competencyTypeMapper->fetchAllActiveByDefault();
781
            }
782
 
783
 
784
            $competencyTypeIds = [];
785
            foreach($records as $record)
16770 efrain 786
            {
16790 efrain 787
                $competencyTypeIds[ $record->id ] =  $record->uuid;
788
 
789
 
790
                array_push($competencyTypes, [
791
                    'uuid' => $record->uuid,
792
                    'name' => $record->name,
793
                ]);
16770 efrain 794
            }
795
 
16790 efrain 796
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
797
            if($currentCompany) {
798
                $records = $competencyMapper->fetchAllActiveByCompanyId($currentCompany->id);
799
            } else {
800
                $records = $competencyMapper->fetchAllActiveByDefault();
801
            }
16770 efrain 802
 
16790 efrain 803
            $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
804
 
805
 
806
 
807
            $competencies = [];
808
 
809
            foreach($records as $record)
16770 efrain 810
            {
16790 efrain 811
                if(!isset($competencyTypeIds[ $record->competency_type_id ])) {
812
                    continue;
813
                }
16770 efrain 814
 
16790 efrain 815
                $behaviors_by_competency = [];
816
 
817
                $competencyBehaviors = $competencyBehaviorMapper->fetchAllByCompetencyId($record->id);
818
                foreach($competencyBehaviors as $competencyBehavior)
16770 efrain 819
                {
16790 efrain 820
                    if(!isset($behaviorIds[ $competencyBehavior->behavior_id ])) {
821
                        continue;
16770 efrain 822
                    }
16790 efrain 823
 
824
                    array_push($behaviors_by_competency, $behaviorIds[ $competencyBehavior->behavior_id ]);
16770 efrain 825
                }
826
 
16790 efrain 827
 
828
                if($behaviors_by_competency) {
829
                    array_push($competencies, [
830
                        'competency_type_uuid' => $competencyTypeIds[ $record->competency_type_id ],
831
                        'uuid' => $record->uuid,
832
                        'name' => $record->name,
833
                        'behaviors' => $behaviors_by_competency,
834
                    ]);
835
                }
836
 
837
 
838
            }
16770 efrain 839
 
840
 
16790 efrain 841
            $jobDescriptionMapper = OrganizationPositionMapper::getInstance($this->adapter);
842
            $records = $jobDescriptionMapper->fetchAllByCompanyId($currentCompany->id);
16770 efrain 843
 
16790 efrain 844
            $jobsDescription = [];
845
 
846
            foreach ($records as $record)
847
            {
848
                if($jobDescription->id == $record->id) {
849
                    continue;
850
                }
16770 efrain 851
 
16790 efrain 852
                $jobsDescription[ $record->uuid ] = $record->name;
853
            }
854
 
855
            $job_description_id_boss = '';
856
            if($jobDescription->job_description_id_boss) {
857
                $jobDescriptionBoss = $jobDescriptionMapper->fetchOne($jobDescription->job_description_id_boss);
858
                if($jobDescriptionBoss) {
859
                    $job_description_id_boss = $jobDescriptionBoss->uuid;
16770 efrain 860
                }
861
            }
16790 efrain 862
 
863
            $data = [
864
                'id' => $jobDescription->uuid,
865
                'name' => $jobDescription->name,
866
                'functions' => $jobDescription->functions,
867
                'objectives' => $jobDescription->objectives,
868
                'status' => $jobDescription->status,
869
                'competencies_selected' => [],
870
                'behaviors' => $behaviors,
871
                'competency_types' => $competencyTypes,
872
                'competencies' => $competencies,
873
                'jobs_description' => $jobsDescription,
874
                'job_description_id_boss' => $job_description_id_boss,
875
            ];
16770 efrain 876
 
16790 efrain 877
            $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
878
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
879
            $competencyTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
16770 efrain 880
 
16790 efrain 881
            $competencyTypes = [];
882
 
883
            $jobDescriptionCompetencyMapper = OrganizationPositionCompetencyMapper::getInstance($this->adapter);
884
            $jobDescriptionCompetencyBehaviorMapper = OrganizationPositionCompetencyBehaviorMapper::getInstance($this->adapter);
885
 
886
            $jobDescriptionCompetencies = $jobDescriptionCompetencyMapper->fetchAllByOrganizationPositionId($jobDescription->id);
887
            foreach($jobDescriptionCompetencies as $jobDescriptionCompetency)
888
            {
889
                $competency = $competencyMapper->fetchOne($jobDescriptionCompetency->competency_id);
890
                if(!$competency) {
891
                    continue;
892
                }
893
 
894
                if(isset($competencyTypes[$competency->competency_type_id])) {
895
                    $competencyType = $competencyTypes[$competency->competency_type_id];
896
                } else {
897
 
898
                    $competencyType = $competencyTypeMapper->fetchOne($competency->competency_type_id);
899
                    if(!$competencyType) {
900
                        continue;
901
                    }
902
 
903
                    $competencyTypes[$competency->competency_type_id] = $competencyType;
904
                }
905
 
906
                $competency_selected = [
907
                    'uuid' => $competency->uuid,
908
                    'competency_type_uuid' => $competencyType->uuid,
909
                    'behaviors' => []
910
                ];
911
 
912
 
913
 
914
                foreach($competencies as $record_competency)
915
                {
916
                    if($record_competency['uuid'] == $competency->uuid) {
917
 
918
                        foreach($record_competency['behaviors'] as $uuid_behavior)
919
                        {
920
 
921
 
922
                            $behavior = $behaviorMapper->fetchOneByUuid($uuid_behavior);
923
                            if($behavior) {
924
 
925
                                $jobDescriptionCompetencyBehavior = $jobDescriptionCompetencyBehaviorMapper->fetchOneByOrganizationPositionIdAndCompetencyIdAndBehaviorId($jobDescription->id, $competency->id, $behavior->id);
926
                                if($jobDescriptionCompetencyBehavior) {
927
                                    $level = $jobDescriptionCompetencyBehavior->level;
928
                                } else {
929
                                    $level = 0;
930
                                }
931
 
932
                                array_push($competency_selected['behaviors'], ['uuid' => $behavior->uuid, 'level' => $level]);
933
                            }
934
                        }
935
 
936
                    }
937
 
938
 
939
                }
940
 
941
 
942
 
943
                array_push($data['competencies_selected'], $competency_selected);
944
 
945
 
946
 
947
            }
948
 
16770 efrain 949
            $response = [
950
                'success' => true,
951
                'data' => $data
952
            ];
16790 efrain 953
 
16770 efrain 954
            return new JsonModel($response);
955
        } else {
956
            $data = [
957
                'success' => false,
958
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
959
            ];
16790 efrain 960
 
16770 efrain 961
            return new JsonModel($data);
962
        }
16790 efrain 963
 
16770 efrain 964
        return new JsonModel($data);
965
    }
16790 efrain 966
 
16770 efrain 967
    public function deleteAction()
968
    {
969
        $currentUserPlugin = $this->plugin('currentUserPlugin');
970
        $currentUser = $currentUserPlugin->getUser();
971
        $currentCompany = $currentUserPlugin->getCompany();
16790 efrain 972
 
16770 efrain 973
        $request = $this->getRequest();
974
        $uuid = $this->params()->fromRoute('id');
16790 efrain 975
 
976
        if (!$uuid) {
16770 efrain 977
            $data = [
16790 efrain 978
                'success' => false,
979
                'data' => 'ERROR_INVALID_PARAMETER'
16770 efrain 980
            ];
16790 efrain 981
 
16770 efrain 982
            return new JsonModel($data);
983
        }
16790 efrain 984
 
985
 
986
        $jobDescriptionMapper = OrganizationPositionMapper::getInstance($this->adapter);
987
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
988
        if (!$jobDescription) {
16770 efrain 989
            $data = [
16790 efrain 990
                'success' => false,
991
                'data' => 'ERROR_RECORD_NOT_FOUND'
16770 efrain 992
            ];
16790 efrain 993
 
16770 efrain 994
            return new JsonModel($data);
995
        }
16790 efrain 996
 
997
        if ($currentCompany && $jobDescription->company_id != $currentCompany->id) {
16770 efrain 998
            $data = [
16790 efrain 999
                'success' => false,
1000
                'data' => 'ERROR_UNAUTHORIZED'
16770 efrain 1001
            ];
16790 efrain 1002
 
16770 efrain 1003
            return new JsonModel($data);
1004
        }
16790 efrain 1005
 
1006
 
1007
        if ($request->isPost()) {
1008
            $jobDescriptionMapper->removeAllOrganizationPositionIdBossByJobDescripcionId($jobDescription->id);
1009
 
1010
 
1011
            $result = $jobDescriptionMapper->delete($jobDescription);
1012
            if ($result) {
1013
                $this->logger->info('Se borro el cargo de trabajo ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1014
 
16770 efrain 1015
                $data = [
1016
                    'success' => true,
1017
                    'data' => 'LABEL_RECORD_DELETED'
1018
                ];
1019
            } else {
1020
 
1021
                $data = [
16790 efrain 1022
                    'success' => false,
1023
                    'data' => $jobDescriptionMapper->getError()
16770 efrain 1024
                ];
1025
 
1026
                return new JsonModel($data);
1027
            }
1028
        } else {
1029
            $data = [
1030
                'success' => false,
1031
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1032
            ];
16790 efrain 1033
 
16770 efrain 1034
            return new JsonModel($data);
1035
        }
16790 efrain 1036
 
16770 efrain 1037
        return new JsonModel($data);
1038
    }
16790 efrain 1039
 
1040
    public function importAction()
16770 efrain 1041
    {
16790 efrain 1042
 
16770 efrain 1043
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1044
        $currentUser = $currentUserPlugin->getUser();
1045
        $currentCompany = $currentUserPlugin->getCompany();
16790 efrain 1046
 
1047
        if (!$currentCompany) {
16770 efrain 1048
            $data = [
16790 efrain 1049
                'success' => false,
1050
                'data' => 'ERROR_UNAUTHORIZED'
16770 efrain 1051
            ];
16790 efrain 1052
 
16770 efrain 1053
            return new JsonModel($data);
1054
        }
1055
 
16790 efrain 1056
        $request = $this->getRequest();
1057
 
1058
        if ($request->isPost()) {
1059
 
1060
            $jobDescriptionMapper = OrganizationPositionMapper::getInstance($this->adapter);
1061
            $jobDescriptionsDefault = $jobDescriptionMapper->fetchAllByDefault();
1062
 
1063
            $new_records = 0;
1064
            foreach ($jobDescriptionsDefault as $jobDescriptionDefault) {
1065
                if ($jobDescriptionDefault->status == OrganizationPosition::STATUS_INACTIVE) {
1066
                    continue;
1067
                }
1068
 
1069
                $jobDescription = $jobDescriptionMapper->fetchOneByCompanyId($currentCompany->id, $jobDescriptionDefault->id);
1070
                if (!$jobDescription) {
1071
 
1072
                    $jobDescription = new OrganizationPosition();
1073
                    $jobDescription->company_id = $currentCompany->id;
1074
                    $jobDescription->job_description_id_default = $jobDescriptionDefault->id;
1075
                    $jobDescription->name = $jobDescriptionDefault->name;
1076
                    $jobDescription->functions = $jobDescriptionDefault->functions;
1077
                    $jobDescription->objectives = $jobDescriptionDefault->objectives;
1078
                    $jobDescription->status = OrganizationPosition::STATUS_ACTIVE;
1079
 
1080
                    $result = $jobDescriptionMapper->insert($jobDescription);
1081
 
1082
                    if ($result) {
1083
 
1084
                        //Add Competencies
1085
                        $competenceMapper = CompetencyMapper::getInstance($this->adapter);
1086
                        $jobDescriptionCompetencyMapper = OrganizationPositionCompetencyMapper::getInstance($this->adapter);
1087
                        $jobDescriptionCompetencies = $jobDescriptionCompetencyMapper->fetchByOrganizationPositionId($jobDescriptionDefault->id);
1088
 
1089
                        if (count($jobDescriptionCompetencies) != 0) {
1090
                            foreach ($jobDescriptionCompetencies as $record) {
1091
 
1092
                                $competence = $competenceMapper->fetchOneByCompanyIdAndCompetencyIdDefault($currentCompany->id, $record->competency_id);
1093
                                if ($competence) {
1094
                                    $newDescriptionCompetency = new OrganizationPositionCompetency();
1095
                                    $newDescriptionCompetency->competency_id = $competence->id;
1096
                                    $newDescriptionCompetency->job_description_id = $jobDescription->id;
1097
                                    $newDescriptionCompetency->level = $record->level;
1098
                                    $jobDescriptionCompetencyMapper->insert($newDescriptionCompetency);
1099
                                }
1100
                            }
1101
                        }
1102
 
1103
                        $new_records++;
1104
                    } else {
1105
                        $data = [
1106
                            'success' => false,
1107
                            'data' => 'ERROR_CANT_ADD_JOB_DESCRIPTION'
1108
                        ];
1109
 
1110
                        return new JsonModel($data);
16770 efrain 1111
                    }
1112
                }
1113
            }
16790 efrain 1114
 
1115
            if ($new_records) {
1116
 
1117
                if (1 == $new_records) {
1118
                    $data = [
1119
                        'success' => true,
1120
                        'data' => 'LABEL_1_JOB_DESCRIPTION_IMPORTED'
1121
                    ];
1122
 
1123
                    return new JsonModel($data);
1124
                } else {
1125
                    $data = [
1126
                        'success' => true,
1127
                        'data' => $new_records . ' LABEL_MULTI_JOB_DESCRIPTION_IMPORTED'
1128
                    ];
1129
 
1130
                    return new JsonModel($data);
16770 efrain 1131
                }
16790 efrain 1132
            } else {
1133
                $data = [
1134
                    'success' => true,
1135
                    'data' => 'LABEL_NO_JOB_DESCRIPTION_IMPORTED'
1136
                ];
1137
 
1138
                return new JsonModel($data);
16770 efrain 1139
            }
1140
        } else {
1141
            $data = [
1142
                'success' => false,
1143
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1144
            ];
1145
 
16790 efrain 1146
            return new JsonModel($data);
16770 efrain 1147
        }
16790 efrain 1148
 
16770 efrain 1149
        return new JsonModel($data);
1150
    }
16790 efrain 1151
 
1152
    public function bossAction()
16770 efrain 1153
    {
1154
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1155
        $currentUser = $currentUserPlugin->getUser();
1156
        $currentCompany = $currentUserPlugin->getCompany();
16790 efrain 1157
 
16770 efrain 1158
        $request = $this->getRequest();
16790 efrain 1159
        $uuid = $this->params()->fromRoute('id');
1160
 
1161
 
1162
        if (!$uuid) {
16770 efrain 1163
            $data = [
16790 efrain 1164
                'success' => false,
1165
                'data' => 'ERROR_INVALID_PARAMETER'
16770 efrain 1166
            ];
16790 efrain 1167
 
16770 efrain 1168
            return new JsonModel($data);
1169
        }
16790 efrain 1170
 
1171
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
1172
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
1173
        if (!$jobDescription) {
16770 efrain 1174
            $data = [
16790 efrain 1175
                'success' => false,
1176
                'data' => 'ERROR_RECORD_NOT_FOUND'
16770 efrain 1177
            ];
16790 efrain 1178
 
16770 efrain 1179
            return new JsonModel($data);
1180
        }
16790 efrain 1181
 
1182
        if ($currentCompany && $jobDescription->company_id != $currentCompany->id) {
16770 efrain 1183
            $data = [
16790 efrain 1184
                'success' => false,
1185
                'data' => 'ERROR_UNAUTHORIZED'
16770 efrain 1186
            ];
16790 efrain 1187
 
16770 efrain 1188
            return new JsonModel($data);
1189
        }
16790 efrain 1190
 
1191
 
16770 efrain 1192
        if ($request->isGet()) {
16790 efrain 1193
 
1194
            $data = [];
16770 efrain 1195
 
16790 efrain 1196
            if(!$jobDescription->job_description_id_boss) {
1197
 
1198
                $organizationPositionMapper = OrganizationPositionMapper::getInstance($this->adapter);
1199
                $users = $organizationPositionMapper->fetchAllByJobDescriptionIdAndCompanyId($job_description_id, $company_id)
1200
 
1201
            }
16770 efrain 1202
 
1203
 
16790 efrain 1204
 
16770 efrain 1205
        } else {
1206
            $data = [
1207
                'success' => false,
1208
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1209
            ];
16790 efrain 1210
 
1211
            return new JsonModel($data);
16770 efrain 1212
        }
16790 efrain 1213
 
16770 efrain 1214
        return new JsonModel($data);
16790 efrain 1215
    }
16770 efrain 1216
 
1217
 
16790 efrain 1218
 
1219
 
16770 efrain 1220
}