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