Proyectos de Subversion LeadersLinked - Backend

Rev

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