Proyectos de Subversion LeadersLinked - Backend

Rev

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

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