Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
/**
3
 *
4
 * Controlador: Mis Perfiles
5
 *
6
 */
7
declare(strict_types=1);
8
 
9
namespace LeadersLinked\Controller;
10
 
11
use Laminas\Db\Adapter\AdapterInterface;
12
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
13
use Laminas\Mvc\Controller\AbstractActionController;
14
use Laminas\Log\LoggerInterface;
15
use Laminas\View\Model\ViewModel;
16
use Laminas\View\Model\JsonModel;
17
 
18
use LeadersLinked\Library\Functions;
19
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
20
use LeadersLinked\Library\Image;
21
 
22
use LeadersLinked\Mapper\CompanyLocationMapper;
23
use LeadersLinked\Form\CompanyProfileSocialNetworkForm;
24
use LeadersLinked\Form\CompanyProfileLocationForm;
25
use LeadersLinked\Form\CompanyProfileExtendedForm;
26
use LeadersLinked\Form\CompanyProfileImageForm;
27
use LeadersLinked\Form\CompanyProfileCoverForm;
768 geraldo 28
use LeadersLinked\Form\CompanyProfileHeaderForm;
29
use LeadersLinked\Form\CompanyProfileFooterForm;
1 www 30
 
31
use LeadersLinked\Form\CompanyProfileIndustryForm;
32
use LeadersLinked\Form\CompanyProfileCompanySizeForm;
33
use LeadersLinked\Form\CompanyProfileFoundationYearForm;
34
use LeadersLinked\Form\CompanyProfileWebsiteForm;
35
use LeadersLinked\Model\Location;
36
use LeadersLinked\Model\CompanyLocation;
37
use LeadersLinked\Model\CompanyUser;
38
 
39
use LeadersLinked\Mapper\LocationMapper;
40
use LeadersLinked\Mapper\CompanyFollowerMapper;
41
use LeadersLinked\Mapper\CompanyUserMapper;
42
use LeadersLinked\Mapper\CompanyMapper;
43
use LeadersLinked\Mapper\IndustryMapper;
44
use LeadersLinked\Mapper\CompanySizeMapper;
45
 
46
class ProfileController extends AbstractActionController
47
{
48
    /**
49
     *
50
     * @var AdapterInterface
51
     */
52
    private $adapter;
53
 
54
 
55
    /**
56
     *
57
     * @var AbstractAdapter
58
     */
59
    private $cache;
60
 
61
    /**
62
     *
63
     * @var  LoggerInterface
64
     */
65
    private $logger;
66
 
67
 
68
    /**
69
     *
70
     * @var array
71
     */
72
    private $config;
73
 
74
    /**
75
     *
76
     * @param AdapterInterface $adapter
77
     * @param AbstractAdapter $cache
78
     * @param LoggerInterface $logger
79
     * @param array $config
80
     */
81
    public function __construct($adapter, $cache , $logger,  $config)
82
    {
83
        $this->adapter      = $adapter;
84
        $this->cache        = $cache;
85
        $this->logger       = $logger;
86
        $this->config       = $config;
87
 
88
    }
89
 
90
 
91
 
92
    /**
93
     * Presenta el perfil con las opciónes de edición de cada sección
94
     * @return \Laminas\Http\Response|\Laminas\View\Model\ViewModel|\Laminas\View\Model\JsonModel
95
     */
96
    public function indexAction()
97
    {
98
        $currentUserPlugin = $this->plugin('currentUserPlugin');
99
        $currentUser = $currentUserPlugin->getUser();
100
        $currentCompany = $currentUserPlugin->getCompany();
101
 
102
 
103
        $request = $this->getRequest();
104
        if($request->isGet()) {
105
 
106
            $sandbox = $this->config['leaderslinked.runmode.sandbox'];
107
            if($sandbox) {
108
                $google_map_key  = $this->config['leaderslinked.google_map.sandbox_api_key'];
109
            } else {
110
                $google_map_key  = $this->config['leaderslinked.google_map.production_api_key'];
111
            }
112
 
113
            $companyMapper = CompanyMapper::getInstance($this->adapter);
114
            $company = $companyMapper->fetchOne($currentCompany->id);
115
 
116
 
117
            $companyLocationMapper = CompanyLocationMapper::getInstance($this->adapter);
118
            $records = $companyLocationMapper->fetchAllLocationByCompanyId($company->id);
119
 
120
 
121
 
122
 
123
            $locations = [];
124
            foreach($records as $record)
125
            {
126
                $location =  [
127
                    'formatted_address'  => $record['formatted_address'],
128
                    'country' => $record['country'],
129
                    'is_main' => $record['is_main'],
130
                    'link_edit' => $this->url()->fromRoute('profile/location', [ 'operation' => 'edit', 'id' =>  $record['company_location_uuid'] ]),
131
                    'link_delete' => $this->url()->fromRoute('profile/location', ['operation' => 'delete', 'id' => $record['company_location_uuid'] ])
132
 
133
                ];
134
 
135
                array_push($locations, $location);
136
            }
137
 
138
            $industryMapper = IndustryMapper::getInstance($this->adapter);
139
            $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
140
 
141
            $formSocialNetwork = new CompanyProfileSocialNetworkForm();
142
            $formLocation = new CompanyProfileLocationForm();
143
            $formExtended = new CompanyProfileExtendedForm();
144
            $formFoundationYear = new CompanyProfileFoundationYearForm();
145
            $formWebsite = new CompanyProfileWebsiteForm();
146
            $formImage = new CompanyProfileImageForm($this->config);
147
            $formCover = new CompanyProfileCoverForm($this->config);
768 geraldo 148
            $formHeader = new CompanyProfileHeaderForm($this->config);
149
            $formFooter = new CompanyProfileFooterForm($this->config);
150
 
151
 
152
 
1 www 153
            $formCompanySize = new CompanyProfileCompanySizeForm($this->adapter);
154
            $formIndustry = new CompanyProfileIndustryForm($this->adapter);
155
 
156
            $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
157
            $follower = $companyFollowerMapper->getCountFollowers($currentCompany->id);
158
 
159
            $image_size_cover = $this->config['leaderslinked.image_sizes.company_cover_upload'];
160
            $image_size_profile = $this->config['leaderslinked.image_sizes.company_image_upload'];
1101 geraldo 161
            $image_size_header = $this->config['leaderslinked.image_sizes.company_header_pdf_upload'];
162
            $image_size_footer= $this->config['leaderslinked.image_sizes.company_footer_pdf_upload'];
1 www 163
 
164
 
165
            $industry = $industryMapper->fetchOne($company->industry_id);
166
            $companySize = $companySizeMapper->fetchOne($company->company_size_id);
167
 
168
 
169
            $this->layout()->setTemplate('layout/layout-backend');
170
            $viewModel = new ViewModel();
171
            $viewModel->setTemplate('leaders-linked/profile/index.phtml');
172
            $viewModel->setVariables([
173
                'google_map_key'        => $google_map_key,
174
                'follower'              => $follower,
175
                'company_id'            => $currentCompany->id,
176
                'company_name'          => $company->name,
177
                'company_uuid'          => $company->uuid,
178
                'name'                  => trim($company->name),
179
                'image'                 => $company->image,
180
                'cover'                 => $company->cover,
782 geraldo 181
                'header'                 => $company->header,
182
                'footer'                 => $company->footer,
1 www 183
                'overview'              => $company->description,
184
                'website'               => $company->website,
185
                'foundation_year'       => $company->foundation_year,
186
                'facebook'              => $company->facebook,
187
                'instagram'             => $company->instagram,
188
                'twitter'               => $company->twitter,
189
                'locations'             => $locations,
190
                'industry'              => $industry->name,
191
                'company_size'          => $companySize->name . ' ('.$companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee .')',
192
                'formLocation'          => $formLocation,
193
                'formSocialNetwork'     => $formSocialNetwork,
194
                'formExtended'          => $formExtended,
195
                'formImage'             => $formImage,
196
                'formCover'             => $formCover,
768 geraldo 197
                'formHeader'             => $formHeader,
198
                'formFooter'             => $formFooter,
1 www 199
                'formFoundationYear'    => $formFoundationYear,
200
                'formWebsite'           => $formWebsite,
201
                'formIndustry'          => $formIndustry,
202
                'formCompanySize'       => $formCompanySize,
203
                'image_size_cover'      => $image_size_cover,
204
                'image_size_profile'    => $image_size_profile,
781 geraldo 205
                'image_size_header'    => $image_size_header,
206
                'image_size_footer'    => $image_size_footer,
1 www 207
            ]);
208
            return $viewModel ;
209
 
210
        } else {
211
            $data = [
212
                'success' => false,
213
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
214
            ];
215
 
216
            return new JsonModel($data);
217
        }
218
 
219
        return new JsonModel($data);
220
    }
221
 
222
 
223
    /**
224
     *
225
     * Borrar un perfil excepto el público
226
     * @return \Laminas\View\Model\JsonModel
227
     */
228
    public function deleteAction()
229
    {
230
 
231
        $currentUserPlugin = $this->plugin('currentUserPlugin');
232
        $currentUser = $currentUserPlugin->getUser();
233
        $currentCompany = $currentUserPlugin->getCompany();
234
 
235
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
236
        $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $currentUser->id);
237
 
238
        if(!$companyUser || $companyUser->type != CompanyUser::CREATOR_YES) {
239
            $response = [
240
                'success' => false,
241
                'data' => 'ERROR_UNAUTHORIZED'
242
            ];
243
 
244
            return new JsonModel($response);
245
        }
246
 
247
 
248
        if($request->isPost()) {
249
            $companyMapper = CompanyMapper::getInstance($this->adapter);
250
            $result = $companyMapper->delete($companyUser->id);
251
            if($result) {
252
                $this->logger->info('Se borro la empresa : ' .  $currentCompany->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
253
 
254
                $data = [
255
                    'success' => true,
256
                    'data' => 'LABEL_RECORD_DELETED'
257
                ];
258
            } else {
259
 
260
                $data = [
261
                    'success'   => false,
262
                    'data'      => $companyUserMapper->getError()
263
                ];
264
 
265
                return new JsonModel($data);
266
            }
267
 
268
        } else {
269
            $data = [
270
                'success' => false,
271
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
272
            ];
273
 
274
            return new JsonModel($data);
275
        }
276
 
277
        return new JsonModel($data);
278
    }
279
 
280
 
281
 
282
    /**
283
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
284
     * @return \Laminas\View\Model\JsonModel
285
     */
286
    public function extendedAction()
287
    {
288
 
289
 
290
        $currentUserPlugin = $this->plugin('currentUserPlugin');
291
        $currentUser = $currentUserPlugin->getUser();
292
        $currentCompany = $currentUserPlugin->getCompany();
293
 
294
 
295
 
296
 
297
 
298
        $request = $this->getRequest();
299
        if($request->isGet()) {
300
            $companyMapper = CompanyMapper::getInstance($this->adapter);
301
            $company = $companyMapper->fetchOne($currentCompany->id);
302
 
303
            $data = [
304
                'success' => true,
305
                'data' => [
306
                    'description' => $company->description,
307
                ]
308
            ];
309
 
310
            return new JsonModel($data);
311
 
312
 
313
        } else if($request->isPost()) {
314
            $form = new CompanyProfileExtendedForm();
315
            $dataPost = $request->getPost()->toArray();
316
 
317
            $form->setData($dataPost);
318
 
319
            if($form->isValid()) {
320
                $companyMapper = CompanyMapper::getInstance($this->adapter);
321
                $company = $companyMapper->fetchOne($currentCompany->id);
322
 
323
                $dataPost = (array) $form->getData();
324
 
325
                $hydrator = new ObjectPropertyHydrator();
326
                $hydrator->hydrate($dataPost, $company);
327
 
328
                $companyMapper->updateExtended($company);
329
 
330
                $this->logger->info('Se actualizo las descripción de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
331
 
332
                return new JsonModel([
333
                    'success'   => true,
334
                    'data' => [
335
                        'description' => $company->description,
336
                    ]
337
                ]);
338
 
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
        }
353
 
354
 
355
        $data = [
356
            'success' => false,
357
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
358
        ];
359
 
360
 
361
        return new JsonModel($data);
362
    }
363
 
364
    /**
365
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
366
     * @return \Laminas\View\Model\JsonModel
367
     */
368
    public function websiteAction()
369
    {
370
        $currentUserPlugin = $this->plugin('currentUserPlugin');
371
        $currentUser = $currentUserPlugin->getUser();
372
        $currentCompany = $currentUserPlugin->getCompany();
373
 
374
        $request = $this->getRequest();
375
        if($request->isGet()) {
376
            $companyMapper = CompanyMapper::getInstance($this->adapter);
377
            $company = $companyMapper->fetchOne($currentCompany->id);
378
 
379
            $data = [
380
                'success' => true,
381
                'data' => [
382
                    'website' => $company->website,
383
                ]
384
            ];
385
 
386
            return new JsonModel($data);
387
 
388
 
389
        } else if($request->isPost()) {
390
 
391
 
392
            $form = new CompanyProfileWebsiteForm();
393
            $dataPost = $request->getPost()->toArray();
394
 
395
            $form->setData($dataPost);
396
 
397
            if($form->isValid()) {
398
                $companyMapper = CompanyMapper::getInstance($this->adapter);
399
                $company = $companyMapper->fetchOne($currentCompany->id);
400
 
401
                $dataPost = (array) $form->getData();
402
 
403
                $hydrator = new ObjectPropertyHydrator();
404
                $hydrator->hydrate($dataPost, $company);
405
 
406
                $companyMapper->updateWebsite($company);
407
 
408
                $this->logger->info('Se actualizo el website de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
409
 
410
                return new JsonModel([
411
                    'success'   => true,
412
                    'data' => [
413
                        'website' => $company->website,
414
                    ]
415
                ]);
416
 
417
            } else {
418
                $messages = [];
419
                $form_messages = (array) $form->getMessages();
420
                foreach($form_messages  as $fieldname => $field_messages)
421
                {
422
                    $messages[$fieldname] = array_values($field_messages);
423
                }
424
 
425
                return new JsonModel([
426
                    'success'   => false,
427
                    'data'   => $messages
428
                ]);
429
            }
430
        }
431
 
432
 
433
        $data = [
434
            'success' => false,
435
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
436
        ];
437
 
438
 
439
        return new JsonModel($data);
440
    }
441
 
442
    /**
443
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
444
     * @return \Laminas\View\Model\JsonModel
445
     */
446
    public function foundationYearAction()
447
    {
448
 
449
        $currentUserPlugin = $this->plugin('currentUserPlugin');
450
        $currentUser = $currentUserPlugin->getUser();
451
        $currentCompany = $currentUserPlugin->getCompany();
452
 
453
 
454
 
455
        $request = $this->getRequest();
456
        if($request->isGet()) {
457
            $companyMapper = CompanyMapper::getInstance($this->adapter);
458
            $company = $companyMapper->fetchOne($currentCompany->id);
459
 
460
            $data = [
461
                'success' => true,
462
                'data' => [
463
                    'foundation_year' => $company->foundation_year,
464
                ]
465
 
466
            ];
467
 
468
            return new JsonModel($data);
469
 
470
 
471
        } else if($request->isPost()) {
472
 
473
 
474
            $form = new CompanyProfileFoundationYearForm();
475
            $dataPost = $request->getPost()->toArray();
476
 
477
            $form->setData($dataPost);
478
 
479
            if($form->isValid()) {
480
                $companyMapper = CompanyMapper::getInstance($this->adapter);
481
                $company = $companyMapper->fetchOne($currentCompany->id);
482
 
483
                $dataPost = (array) $form->getData();
484
 
485
                $hydrator = new ObjectPropertyHydrator();
486
                $hydrator->hydrate($dataPost, $company);
487
 
488
                $companyMapper->updateFoundationYear($company);
489
 
490
                $this->logger->info('Se actualizo el año de fundación de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
491
 
492
                return new JsonModel([
493
                    'success'   => true,
494
                    'data' => $company->foundation_year,
495
                ]);
496
 
497
            } else {
498
                $messages = [];
499
                $form_messages = (array) $form->getMessages();
500
                foreach($form_messages  as $fieldname => $field_messages)
501
                {
502
                    $messages[$fieldname] = array_values($field_messages);
503
                }
504
 
505
                return new JsonModel([
506
                    'success'   => false,
507
                    'data'   => $messages
508
                ]);
509
            }
510
        }
511
 
512
 
513
        $data = [
514
            'success' => false,
515
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
516
        ];
517
 
518
 
519
        return new JsonModel($data);
520
    }
521
 
522
 
523
    /**
524
     * Actualización de la ubucación
525
     * @return \Laminas\View\Model\JsonModel
526
     */
527
    public function locationAction()
528
    {
529
        $currentUserPlugin = $this->plugin('currentUserPlugin');
530
        $currentUser = $currentUserPlugin->getUser();
531
        $currentCompany = $currentUserPlugin->getCompany();
532
 
533
        $operation = $this->params()->fromRoute('operation');
534
        if($operation == 'edit' || $operation == 'delete') {
535
            $id = $this->params()->fromRoute('id');
4093 stevensc 536
            if(!$id) {
1 www 537
                $response = [
538
                    'success' => false,
539
                    'data' => 'ERROR_INVALID_PARAMETER'
540
                ];
541
 
542
                return new JsonModel($response);
543
            }
544
        } else {
545
            $id = '';
546
        }
547
 
548
        $request = $this->getRequest();
549
        if($request->isPost()) {
550
            $companyMapper = CompanyMapper::getInstance($this->adapter);
551
            $company = $companyMapper->fetchOne($currentCompany->id);
552
 
553
            $companyLocationMapper = CompanyLocationMapper::getInstance($this->adapter);
554
 
555
 
556
            $companyLocation = null;
557
            if($id) {
558
                $companyLocation = $companyLocationMapper->fetchOneByUuid($id);
559
                if(!$companyLocation) {
560
                    $response = [
561
                        'success' => false,
562
                        'data' => 'ERROR_RECORD_NOT_FOUND'
563
                    ];
564
 
565
                    return new JsonModel($response);
566
                }
567
 
568
                if($companyLocation->company_id != $company->id) {
569
                    $response = [
570
                        'success' => false,
571
                        'data' => 'ERROR_UNAUTHORIZED'
572
                    ];
573
                    return new JsonModel($response);
574
                }
575
            }
576
 
577
            $locationMapper = LocationMapper::getInstance($this->adapter);
578
            $response = [];
579
 
580
 
581
 
582
            if($operation == 'delete' && $companyLocation ) {
583
 
584
                if($companyLocationMapper->delete($companyLocation->id)) {
585
                    $this->logger->info('Se borrar una ubicación de la empresa: ' . $company->name, ['user_id' => $currentUser->id,  'ip' => Functions::getUserIP()]);
586
                    $locationMapper->delete($companyLocation->location_id);
587
 
588
                } else {
589
                    $response = [
590
                        'success'   => false,
591
                        'data' => 'ERROR_THERE_WAS_AN_ERROR'
592
                    ];
593
                }
594
 
595
 
596
            } else {
597
                $form = new CompanyProfileLocationForm();
598
                $dataPost = $request->getPost()->toArray();
599
 
600
 
601
                $dataPost['is_main'] = isset($dataPost['is_main']) ? $dataPost['is_main'] : CompanyLocation::IS_MAIN_NO;
602
 
603
 
604
                $form->setData($dataPost);
605
 
606
                if($form->isValid()) {
607
 
608
                    if($operation == 'edit') {
609
                        $location = $locationMapper->fetchOne($companyLocation->location_id);
610
                    } else {
611
                        $location = new Location();
612
                    }
613
 
614
                    $dataPost = (array) $form->getData();
615
                    $hydrator = new ObjectPropertyHydrator();
616
                    $hydrator->hydrate($dataPost, $location);
617
 
618
                    if($operation == 'edit') {
619
 
620
 
621
                        $companyLocation->is_main = $dataPost['is_main'];
622
                        $companyLocationMapper->update($companyLocation);
623
 
624
 
625
                        $location = $locationMapper->update($location);
626
                        $this->logger->info('Se actualizo una ubicación de la empresa: '. $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
627
 
628
 
629
                    } else {
630
 
631
                        $result = $locationMapper->insert($location);
632
 
633
                        if($result) {
634
                            $companyLocation = new CompanyLocation();
635
                            $companyLocation->company_id = $currentCompany->id;
636
                            $companyLocation->location_id = $location->id;
637
                            $companyLocation->is_main = $dataPost['is_main'];
638
 
639
                            $companyLocationMapper->insert($companyLocation);
640
                            $this->logger->info('Se agrego una ubicación a la empresa: '. $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
641
                        } else {
642
                            $response = [
643
                                'success'   => false,
644
                                'data' => 'ERROR_THERE_WAS_AN_ERROR'
645
                            ];
646
                        }
647
                    }
648
                } else {
649
                    return new JsonModel([
650
                        'success'   => false,
651
                        'data'   =>   'ERROR_PLACED_AUTOCOMPLETE_DOES_NOT_CONTAIN_GEOMETRY'
652
                    ]);
653
                }
654
            }
655
 
656
            if(!$response) {
657
                $records = $companyLocationMapper->fetchAllLocationByCompanyId($currentCompany->id);
658
 
659
 
660
                $locations = [];
661
                foreach($records as $record)
662
                {
663
                    $location =  [
664
                        'formatted_address'  => $record['formatted_address'],
665
                        'country' => $record['country'],
666
                        'is_main' => $record['is_main'],
4278 efrain 667
                        'link_edit' => $this->url()->fromRoute('profile/location', ['id' => $record['company_location_uuid'], 'operation' => 'edit' ]),
668
                        'link_delete' => $this->url()->fromRoute('profile/location', ['id' => $record['company_location_uuid'], 'operation' => 'delete'])
1 www 669
 
670
                    ];
671
 
672
                    array_push($locations, $location);
673
                }
674
 
675
                $response = [
676
                    'success' => true,
677
                    'data' => $locations
678
                ];
679
 
680
 
681
            }
682
 
683
            return new JsonModel($response);
684
 
685
        } else if($request->isGet() && $operation == 'edit') {
686
 
687
 
688
 
689
            $companyLocationMapper = CompanyLocationMapper::getInstance($this->adapter);
690
            $companyLocation = $companyLocationMapper->fetchOneByUuid($id);
691
 
692
 
693
 
694
            if(!$companyLocation) {
695
                $response = [
696
                    'success' => false,
697
                    'data' => 'ERROR_RECORD_NOT_FOUND'
698
                ];
699
 
700
                return new JsonModel($response);
701
            }
702
 
703
            if($companyLocation->company_id != $currentCompany->id) {
704
                $response = [
705
                    'success' => false,
706
                    'data' => 'ERROR_UNAUTHORIZED'
707
                ];
708
                return new JsonModel($response);
709
            }
710
 
711
            $locationMapper = LocationMapper::getInstance($this->adapter);
712
            $location = $locationMapper->fetchOne($companyLocation->location_id);
713
 
714
            $hydrator = new ObjectPropertyHydrator();
715
            $data = $hydrator->extract($location);
716
 
717
            $data['is_main'] = $companyLocation->is_main;
718
 
719
            $response = [
720
                'success' => true,
721
                'data' =>  $data
722
            ];
723
 
724
            return new JsonModel($response);
725
 
726
        }
727
 
728
        $response = [
729
            'success' => false,
730
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
731
        ];
732
 
733
 
734
        return new JsonModel($response);
735
    }
736
 
737
    /**
738
     * Actualización de las redes sociales
739
     * @return \Laminas\View\Model\JsonModel
740
     */
741
    public function socialNetworkAction()
742
    {
743
 
744
 
745
        $currentUserPlugin = $this->plugin('currentUserPlugin');
746
        $currentUser = $currentUserPlugin->getUser();
747
        $currentCompany = $currentUserPlugin->getCompany();
748
 
749
 
750
 
751
        $request = $this->getRequest();
752
        if($request->isGet()) {
753
            $companyMapper = CompanyMapper::getInstance($this->adapter);
754
            $company = $companyMapper->fetchOne($currentCompany->id);
755
 
756
            $data = [
757
                'success' => true,
758
                'data' => [
759
                    'facebook' => $company->facebook,
760
                    'instagram' => $company->instagram,
761
                    'twitter' => $company->twitter
762
                ]
763
            ];
764
 
765
            return new JsonModel($data);
766
 
767
 
768
        } else if($request->isPost()) {
769
 
770
            $form = new CompanyProfileSocialNetworkForm();
771
            $dataPost = $request->getPost()->toArray();
772
 
773
            $form->setData($dataPost);
774
 
775
            if($form->isValid()) {
776
                $companyMapper = CompanyMapper::getInstance($this->adapter);
777
                $company = $companyMapper->fetchOne($currentCompany->id);
778
 
779
                $this->logger->info('Se actualizaron las redes sociales de la empresa: '. $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
780
 
781
                $dataPost = (array) $form->getData();
782
 
783
                $hydrator = new ObjectPropertyHydrator();
784
                $hydrator->hydrate($dataPost, $company);
785
 
786
                $companyMapper->updateSocialNetwork($company);
787
                return new JsonModel([
788
                    'success'   => true,
789
                    'data' => [
790
                        'facebook' => $company->facebook,
791
                        'instagram' => $company->instagram,
792
                        'twitter' => $company->twitter
793
                    ]
794
                ]);
795
 
796
            } else {
797
                $messages = [];
798
                $form_messages = (array) $form->getMessages();
799
                foreach($form_messages  as $fieldname => $field_messages)
800
                {
801
                    $messages[$fieldname] = array_values($field_messages);
802
                }
803
 
804
                return new JsonModel([
805
                    'success'   => false,
806
                    'data'   => $messages
807
                ]);
808
            }
809
        }
810
 
811
 
812
        $data = [
813
            'success' => false,
814
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
815
        ];
816
 
817
 
818
        return new JsonModel($data);
819
    }
820
 
821
 
822
 
823
    /**
824
     * Cambio de la imagen del image del perfil
825
     * @return \Laminas\View\Model\JsonModel
826
     */
827
    public function imageAction()
828
    {
829
        $currentUserPlugin = $this->plugin('currentUserPlugin');
830
        $currentUser = $currentUserPlugin->getUser();
831
        $currentCompany = $currentUserPlugin->getCompany();
832
 
833
        $operation  = $this->params()->fromRoute('operation');
834
 
835
 
836
 
837
 
838
 
839
        $request = $this->getRequest();
840
        if($request->isPost()) {
841
            $companyMapper = CompanyMapper::getInstance($this->adapter);
842
            $company = $companyMapper->fetchOne($currentCompany->id);
843
 
844
            $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
845
 
846
            if($operation == 'delete') {
847
                $this->logger->info('Se borro la imagen de la empresa: '. $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
848
 
849
                if($company->image) {
850
 
851
                    if(!Image::delete($target_path, $company->image)) {
852
                        return new JsonModel([
853
                            'success'   => false,
854
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
855
                        ]);
856
                    }
857
 
858
 
859
                }
860
 
861
                $company->image = '';
862
                if(!$companyMapper->updateImage($company)) {
863
                    return new JsonModel([
864
                        'success'   => false,
865
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
866
                    ]);
867
                }
868
 
869
            } else {
870
                $form = new CompanyProfileImageForm($this->config);
871
                $data 	= array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
872
 
873
                $form->setData($data);
874
 
875
                if($form->isValid()) {
876
 
877
                    $files = $request->getFiles()->toArray();
878
                    if(!empty($files['image']['error'])) {
879
 
880
                        return new JsonModel([
881
                            'success'   => false,
882
                            'data'   =>  'ERROR_UPLOAD_FILE'
883
                        ]);
884
 
885
 
886
                    }
887
 
888
 
889
                    if($company->image) {
890
 
891
                        if(!Image::delete($target_path, $company->image)) {
892
                            return new JsonModel([
893
                                'success'   => false,
894
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
895
                            ]);
896
                        }
897
 
898
 
899
                    }
900
 
901
                    list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.company_image_size']);
902
                    $source             = $files['image']['tmp_name'];
903
                    $target_filename    = 'company-image-' . uniqid() . '.png';
904
                    $crop_to_dimensions = true;
905
 
906
                    if(!Image::uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions)) {
907
                        return new JsonModel([
908
                            'success'   => false,
909
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
910
                        ]);
911
                    }
912
 
913
 
914
                    $company->image = $target_filename;
915
                    if(!$companyMapper->updateImage($company)) {
916
                        return new JsonModel([
917
                            'success'   => false,
918
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
919
                        ]);
920
                    }
921
 
922
                    $this->logger->info('Se actualizo la imagen de la empresa: '. $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
923
 
924
                } else {
925
                    $messages = [];
926
                    $form_messages = (array) $form->getMessages();
927
                    foreach($form_messages  as $fieldname => $field_messages)
928
                    {
929
                        $messages[$fieldname] = array_values($field_messages);
930
                    }
931
 
932
                    return new JsonModel([
933
                        'success'   => false,
934
                        'data'   => $messages
935
                    ]);
936
                }
937
            }
938
            return new JsonModel([
939
                'success'   => true,
940
                'data' => $this->url()->fromRoute('storage', ['type' => 'company', 'code' => $company->uuid, 'filename' => $company->image])
941
 
942
            ]);
943
        }
944
 
945
 
946
        $data = [
947
            'success' => false,
948
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
949
        ];
950
 
951
 
952
        return new JsonModel($data);
953
    }
954
 
955
    /**
956
     * Cambio de la imagen de fondo superior (cover) del perfil
957
     * @return \Laminas\View\Model\JsonModel
958
     */
959
    public function coverAction()
960
    {
961
        $operation          = $this->params()->fromRoute('operation');
962
 
963
 
964
        $currentUserPlugin = $this->plugin('currentUserPlugin');
965
        $currentUser = $currentUserPlugin->getUser();
966
        $currentCompany = $currentUserPlugin->getCompany();
967
 
968
 
969
 
970
 
971
        $request = $this->getRequest();
972
        if($request->isPost()) {
973
 
974
            $companyMapper = CompanyMapper::getInstance($this->adapter);
975
            $company = $companyMapper->fetchOne($currentCompany->id);
976
 
977
            $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
978
 
979
 
980
 
981
            if($operation == 'delete') {
982
                if($company->cover) {
983
 
984
                    if(!Image::delete($target_path, $company->cover)) {
985
                        return new JsonModel([
986
                            'success'   => false,
987
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
988
                        ]);
989
                    }
990
                }
991
 
992
                $this->logger->info('Se borro el cover de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
993
                $company->cover = '';
994
                if(!$companyMapper->updateCover($company)) {
995
                    return new JsonModel([
996
                        'success'   => false,
997
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
998
                    ]);
999
                }
1000
 
1001
            } else {
1002
                $form = new CompanyProfileCoverForm($this->config);
1003
                $data 	= array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1004
 
1005
                $form->setData($data);
1006
 
1007
                if($form->isValid()) {
1008
 
1009
 
1010
                    $files = $request->getFiles()->toArray();
1011
                    if(!empty($files['cover']['error'])) {
1012
 
1013
                        return new JsonModel([
1014
                            'success'   => false,
1015
                            'data'   =>  'ERROR_UPLOAD_FILE'
1016
                        ]);
1017
 
1018
 
1019
                    }
1020
 
1021
                    if($company->cover) {
1022
 
1023
                        if(!Image::delete($target_path, $company->cover)) {
1024
                            return new JsonModel([
1025
                                'success'   => false,
1026
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1027
                            ]);
1028
                        }
1029
                    }
1030
 
1031
                    list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.company_cover_size']);
1032
                    $source             = $files['cover']['tmp_name'];
1033
                    $target_filename    = 'company-cover-' . uniqid() . '.png';
1034
                    $crop_to_dimensions = false;
1035
 
1036
                    if(!Image::uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions)) {
1037
                        return new JsonModel([
1038
                            'success'   => false,
1039
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1040
                        ]);
1041
                    }
1042
 
1043
 
1044
                    $company->cover = $target_filename;
1045
                    if(!$companyMapper->updateCover($company)) {
1046
                        return new JsonModel([
1047
                            'success'   => false,
1048
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1049
                        ]);
1050
                    }
1051
 
1052
 
1053
 
1054
 
1055
                    $this->logger->info('Se actualizo el cover  de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1056
 
1057
                } else {
1058
                    $messages = [];
1059
                    $form_messages = (array) $form->getMessages();
1060
                    foreach($form_messages  as $fieldname => $field_messages)
1061
                    {
1062
                        $messages[$fieldname] = array_values($field_messages);
1063
                    }
1064
 
1065
                    return new JsonModel([
1066
                        'success'   => false,
1067
                        'data'   => $messages
1068
                    ]);
1069
                }
1070
            }
1071
            return new JsonModel([
1072
                'success'   => true,
1073
                'data' => $this->url()->fromRoute('storage', ['type' => 'company-cover', 'code' => $company->uuid, 'filename' => $company->cover])
1074
 
1075
            ]);
1076
        }
1077
 
1078
 
1079
        $data = [
1080
            'success' => false,
1081
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1082
        ];
1083
 
1084
 
1085
        return new JsonModel($data);
1086
    }
768 geraldo 1087
 
1088
 
1089
 
1090
     /**
1091
     * Cambio de la imagen de de la cabecera de los reportes
1092
     * @return \Laminas\View\Model\JsonModel
1093
     */
1094
    public function headerAction()
1095
    {
1096
        $operation = $this->params()->fromRoute('operation');
1097
 
1098
 
1099
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1100
        $currentUser = $currentUserPlugin->getUser();
1101
        $currentCompany = $currentUserPlugin->getCompany();
1102
 
1103
 
1104
 
1105
 
1106
        $request = $this->getRequest();
1107
        if($request->isPost()) {
1108
 
1109
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1110
            $company = $companyMapper->fetchOne($currentCompany->id);
1111
 
1112
            $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
1113
 
1114
 
1115
 
1116
            if($operation == 'delete') {
1117
                if($company->header) {
1118
 
1119
                    if(!Image::delete($target_path, $company->header)) {
1120
                        return new JsonModel([
1121
                            'success'   => false,
1122
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1123
                        ]);
1124
                    }
1125
                }
1126
 
1127
                $this->logger->info('Se borro el header  de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1128
                $company->header = '';
1129
                if(!$companyMapper->updateHeader($company)) {
1130
                    return new JsonModel([
1131
                        'success'   => false,
1132
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1133
                    ]);
1134
                }
1135
 
1136
            } else {
1137
                $form = new CompanyProfileHeaderForm($this->config);
1138
                $data   = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1139
 
1140
                $form->setData($data);
1141
 
1142
                if($form->isValid()) {
1143
 
1144
 
1145
                    $files = $request->getFiles()->toArray();
1146
                    if(!empty($files['header']['error'])) {
1147
 
1148
                        return new JsonModel([
1149
                            'success'   => false,
1150
                            'data'   =>  'ERROR_UPLOAD_FILE'
1151
                        ]);
1152
 
1153
 
1154
                    }
1155
 
1156
                    if($company->header) {
1157
 
1158
                        if(!Image::delete($target_path, $company->header)) {
1159
                            return new JsonModel([
1160
                                'success'   => false,
1161
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1162
                            ]);
1163
                        }
1164
                    }
1165
 
1101 geraldo 1166
                    list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.company_header_pdf_size']);
768 geraldo 1167
                    $source             = $files['header']['tmp_name'];
1168
                    $target_filename    = 'company-header-' . uniqid() . '.png';
1169
                    $crop_to_dimensions = false;
1170
 
1171
                    if(!Image::uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions)) {
1172
                        return new JsonModel([
1173
                            'success'   => false,
1174
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1175
                        ]);
1176
                    }
1177
 
1178
 
1179
                    $company->header = $target_filename;
1180
                    if(!$companyMapper->updateHeader($company)) {
1181
                        return new JsonModel([
1182
                            'success'   => false,
1183
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1184
                        ]);
1185
                    }
1186
 
1187
 
1188
                    $this->logger->info('Se actualizo el header  de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1189
 
1190
                } else {
1191
                    $messages = [];
1192
                    $form_messages = (array) $form->getMessages();
1193
                    foreach($form_messages  as $fieldname => $field_messages)
1194
                    {
1195
                        $messages[$fieldname] = array_values($field_messages);
1196
                    }
1197
 
1198
                    return new JsonModel([
1199
                        'success'   => false,
1200
                        'data'   => $messages
1201
                    ]);
1202
                }
1203
            }
1204
            return new JsonModel([
1205
                'success'   => true,
810 geraldo 1206
                'data' => $this->url()->fromRoute('storage', ['type' => 'company', 'code' => $company->uuid, 'filename' => $company->header])
768 geraldo 1207
 
1208
            ]);
1209
        }
1210
 
1211
 
1212
        $data = [
1213
            'success' => false,
1214
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1215
        ];
1216
 
1217
 
1218
        return new JsonModel($data);
1219
    }
1220
 
1221
 
1222
     /**
1223
     * Cambio de la imagen de de la cabecera de los reportes
1224
     * @return \Laminas\View\Model\JsonModel
1225
     */
1226
    public function footerAction()
1227
    {
1228
        $operation          = $this->params()->fromRoute('operation');
1229
 
1230
 
1231
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1232
        $currentUser = $currentUserPlugin->getUser();
1233
        $currentCompany = $currentUserPlugin->getCompany();
1234
 
1235
 
1236
 
1237
 
1238
        $request = $this->getRequest();
1239
        if($request->isPost()) {
1240
 
1241
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1242
            $company = $companyMapper->fetchOne($currentCompany->id);
1243
 
1244
            $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
1245
 
1246
 
1247
 
1248
            if($operation == 'delete') {
1249
                if($company->footer) {
1250
 
1251
                    if(!Image::delete($target_path, $company->footer)) {
1252
                        return new JsonModel([
1253
                            'success'   => false,
1254
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1255
                        ]);
1256
                    }
1257
                }
1258
 
1259
                $this->logger->info('Se borro el footer  de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1260
                $company->footer = '';
1261
                if(!$companyMapper->updateFooter($company)) {
1262
                    return new JsonModel([
1263
                        'success'   => false,
1264
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1265
                    ]);
1266
                }
1267
 
1268
            } else {
1269
                $form = new CompanyProfileFooterForm($this->config);
1270
                $data   = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1271
 
1272
                $form->setData($data);
1273
 
1274
                if($form->isValid()) {
1275
 
1276
 
1277
                    $files = $request->getFiles()->toArray();
1278
                    if(!empty($files['footer']['error'])) {
1279
 
1280
                        return new JsonModel([
1281
                            'success'   => false,
1282
                            'data'   =>  'ERROR_UPLOAD_FILE'
1283
                        ]);
1284
 
1285
 
1286
                    }
1287
 
1288
                    if($company->footer) {
1289
 
1290
                        if(!Image::delete($target_path, $company->footer)) {
1291
                            return new JsonModel([
1292
                                'success'   => false,
1293
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1294
                            ]);
1295
                        }
1296
                    }
1297
 
1101 geraldo 1298
                    list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.company_footer_pdf_size']);
768 geraldo 1299
                    $source             = $files['footer']['tmp_name'];
1300
                    $target_filename    = 'company-footer-' . uniqid() . '.png';
1301
                    $crop_to_dimensions = false;
1302
 
1303
                    if(!Image::uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions)) {
1304
                        return new JsonModel([
1305
                            'success'   => false,
1306
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1307
                        ]);
1308
                    }
1309
 
1310
 
1311
                    $company->footer = $target_filename;
1312
                    if(!$companyMapper->updateFooter($company)) {
1313
                        return new JsonModel([
1314
                            'success'   => false,
1315
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1316
                        ]);
1317
                    }
1318
 
1319
 
1320
 
1321
 
1322
                    $this->logger->info('Se actualizo el footer  de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1323
 
1324
                } else {
1325
                    $messages = [];
1326
                    $form_messages = (array) $form->getMessages();
1327
                    foreach($form_messages  as $fieldname => $field_messages)
1328
                    {
1329
                        $messages[$fieldname] = array_values($field_messages);
1330
                    }
1331
 
1332
                    return new JsonModel([
1333
                        'success'   => false,
1334
                        'data'   => $messages
1335
                    ]);
1336
                }
1337
            }
1338
            return new JsonModel([
1339
                'success'   => true,
810 geraldo 1340
                'data' => $this->url()->fromRoute('storage', ['type' => 'company', 'code' => $company->uuid, 'filename' => $company->footer])
768 geraldo 1341
 
1342
            ]);
1343
        }
1344
 
1345
 
1346
        $data = [
1347
            'success' => false,
1348
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1349
        ];
1350
 
1351
 
1352
        return new JsonModel($data);
1353
    }
1 www 1354
 
1355
    /**
1356
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
1357
     * @return \Laminas\View\Model\JsonModel
1358
     */
1359
    public function industryAction()
1360
    {
1361
 
1362
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1363
        $currentUser = $currentUserPlugin->getUser();
1364
        $currentCompany = $currentUserPlugin->getCompany();
1365
 
1366
 
1367
 
1368
 
1369
 
1370
 
1371
        $request = $this->getRequest();
1372
        if($request->isGet()) {
1373
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1374
            $company = $companyMapper->fetchOne($currentCompany->id);
1375
 
1376
            $industryMapper = IndustryMapper::getInstance($this->adapter);
1377
            $industry = $industryMapper->fetchOne($company->industry_id);
1378
 
1379
            $data = [
1380
                'success' => true,
1381
                'data' => [
1382
                    'industry_id' => $industry->uuid,
1383
                ]
1384
            ];
1385
 
1386
            return new JsonModel($data);
1387
 
1388
 
1389
        } else if($request->isPost()) {
1390
 
1391
 
1392
            $form = new CompanyProfileIndustryForm($this->adapter);
1393
            $dataPost = $request->getPost()->toArray();
1394
 
1395
            $form->setData($dataPost);
1396
 
1397
            if($form->isValid()) {
1398
                $companyMapper = CompanyMapper::getInstance($this->adapter);
1399
                $company = $companyMapper->fetchOne($currentCompany->id);
1400
 
1401
                $dataPost = (array) $form->getData();
1402
 
1403
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1404
                $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
1405
 
1406
                $company->industry_id = $industry->id;
1407
                $companyMapper->updateIndustry($company);
1408
 
1409
                $this->logger->info('Se actualizo la industria de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1410
 
1411
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1412
                $industry = $industryMapper->fetchOne($company->industry_id);
1413
 
1414
                return new JsonModel([
1415
                    'success'   => true,
1416
                    'data' =>  $industry->name,
1417
 
1418
                ]);
1419
 
1420
            } else {
1421
                $messages = [];
1422
                $form_messages = (array) $form->getMessages();
1423
                foreach($form_messages  as $fieldname => $field_messages)
1424
                {
1425
                    $messages[$fieldname] = array_values($field_messages);
1426
                }
1427
 
1428
                return new JsonModel([
1429
                    'success'   => false,
1430
                    'data'   => $messages
1431
                ]);
1432
            }
1433
        }
1434
 
1435
 
1436
        $data = [
1437
            'success' => false,
1438
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1439
        ];
1440
 
1441
 
1442
        return new JsonModel($data);
1443
    }
1444
 
1445
    /**
1446
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
1447
     * @return \Laminas\View\Model\JsonModel
1448
     */
1449
    public function companySizeAction()
1450
    {
1451
 
1452
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1453
        $currentUser = $currentUserPlugin->getUser();
1454
        $currentCompany = $currentUserPlugin->getCompany();
1455
 
1456
 
1457
 
1458
 
1459
        $request = $this->getRequest();
1460
        if($request->isGet()) {
1461
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1462
            $company = $companyMapper->fetchOne($currentCompany->id);
1463
 
1464
            $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1465
            $companySize = $companySizeMapper->fetchOne($company->company_size_id);
1466
 
1467
            $data = [
1468
                'success' => true,
1469
                'data' => [
1470
                    'company_size_id' => $companySize->uuid
1471
                ]
1472
            ];
1473
 
1474
            return new JsonModel($data);
1475
 
1476
 
1477
        } else if($request->isPost()) {
1478
 
1479
 
1480
            $form = new CompanyProfileCompanySizeForm($this->adapter);
1481
            $dataPost = $request->getPost()->toArray();
1482
 
1483
            $form->setData($dataPost);
1484
 
1485
            if($form->isValid()) {
1486
                $companyMapper = CompanyMapper::getInstance($this->adapter);
1487
                $company = $companyMapper->fetchOne($currentCompany->id);
1488
 
1489
                $dataPost = (array) $form->getData();
1490
 
1491
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1492
                $companySize = $companySizeMapper->fetchOneByUuid($dataPost['company_size_id']);
1493
 
1494
                $company->company_size_id = $companySize->id;
1495
                $companyMapper->updateIndustry($company);
1496
 
1497
                $this->logger->info('Se actualizo la industria de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1498
 
1499
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1500
                $companySize = $companySizeMapper->fetchOne($company->company_size_id);
1501
 
1502
                return new JsonModel([
1503
                    'success'   => true,
1504
                    'data' => $companySize->name . ' (' . $companySize->minimum_no_of_employee .'-' . $companySize->maximum_no_of_employee . ')',
1505
 
1506
                ]);
1507
 
1508
            } else {
1509
                $messages = [];
1510
                $form_messages = (array) $form->getMessages();
1511
                foreach($form_messages  as $fieldname => $field_messages)
1512
                {
1513
                    $messages[$fieldname] = array_values($field_messages);
1514
                }
1515
 
1516
                return new JsonModel([
1517
                    'success'   => false,
1518
                    'data'   => $messages
1519
                ]);
1520
            }
1521
        }
1522
 
1523
 
1524
        $data = [
1525
            'success' => false,
1526
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1527
        ];
1528
 
1529
 
1530
        return new JsonModel($data);
1531
    }
1532
}