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
4384 eleazar 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 8
 
4384 eleazar 9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
4579 eleazar 11
use Laminas\View\Model\ViewModel;
4384 eleazar 12
use Laminas\View\Model\JsonModel;
5027 efrain 13
use LeadersLinked\Library\Functions;
4579 eleazar 14
use LeadersLinked\Mapper\SurveyMapper;
4678 eleazar 15
use LeadersLinked\Mapper\SurveyFormMapper;
4883 eleazar 16
use LeadersLinked\Mapper\SurveyJobDescriptionMapper;
17
use LeadersLinked\Mapper\SurveyIndustryMapper;
5074 eleazar 18
use LeadersLinked\Mapper\SurveySkillMapper;
4883 eleazar 19
use LeadersLinked\Mapper\SurveyLocationMapper;
5074 eleazar 20
use LeadersLinked\Mapper\SurveyLanguageMapper;
4855 eleazar 21
use LeadersLinked\Mapper\IndustryMapper;
22
use LeadersLinked\Mapper\JobDescriptionMapper;
5074 eleazar 23
use LeadersLinked\Mapper\LanguageMapper;
24
use LeadersLinked\Mapper\SkillMapper;
4579 eleazar 25
use LeadersLinked\Form\SurveyForm;
26
use LeadersLinked\Model\Survey;
27
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
5026 efrain 28
use LeadersLinked\Model\SurveyJobDescription;
5074 eleazar 29
use LeadersLinked\Model\SurveySkill;
5027 efrain 30
use LeadersLinked\Model\SurveyIndustry;
5074 eleazar 31
use LeadersLinked\Model\SurveyLanguage;
6376 eleazar 32
use LeadersLinked\Model\SurveyLocation;
4384 eleazar 33
 
34
class SurveyController extends AbstractActionController {
35
 
36
    /**
37
     *
38
     * @var AdapterInterface
39
     */
40
    private $adapter;
16768 efrain 41
 
4384 eleazar 42
    /**
43
     *
44
     * @var  LoggerInterface
45
     */
46
    private $logger;
16768 efrain 47
 
4384 eleazar 48
    /**
49
     *
50
     * @var array
51
     */
52
    private $config;
16768 efrain 53
 
4384 eleazar 54
    /**
55
     *
56
     * @param AdapterInterface $adapter
57
     * @param LoggerInterface $logger
58
     * @param array $config
59
     */
16768 efrain 60
    public function __construct($adapter, $logger, $config)
61
    {
4384 eleazar 62
        $this->adapter = $adapter;
63
        $this->logger = $logger;
64
        $this->config = $config;
65
    }
66
 
67
    public function indexAction() {
4579 eleazar 68
        $request = $this->getRequest();
69
        $currentUserPlugin = $this->plugin('currentUserPlugin');
70
        $currentCompany = $currentUserPlugin->getCompany();
71
        $currentUser = $currentUserPlugin->getUser();
4384 eleazar 72
 
4579 eleazar 73
 
74
        $request = $this->getRequest();
75
        if ($request->isGet()) {
6341 eleazar 76
            $sandbox = $this->config['leaderslinked.runmode.sandbox'];
77
            if($sandbox) {
78
                $google_map_key  = $this->config['leaderslinked.google_map.sandbox_api_key'];
79
            } else {
80
                $google_map_key  = $this->config['leaderslinked.google_map.production_api_key'];
81
            }
4579 eleazar 82
 
83
            $headers = $request->getHeaders();
84
 
85
            $isJson = false;
86
            if ($headers->has('Accept')) {
87
                $accept = $headers->get('Accept');
88
 
89
                $prioritized = $accept->getPrioritized();
90
 
91
                foreach ($prioritized as $key => $value) {
92
                    $raw = trim($value->getRaw());
93
 
94
                    if (!$isJson) {
95
                        $isJson = strpos($raw, 'json');
96
                    }
97
                }
98
            }
99
 
100
            if ($isJson) {
101
                $search = $this->params()->fromQuery('search', []);
16766 efrain 102
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
4579 eleazar 103
 
15371 efrain 104
                $start = intval($this->params()->fromQuery('start', 0), 10);
4579 eleazar 105
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
15371 efrain 106
                $page =  intval($start / $records_x_page);
107
                $page++;
108
 
4579 eleazar 109
                $order = $this->params()->fromQuery('order', []);
110
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
16766 efrain 111
                $order_direction = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
4579 eleazar 112
 
113
                $fields = ['name'];
114
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
115
 
116
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
117
                    $order_direction = 'ASC';
118
                }
119
 
4661 eleazar 120
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
121
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'survey/edit');
122
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'survey/delete');
15150 efrain 123
 
4579 eleazar 124
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
125
                $paginator = $surveyMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
126
 
127
                $items = [];
128
                $records = $paginator->getCurrentItems();
15150 efrain 129
 
130
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
131
 
132
                $forms = [];
133
 
4579 eleazar 134
                foreach ($records as $record) {
15150 efrain 135
 
136
                    if(isset($forms[$record->form_id])) {
137
 
138
                        $surveyForm = $forms[$record->form_id];
139
                    } else {
140
                        $surveyForm = $surveyFormMapper->fetchOne($record->form_id);
141
                        $forms[$record->form_id] = $surveyForm;
142
                    }
143
 
144
                    $dtSinceDate = \DateTime::createFromFormat('Y-m-d', $record->since_date);
145
                    $dtLastDate = \DateTime::createFromFormat('Y-m-d', $record->last_date);
146
 
147
 
148
 
4579 eleazar 149
                    $item = [
150
                        'id' => $record->id,
151
                        'name' => $record->name,
4754 eleazar 152
                        'form' => $surveyForm->name,
15150 efrain 153
                        'since_date' => $dtSinceDate->format('d/m/Y'),
154
                        'last_date' => $dtLastDate->format('d/m/Y'),
4579 eleazar 155
                        'status' => $record->status,
156
                        'actions' => [
15150 efrain 157
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('survey/edit', ['id' => $record->uuid]) : '',
158
                            'link_delete' => $allowDelete  ? $this->url()->fromRoute('survey/delete', ['id' => $record->uuid]) : '',
4579 eleazar 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 SurveyForm($this->adapter, $currentCompany->id);
175
 
176
                $this->layout()->setTemplate('layout/layout-backend');
177
                $viewModel = new ViewModel();
178
                $viewModel->setTemplate('leaders-linked/survey/index.phtml');
179
                $viewModel->setVariable('form', $form);
6351 eleazar 180
                $viewModel->setVariable('google_map_key', $google_map_key);
4579 eleazar 181
                return $viewModel;
182
            }
183
        } else {
184
            return new JsonModel([
185
                'success' => false,
186
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
187
            ]);
188
        }
4384 eleazar 189
    }
190
 
4579 eleazar 191
    public function addAction() {
192
        $request = $this->getRequest();
193
        $currentUserPlugin = $this->plugin('currentUserPlugin');
194
        $currentCompany = $currentUserPlugin->getCompany();
195
        $currentUser = $currentUserPlugin->getUser();
12030 eleazar 196
 
4579 eleazar 197
        $request = $this->getRequest();
198
 
199
        if ($request->isPost()) {
200
 
201
            $form = new SurveyForm($this->adapter, $currentCompany->id);
4672 eleazar 202
 
4579 eleazar 203
            $dataPost = $request->getPost()->toArray();
4661 eleazar 204
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
6659 eleazar 205
 
4579 eleazar 206
            $form->setData($dataPost);
207
 
208
            if ($form->isValid()) {
209
                $dataPost = (array) $form->getData();
210
 
211
                $hydrator = new ObjectPropertyHydrator();
6376 eleazar 212
 
4579 eleazar 213
                $survey = new Survey();
214
                $hydrator->hydrate($dataPost, $survey);
215
 
216
                if (!$survey->status) {
217
                    $survey->status = Survey::STATUS_INACTIVE;
218
                }
219
                $survey->company_id = $currentCompany->id;
220
 
4677 eleazar 221
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
222
                $surveyForm = $surveyFormMapper->fetchOneByUuid($dataPost['form_id']);
223
                $survey->form_id = $surveyForm->id;
4579 eleazar 224
 
4724 eleazar 225
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
5150 eleazar 226
                //$result = $surveyMapper->insert($survey);
227
 
228
                if($surveyMapper->insert($survey)) {
229
                    $survey = $surveyMapper->fetchOne($survey->id);
230
 
6627 eleazar 231
                    if(!empty($dataPost['location_search'])){
15150 efrain 232
                                                $record = new SurveyLocation();
233
                        $record->formatted_address = $dataPost['formatted_address'];
234
                        $record->city1 = $dataPost['city1'];
235
                        $record->city2 = $dataPost['city2'];
236
                        $record->address1 = $dataPost['address1'];
237
                        $record->address2 = $dataPost['address2'];
238
                        $record->country = $dataPost['country'];
239
                        $record->latitude = $dataPost['latitude'];
240
                        $record->longitude = $dataPost['longitude'];
241
                        $record->postal_code = $dataPost['postal_code'];
242
                        $record->state = $dataPost['state'];
243
                        $record->survey_id = $survey->id;
244
 
6627 eleazar 245
                        $surveyLocationMapper = SurveyLocationMapper::getInstance($this->adapter);
15150 efrain 246
                        $surveyLocationMapper->insert($record);
6627 eleazar 247
                    }
6376 eleazar 248
 
5150 eleazar 249
                    if(!empty($dataPost['job_description_id'])){
250
                        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
251
                        $surveyJobDescriptionMapper = SurveyJobDescriptionMapper::getInstance($this->adapter);
252
 
15150 efrain 253
                        foreach($dataPost['job_description_id'] as $jobDescriptionUuid)
254
                        {
5150 eleazar 255
                            $jobDescription = $jobDescriptionMapper->fetchOneByUuid($jobDescriptionUuid);
256
                            if($jobDescription && $jobDescription->company_id == $currentCompany->id) {
257
                                $record = new SurveyJobDescription();
258
                                $record->job_description_id = $jobDescription->id;
259
                                $record->survey_id = $survey->id;
260
 
15150 efrain 261
                                $surveyJobDescriptionMapper->insert($record);
5150 eleazar 262
                            }
263
                        }
264
                    }
265
                    if(!empty($dataPost['skill_id'])){
266
                         $skillMapper = SkillMapper::getInstance($this->adapter);
267
                         $surveySkillMapper = SurveySkillMapper::getInstance($this->adapter);
268
 
15150 efrain 269
                         foreach($dataPost['skill_id'] as $skillUuid)
270
                         {
5150 eleazar 271
                             $skill = $skillMapper->fetchOneByUuid($skillUuid);
272
 
15150 efrain 273
                             if($skill) {
5150 eleazar 274
                                $record = new SurveySkill();
275
                                $record->skill_id = $skill->id;
276
                                $record->survey_id = $survey->id;
277
 
15150 efrain 278
                                $surveySkillMapper->insert($record);
279
                            }
5150 eleazar 280
                         }
281
                     }
282
 
283
                    if(!empty($dataPost['industry_id'])){
284
                         $industryMapper = IndustryMapper::getInstance($this->adapter);
285
                         $surveyIndustryMapper = SurveyIndustryMapper::getInstance($this->adapter);
286
 
15150 efrain 287
                         foreach($dataPost['industry_id'] as $industryUuid)
288
                         {
5150 eleazar 289
                             $industry = $industryMapper->fetchOneByUuid($industryUuid);
15150 efrain 290
                             if($industry) {
5150 eleazar 291
                                 $record = new SurveyIndustry();
292
                                 $record->industry_id = $industry->id;
293
                                 $record->survey_id = $survey->id;
294
 
15150 efrain 295
                                 $surveyIndustryMapper->insert($record);
296
                             }
5150 eleazar 297
                         }
298
                     }
299
 
300
                    if(!empty($dataPost['language_id'])){
15150 efrain 301
                        $languageMapper = LanguageMapper::getInstance($this->adapter);
5150 eleazar 302
                        $surveyLanguageMapper = SurveyLanguageMapper::getInstance($this->adapter);
303
 
15150 efrain 304
                        foreach($dataPost['language_id'] as $language_id)
305
                        {
5150 eleazar 306
 
15150 efrain 307
                            $language = $languageMapper->fetchOne($language_id);
308
                            if($language) {
5150 eleazar 309
                                $record = new SurveyLanguage();
310
                                $record->language_id = $language_id;
311
                                $record->survey_id = $survey->id;
312
 
15150 efrain 313
                                $surveyLanguageMapper->insert($record);
314
                            }
5150 eleazar 315
                        }
15150 efrain 316
                    }
5150 eleazar 317
 
318
                    $data = [
319
                        'success' => true,
320
                        'data' => 'LABEL_RECORD_ADDED'
321
                    ];
322
 
323
                } else {
324
                    $data = [
325
                        'success'   => false,
326
                        'data'      => $surveyMapper->getError()
327
                    ];
328
                }
329
 
5383 eleazar 330
 
4579 eleazar 331
                return new JsonModel($data);
332
            } else {
333
                $messages = [];
334
                $form_messages = (array) $form->getMessages();
335
                foreach ($form_messages as $fieldname => $field_messages) {
336
 
337
                    $messages[$fieldname] = array_values($field_messages);
338
                }
339
 
340
                return new JsonModel([
341
                    'success' => false,
342
                    'data' => $messages
343
                ]);
344
            }
345
        } else {
346
            $data = [
347
                'success' => false,
348
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
349
            ];
350
 
351
            return new JsonModel($data);
352
        }
353
 
354
        return new JsonModel($data);
355
    }
356
 
15150 efrain 357
    /*
4856 eleazar 358
    public function segmentAction() {
4855 eleazar 359
        $request = $this->getRequest();
360
        $currentUserPlugin = $this->plugin('currentUserPlugin');
361
        $currentCompany = $currentUserPlugin->getCompany();
362
        $currentUser = $currentUserPlugin->getUser();
363
 
364
        $request = $this->getRequest();
365
        $uuid = $this->params()->fromRoute('id');
366
 
367
 
368
        if (!$uuid) {
369
            $data = [
370
                'success' => false,
371
                'data' => 'ERROR_INVALID_PARAMETER'
372
            ];
373
 
374
            return new JsonModel($data);
375
        }
376
 
377
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
378
        $survey = $surveyMapper->fetchOneByUuid($uuid);
379
        if (!$survey) {
380
            $data = [
381
                'success' => false,
382
                'data' => 'ERROR_RECORD_NOT_FOUND'
383
            ];
384
 
385
            return new JsonModel($data);
386
        }
387
 
388
        if ($survey->company_id != $currentCompany->id) {
389
            return new JsonModel([
390
                'success' => false,
391
                'data' => 'ERROR_UNAUTHORIZED'
392
            ]);
393
        }
394
 
4863 eleazar 395
        if($request->isPost()){
4865 eleazar 396
            $form = new SurveySegmentedForm($this->adapter, $currentCompany->id);
5039 efrain 397
 
398
 
4855 eleazar 399
            $dataPost = $request->getPost()->toArray();
5006 eleazar 400
            $form->setData($dataPost);
5019 efrain 401
 
4855 eleazar 402
 
5020 efrain 403
 
4855 eleazar 404
            if ($form->isValid()) {
405
                $dataPost = (array) $form->getData();
5029 efrain 406
 
5031 efrain 407
                if(!empty($dataPost['job_description_id'])){
5026 efrain 408
                    $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
409
                    $surveyJobDescriptionMapper = SurveyJobDescriptionMapper::getInstance($this->adapter);
410
 
15150 efrain 411
                    foreach($dataPost['job_description_id'] as $jobDescriptionUuid)
412
                    {
5026 efrain 413
                        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($jobDescriptionUuid);
5034 efrain 414
                        if($jobDescription && $jobDescription->company_id == $currentCompany->id) {
5026 efrain 415
                            $record = new SurveyJobDescription();
416
                            $record->job_description_id = $jobDescription->id;
417
                            $record->survey_id = $survey->id;
15150 efrain 418
 
419
                            $surveyJobDescriptionMapper->insert($record);
4985 eleazar 420
                        }
4916 eleazar 421
                    }
4883 eleazar 422
                }
5028 efrain 423
 
5063 eleazar 424
 
5074 eleazar 425
                if(!empty($dataPost['skill_id'])){
426
                     $skillMapper = SkillMapper::getInstance($this->adapter);
427
                     $surveySkillMapper = SurveySkillMapper::getInstance($this->adapter);
5042 eleazar 428
 
15150 efrain 429
                     foreach($dataPost['skill_id'] as $skillUuid)
430
                     {
5074 eleazar 431
                         $skill = $skillMapper->fetchOneByUuid($skillUuid);
5042 eleazar 432
 
15150 efrain 433
                         if($skill) {
5074 eleazar 434
                            $record = new SurveySkill();
435
                            $record->skill_id = $skill->id;
5053 eleazar 436
                            $record->survey_id = $survey->id;
437
 
15150 efrain 438
                            $surveySkillMapper->insert($record);
439
                         }
5042 eleazar 440
                     }
441
                 }
442
 
443
                if(!empty($dataPost['industry_id'])){
444
 
445
                     $industryMapper = IndustryMapper::getInstance($this->adapter);
446
                     $surveyIndustryMapper = SurveyIndustryMapper::getInstance($this->adapter);
447
 
448
                     $ok = true;
449
 
15150 efrain 450
                     foreach($dataPost['industry_id'] as $industryUuid)
451
                     {
5042 eleazar 452
                         $industry = $industryMapper->fetchOneByUuid($industryUuid);
15150 efrain 453
                         if($industry) {
5042 eleazar 454
                             $record = new SurveyIndustry();
455
                             $record->industry_id = $industry->id;
456
                             $record->survey_id = $survey->id;
457
 
15150 efrain 458
                             $surveyIndustryMapper->insert($record);
459
                          }
5042 eleazar 460
                     }
461
                 }
462
 
5074 eleazar 463
 
5083 eleazar 464
                if(!empty($dataPost['language_id'])){
15150 efrain 465
                    $languageMapper = LanguageMapper::getInstance($this->adapter);
5074 eleazar 466
                    $surveyLanguageMapper = SurveyLanguageMapper::getInstance($this->adapter);
5056 eleazar 467
 
15150 efrain 468
                    foreach($dataPost['language_id'] as $language_id)
469
                    {
470
                        $language = $languageMapper->fetchOne($language_id);
471
                        if($language) {
5074 eleazar 472
                            $record = new SurveyLanguage();
5085 eleazar 473
                            $record->language_id = $language_id;
5056 eleazar 474
                            $record->survey_id = $survey->id;
475
 
15150 efrain 476
                            $surveyLanguageMapper->insert($record);
477
                        }
5056 eleazar 478
                    }
5042 eleazar 479
                 }
480
 
481
                $data = [
482
                    'success' => true,
483
                    'data' => 'LABEL_RECORD_ADDED'
484
                ];
5074 eleazar 485
               // return new JsonModel($data); exit;
486
 
5060 eleazar 487
               // return new JsonModel($data);
4855 eleazar 488
            } else {
5013 eleazar 489
                $messages = [];
4855 eleazar 490
                $form_messages = (array) $form->getMessages();
491
                foreach ($form_messages as $fieldname => $field_messages) {
492
 
493
                    $messages[$fieldname] = array_values($field_messages);
494
                }
495
 
496
                return new JsonModel([
497
                    'success' => false,
5011 eleazar 498
                    'data' => $messages
5013 eleazar 499
                ]);
4855 eleazar 500
            }
501
        } else {
502
            $data = [
503
                'success' => false,
504
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
505
            ];
506
 
507
            return new JsonModel($data);
508
        }
509
 
5060 eleazar 510
       return new JsonModel($data);
4855 eleazar 511
 
15150 efrain 512
    }*/
4855 eleazar 513
 
4579 eleazar 514
    public function editAction() {
515
        $request = $this->getRequest();
516
        $currentUserPlugin = $this->plugin('currentUserPlugin');
517
        $currentCompany = $currentUserPlugin->getCompany();
518
        $currentUser = $currentUserPlugin->getUser();
519
 
520
        $request = $this->getRequest();
521
        $uuid = $this->params()->fromRoute('id');
522
 
6705 eleazar 523
 
4579 eleazar 524
        if (!$uuid) {
525
            $data = [
526
                'success' => false,
527
                'data' => 'ERROR_INVALID_PARAMETER'
528
            ];
529
 
530
            return new JsonModel($data);
531
        }
6691 eleazar 532
 
533
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
534
        $survey = $surveyMapper->fetchOneByUuid($uuid);
6692 eleazar 535
 
4579 eleazar 536
        if (!$survey) {
537
            $data = [
538
                'success' => false,
539
                'data' => 'ERROR_RECORD_NOT_FOUND'
540
            ];
541
 
542
            return new JsonModel($data);
543
        }
544
 
545
        if ($survey->company_id != $currentCompany->id) {
546
            return new JsonModel([
547
                'success' => false,
548
                'data' => 'ERROR_UNAUTHORIZED'
549
            ]);
550
        }
551
 
6705 eleazar 552
 
4579 eleazar 553
        if ($request->isPost()) {
6698 eleazar 554
            $form = new SurveyForm($this->adapter, $currentCompany->id);
4579 eleazar 555
            $dataPost = $request->getPost()->toArray();
556
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
6705 eleazar 557
 
4579 eleazar 558
            $form->setData($dataPost);
559
 
560
            if ($form->isValid()) {
561
                $dataPost = (array) $form->getData();
6710 eleazar 562
 
4579 eleazar 563
                $hydrator = new ObjectPropertyHydrator();
564
                $hydrator->hydrate($dataPost, $survey);
565
 
566
                if (!$survey->status) {
567
                    $survey->status = Survey::STATUS_INACTIVE;
568
                }
4749 eleazar 569
 
570
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
571
                $surveyForm = $surveyFormMapper->fetchOneByUuid($dataPost['form_id']);
572
                $survey->form_id = $surveyForm->id;
15150 efrain 573
 
6708 eleazar 574
                $result = $surveyMapper->update($survey);
575
                if($result){
6705 eleazar 576
                    $jobDescription = new SurveyJobDescription();
577
                    $skill = new SurveySkill();
578
                    $industry = new SurveyIndustry();
579
                    $language = new SurveyLanguage();
580
 
581
                    if(!empty($dataPost['location_search'])){
582
 
583
                        $surveyLocationMapper = SurveyLocationMapper::getInstance($this->adapter);
15150 efrain 584
                        $surveyLocation = $surveyLocationMapper->fetchOneBySurveyId($survey->id);
6705 eleazar 585
 
15150 efrain 586
                        if($surveyLocation) {
587
                            $surveyLocation->formatted_address = $dataPost['formatted_address'];
588
                            $surveyLocation->city1 = $dataPost['city1'];
589
                            $surveyLocation->city2 = $dataPost['city2'];
590
                            $surveyLocation->address1 = $dataPost['address1'];
591
                            $surveyLocation->address2 = $dataPost['address2'];
592
                            $surveyLocation->country = $dataPost['country'];
593
                            $surveyLocation->latitude = $dataPost['latitude'];
594
                            $surveyLocation->longitude = $dataPost['longitude'];
595
                            $surveyLocation->postal_code = $dataPost['postal_code'];
596
                            $surveyLocation->state = $dataPost['state'];
597
                            $surveyLocationMapper->update($surveyLocation);
598
                        } else {
599
                            $surveyLocation = new SurveyLocation();
600
                            $surveyLocation->formatted_address = $dataPost['formatted_address'];
601
                            $surveyLocation->city1 = $dataPost['city1'];
602
                            $surveyLocation->city2 = $dataPost['city2'];
603
                            $surveyLocation->address1 = $dataPost['address1'];
604
                            $surveyLocation->address2 = $dataPost['address2'];
605
                            $surveyLocation->country = $dataPost['country'];
606
                            $surveyLocation->latitude = $dataPost['latitude'];
607
                            $surveyLocation->longitude = $dataPost['longitude'];
608
                            $surveyLocation->postal_code = $dataPost['postal_code'];
609
                            $surveyLocation->state = $dataPost['state'];
610
                            $surveyLocation->survey_id = $survey->id;
611
                            $surveyLocationMapper->insert($surveyLocation);
6705 eleazar 612
                        }
613
                    }
614
 
615
                    if(!empty($dataPost['job_description_id'])){
616
                        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
617
                        $surveyJobDescriptionMapper = SurveyJobDescriptionMapper::getInstance($this->adapter);
618
 
15150 efrain 619
                        $surveyJobDescriptionMapper->deleteBySurveyId($survey->id);
6705 eleazar 620
 
621
                        foreach($dataPost['job_description_id'] as $jobDescriptionUuid) {
622
 
623
                            $jobDescription = $jobDescriptionMapper->fetchOneByUuid($jobDescriptionUuid);
624
                            if($jobDescription && $jobDescription->company_id == $currentCompany->id) {
625
                                $record = new SurveyJobDescription();
626
                                $record->job_description_id = $jobDescription->id;
627
                                $record->survey_id = $survey->id;
628
 
15150 efrain 629
                                $surveyJobDescriptionMapper->insert($record);
6705 eleazar 630
                            }
631
                        }
632
 
633
                    }
634
 
635
 
636
                    if(!empty($dataPost['skill_id'])){
637
                         $skillMapper = SkillMapper::getInstance($this->adapter);
638
                         $surveySkillMapper = SurveySkillMapper::getInstance($this->adapter);
639
 
6762 eleazar 640
 
15150 efrain 641
                         $surveySkillMapper->deleteBySurveyId($survey->id);
6762 eleazar 642
 
6705 eleazar 643
 
644
                         foreach($dataPost['skill_id'] as $skillUuid) {
645
                             $skill = $skillMapper->fetchOneByUuid($skillUuid);
15150 efrain 646
                             if($skill) {
6705 eleazar 647
                                $record = new SurveySkill();
648
                                $record->skill_id = $skill->id;
649
                                $record->survey_id = $survey->id;
650
 
15150 efrain 651
                                $surveySkillMapper->insert($record);
652
                            }
6705 eleazar 653
                         }
6759 eleazar 654
                    }
6705 eleazar 655
 
656
                    if(!empty($dataPost['industry_id'])){
657
                         $industryMapper = IndustryMapper::getInstance($this->adapter);
658
                         $surveyIndustryMapper = SurveyIndustryMapper::getInstance($this->adapter);
659
 
15150 efrain 660
                         $surveyIndustryMapper->deleteBySurveyId($survey->id);
6705 eleazar 661
 
662
                         foreach($dataPost['industry_id'] as $industryUuid) {
663
                             $industry = $industryMapper->fetchOneByUuid($industryUuid);
664
 
15150 efrain 665
                             if($industry) {
6705 eleazar 666
                                 $record = new SurveyIndustry();
667
                                 $record->industry_id = $industry->id;
668
                                 $record->survey_id = $survey->id;
669
 
15150 efrain 670
                                 $surveyIndustryMapper->insert($record);
671
 
672
                             }
6705 eleazar 673
                         }
6712 eleazar 674
                    }
6705 eleazar 675
 
15150 efrain 676
 
6705 eleazar 677
                    if(!empty($dataPost['language_id'])){
678
 
679
                        $surveyLanguageMapper = SurveyLanguageMapper::getInstance($this->adapter);
15150 efrain 680
                        $surveyLanguageMapper->deleteBySurveyId($survey->id);
6705 eleazar 681
 
15150 efrain 682
                        $languageMapper = LanguageMapper::getInstance($this->adapter);
683
 
684
                        foreach($dataPost['language_id'] as $language_id)
685
                        {
686
                            $language = $languageMapper->fetchOne($language_id);
687
                            if($language) {
6705 eleazar 688
                                $record = new SurveyLanguage();
689
                                $record->language_id = $language_id;
690
                                $record->survey_id = $survey->id;
691
 
692
 
15150 efrain 693
                                $surveyLanguageMapper->insert($record);
694
                            }
6705 eleazar 695
                        }
696
                     }
697
                }
15150 efrain 698
 
6705 eleazar 699
 
4579 eleazar 700
                if ($result) {
701
                    $this->logger->info('Se edito la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
702
                    $data = [
703
                        'success' => true,
704
                        'data' => 'LABEL_RECORD_UPDATED'
705
                    ];
706
                } else {
707
                    $data = [
708
                        'success' => false,
709
                        'data' => $surveyMapper->getError()
710
                    ];
711
                }
712
 
713
                return new JsonModel($data);
714
            } else {
715
                $messages = [];
716
                $form_messages = (array) $form->getMessages();
717
                foreach ($form_messages as $fieldname => $field_messages) {
718
                    $messages[$fieldname] = array_values($field_messages);
719
                }
720
 
721
                return new JsonModel([
722
                    'success' => false,
723
                    'data' => $messages
724
                ]);
725
            }
726
        } else if ($request->isGet()) {
727
            $hydrator = new ObjectPropertyHydrator();
6720 eleazar 728
 
729
            $surveylenguageMapper = SurveyLanguageMapper::getInstance($this->adapter);
730
            $languages  = $surveylenguageMapper->fetchAllBySurveyId($survey->id);
6721 eleazar 731
 
6714 eleazar 732
            $lenguage = [];
733
            foreach($languages as $language)
734
            {
735
                array_push($lenguage, $language->language_id);
736
            }
6735 eleazar 737
 
738
            $surveySkillMapper = SurveySkillMapper::getInstance($this->adapter);
739
            $skills  = $surveySkillMapper->fetchAllBySurveyId($survey->id);
740
 
741
            $skilla = [];
742
            foreach($skills as $skill)
743
            {
6749 eleazar 744
                array_push($skilla, $skill->skill_id);
6735 eleazar 745
            }
6742 eleazar 746
 
6738 eleazar 747
 
748
            $skillMaster = [];
6744 eleazar 749
            foreach($skilla as $skillb)
6738 eleazar 750
            {
6746 eleazar 751
                $skillMapper = SkillMapper::getInstance($this->adapter);
752
                $skillm = $skillMapper->fetchOne($skillb);
6738 eleazar 753
                array_push($skillMaster, $skillm->uuid);
754
            }
6751 eleazar 755
 
756
            $SurveyJobDescriptionMapper = SurveyJobDescriptionMapper::getInstance($this->adapter);
757
            $SurveyJobDescriptions  = $SurveyJobDescriptionMapper->fetchAllBySurveyId($survey->id);
758
 
759
            $SurveyJobDescriptionA = [];
760
            foreach($SurveyJobDescriptions as $SurveyJobDescription)
761
            {
762
                array_push($SurveyJobDescriptionA, $SurveyJobDescription->job_description_id);
763
            }
6765 eleazar 764
 
6751 eleazar 765
            $SurveyJobDescriptionMaster = [];
6752 eleazar 766
            foreach($SurveyJobDescriptionA as $SurveyJobDescriptionB)
6751 eleazar 767
            {
6752 eleazar 768
                $JobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
6754 eleazar 769
                $JobDescriptionm = $JobDescriptionMapper->fetchOne($SurveyJobDescriptionB);
6758 eleazar 770
                array_push($SurveyJobDescriptionMaster, $JobDescriptionm->uuid);
6751 eleazar 771
            }
772
 
6765 eleazar 773
            $surveyIndustryMapper = SurveyIndustryMapper::getInstance($this->adapter);
774
            $industrys = $surveyIndustryMapper->fetchAllBySurveyId($survey->id);
775
 
776
            $industrya = [];
777
            foreach($industrys as $industry)
778
            {
779
                array_push($industrya, $industry->industry_id);
780
            }
781
 
782
 
783
            $industryMaster = [];
784
            foreach($industrya as $industryb)
785
            {
786
                $industryMapper = IndustryMapper::getInstance($this->adapter);
787
                $industrym = $industryMapper->fetchOne($industryb);
788
                array_push($industryMaster, $industrym->uuid);
789
            }
790
 
791
 
6767 eleazar 792
            $surveyLocationMapper = SurveyLocationMapper::getInstance($this->adapter);
15150 efrain 793
            $surveyLocation  = $surveyLocationMapper->fetchOneBySurveyId($survey->id);
6765 eleazar 794
 
795
 
6742 eleazar 796
 
4749 eleazar 797
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
798
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
15150 efrain 799
 
4579 eleazar 800
            $data = [
801
                'success' => true,
802
                'data' => [
4653 eleazar 803
                    'name' => $survey->name,
4749 eleazar 804
                    'form_id' => $surveyForm->uuid,
4653 eleazar 805
                    'target' => $survey->target,
806
                    'identity' => $survey->identity,
807
                    'since_date' => $survey->since_date,
808
                    'last_date' => $survey->last_date,
809
                    'status' => $survey->status,
6714 eleazar 810
                    'lenguage_id' => $lenguage,
6738 eleazar 811
                    'skill_id' => $skillMaster,
6752 eleazar 812
                    'job_description_id' => $SurveyJobDescriptionMaster,
6765 eleazar 813
                    'industry_id' => $industryMaster,
15150 efrain 814
 
815
 
816
                    'formatted_address' => $surveyLocation  ? $surveyLocation->formatted_address : '',
817
                    'city1' => $surveyLocation  ? $surveyLocation ->city1 : '',
818
                    'city2' => $surveyLocation  ? $surveyLocation ->city2 : '',
819
                    'address1' => $surveyLocation  ? $surveyLocation ->address1: '',
820
                    'address2' => $surveyLocation  ? $surveyLocation ->address2 : '',
821
                    'country' => $surveyLocation  ? $surveyLocation ->country : '',
822
                    'latitude' => $surveyLocation  ? $surveyLocation ->latitude : '',
823
                    'longitude' => $surveyLocation  ? $surveyLocation ->longitude : '',
824
                    'postal_code' => $surveyLocation  ? $surveyLocation ->postal_code : '',
825
                    'state' => $surveyLocation  ? $surveyLocation ->state : '',
826
 
827
 
4579 eleazar 828
                ]
829
            ];
830
 
831
            return new JsonModel($data);
832
        } else {
833
            $data = [
834
                'success' => false,
835
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
836
            ];
837
 
838
            return new JsonModel($data);
839
        }
840
 
841
        return new JsonModel($data);
842
    }
843
 
844
    public function deleteAction() {
845
        $request = $this->getRequest();
846
        $currentUserPlugin = $this->plugin('currentUserPlugin');
847
        $currentCompany = $currentUserPlugin->getCompany();
848
        $currentUser = $currentUserPlugin->getUser();
849
 
850
        $request = $this->getRequest();
851
        $uuid = $this->params()->fromRoute('id');
852
 
853
        if (!$uuid) {
854
            $data = [
855
                'success' => false,
856
                'data' => 'ERROR_INVALID_PARAMETER'
857
            ];
858
 
859
            return new JsonModel($data);
860
        }
861
 
862
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
863
        $survey = $surveyMapper->fetchOneByUuid($uuid);
864
        if (!$survey) {
865
            $data = [
866
                'success' => false,
867
                'data' => 'ERROR_RECORD_NOT_FOUND'
868
            ];
869
 
870
            return new JsonModel($data);
871
        }
872
 
873
        if ($survey->company_id != $currentCompany->id) {
874
            return new JsonModel([
875
                'success' => false,
876
                'data' => 'ERROR_UNAUTHORIZED'
877
            ]);
878
        }
879
 
880
        if ($request->isPost()) {
881
 
882
            $result = $surveyMapper->delete($survey->id);
883
            if ($result) {
15150 efrain 884
                $this->logger->info('Se borro la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
4579 eleazar 885
 
886
                $data = [
887
                    'success' => true,
888
                    'data' => 'LABEL_RECORD_DELETED'
889
                ];
890
            } else {
891
 
892
                $data = [
893
                    'success' => false,
894
                    'data' => $surveyMapper->getError()
895
                ];
896
 
897
                return new JsonModel($data);
898
            }
899
        } else {
900
            $data = [
901
                'success' => false,
902
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
903
            ];
904
 
905
            return new JsonModel($data);
906
        }
907
 
908
        return new JsonModel($data);
909
    }
910
 
4384 eleazar 911
}