Proyectos de Subversion LeadersLinked - Backend

Rev

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