Proyectos de Subversion LeadersLinked - Backend

Rev

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