Proyectos de Subversion LeadersLinked - Backend

Rev

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