Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
66 efrain 1
<?php
649 geraldo 2
 
66 efrain 3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
16768 efrain 7
 
66 efrain 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\View\Model\ViewModel;
10
use Laminas\View\Model\JsonModel;
15403 efrain 11
use LeadersLinked\Form\JobDescription\JobDescriptionForm;
12
use LeadersLinked\Model\Company;
66 efrain 13
use LeadersLinked\Model\JobDescription;
14
use LeadersLinked\Model\JobDescriptionCompetency;
15403 efrain 15
use LeadersLinked\Model\JobDescriptionCompetencyBehavior;
1320 efrain 16
use LeadersLinked\Mapper\BehaviorMapper;
15403 efrain 17
use LeadersLinked\Mapper\CompetencyMapper;
15030 efrain 18
use LeadersLinked\Mapper\CompetencyBehaviorMapper;
15403 efrain 19
use LeadersLinked\Mapper\CompetencyTypeMapper;
20
use LeadersLinked\Mapper\JobDescriptionMapper;
21
use LeadersLinked\Mapper\JobDescriptionCompetencyBehaviorMapper;
22
use LeadersLinked\Mapper\JobDescriptionCompetencyMapper;
23
use LeadersLinked\Library\Functions;
24
use LeadersLinked\Library\JobDescriptionPdf;
25
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
16770 efrain 26
use LeadersLinked\Model\User;
27
use Laminas\Http\Response;
15403 efrain 28
 
16766 efrain 29
 
14203 stevensc 30
class JobDescriptionController extends AbstractActionController
31
{
66 efrain 32
    /**
33
     *
16769 efrain 34
     * @var \Laminas\Db\Adapter\AdapterInterface
66 efrain 35
     */
36
    private $adapter;
16769 efrain 37
 
66 efrain 38
    /**
39
     *
16769 efrain 40
     * @var \LeadersLinked\Cache\CacheInterface
66 efrain 41
     */
16769 efrain 42
    private $cache;
43
 
44
 
45
    /**
46
     *
47
     * @var \Laminas\Log\LoggerInterface
48
     */
66 efrain 49
    private $logger;
16769 efrain 50
 
66 efrain 51
    /**
52
     *
53
     * @var array
54
     */
55
    private $config;
15403 efrain 56
 
16769 efrain 57
 
15403 efrain 58
    /**
16769 efrain 59
     *
60
     * @var \Laminas\Mvc\I18n\Translator
15403 efrain 61
     */
62
    private $translator;
16769 efrain 63
 
64
 
66 efrain 65
    /**
66
     *
16769 efrain 67
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
68
     * @param \LeadersLinked\Cache\CacheInterface $cache
69
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
66 efrain 70
     * @param array $config
16769 efrain 71
     * @param \Laminas\Mvc\I18n\Translator $translator
66 efrain 72
     */
16769 efrain 73
    public function __construct($adapter, $cache, $logger, $config, $translator)
14203 stevensc 74
    {
16769 efrain 75
        $this->adapter      = $adapter;
76
        $this->cache        = $cache;
77
        $this->logger       = $logger;
78
        $this->config       = $config;
79
        $this->translator   = $translator;
66 efrain 80
    }
649 geraldo 81
 
14203 stevensc 82
    public function indexAction()
83
    {
66 efrain 84
        $currentUserPlugin = $this->plugin('currentUserPlugin');
85
        $currentUser = $currentUserPlugin->getUser();
86
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 87
 
16770 efrain 88
 
66 efrain 89
        $request = $this->getRequest();
649 geraldo 90
        $headers = $request->getHeaders();
91
 
16770 efrain 92
 
93
        if ($request->isPost()) {
94
 
95
            $data = $this->params()->fromPost('data');
96
 
97
            if(!empty($data)) {
98
 
99
                $json = json_decode($data, true);
100
                if(is_array($json)) {
101
 
102
                    $jobDescriptionIdBoss = 0;
103
                    $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
104
 
105
 
106
 
107
                    $position = 0;
108
                    foreach($json as $item)
109
                    {
649 geraldo 110
 
16770 efrain 111
                        $this->recursiveSave($currentCompany, $jobDescriptionMapper, $jobDescriptionIdBoss, $item, $position);
112
                        $position++;
113
                    }
649 geraldo 114
 
16770 efrain 115
                }
116
            }
117
            return new JsonModel([
118
                'success' => true,
119
            ]);
120
 
121
 
122
        } else if ($request->isGet()) {
123
 
124
 
649 geraldo 125
            $headers = $request->getHeaders();
126
 
66 efrain 127
            $isJson = false;
649 geraldo 128
            if ($headers->has('Accept')) {
66 efrain 129
                $accept = $headers->get('Accept');
649 geraldo 130
 
66 efrain 131
                $prioritized = $accept->getPrioritized();
649 geraldo 132
 
133
                foreach ($prioritized as $key => $value) {
66 efrain 134
                    $raw = trim($value->getRaw());
649 geraldo 135
 
136
                    if (!$isJson) {
66 efrain 137
                        $isJson = strpos($raw, 'json');
138
                    }
139
                }
140
            }
649 geraldo 141
 
142
            if ($isJson) {
15371 efrain 143
 
16770 efrain 144
 
145
 
146
                $items = [];
147
 
16779 efrain 148
                $level = 0;
16770 efrain 149
                $jobDescriptionIdBoss = 0;
66 efrain 150
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
16770 efrain 151
 
152
 
153
 
16779 efrain 154
                $items = $this->recursiveLoad($currentUser, $currentCompany, $jobDescriptionMapper, $jobDescriptionIdBoss, $level);
155
 
156
 
157
 
158
 
66 efrain 159
                return new JsonModel([
160
                    'success' => true,
161
                    'data' => [
162
                        'items' => $items,
16770 efrain 163
 
66 efrain 164
                    ]
165
                ]);
16770 efrain 166
 
649 geraldo 167
            } else {
15403 efrain 168
 
169
 
170
 
649 geraldo 171
 
847 geraldo 172
                $form = new JobDescriptionForm($this->adapter, $currentCompany ? $currentCompany->id : null);
842 geraldo 173
 
66 efrain 174
                $this->layout()->setTemplate('layout/layout-backend');
175
                $viewModel = new ViewModel();
176
                $viewModel->setTemplate('leaders-linked/jobs-description/index.phtml');
15403 efrain 177
                $viewModel->setVariables([
178
                    'form' => $form,
15444 efrain 179
 
15403 efrain 180
 
181
                ]);
649 geraldo 182
                return $viewModel;
183
            }
66 efrain 184
        } else {
185
            return new JsonModel([
186
                'success' => false,
187
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
16770 efrain 188
            ]);
66 efrain 189
        }
190
    }
16770 efrain 191
 
192
    /**
193
     *
194
     * @param Company $currentCompany
195
     * @param JobDescriptionMapper $jobDescriptionMapper
196
     * @param int $jobDescriptionIdBoss
197
     * @param int $position
198
     * @param array $item
199
     */
200
    private function recursiveSave($currentCompany, $jobDescriptionMapper, $jobDescriptionIdBoss, $item, $position)
201
    {
202
 
649 geraldo 203
 
16770 efrain 204
        if(!empty($item) && is_array($item)) {
205
            foreach($item as $subitem)
206
            {
207
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($subitem['id']);
208
                if($jobDescriptionMapper) {
209
                    if($jobDescriptionIdBoss) {
210
 
211
 
212
                        $jobDescriptionMapper->setJobDescriptionIdBossByJobDescripcionId($jobDescription->id,  $jobDescriptionIdBoss, $position);
213
                    } else {
214
                        $jobDescriptionMapper->removeJobDescriptionIdBossByJobDescriptionId($jobDescription->id, $position);
215
                    }
216
 
217
                    $position++;
218
 
219
                    if(!empty($subitem['children']) && is_array($subitem['children'])) {
220
 
221
                        $position2 = 0;
222
                        foreach($subitem['children'] as $subitem2)
223
                        {
224
                            $this->recursiveSave($currentCompany, $jobDescriptionMapper, $jobDescription->id,  $subitem2, $position2);
225
                            $position2++;
226
                        }
227
 
228
                    }
229
 
230
 
231
                }
232
            }
233
        }
234
    }
235
 
236
 
237
    /**
238
     *
239
     * @param User $currentUser
240
     * @param Company $currentCompany
241
     * @param JobDescriptionMapper $jobDescriptionMapper
242
     * @param int $jobDescriptionIdBoss
16779 efrain 243
     * @param int $level
16770 efrain 244
     * @return array
245
     */
16779 efrain 246
    private function recursiveLoad($currentUser, $currentCompany, $jobDescriptionMapper, $jobDescriptionIdBoss, $level)
16770 efrain 247
    {
248
        $acl            = $this->getEvent()->getViewModel()->getVariable('acl');
249
        $allowAdd       = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/add') ? 1 : 0;
250
        $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/edit') ? 1 : 0;
251
        $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/delete') ? 1 : 0;
252
        $allowReport    = $acl->isAllowed($currentUser->usertype_id, 'jobs-description/report') ? 1 : 0;
253
 
254
 
255
        $items = [];
256
 
257
 
258
        if ($currentCompany) {
259
            $records = $jobDescriptionMapper->fetchAllByCompanyIdAndJobDescriptionIdBoss($currentCompany->id, $jobDescriptionIdBoss);
260
        } else {
261
            $records = $jobDescriptionMapper->fetchAllDefaultAndJobDescriptionIdBoss($jobDescriptionIdBoss);
262
        }
263
 
16778 efrain 264
 
16779 efrain 265
        $nextLevel = $level + 1;
16778 efrain 266
 
16770 efrain 267
        foreach($records as $record)
268
        {
16778 efrain 269
 
270
 
271
 
16770 efrain 272
            array_push($items, [
273
                'uuid' => $record->uuid,
274
                'name' => $record->name,
16779 efrain 275
                'children' => $this->recursiveLoad($currentUser, $currentCompany, $jobDescriptionMapper, $record->id, $nextLevel),
16770 efrain 276
                'status' => $record->status,
16779 efrain 277
                'level' => $level,
16770 efrain 278
                'link_report' => $allowReport ? $this->url()->fromRoute('jobs-description/report', ['id' => $record->uuid]) : '',
279
                'link_edit' => $allowEdit ? $this->url()->fromRoute('jobs-description/edit', ['id' => $record->uuid]) : '',
280
                'link_delete' => $allowDelete ? $this->url()->fromRoute('jobs-description/delete', ['id' => $record->uuid]) : '',
281
                'link_add' => $allowAdd ? $this->url()->fromRoute('jobs-description/add', ['id' => $record->uuid]) : '',
282
            ]);
283
        }
284
 
285
        return $items;
286
    }
287
 
14203 stevensc 288
    public function addAction()
289
    {
16778 efrain 290
 
291
 
66 efrain 292
        $currentUserPlugin = $this->plugin('currentUserPlugin');
293
        $currentUser = $currentUserPlugin->getUser();
294
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 295
 
66 efrain 296
        $request = $this->getRequest();
649 geraldo 297
 
298
 
299
        if ($request->isGet()) {
15444 efrain 300
            $behaviors = [];
301
            $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
15403 efrain 302
 
15444 efrain 303
 
304
            if($currentCompany) {
305
                $records = $behaviorMapper->fetchAllActiveByCompanyId($currentCompany->id);
306
            } else {
307
                $records = $behaviorMapper->fetchAllActiveByDefault();
308
            }
309
 
310
            $behaviorIds = [];
311
            foreach($records as $record)
312
            {
313
 
314
                $behaviorIds[ $record->id ] = $record->uuid;
315
 
316
                array_push($behaviors, [
317
                    'uuid' => $record->uuid,
318
                    'description' => $record->description
319
                ]);
320
            }
321
 
322
 
323
            $competencyTypes = [];
324
 
325
 
326
            $competencyTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
327
 
328
            if($currentCompany) {
329
                $records = $competencyTypeMapper->fetchAllActiveByCompanyId($currentCompany->id);
330
            } else {
331
                $records = $competencyTypeMapper->fetchAllActiveByDefault();
332
            }
333
 
334
 
335
            $competencyTypeIds = [];
336
            foreach($records as $record)
337
            {
338
                $competencyTypeIds[ $record->id ] =  $record->uuid;
339
 
340
 
341
                array_push($competencyTypes, [
342
                    'uuid' => $record->uuid,
343
                    'name' => $record->name,
344
                ]);
345
            }
346
 
347
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
348
            if($currentCompany) {
349
                $records = $competencyMapper->fetchAllActiveByCompanyId($currentCompany->id);
350
            } else {
351
                $records = $competencyMapper->fetchAllActiveByDefault();
352
            }
353
 
354
            $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
355
 
356
 
357
 
358
            $competencies = [];
359
 
360
            foreach($records as $record)
361
            {
362
                if(!isset($competencyTypeIds[ $record->competency_type_id ])) {
363
                    continue;
364
                }
365
 
366
                $behaviors_by_competency = [];
367
 
368
                $competencyBehaviors = $competencyBehaviorMapper->fetchAllByCompetencyId($record->id);
369
                foreach($competencyBehaviors as $competencyBehavior)
370
                {
371
                    if(!isset($behaviorIds[ $competencyBehavior->behavior_id ])) {
372
                        continue;
373
                    }
374
 
375
                    array_push($behaviors_by_competency, $behaviorIds[ $competencyBehavior->behavior_id ]);
376
                }
377
 
378
 
379
                if($behaviors_by_competency) {
380
                    array_push($competencies, [
381
                        'competency_type_uuid' => $competencyTypeIds[ $record->competency_type_id ],
382
                        'uuid' => $record->uuid,
383
                        'name' => $record->name,
384
                        'behaviors' => $behaviors_by_competency,
385
                    ]);
386
                }
387
 
388
 
389
            }
390
 
391
 
392
 
649 geraldo 393
 
394
 
15403 efrain 395
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
396
            if($currentCompany) {
397
                $records = $jobDescriptionMapper->fetchAllActiveByCompanyId($currentCompany->id);
398
            } else {
399
                $jobDescriptionMapper->fetchAllActiveByDefault();
856 geraldo 400
            }
15403 efrain 401
 
402
            $jobsDescription = [];
403
 
404
            foreach ($records as $record)
405
            {
406
                $jobsDescription[ $record->uuid ] = $record->name;
856 geraldo 407
            }
649 geraldo 408
 
409
 
66 efrain 410
            $data = [
649 geraldo 411
                'success' => true,
15403 efrain 412
                'data' => [
413
                    'jobs_description' => $jobsDescription,
15444 efrain 414
                    'behaviors' => $behaviors,
415
                    'competency_types' => $competencyTypes,
416
                    'competencies' => $competencies,
15403 efrain 417
                ]
66 efrain 418
            ];
649 geraldo 419
 
420
 
66 efrain 421
            return new JsonModel($data);
649 geraldo 422
        } else if ($request->isPost()) {
854 geraldo 423
            $form = new JobDescriptionForm($this->adapter, $currentCompany ? $currentCompany->id : null);
66 efrain 424
            $dataPost = $request->getPost()->toArray();
649 geraldo 425
 
426
 
66 efrain 427
            $form->setData($dataPost);
649 geraldo 428
 
429
            if ($form->isValid()) {
16770 efrain 430
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
15403 efrain 431
 
16770 efrain 432
                $position =  0;
433
                $job_description_boss_id = 0;
15403 efrain 434
 
16770 efrain 435
                $id = $this->params()->fromRoute('id');
436
                if($id) {
15403 efrain 437
 
16770 efrain 438
                    $jobDescriptionBoos = $jobDescriptionMapper->fetchOneByUuid($id);
439
                    if($jobDescriptionBoos && $jobDescriptionBoos->company_id == $currentCompany->id) {
440
                        $job_description_boss_id = $jobDescriptionBoos->id;
15030 efrain 441
 
16770 efrain 442
                        $position = $jobDescriptionMapper->fetchMaxPositionByJobDescriptionIdBoss($jobDescriptionBoos->id);
443
                   }
444
 
445
 
66 efrain 446
                }
15030 efrain 447
 
16770 efrain 448
 
449
                $dataPost = (array) $form->getData();
450
                $dataPost['status'] = empty($dataPost['status']) ? JobDescription::STATUS_INACTIVE : $dataPost['status'];
451
 
452
                $hydrator = new ObjectPropertyHydrator();
453
                $jobDescription = new JobDescription();
454
                $jobDescription->job_description_id_boss = $job_description_boss_id;
455
                $jobDescription->position = $position;
456
 
649 geraldo 457
                if ($currentCompany) {
847 geraldo 458
                    $jobDescription->company_id = $currentCompany ? $currentCompany->id : null;
66 efrain 459
                }
16770 efrain 460
 
461
                $hydrator->hydrate($dataPost, $jobDescription);
462
 
66 efrain 463
                $result = $jobDescriptionMapper->insert($jobDescription);
649 geraldo 464
 
1195 geraldo 465
                if ($result) {
15403 efrain 466
                    $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
467
                    $competencyMapper = CompetencyMapper::getInstance($this->adapter);
468
                    $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
469
 
470
 
471
                    $jobDescriptionCompetencyBehaviorMapper = JobDescriptionCompetencyBehaviorMapper::getInstance($this->adapter);
472
                    $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
473
 
474
                    $competencies_selected = isset($dataPost['competencies_selected']) ? $dataPost['competencies_selected'] : [];
475
                    foreach($competencies_selected as $competency_selected)
476
                    {
16770 efrain 477
 
15403 efrain 478
                        $competency_uuid = isset($competency_selected['competency_uuid']) ? $competency_selected['competency_uuid'] : '';
479
                        $competency = $competencyMapper->fetchOneByUuid($competency_uuid);
480
 
481
 
482
                        if($competency) {
483
                            if($currentCompany) {
484
                                $ok = $competency->company_id == $currentCompany->id;
485
                            } else {
486
                                $ok = empty($competency->company_id);
487
                            }
488
                        } else {
489
                            $ok = false;
490
                        }
491
 
492
                        if(!$ok) {
493
                            continue;
494
                        }
495
 
496
 
497
                        $behavior_uuid = isset($competency_selected['behavior_uuid']) ? $competency_selected['behavior_uuid'] : '';
498
                        $behavior = $behaviorMapper->fetchOneByUuid($behavior_uuid);
499
                        if($behavior) {
500
                            if($currentCompany) {
501
                                $ok = $behavior->company_id == $currentCompany->id;
502
                            } else {
503
                                $ok = empty($behavior->company_id);
504
                            }
505
 
506
 
507
                        } else {
508
                            $ok = false;
509
                        }
510
 
511
                        if($competency->company_id != $behavior->company_id) {
512
                            continue;
513
                        }
514
 
515
                        $competencyBehavior = $competencyBehaviorMapper->fetchOneByBehaviorIdAndCompetencyId($behavior->id, $competency->id);
516
                        if(!$competencyBehavior) {
517
                            continue;
518
                        }
519
 
520
                        $level = isset($competency_selected['level']) ? $competency_selected['level'] : 0;
521
                        $level = intval($level, 10);
522
 
523
 
524
                        $jobDescriptionCompetency =  $jobDescriptionCompetencyMapper->fetchOneByJobDescriptionIdAndCompetencyId($jobDescription->id, $competency->id);
525
                        if(!$jobDescriptionCompetency) {
66 efrain 526
                            $jobDescriptionCompetency = new JobDescriptionCompetency();
527
                            $jobDescriptionCompetency->competency_id = $competency->id;
528
                            $jobDescriptionCompetency->job_description_id = $jobDescription->id;
529
                            $jobDescriptionCompetencyMapper->insert($jobDescriptionCompetency);
15403 efrain 530
 
531
                        }
532
 
533
 
534
 
535
                        $jobDescriptionCompetencyBehavior = new JobDescriptionCompetencyBehavior();
536
                        $jobDescriptionCompetencyBehavior->job_description_id = $jobDescription->id;
537
                        $jobDescriptionCompetencyBehavior->competency_id = $competency->id;
538
                        $jobDescriptionCompetencyBehavior->behavior_id = $behavior->id;
539
                        $jobDescriptionCompetencyBehavior->level = $level;
540
                        $jobDescriptionCompetencyBehaviorMapper->insert($jobDescriptionCompetencyBehavior);
541
                    }
542
 
649 geraldo 543
 
66 efrain 544
                    $this->logger->info('Se agrego el cargo ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
649 geraldo 545
 
66 efrain 546
                    $data = [
649 geraldo 547
                        'success' => true,
548
                        'data' => 'LABEL_RECORD_ADDED'
66 efrain 549
                    ];
550
                } else {
551
                    $data = [
649 geraldo 552
                        'success' => false,
553
                        'data' => $jobDescriptionMapper->getError()
66 efrain 554
                    ];
555
                }
649 geraldo 556
 
66 efrain 557
                return new JsonModel($data);
558
            } else {
559
                $messages = [];
560
                $form_messages = (array) $form->getMessages();
649 geraldo 561
                foreach ($form_messages as $fieldname => $field_messages) {
562
 
66 efrain 563
                    $messages[$fieldname] = array_values($field_messages);
564
                }
649 geraldo 565
 
66 efrain 566
                return new JsonModel([
649 geraldo 567
                    'success' => false,
568
                    'data' => $messages
66 efrain 569
                ]);
570
            }
571
        } else {
572
            $data = [
573
                'success' => false,
574
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
575
            ];
649 geraldo 576
 
66 efrain 577
            return new JsonModel($data);
578
        }
649 geraldo 579
 
66 efrain 580
        return new JsonModel($data);
581
    }
649 geraldo 582
 
14203 stevensc 583
    public function editAction()
584
    {
66 efrain 585
        $currentUserPlugin = $this->plugin('currentUserPlugin');
586
        $currentUser = $currentUserPlugin->getUser();
587
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 588
 
66 efrain 589
        $request = $this->getRequest();
590
        $uuid = $this->params()->fromRoute('id');
591
 
649 geraldo 592
 
593
        if (!$uuid) {
66 efrain 594
            $data = [
649 geraldo 595
                'success' => false,
596
                'data' => 'ERROR_INVALID_PARAMETER'
66 efrain 597
            ];
649 geraldo 598
 
66 efrain 599
            return new JsonModel($data);
600
        }
601
 
602
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
603
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
649 geraldo 604
        if (!$jobDescription) {
66 efrain 605
            $data = [
649 geraldo 606
                'success' => false,
607
                'data' => 'ERROR_RECORD_NOT_FOUND'
66 efrain 608
            ];
649 geraldo 609
 
66 efrain 610
            return new JsonModel($data);
611
        }
649 geraldo 612
 
851 geraldo 613
        if ($currentCompany && $jobDescription->company_id != $currentCompany->id) {
66 efrain 614
            $data = [
649 geraldo 615
                'success' => false,
616
                'data' => 'ERROR_UNAUTHORIZED'
66 efrain 617
            ];
649 geraldo 618
 
66 efrain 619
            return new JsonModel($data);
620
        }
621
 
649 geraldo 622
 
623
        if ($request->isPost()) {
847 geraldo 624
            $form = new JobDescriptionForm($this->adapter, $currentCompany ? $currentCompany->id : null, $jobDescription->id);
66 efrain 625
            $dataPost = $request->getPost()->toArray();
649 geraldo 626
 
627
 
66 efrain 628
            $form->setData($dataPost);
649 geraldo 629
 
630
            if ($form->isValid()) {
15403 efrain 631
 
632
 
633
 
66 efrain 634
                $dataPost = (array) $form->getData();
15403 efrain 635
 
636
                $hydrator = new ObjectPropertyHydrator();
637
                $hydrator->hydrate($dataPost, $jobDescription);
638
 
66 efrain 639
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : JobDescription::STATUS_INACTIVE;
15403 efrain 640
 
641
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
642
 
643
 
66 efrain 644
                $result = $jobDescriptionMapper->update($jobDescription);
649 geraldo 645
 
646
                if ($result) {
15403 efrain 647
                    $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
648
                    $competencyMapper = CompetencyMapper::getInstance($this->adapter);
649
                    $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
650
 
651
 
652
                    $jobDescriptionCompetencyBehaviorMapper = JobDescriptionCompetencyBehaviorMapper::getInstance($this->adapter);
653
                    $jobDescriptionCompetencyBehaviorMapper->deleteAllBJobDescriptionId($jobDescription->id);
654
 
1195 geraldo 655
                    $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
15403 efrain 656
                    $jobDescriptionCompetencyMapper->deleteAllBJobDescriptionId($jobDescription->id);
657
 
658
                    $competencies_selected = isset($dataPost['competencies_selected']) ? $dataPost['competencies_selected'] : [];
659
                    foreach($competencies_selected as $competency_selected)
660
                    {
661
                        $competency_uuid = isset($competency_selected['competency_uuid']) ? $competency_selected['competency_uuid'] : '';
662
                        $competency = $competencyMapper->fetchOneByUuid($competency_uuid);
663
 
664
 
665
                        if($competency) {
666
                            if($currentCompany) {
667
                                $ok = $competency->company_id == $currentCompany->id;
668
                            } else {
669
                                $ok = empty($competency->company_id);
670
                            }
671
                        } else {
672
                            $ok = false;
673
                        }
674
 
675
                        if(!$ok) {
676
                            continue;
677
                        }
678
 
679
 
680
                        $behavior_uuid = isset($competency_selected['behavior_uuid']) ? $competency_selected['behavior_uuid'] : '';
681
                        $behavior = $behaviorMapper->fetchOneByUuid($behavior_uuid);
682
                        if($behavior) {
683
                            if($currentCompany) {
684
                                $ok = $behavior->company_id == $currentCompany->id;
685
                            } else {
686
                                $ok = empty($behavior->company_id);
687
                            }
688
 
689
 
690
                        } else {
691
                            $ok = false;
692
                        }
693
 
694
                        if($competency->company_id != $behavior->company_id) {
695
                            continue;
696
                        }
697
 
698
                        $competencyBehavior = $competencyBehaviorMapper->fetchOneByBehaviorIdAndCompetencyId($behavior->id, $competency->id);
699
                        if(!$competencyBehavior) {
700
                            continue;
701
                        }
702
 
703
                        $level = isset($competency_selected['level']) ? $competency_selected['level'] : 0;
704
                        $level = intval($level, 10);
705
 
706
 
707
                        $jobDescriptionCompetency =  $jobDescriptionCompetencyMapper->fetchOneByJobDescriptionIdAndCompetencyId($jobDescription->id, $competency->id);
708
                        if(!$jobDescriptionCompetency) {
66 efrain 709
                            $jobDescriptionCompetency = new JobDescriptionCompetency();
710
                            $jobDescriptionCompetency->competency_id = $competency->id;
711
                            $jobDescriptionCompetency->job_description_id = $jobDescription->id;
712
                            $jobDescriptionCompetencyMapper->insert($jobDescriptionCompetency);
15403 efrain 713
 
66 efrain 714
                        }
15030 efrain 715
 
15403 efrain 716
 
717
 
718
                        $jobDescriptionCompetencyBehavior = new JobDescriptionCompetencyBehavior();
719
                        $jobDescriptionCompetencyBehavior->job_description_id = $jobDescription->id;
720
                        $jobDescriptionCompetencyBehavior->competency_id = $competency->id;
721
                        $jobDescriptionCompetencyBehavior->behavior_id = $behavior->id;
722
                        $jobDescriptionCompetencyBehavior->level = $level;
723
                        $jobDescriptionCompetencyBehaviorMapper->insert($jobDescriptionCompetencyBehavior);
66 efrain 724
                    }
15403 efrain 725
 
726
 
727
 
66 efrain 728
                    $this->logger->info('Se actualizo el cargo ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
649 geraldo 729
 
66 efrain 730
                    $data = [
731
                        'success' => true,
732
                        'data' => 'LABEL_RECORD_UPDATED'
733
                    ];
734
                } else {
735
                    $data = [
649 geraldo 736
                        'success' => false,
737
                        'data' => $jobDescriptionMapper->getError()
66 efrain 738
                    ];
739
                }
649 geraldo 740
 
66 efrain 741
                return new JsonModel($data);
742
            } else {
743
                $messages = [];
744
                $form_messages = (array) $form->getMessages();
649 geraldo 745
                foreach ($form_messages as $fieldname => $field_messages) {
66 efrain 746
                    $messages[$fieldname] = array_values($field_messages);
747
                }
649 geraldo 748
 
66 efrain 749
                return new JsonModel([
649 geraldo 750
                    'success' => false,
751
                    'data' => $messages
66 efrain 752
                ]);
753
            }
754
        } else if ($request->isGet()) {
15444 efrain 755
            $behaviors = [];
756
            $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
757
 
758
 
759
            if($currentCompany) {
760
                $records = $behaviorMapper->fetchAllActiveByCompanyId($currentCompany->id);
761
            } else {
762
                $records = $behaviorMapper->fetchAllActiveByDefault();
763
            }
764
 
765
            $behaviorIds = [];
766
            foreach($records as $record)
767
            {
768
 
769
                $behaviorIds[ $record->id ] = $record->uuid;
770
 
771
                array_push($behaviors, [
772
                    'uuid' => $record->uuid,
773
                    'description' => $record->description
774
                ]);
775
            }
776
 
777
 
778
            $competencyTypes = [];
779
 
780
 
781
            $competencyTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
782
 
783
            if($currentCompany) {
784
                $records = $competencyTypeMapper->fetchAllActiveByCompanyId($currentCompany->id);
785
            } else {
786
                $records = $competencyTypeMapper->fetchAllActiveByDefault();
787
            }
788
 
789
 
790
            $competencyTypeIds = [];
791
            foreach($records as $record)
792
            {
793
                $competencyTypeIds[ $record->id ] =  $record->uuid;
794
 
795
 
796
                array_push($competencyTypes, [
797
                    'uuid' => $record->uuid,
798
                    'name' => $record->name,
799
                ]);
800
            }
801
 
802
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
803
            if($currentCompany) {
804
                $records = $competencyMapper->fetchAllActiveByCompanyId($currentCompany->id);
805
            } else {
806
                $records = $competencyMapper->fetchAllActiveByDefault();
807
            }
808
 
809
            $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
810
 
811
 
812
 
813
            $competencies = [];
814
 
815
            foreach($records as $record)
816
            {
817
                if(!isset($competencyTypeIds[ $record->competency_type_id ])) {
818
                    continue;
819
                }
820
 
821
                $behaviors_by_competency = [];
822
 
823
                $competencyBehaviors = $competencyBehaviorMapper->fetchAllByCompetencyId($record->id);
824
                foreach($competencyBehaviors as $competencyBehavior)
825
                {
826
                    if(!isset($behaviorIds[ $competencyBehavior->behavior_id ])) {
827
                        continue;
828
                    }
829
 
830
                    array_push($behaviors_by_competency, $behaviorIds[ $competencyBehavior->behavior_id ]);
831
                }
832
 
833
 
834
                if($behaviors_by_competency) {
835
                    array_push($competencies, [
836
                        'competency_type_uuid' => $competencyTypeIds[ $record->competency_type_id ],
837
                        'uuid' => $record->uuid,
838
                        'name' => $record->name,
839
                        'behaviors' => $behaviors_by_competency,
840
                    ]);
841
                }
842
 
843
 
844
            }
649 geraldo 845
 
15403 efrain 846
            $data = [
847
                'name' => $jobDescription->name,
848
                'functions' => $jobDescription->functions,
849
                'objectives' => $jobDescription->objectives,
850
                'status' => $jobDescription->status,
851
                'competencies_selected' => [],
15444 efrain 852
                'behaviors' => $behaviors,
853
                'competency_types' => $competencyTypes,
854
                'competencies' => $competencies,
15403 efrain 855
            ];
856
 
1320 efrain 857
            $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
66 efrain 858
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
15403 efrain 859
            $competencyTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
860
 
861
            $competencyTypes = [];
649 geraldo 862
 
15403 efrain 863
            $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
864
            $jobDescriptionCompetencyBehaviorMapper = JobDescriptionCompetencyBehaviorMapper::getInstance($this->adapter);
865
 
866
            $jobDescriptionCompetencies = $jobDescriptionCompetencyMapper->fetchAllByJobDescriptionId($jobDescription->id);
867
            foreach($jobDescriptionCompetencies as $jobDescriptionCompetency)
868
            {
869
                $competency = $competencyMapper->fetchOne($jobDescriptionCompetency->competency_id);
870
                if(!$competency) {
871
                    continue;
872
                }
873
 
874
                if(isset($competencyTypes[$competency->competency_type_id])) {
875
                    $competencyType = $competencyTypes[$competency->competency_type_id];
876
                } else {
877
 
878
                    $competencyType = $competencyTypeMapper->fetchOne($competency->competency_type_id);
879
                    if(!$competencyType) {
880
                        continue;
1154 geraldo 881
                    }
15403 efrain 882
 
883
                    $competencyTypes[$competency->competency_type_id] = $competencyType;
884
                }
885
 
886
                $competency_selected = [
887
                    'uuid' => $competency->uuid,
888
                    'competency_type_uuid' => $competencyType->uuid,
889
                    'behaviors' => []
890
                ];
891
 
15444 efrain 892
 
15403 efrain 893
 
15444 efrain 894
                foreach($competencies as $record_competency)
15403 efrain 895
                {
15444 efrain 896
                    if($record_competency['uuid'] == $competency->uuid) {
897
 
898
                        foreach($record_competency['behaviors'] as $uuid_behavior)
899
                        {
1154 geraldo 900
 
15444 efrain 901
 
902
                            $behavior = $behaviorMapper->fetchOneByUuid($uuid_behavior);
903
                            if($behavior) {
904
 
905
                                $jobDescriptionCompetencyBehavior = $jobDescriptionCompetencyBehaviorMapper->fetchOneByJobDescriptionIdAndCompetencyIdAndBehaviorId($jobDescription->id, $competency->id, $behavior->id);
906
                                if($jobDescriptionCompetencyBehavior) {
907
                                    $level = $jobDescriptionCompetencyBehavior->level;
908
                                } else {
909
                                    $level = 0;
910
                                }
911
 
912
                                array_push($competency_selected['behaviors'], ['uuid' => $behavior->uuid, 'level' => $level]);
913
                            }
914
                        }
915
 
1197 geraldo 916
                    }
15403 efrain 917
 
15444 efrain 918
 
66 efrain 919
                }
15403 efrain 920
 
15444 efrain 921
 
922
 
15403 efrain 923
                array_push($data['competencies_selected'], $competency_selected);
924
 
925
 
926
 
66 efrain 927
            }
928
 
929
            $response = [
930
                'success' => true,
931
                'data' => $data
932
            ];
649 geraldo 933
 
66 efrain 934
            return new JsonModel($response);
935
        } else {
936
            $data = [
937
                'success' => false,
938
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
939
            ];
649 geraldo 940
 
66 efrain 941
            return new JsonModel($data);
942
        }
649 geraldo 943
 
66 efrain 944
        return new JsonModel($data);
945
    }
649 geraldo 946
 
14203 stevensc 947
    public function deleteAction()
948
    {
66 efrain 949
        $currentUserPlugin = $this->plugin('currentUserPlugin');
950
        $currentUser = $currentUserPlugin->getUser();
951
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 952
 
66 efrain 953
        $request = $this->getRequest();
954
        $uuid = $this->params()->fromRoute('id');
649 geraldo 955
 
956
        if (!$uuid) {
66 efrain 957
            $data = [
649 geraldo 958
                'success' => false,
959
                'data' => 'ERROR_INVALID_PARAMETER'
66 efrain 960
            ];
649 geraldo 961
 
66 efrain 962
            return new JsonModel($data);
963
        }
649 geraldo 964
 
965
 
66 efrain 966
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
967
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
649 geraldo 968
        if (!$jobDescription) {
66 efrain 969
            $data = [
649 geraldo 970
                'success' => false,
971
                'data' => 'ERROR_RECORD_NOT_FOUND'
66 efrain 972
            ];
649 geraldo 973
 
66 efrain 974
            return new JsonModel($data);
975
        }
649 geraldo 976
 
851 geraldo 977
        if ($currentCompany && $jobDescription->company_id != $currentCompany->id) {
66 efrain 978
            $data = [
649 geraldo 979
                'success' => false,
980
                'data' => 'ERROR_UNAUTHORIZED'
66 efrain 981
            ];
649 geraldo 982
 
66 efrain 983
            return new JsonModel($data);
984
        }
985
 
649 geraldo 986
 
987
        if ($request->isPost()) {
16770 efrain 988
            $jobDescriptionMapper->removeAllJobDescriptionIdBossByJobDescripcionId($jobDescription->id);
989
 
990
 
66 efrain 991
            $result = $jobDescriptionMapper->delete($jobDescription);
649 geraldo 992
            if ($result) {
66 efrain 993
                $this->logger->info('Se borro el cargo de trabajo ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
649 geraldo 994
 
66 efrain 995
                $data = [
996
                    'success' => true,
997
                    'data' => 'LABEL_RECORD_DELETED'
998
                ];
999
            } else {
1000
 
1001
                $data = [
649 geraldo 1002
                    'success' => false,
1003
                    'data' => $jobDescriptionMapper->getError()
66 efrain 1004
                ];
1005
 
1006
                return new JsonModel($data);
1007
            }
1008
        } else {
1009
            $data = [
1010
                'success' => false,
1011
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1012
            ];
649 geraldo 1013
 
66 efrain 1014
            return new JsonModel($data);
1015
        }
649 geraldo 1016
 
66 efrain 1017
        return new JsonModel($data);
1018
    }
561 geraldo 1019
 
14203 stevensc 1020
    public function importAction()
1021
    {
850 geraldo 1022
 
852 geraldo 1023
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1024
        $currentUser = $currentUserPlugin->getUser();
1025
        $currentCompany = $currentUserPlugin->getCompany();
854 geraldo 1026
 
1027
        if (!$currentCompany) {
852 geraldo 1028
            $data = [
1029
                'success' => false,
1030
                'data' => 'ERROR_UNAUTHORIZED'
1031
            ];
854 geraldo 1032
 
852 geraldo 1033
            return new JsonModel($data);
1034
        }
854 geraldo 1035
 
852 geraldo 1036
        $request = $this->getRequest();
854 geraldo 1037
 
1038
        if ($request->isPost()) {
1039
 
852 geraldo 1040
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
1041
            $jobDescriptionsDefault = $jobDescriptionMapper->fetchAllByDefault();
854 geraldo 1042
 
852 geraldo 1043
            $new_records = 0;
854 geraldo 1044
            foreach ($jobDescriptionsDefault as $jobDescriptionDefault) {
1045
                if ($jobDescriptionDefault->status == JobDescription::STATUS_INACTIVE) {
852 geraldo 1046
                    continue;
1047
                }
854 geraldo 1048
 
852 geraldo 1049
                $jobDescription = $jobDescriptionMapper->fetchOneByCompanyId($currentCompany->id, $jobDescriptionDefault->id);
854 geraldo 1050
                if (!$jobDescription) {
1051
 
852 geraldo 1052
                    $jobDescription = new JobDescription();
1053
                    $jobDescription->company_id = $currentCompany->id;
1054
                    $jobDescription->job_description_id_default = $jobDescriptionDefault->id;
1055
                    $jobDescription->name = $jobDescriptionDefault->name;
1056
                    $jobDescription->functions = $jobDescriptionDefault->functions;
1057
                    $jobDescription->objectives = $jobDescriptionDefault->objectives;
1058
                    $jobDescription->status = JobDescription::STATUS_ACTIVE;
854 geraldo 1059
 
858 geraldo 1060
                    $result = $jobDescriptionMapper->insert($jobDescription);
854 geraldo 1061
 
858 geraldo 1062
                    if ($result) {
1063
 
1064
                        //Add Competencies
955 geraldo 1065
                        $competenceMapper = CompetencyMapper::getInstance($this->adapter);
858 geraldo 1066
                        $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
953 geraldo 1067
                        $jobDescriptionCompetencies = $jobDescriptionCompetencyMapper->fetchByJobDescriptionId($jobDescriptionDefault->id);
939 geraldo 1068
 
953 geraldo 1069
                        if (count($jobDescriptionCompetencies) != 0) {
1070
                            foreach ($jobDescriptionCompetencies as $record) {
955 geraldo 1071
 
1072
                                $competence = $competenceMapper->fetchOneByCompanyIdAndCompetencyIdDefault($currentCompany->id, $record->competency_id);
956 geraldo 1073
                                if ($competence) {
1074
                                    $newDescriptionCompetency = new JobDescriptionCompetency();
1075
                                    $newDescriptionCompetency->competency_id = $competence->id;
1076
                                    $newDescriptionCompetency->job_description_id = $jobDescription->id;
1077
                                    $newDescriptionCompetency->level = $record->level;
1078
                                    $jobDescriptionCompetencyMapper->insert($newDescriptionCompetency);
955 geraldo 1079
                                }
858 geraldo 1080
                            }
1081
                        }
1082
 
852 geraldo 1083
                        $new_records++;
1084
                    } else {
1085
                        $data = [
1086
                            'success' => false,
1087
                            'data' => 'ERROR_CANT_ADD_JOB_DESCRIPTION'
1088
                        ];
854 geraldo 1089
 
852 geraldo 1090
                        return new JsonModel($data);
1091
                    }
1092
                }
1093
            }
854 geraldo 1094
 
1095
            if ($new_records) {
1096
 
1097
                if (1 == $new_records) {
852 geraldo 1098
                    $data = [
1099
                        'success' => true,
1100
                        'data' => 'LABEL_1_JOB_DESCRIPTION_IMPORTED'
1101
                    ];
854 geraldo 1102
 
852 geraldo 1103
                    return new JsonModel($data);
1104
                } else {
1105
                    $data = [
1106
                        'success' => true,
854 geraldo 1107
                        'data' => $new_records . ' LABEL_MULTI_JOB_DESCRIPTION_IMPORTED'
852 geraldo 1108
                    ];
854 geraldo 1109
 
852 geraldo 1110
                    return new JsonModel($data);
1111
                }
1112
            } else {
1113
                $data = [
1114
                    'success' => true,
1115
                    'data' => 'LABEL_NO_JOB_DESCRIPTION_IMPORTED'
1116
                ];
854 geraldo 1117
 
852 geraldo 1118
                return new JsonModel($data);
1119
            }
1120
        } else {
1121
            $data = [
1122
                'success' => false,
1123
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1124
            ];
854 geraldo 1125
 
852 geraldo 1126
            return new JsonModel($data);
1127
        }
854 geraldo 1128
 
852 geraldo 1129
        return new JsonModel($data);
850 geraldo 1130
    }
1131
 
14203 stevensc 1132
    public function reportAction()
1133
    {
561 geraldo 1134
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1135
        $currentUser = $currentUserPlugin->getUser();
1136
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 1137
 
561 geraldo 1138
        $request = $this->getRequest();
1139
        $uuid = $this->params()->fromRoute('id');
1140
 
649 geraldo 1141
 
1142
        if (!$uuid) {
561 geraldo 1143
            $data = [
649 geraldo 1144
                'success' => false,
1145
                'data' => 'ERROR_INVALID_PARAMETER'
561 geraldo 1146
            ];
649 geraldo 1147
 
561 geraldo 1148
            return new JsonModel($data);
1149
        }
1150
 
1151
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
1152
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
649 geraldo 1153
        if (!$jobDescription) {
561 geraldo 1154
            $data = [
649 geraldo 1155
                'success' => false,
1156
                'data' => 'ERROR_RECORD_NOT_FOUND'
561 geraldo 1157
            ];
649 geraldo 1158
 
561 geraldo 1159
            return new JsonModel($data);
1160
        }
649 geraldo 1161
 
851 geraldo 1162
        if ($currentCompany && $jobDescription->company_id != $currentCompany->id) {
561 geraldo 1163
            $data = [
649 geraldo 1164
                'success' => false,
1165
                'data' => 'ERROR_UNAUTHORIZED'
561 geraldo 1166
            ];
649 geraldo 1167
 
561 geraldo 1168
            return new JsonModel($data);
1169
        }
1170
 
649 geraldo 1171
 
561 geraldo 1172
        if ($request->isGet()) {
1173
 
15444 efrain 1174
 
16770 efrain 1175
            /*
15403 efrain 1176
            $filename = Functions::normalizeStringFilename($jobDescription->name . '-' . date('Y-m-d H:i a') . '.pdf');
1213 geraldo 1177
 
15403 efrain 1178
 
15444 efrain 1179
 
1180
 
15403 efrain 1181
            $content = base64_encode($this->renderPdf($currentCompany, $jobDescription));
15444 efrain 1182
            $data = [
1183
                'success' => true,
1184
                'data' => [
1185
                    'basename' => $filename,
1186
                    'content' => $content
1187
                ]
1188
            ];
15403 efrain 1189
 
16770 efrain 1190
            return new JsonModel($data);*/
15444 efrain 1191
 
16770 efrain 1192
 
15444 efrain 1193
 
1194
            $content = $this->renderPdf($currentCompany, $jobDescription);
15403 efrain 1195
            $response = new Response();
1196
            $response->setStatusCode(200);
1197
            $response->setContent($content);
16770 efrain 1198
 
15403 efrain 1199
            $headers = $response->getHeaders();
1200
            $headers->clearHeaders();
1201
 
1202
            $headers->addHeaderLine('Content-Description: File Transfer');
1203
            $headers->addHeaderLine('Content-Type: application/pdf');
1204
            //$headers->addHeaderLine('Content-Disposition: attachment; filename=' . $filename);
1205
            $headers->addHeaderLine('Content-Transfer-Encoding: binary');
1206
            $headers->addHeaderLine('Expires: 0');
1207
            $headers->addHeaderLine('Cache-Control: must-revalidate');
1208
            $headers->addHeaderLine('Pragma: public');
1209
            return $response;
16770 efrain 1210
 
15403 efrain 1211
 
15444 efrain 1212
 
15403 efrain 1213
 
1214
 
561 geraldo 1215
        } else {
1216
            $data = [
1217
                'success' => false,
1218
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1219
            ];
649 geraldo 1220
 
561 geraldo 1221
            return new JsonModel($data);
1222
        }
649 geraldo 1223
 
561 geraldo 1224
        return new JsonModel($data);
1225
    }
15403 efrain 1226
 
1227
 
842 geraldo 1228
 
15403 efrain 1229
 
1230
 
788 geraldo 1231
    /**
1232
     * Render PDF
15079 efrain 1233
     * @param Company $company;
1336 efrain 1234
     * @param JobDescription $jobDescription
1235
     * @return mixed
788 geraldo 1236
     */
15403 efrain 1237
    private function renderPDF($company, $jobDescription)
14203 stevensc 1238
    {
561 geraldo 1239
 
1240
        //Generate New PDF
15403 efrain 1241
        $pdf = new JobDescriptionPdf();
1242
        $pdf->SetTitle($jobDescription->name);
842 geraldo 1243
 
15079 efrain 1244
 
1245
        $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
1246
 
1247
        $header = $company->header ? $target_path . DIRECTORY_SEPARATOR . $company->header : '';
1248
        if(empty($header) || !file_exists($header)) {
1249
            $header = $this->config['leaderslinked.images_default.company_pdf_header'];
1250
        }
1251
 
1252
        $footer = $company->footer ? $target_path . DIRECTORY_SEPARATOR . $company->footer : '';
1253
        if(empty($footer) || !file_exists($footer)) {
1254
            $footer = $this->config['leaderslinked.images_default.company_pdf_footer'];
1255
        }
1256
 
1257
        $pdf->header = $header;
1258
        $pdf->footer = $footer;
15403 efrain 1259
        $pdf->translator = $this->translator;
842 geraldo 1260
 
561 geraldo 1261
 
927 geraldo 1262
        $pdf->SetMargins(10, 0, 10);
714 geraldo 1263
 
561 geraldo 1264
        $pdf->AliasNbPages();
1265
        $pdf->AddPage();
564 geraldo 1266
 
697 geraldo 1267
        // Add Title
1268
        $pdf->customHeader();
654 geraldo 1269
 
649 geraldo 1270
        // Job Description Name
654 geraldo 1271
 
649 geraldo 1272
        $job_descrition[] = array(
15403 efrain 1273
            'title' => $this->translator->translate('LABEL_PDF_JOB_DESCRIPTION_LABORAL_CATEGORY') .  ' : ' ,
709 geraldo 1274
            'content' => ' ' . $jobDescription->name
649 geraldo 1275
        );
654 geraldo 1276
 
649 geraldo 1277
 
15403 efrain 1278
        $pdf->borderTable($this->translator->translate('LABEL_PDF_JOB_TITLE_NAME') . ' : ', $job_descrition);
649 geraldo 1279
 
15403 efrain 1280
 
654 geraldo 1281
        //Hierarchical Relationship
1282
 
650 geraldo 1283
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
16770 efrain 1284
 
714 geraldo 1285
        $subordinate = '';
16770 efrain 1286
 
1287
        if($jobDescription->company_id) {
1288
            $jobDescriptionSubordinates = $jobDescriptionMapper->fetchAllByCompanyIdAndJobDescriptionIdBoss($jobDescription->company_id,$jobDescription->id);
1289
        } else {
1290
            $jobDescriptionSubordinates = $jobDescriptionMapper->fetchAllDefaultAndJobDescriptionIdBoss($jobDescription->id);
1291
        }
1292
 
1293
        if ($jobDescriptionSubordinates) {
1294
            foreach ($jobDescriptionSubordinates as $jobDescriptionSubordinate) {
1295
                $subordinate .= $jobDescriptionSubordinate->name . ' ';
717 geraldo 1296
            }
1297
        }
714 geraldo 1298
 
649 geraldo 1299
        $jobDescriptionBoss = $jobDescriptionMapper->fetchOne($jobDescription->job_description_id_boss);
1300
 
654 geraldo 1301
        $relationship = [
15403 efrain 1302
            [
1303
                'title' => $this->translator->translate('LABEL_PDF_JOB_SUPERVISES') . ': ',
726 geraldo 1304
                'content' => $subordinate ? ' ' . $subordinate : ' No aplica'
15403 efrain 1305
            ],
1306
            [
1307
                'title' => $this->translator->translate('LABEL_PDF_JOB_SUPERVISED_BY') . ': ',
1308
                'content' => $jobDescriptionBoss ? ' ' . $jobDescriptionBoss->name : ' ' .  $this->translator->translate('LABEL_PDF_JOB_DOES_NOT_APPLY'),
1309
            ]
654 geraldo 1310
        ];
579 geraldo 1311
 
15403 efrain 1312
        $pdf->borderTable($this->translator->translate('LABEL_PDF_JOB_HIERARCHICAL_RELATIONSHIP') . ' : ', $relationship);
628 geraldo 1313
 
654 geraldo 1314
        // Objetives
628 geraldo 1315
 
1259 geraldo 1316
        $objetives[] = array('content' => strip_tags($jobDescription->objectives));
15403 efrain 1317
        $pdf->borderTable($this->translator->translate('LABEL_PDF_JOB_OBJECTIVES') . ' : ', $objetives);
653 geraldo 1318
 
654 geraldo 1319
        // Functions
628 geraldo 1320
 
1259 geraldo 1321
        $functions[] = array('content' => strip_tags($jobDescription->functions));
15403 efrain 1322
        $pdf->borderTable($this->translator->translate('LABEL_PDF_JOB_PRINCIPAL_FUNCTIONS') . ' : ', $functions);
628 geraldo 1323
 
749 geraldo 1324
        // add section signature
709 geraldo 1325
        $pdf->sectionSignature();
1326
 
654 geraldo 1327
        //Competencies
628 geraldo 1328
 
658 geraldo 1329
        $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
15444 efrain 1330
        $jobDescriptionCompetencies = $jobDescriptionCompetencyMapper->fetchAllByJobDescriptionId($jobDescription->id);
649 geraldo 1331
 
672 geraldo 1332
        $competencyMapper = CompetencyMapper::getInstance($this->adapter);
933 geraldo 1333
        $competenceTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
15030 efrain 1334
        $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
15403 efrain 1335
        $jobDescriptionCompetencyBehaviorMapper = JobDescriptionCompetencyBehaviorMapper::getInstance($this->adapter);
1320 efrain 1336
        $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
842 geraldo 1337
 
939 geraldo 1338
 
15444 efrain 1339
        $max = count($jobDescriptionCompetencies);
672 geraldo 1340
 
15444 efrain 1341
        if ($max) {
842 geraldo 1342
 
748 geraldo 1343
            // add new page
842 geraldo 1344
 
748 geraldo 1345
            $pdf->AddPage();
679 geraldo 1346
 
745 geraldo 1347
            $competencies_header[] = array('content' => 'De acuerdo a la siguiente escala, se deberá asignar un valor a cada conducta deseable correspondiente a cada una de las competencias requeridas para el cargo. La valoración refiere a las conductas deseables requeridas en cada cargo, y no refiere a la persona que lo ocupa');
679 geraldo 1348
 
745 geraldo 1349
            $pdf->singleTable('Competencias requeridas para el cargo:', $competencies_header);
709 geraldo 1350
 
745 geraldo 1351
 
1352
            // Add scale section
15444 efrain 1353
           // $pdf->sectionScale();
745 geraldo 1354
 
1355
 
15444 efrain 1356
            for ($i = 0; $i < $max; $i++)  {
745 geraldo 1357
 
15444 efrain 1358
                $record = $jobDescriptionCompetencies[$i];
1359
 
15030 efrain 1360
                $behaviorCompetencies = $competencyBehaviorMapper->fetchAllByCompetencyId($record->competency_id);
1213 geraldo 1361
                $behaviors = [];
1362
 
1363
                foreach ($behaviorCompetencies as $rows) {
1364
 
1365
                    $behavior = $behaviorMapper->fetchOne($rows->behavior_id);
15444 efrain 1366
                    if ($behavior) {
1367
                        $jobDescriptionCompetencyBehavior = $jobDescriptionCompetencyBehaviorMapper->fetchOneByJobDescriptionIdAndCompetencyIdAndBehaviorId($jobDescription->id, $record->competency_id, $rows->behavior_id);
1368
                        if($jobDescriptionCompetencyBehavior) {
1369
                            $level = $jobDescriptionCompetencyBehavior->level;
1370
                        } else {
1371
                            $level = 0;
1372
                        }
1213 geraldo 1373
 
1374
                        array_push($behaviors, [
1375
                            'description' => $behavior->description,
15444 efrain 1376
                            'level' => $level
1213 geraldo 1377
                        ]);
1378
                    }
1379
                }
1380
 
745 geraldo 1381
                $competency = $competencyMapper->fetchOne($record->competency_id);
933 geraldo 1382
                $competenceType = $competenceTypeMapper->fetchOne($competency->competency_type_id);
745 geraldo 1383
 
15444 efrain 1384
                $j = $i + 1;
1385
                $last = $j == $max;
1386
 
1387
                $pdf->competencyTable($j, $competenceType, $competency, $behaviors, $last);
1388
 
1389
                if(!$last) {
1390
                    $y = $pdf->getY();
1391
                    if($y >= JobDescriptionPdf::MAX_Y_ADD_PAGE) {
1392
                        $this->addPage();
1393
                    }
1394
                }
933 geraldo 1395
 
709 geraldo 1396
            }
669 geraldo 1397
        }
15403 efrain 1398
 
1399
        /*
1400
 
1401
        $temp_file = tempnam(sys_get_temp_dir(), 'job-description');
1402
        $pdf->Output('F', $temp_file);
658 geraldo 1403
 
15403 efrain 1404
        return $temp_file;*/
1405
 
15444 efrain 1406
 
1407
 
15403 efrain 1408
        return $pdf->Output('S');
561 geraldo 1409
    }
15079 efrain 1410
 
15403 efrain 1411
 
1412
 
15079 efrain 1413
 
66 efrain 1414
}