Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1 | Rev 3255 | 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\Form\UserProfile\SkillForm;
19
use LeadersLinked\Form\UserProfile\LanguageForm;
20
 
21
use LeadersLinked\Library\Functions;
22
use LeadersLinked\Mapper\UserProfileMapper;
23
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
24
use LeadersLinked\Model\UserProfile;
25
use LeadersLinked\Mapper\CompanyFollowerMapper;
26
use LeadersLinked\Mapper\LocationMapper;
27
use LeadersLinked\Model\UserLanguage;
28
use LeadersLinked\Mapper\UserLanguageMapper;
29
use LeadersLinked\Mapper\UserSkillMapper;
30
 
31
use LeadersLinked\Model\UserSkill;
32
use LeadersLinked\Mapper\UserMapper;
33
use LeadersLinked\Form\UserProfile\ExtendedForm;
34
use LeadersLinked\Form\UserProfile\LocationForm;
35
use LeadersLinked\Model\Location;
36
use LeadersLinked\Form\UserProfile\SocialNetworkForm;
37
use LeadersLinked\Form\UserProfile\EducationForm;
38
use LeadersLinked\Model\UserEducation;
39
use LeadersLinked\Mapper\UserEducationMapper;
40
use LeadersLinked\Mapper\DegreeMapper;
41
use LeadersLinked\Form\UserProfile\ExperienceForm;
42
use LeadersLinked\Mapper\LanguageMapper;
43
use LeadersLinked\Mapper\UserExperienceMapper;
44
use LeadersLinked\Mapper\IndustryMapper;
45
use LeadersLinked\Mapper\CompanySizeMapper;
46
use LeadersLinked\Model\UserExperience;
47
use LeadersLinked\Mapper\ConnectionMapper;
48
use LeadersLinked\Form\UserProfile\ImageForm;
49
use LeadersLinked\Library\Image;
50
use LeadersLinked\Form\UserProfile\CoverForm;
51
use LeadersLinked\Mapper\SkillMapper;
52
use LeadersLinked\Form\MyProfiles\CreateForm;
53
 
54
 
55
class MyProfilesController extends AbstractActionController
56
{
57
    /**
58
     *
59
     * @var AdapterInterface
60
     */
61
    private $adapter;
62
 
63
 
64
    /**
65
     *
66
     * @var AbstractAdapter
67
     */
68
    private $cache;
69
 
70
    /**
71
     *
72
     * @var  LoggerInterface
73
     */
74
    private $logger;
75
 
76
 
77
    /**
78
     *
79
     * @var array
80
     */
81
    private $config;
82
 
83
    /**
84
     *
85
     * @param AdapterInterface $adapter
86
     * @param AbstractAdapter $cache
87
     * @param LoggerInterface $logger
88
     * @param array $config
89
     */
90
    public function __construct($adapter, $cache , $logger,  $config)
91
    {
92
        $this->adapter      = $adapter;
93
        $this->cache        = $cache;
94
        $this->logger       = $logger;
95
        $this->config       = $config;
96
 
97
    }
98
 
99
    /**
100
     *
101
     * Generación del listado de perfiles
102
     * {@inheritDoc}
103
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
104
     */
105
    public function indexAction()
106
    {
107
        $currentUserPlugin = $this->plugin('currentUserPlugin');
108
        $currentUser = $currentUserPlugin->getUser();
109
 
110
        $request = $this->getRequest();
111
        if($request->isGet()) {
112
 
113
 
114
            $headers  = $request->getHeaders();
115
            $isJson = false;
116
            if($headers->has('Accept')) {
117
                $accept = $headers->get('Accept');
118
 
119
                $prioritized = $accept->getPrioritized();
120
 
121
                foreach($prioritized as $key => $value) {
122
                    $raw = trim($value->getRaw());
123
 
124
                    if(!$isJson) {
125
                        $isJson = str_contains($raw, 'json');
126
                    }
127
 
128
                }
129
            }
130
            if($isJson) {
131
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
132
                $allowView = $acl->isAllowed($currentUser->usertype_id, 'profile/view');
133
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'profile/my-profiles/edit');
134
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'profile/my-profiles/delete');
135
 
136
 
137
                $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
138
                $records  = $userProfileMapper->fetchAllByUserId($currentUser->id);
139
 
140
 
141
                $items = [];
142
                foreach($records as $record)
143
                {
144
 
145
                    $item = [
146
                        'id' => $record->id,
147
                        'name' => $record->name,
148
                        'image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $currentUser->uuid, 'filename' => $record->image ]),
149
                        'link_view' => $allowView ? $this->url()->fromRoute('profile/view', ['id' => $record->uuid ])  : '',
150
                        'link_edit' => $allowEdit ? $this->url()->fromRoute('profile/my-profiles/edit', ['id' => $record->uuid ])  : '',
151
                        'link_delete' => $allowDelete && $record->public == UserProfile::PUBLIC_NO ? $this->url()->fromRoute('profile/my-profiles/delete', ['id' =>$record->uuid ]) : '',
152
                    ];
153
 
154
                    array_push($items, $item);
155
                }
156
 
157
 
158
 
159
                $response = [
160
                    'success' => true,
161
                    'data' => $items
162
                ];
163
 
164
                return new JsonModel($response);
165
 
166
 
167
            } else {
168
                $formAdd = new CreateForm();
169
 
170
                $this->layout()->setTemplate('layout/layout.phtml');
171
                $viewModel = new ViewModel();
172
                $viewModel->setTemplate('leaders-linked/my-profiles/index.phtml');
173
                $viewModel->setVariables([
174
                    'formAdd' => $formAdd,
175
 
176
                ]);
177
                return $viewModel ;
178
            }
179
 
180
        } else {
181
            return new JsonModel([
182
                'success' => false,
183
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
184
            ]);
185
        }
186
    }
187
 
188
 
189
 
190
    /**
191
     *
192
     * Agregar un nuevo perfil
193
     * @return \Laminas\View\Model\JsonModel
194
     */
195
    public function addAction()
196
    {
197
        $request = $this->getRequest();
198
 
199
 
200
        if($request->isPost()) {
201
            $form = new  CreateForm();
202
            $dataPost = $request->getPost()->toArray();
203
 
204
            $form->setData($dataPost);
205
 
206
            if($form->isValid()) {
207
                $dataPost = (array) $form->getData();
208
 
209
                $hydrator = new ObjectPropertyHydrator();
210
                $userProfile = new UserProfile();
211
                $hydrator->hydrate($dataPost, $userProfile);
212
 
213
                $currentUserPlugin = $this->plugin('currentUserPlugin');
214
                $currentUser = $currentUserPlugin->getUser();
215
 
216
                $userProfile->uuid = Functions::genUUID();
217
                $userProfile->user_id = $currentUser->id;
218
                $userProfile->public = \LeadersLinked\Model\UserProfile::PUBLIC_NO;
219
 
220
                $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
221
                $result = $userProfileMapper->insert($userProfile);
222
 
223
                if($result) {
224
                    $this->logger->info('Se agrego el perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
225
 
226
                    $data = [
227
                        'success'   => true,
228
                        'data'   => 'LABEL_RECORD_ADDED'
229
                    ];
230
                } else {
231
                    $data = [
232
                        'success'   => false,
233
                        'data'      => $userProfile->getError()
234
                    ];
235
 
236
                }
237
 
238
                return new JsonModel($data);
239
 
240
            } else {
241
                $messages = [];
242
                $form_messages = (array) $form->getMessages();
243
                foreach($form_messages  as $fieldname => $field_messages)
244
                {
245
 
246
                    $messages[$fieldname] = array_values($field_messages);
247
                }
248
 
249
                return new JsonModel([
250
                    'success'   => false,
251
                    'data'   => $messages
252
                ]);
253
            }
254
 
255
        } else {
256
            $data = [
257
                'success' => false,
258
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
259
            ];
260
 
261
            return new JsonModel($data);
262
        }
263
 
264
        return new JsonModel($data);
265
    }
266
 
267
    /**
268
     *
269
     * Borrar un perfil excepto el público
270
     * @return \Laminas\View\Model\JsonModel
271
     */
272
    public function deleteAction()
273
    {
274
        $request = $this->getRequest();
275
        $id = $this->params()->fromRoute('id');
276
 
277
        if(!$id) {
278
            $data = [
279
                'success'   => false,
280
                'data'   => 'ERROR_INVALID_PARAMETER'
281
            ];
282
 
283
            return new JsonModel($data);
284
        }
285
 
286
 
287
 
288
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
289
        $userProfile = $userProfileMapper->fetchOneByUuid($id);
290
        if(!$userProfile) {
291
            $data = [
292
                'success'   => false,
293
                'data'   => 'ERROR_RECORD_NOT_FOUND'
294
            ];
295
 
296
            return new JsonModel($data);
297
        }
298
 
299
        $currentUser = $this->plugin('currentUserPlugin');
300
        if($currentUser->getUserId() != $userProfile->user_id) {
301
            $response = [
302
                'success' => false,
303
                'data' => 'ERROR_UNAUTHORIZED'
304
            ];
305
 
306
            return new JsonModel($response);
307
        }
308
 
309
        if(!$userProfile->public == UserProfile::PUBLIC_YES) {
310
            $data = [
311
                'success'   => false,
312
                'data'   => 'ERROR_PUBLIC_PROFILE'
313
            ];
314
 
315
            return new JsonModel($data);
316
        }
317
 
318
        if($request->isPost()) {
319
            $result = $userProfileMapper->delete($userProfile);
320
            if($result) {
321
                $this->logger->info('Se borro el perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
322
 
323
                $data = [
324
                    'success' => true,
325
                    'data' => 'LABEL_RECORD_DELETED'
326
                ];
327
            } else {
328
 
329
                $data = [
330
                    'success'   => false,
331
                    'data'      => $userProfileMapper->getError()
332
                ];
333
 
334
                return new JsonModel($data);
335
            }
336
 
337
        } else {
338
            $data = [
339
                'success' => false,
340
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
341
            ];
342
 
343
            return new JsonModel($data);
344
        }
345
 
346
        return new JsonModel($data);
347
    }
348
 
349
    /**
350
     * Presenta el perfil con las opciónes de edición de cada sección
351
     * @return \Laminas\Http\Response|\Laminas\View\Model\ViewModel|\Laminas\View\Model\JsonModel
352
     */
353
    public function editAction()
354
    {
355
        $flashMessenger = $this->plugin('FlashMessenger');
356
 
357
 
358
        $request = $this->getRequest();
359
        $id = $this->params()->fromRoute('id');
360
 
361
 
362
        if(!$id) {
363
            $flashMessenger->addErrorMessage('ERROR_INVALID_PARAMETER');
364
            return $this->redirect()->toRoute('dashboard');
365
        }
366
 
367
 
368
 
369
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
370
        $userProfile = $userProfileMapper->fetchOneByUuid($id);
371
 
372
        if(!$userProfile) {
373
            $flashMessenger->addErrorMessage('ERROR_RECORD_NOT_FOUND');
374
            return $this->redirect()->toRoute('dashboard');
375
        }
376
 
377
        $currentUser = $this->plugin('currentUserPlugin');
378
        if($currentUser->getUserId() != $userProfile->user_id) {
379
            $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
380
            return $this->redirect()->toRoute('dashboard');
381
        }
382
 
383
 
384
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
385
        if($sandbox) {
386
            $google_map_key  = $this->config['leaderslinked.google_map.sandbox_api_key'];
387
        } else {
388
            $google_map_key  = $this->config['leaderslinked.google_map.production_api_key'];
389
        }
390
 
391
        if($request->isGet()) {
392
 
393
            if($userProfile->location_id) {
394
                $locationMapper= LocationMapper::getInstance($this->adapter);
395
                $location = $locationMapper->fetchOne($userProfile->location_id);
396
 
397
                $formattedAddress = $location->formatted_address;
398
                $country = $location->country;
399
            } else {
400
                $formattedAddress = '';
401
                $country = '';
402
            }
403
 
404
 
405
 
406
            $userMapper = UserMapper::getInstance($this->adapter);
407
            $user = $userMapper->fetchOne($userProfile->user_id);
408
 
409
            $userLanguages = [];
410
            $languageMapper = LanguageMapper::getInstance($this->adapter);
411
            $userLanguageMapper = UserLanguageMapper::getInstance($this->adapter);
412
            $records = $userLanguageMapper->fetchAllByUserProfileId($userProfile->id);
413
            foreach ($records as $record)
414
            {
415
                $language = $languageMapper->fetchOne($record->language_id);
416
                $userLanguages[$language->id] = $language->name;
417
            }
418
 
419
 
420
            $locationMapper = LocationMapper::getInstance($this->adapter);
421
            $degreeMapper = DegreeMapper::getInstance($this->adapter);
422
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
423
            $userEducations = $userEducationMapper->fetchAllByUserProfileId($userProfile->id);
424
 
425
 
426
 
427
            foreach($userEducations  as &$userEducation)
428
            {
429
                $location = $locationMapper->fetchOne($userEducation->location_id);
430
                $degree = $degreeMapper->fetchOne($userEducation->degree_id);
431
 
432
                $userEducation = [
433
                    'university' => $userEducation->university,
434
                    'degree' => $degree->name,
435
                    'field_of_study' => $userEducation->field_of_study,
436
                    'grade_or_percentage' => $userEducation->grade_or_percentage,
437
                    'formatted_address' => $location->formatted_address,
438
                    'from_year' => $userEducation->from_year,
439
                    'to_year' => $userEducation->to_year,
440
                    'description' => $userEducation->description,
441
                    'link_edit' => $this->url()->fromRoute('profile/my-profiles/education', [ 'id' => $userProfile->uuid, 'operation' => 'edit', 'user_education_id' => $userEducation->uuid ]),
442
                    'link_delete' => $this->url()->fromRoute('profile/my-profiles/education', [ 'id' => $userProfile->uuid, 'operation' => 'delete', 'user_education_id' => $userEducation->uuid ]),
443
                ];
444
            }
445
 
446
            $industryMapper = IndustryMapper::getInstance($this->adapter);
447
            $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
448
 
449
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
450
            $userExperiences = $userExperienceMapper->fetchAllByUserProfileId($userProfile->id);
451
 
452
            foreach($userExperiences  as &$userExperience)
453
            {
454
                $location = $locationMapper->fetchOne($userExperience->location_id);
455
                $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
456
                $industry = $industryMapper->fetchOne($userExperience->industry_id);
457
 
458
                $userExperience = [
459
                    'company' => $userExperience->company,
460
                    'industry' => $industry->name,
461
                    'size' => $companySize->name . ' ('.$companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee .')',
462
                    'title' => $userExperience->title,
463
                    'formatted_address' => $location->formatted_address,
464
                    'from_year' => $userExperience->from_year,
465
                    'from_month' => $userExperience->from_month,
466
                    'to_year' => $userExperience->to_year,
467
                    'to_month' => $userExperience->to_month,
468
                    'description' => $userExperience->description,
469
                    'is_current' => $userExperience->is_current,
470
                    'link_edit' => $this->url()->fromRoute('profile/my-profiles/experience', [ 'id' => $userProfile->uuid, 'operation' => 'edit', 'user_experience_id' => $userExperience->uuid ]),
471
                    'link_delete' => $this->url()->fromRoute('profile/my-profiles/experience', [ 'id' => $userProfile->uuid, 'operation' => 'delete', 'user_experience_id' => $userExperience->uuid ]),
472
                ];
473
            }
474
 
475
 
476
            $headers  = $request->getHeaders();
477
 
478
            $isJson = false;
479
            if($headers->has('Accept')) {
480
                $accept = $headers->get('Accept');
481
 
482
                $prioritized = $accept->getPrioritized();
483
 
484
                foreach($prioritized as $key => $value) {
485
                    $raw = trim($value->getRaw());
486
 
487
                    if(!$isJson) {
488
                        $isJson = strpos($raw, 'json');
489
                    }
490
 
491
                }
492
            }
493
 
494
 
495
 
496
            $userSkills = [];
497
            $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
498
            $skillMapper = SkillMapper::getInstance($this->adapter);
499
            $records  = $userSkillMapper->fetchAllByUserProfileId($userProfile->id);
500
            foreach($records as $record)
501
            {
502
                $skill = $skillMapper->fetchOne($record->skill_id);
503
                $userSkills[$skill->uuid] = $skill->name;
504
            }
505
 
506
 
507
            $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
508
            $following = $companyFollowerMapper->getCountFollowing($user->id);
509
 
510
            $connectionMapper = ConnectionMapper::getInstance($this->adapter);
511
            $follower = $connectionMapper->fetchTotalConnectionByUser($user->id);
512
 
513
 
514
            $image_size_cover = $this->config['leaderslinked.image_sizes.user_cover_upload'];
515
            $image_size_profile = $this->config['leaderslinked.image_sizes.user_upload'];
516
 
517
 
518
            if($isJson) {
519
 
520
                $data = [
521
                    'following'         => $following ,
522
                    'follower'          => $follower,
523
                    'user_id'           => $user->id,
524
                    'user_uuid'         => $user->uuid,
525
                    'full_name'         => trim($user->first_name . ' ' . $user->last_name),
526
                    'user_profile_id'   => $userProfile->id,
527
                    'user_profile_uuid' => $userProfile->uuid,
528
                    'image'             => $userProfile->image,
529
                    'cover'             => $userProfile->cover,
530
                    'overview'          => $userProfile->description,
531
                    'facebook'          => $userProfile->facebook,
532
                    'instagram'         => $userProfile->instagram,
533
                    'twitter'           => $userProfile->twitter,
534
                    'formatted_address' => $formattedAddress,
535
                    'country'           => $country,
536
                    'user_skills'       => $userSkills,
537
                    'user_languages'    => $userLanguages,
538
                    'user_educations'   => $userEducations,
539
                    'user_experiences'  => $userExperiences,
540
                    'image_size_cover' =>  $image_size_cover,
541
                    'image_size_profile' => $image_size_profile
542
                ];
543
 
544
                $viewModel = new JsonModel($data);
545
 
546
            } else {
547
                $industries = [];
548
                $industryMapper = IndustryMapper::getInstance($this->adapter);
549
 
550
                $records = $industryMapper->fetchAllActives();
551
                foreach($records as $record)
552
                {
553
                    $industries[$record->uuid] = $record->name;
554
                }
555
 
556
                $companySizes = [];
557
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
558
                $records = $companySizeMapper->fetchAllActives();
559
                foreach($records as $record)
560
                {
561
                    $companySizes[$record->uuid] = $record->name . ' ('.$record->minimum_no_of_employee . '-' . $record->maximum_no_of_employee .')';
562
                }
563
 
564
                $degrees = [];
565
                $degreeMapper = DegreeMapper::getInstance($this->adapter);
566
                $records = $degreeMapper->fetchAllActives();
567
                foreach($records as $record)
568
                {
569
                    $degrees[$record->uuid] = $record->name;
570
                }
571
 
572
                $languages = [];
573
                $languageMapper = LanguageMapper::getInstance($this->adapter);
574
                $records = $languageMapper->fetchAllActives();
575
                foreach($records as $record) {
576
                    $languages[$record->id] = $record->name;
577
                }
578
 
579
                $skills = [];
580
                $skillMapper = SkillMapper::getInstance($this->adapter);
581
                $records =  $skillMapper->fetchAllActives();
582
                foreach($records as $record) {
583
                    $skills[$record->uuid] = $record->name;
584
                }
585
 
586
 
587
                $formSkill = new SkillForm($this->adapter);
588
                $formLanguage = new LanguageForm($this->adapter);
589
                $formLocation = new LocationForm();
590
                $formExtended = new ExtendedForm();
591
                $formExperience = new ExperienceForm($this->adapter);
592
                $formEducation = new EducationForm($this->adapter);
593
                $formSocialNetwork = new SocialNetworkForm();
594
                $formImage = new ImageForm($this->config);
595
                $formCover = new CoverForm($this->config);
596
 
597
 
598
 
599
 
600
                $this->layout()->setTemplate('layout/layout.phtml');
601
                $viewModel = new ViewModel();
602
                $viewModel->setTemplate('leaders-linked/my-profiles/edit.phtml');
603
                $viewModel->setVariables([
604
                    'google_map_key'    => $google_map_key,
605
                    'following'         => $following ,
606
                    'follower'          => $follower,
607
                    'user_id'           => $user->id,
608
                    'user_uuid' => $user->uuid,
609
                    'full_name'         => trim($user->first_name . ' ' . $user->last_name),
610
                    'user_profile_id'   => $userProfile->id,
611
                    'user_profile_uuid' => $userProfile->uuid,
612
                    'public'            => $userProfile->public,
613
                    'image'             => $userProfile->image,
614
                    'cover'             => $userProfile->cover,
615
                    'overview'          => $userProfile->description,
616
                    'facebook'          => $userProfile->facebook,
617
                    'instagram'         => $userProfile->instagram,
618
                    'twitter'           => $userProfile->twitter,
619
                    'formatted_address' => $formattedAddress,
620
                    'country'           => $country,
621
                    'user_skills'       => $userSkills,
622
                    'user_languages'    => $userLanguages,
623
                    'user_educations'   => $userEducations,
624
                    'user_experiences'  => $userExperiences,
625
                    'company_sizes'     => $companySizes,
626
                    'degrees'           => $degrees,
627
                    'industries'        => $industries,
628
                    'languages'         => $languages,
629
                    'skills'            => $skills,
630
 
631
 
632
                    'formLanguage'      => $formLanguage,
633
                    'formLocation'      => $formLocation,
634
                    'formSkill'         => $formSkill,
635
                    'formSocialNetwork' => $formSocialNetwork,
636
                    'formExtended'      => $formExtended,
637
                    'formExperience'    => $formExperience,
638
                    'formEducation'     => $formEducation,
639
                    'formImage'        => $formImage,
640
                    'formCover'         => $formCover,
641
                    'image_size_cover' =>  $image_size_cover,
642
                    'image_size_profile' => $image_size_profile
643
                ]);
644
            }
645
            return $viewModel ;
646
 
647
        } else {
648
            $data = [
649
                'success' => false,
650
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
651
            ];
652
 
653
            return new JsonModel($data);
654
        }
655
 
656
        return new JsonModel($data);
657
    }
658
 
659
    /**
660
     * Actualización de las habilidades
661
     * @return \Laminas\View\Model\JsonModel
662
     */
663
    public function skillAction()
664
    {
665
 
666
        $user_profile_id = $this->params()->fromRoute('id');
667
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
668
 
669
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
670
        if(!$userProfile) {
671
            $response = [
672
                'success' => false,
673
                'data' => 'ERROR_INVALID_PARAMETER'
674
            ];
675
 
676
            return new JsonModel($response);
677
 
678
        }
679
 
680
        $currentUser = $this->plugin('currentUserPlugin');
681
        if($currentUser->getUserId() != $userProfile->user_id) {
682
            $response = [
683
                'success' => false,
684
                'data' => 'ERROR_UNAUTHORIZED'
685
            ];
686
 
687
            return new JsonModel($response);
688
        }
689
 
690
 
691
 
692
        $request = $this->getRequest();
693
        if($request->isGet()) {
694
            $skillMapper = SkillMapper::getInstance($this->adapter);
695
 
696
 
697
            $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
698
            $userSkills  = $userSkillMapper->fetchAllByUserProfileId($userProfile->id);
699
 
700
            $items = [];
701
            foreach($userSkills as $userSkill)
702
            {
703
                $skill = $skillMapper->fetchOne($userSkill->skill_id);
704
                array_push($items, $skill->uuid);
705
            }
706
 
707
            $data = [
708
                'success' => true,
709
                'data' => $items
710
            ];
711
 
712
            return new JsonModel($data);
713
 
714
 
715
        } else if($request->isPost()) {
716
 
717
            $form = new SkillForm($this->adapter);
718
            $dataPost = $request->getPost()->toArray();
719
 
720
            $form->setData($dataPost);
721
 
722
            if($form->isValid()) {
723
                $this->logger->info('Se actualizaron las habilidades del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
724
 
725
                $skillMapper = SkillMapper::getInstance($this->adapter);
726
 
727
 
728
                $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
729
                $userSkillMapper->deleteByUserProfileId($userProfile->id);
730
 
731
                $dataPost = (array) $form->getData();
732
                $skills = $dataPost['skills'];
733
                foreach($skills as $skill_uuid)
734
                {
735
 
736
                    $skill = $skillMapper->fetchOneByUuid($skill_uuid);
737
 
738
 
739
 
740
                    $userSkill = new UserSkill();
741
                    $userSkill->user_id = $userProfile->user_id;
742
                    $userSkill->user_profile_id = $userProfile->id;
743
                    $userSkill->skill_id =  $skill->id;
744
 
745
                    $userSkillMapper->insert($userSkill);
746
                }
747
 
748
                $items = [];
749
 
750
                $records = $userSkillMapper->fetchAllByUserProfileId($userProfile->id);
751
                foreach($records as $record)
752
                {
753
                    $skill = $skillMapper->fetchOne($record->skill_id);
754
                    array_push($items,  ['value' => $skill->uuid, 'label' => $skill->name]);
755
                }
756
 
757
                return new JsonModel([
758
                    'success'   => true,
759
                    'data'   => $items
760
                ]);
761
 
762
            } else {
763
                $messages = [];
764
                $form_messages = (array) $form->getMessages();
765
                foreach($form_messages  as $fieldname => $field_messages)
766
                {
767
                    $messages[$fieldname] = array_values($field_messages);
768
                }
769
 
770
                return new JsonModel([
771
                    'success'   => false,
772
                    'data'   => $messages
773
                ]);
774
            }
775
 
776
            $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
777
 
778
        }
779
 
780
 
781
        $data = [
782
            'success' => false,
783
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
784
        ];
785
 
786
 
787
        return new JsonModel($data);
788
    }
789
 
790
    /**
791
     * Actualización de los idiomas
792
     * @return \Laminas\View\Model\JsonModel
793
     */
794
    public function languageAction()
795
    {
796
 
797
        $user_profile_id = $this->params()->fromRoute('id');
798
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
799
 
800
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
801
        if(!$userProfile) {
802
            $response = [
803
                'success' => false,
804
                'data' => 'ERROR_INVALID_PARAMETER'
805
            ];
806
 
807
            return new JsonModel($response);
808
 
809
        }
810
 
811
        $currentUser = $this->plugin('currentUserPlugin');
812
        if($currentUser->getUserId() != $userProfile->user_id) {
813
            $response = [
814
                'success' => false,
815
                'data' => 'ERROR_UNAUTHORIZED'
816
            ];
817
 
818
            return new JsonModel($response);
819
        }
820
 
821
 
822
 
823
        $request = $this->getRequest();
824
        if($request->isGet()) {
825
            $this->logger->info('Se actualizaron los idiomas del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
826
 
827
            $userLanguageMapper = UserLanguageMapper::getInstance($this->adapter);
828
            $languages  = $userLanguageMapper->fetchAllByUserProfileId($userProfile->id);
829
 
830
            $items = [];
831
            foreach($languages as $language)
832
            {
833
                array_push($items, $language->language_id);
834
            }
835
 
836
            $data = [
837
                'success' => true,
838
                'data' => $items
839
            ];
840
 
841
            return new JsonModel($data);
842
 
843
 
844
        } else if($request->isPost()) {
845
 
846
            $form = new LanguageForm($this->adapter);
847
            $dataPost = $request->getPost()->toArray();
848
 
849
            $form->setData($dataPost);
850
 
851
            if($form->isValid()) {
852
 
853
                $languageMapper = LanguageMapper::getInstance($this->adapter);
854
                $userLanguageMapper = UserLanguageMapper::getInstance($this->adapter);
855
                $userLanguageMapper->deleteByUserProfileId($userProfile->id);
856
 
857
                $dataPost = (array) $form->getData();
858
                $languages = $dataPost['languages'];
859
                foreach($languages as $language_id)
860
                {
861
                    $language = $languageMapper->fetchOne($language_id);
862
 
863
                    $userLanguage = new UserLanguage();
864
                    $userLanguage->user_id = $userProfile->user_id;
865
                    $userLanguage->user_profile_id = $userProfile->id;
866
                    $userLanguage->language_id = $language->id;
867
 
868
                    $userLanguageMapper->insert($userLanguage);
869
                }
870
 
871
                $items = [];
872
                $records = $userLanguageMapper->fetchAllByUserProfileId($userProfile->id);
873
                foreach($records as $record)
874
                {
875
                    $language = $languageMapper->fetchOne($record->language_id);
876
                    array_push($items,  ['value' => $language->id, 'label' => $language->name]);
877
                }
878
 
879
                return new JsonModel([
880
                    'success'   => true,
881
                    'data'   => $items
882
                ]);
883
 
884
            } else {
885
                $messages = [];
886
                $form_messages = (array) $form->getMessages();
887
                foreach($form_messages  as $fieldname => $field_messages)
888
                {
889
                    $messages[$fieldname] = array_values($field_messages);
890
                }
891
 
892
                return new JsonModel([
893
                    'success'   => false,
894
                    'data'   => $messages
895
                ]);
896
            }
897
        }
898
 
899
 
900
        $data = [
901
            'success' => false,
902
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
903
        ];
904
 
905
 
906
        return new JsonModel($data);
907
    }
908
 
909
    /**
910
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
911
     * @return \Laminas\View\Model\JsonModel
912
     */
913
    public function extendedAction()
914
    {
915
 
916
        $user_profile_id = $this->params()->fromRoute('id');
917
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
918
 
919
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
920
        if(!$userProfile) {
921
            $response = [
922
                'success' => false,
923
                'data' => 'ERROR_INVALID_PARAMETER'
924
            ];
925
 
926
            return new JsonModel($response);
927
 
928
        }
929
 
930
        $currentUser = $this->plugin('currentUserPlugin');
931
        if($currentUser->getUserId() != $userProfile->user_id) {
932
            $response = [
933
                'success' => false,
934
                'data' => 'ERROR_UNAUTHORIZED'
935
            ];
936
 
937
            return new JsonModel($response);
938
        }
939
 
940
 
941
 
942
        $request = $this->getRequest();
943
        if($request->isGet()) {
944
            $data = [
945
                'success' => true,
946
                'data' => [
947
                    'description' => $userProfile->description,
948
                ]
949
            ];
950
 
951
            return new JsonModel($data);
952
 
953
 
954
        } else if($request->isPost()) {
955
 
956
 
957
            $form = new ExtendedForm();
958
            $dataPost = $request->getPost()->toArray();
959
 
960
            $form->setData($dataPost);
961
 
962
            if($form->isValid()) {
963
                $dataPost = (array) $form->getData();
964
 
965
                $hydrator = new ObjectPropertyHydrator();
966
                $hydrator->hydrate($dataPost, $userProfile);
967
 
968
                $userProfileMapper->updateExtended($userProfile);
969
 
970
                $this->logger->info('Se actualizo las descripción del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
971
 
972
                return new JsonModel([
973
                    'success'   => true,
974
                    'data' => [
975
                        'description' => $userProfile->description,
976
                    ]
977
                ]);
978
 
979
            } else {
980
                $messages = [];
981
                $form_messages = (array) $form->getMessages();
982
                foreach($form_messages  as $fieldname => $field_messages)
983
                {
984
                    $messages[$fieldname] = array_values($field_messages);
985
                }
986
 
987
                return new JsonModel([
988
                    'success'   => false,
989
                    'data'   => $messages
990
                ]);
991
            }
992
        }
993
 
994
 
995
        $data = [
996
            'success' => false,
997
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
998
        ];
999
 
1000
 
1001
        return new JsonModel($data);
1002
    }
1003
 
1004
    /**
1005
     * Actualización de la ubucación
1006
     * @return \Laminas\View\Model\JsonModel
1007
     */
1008
    public function locationAction()
1009
    {
1010
 
1011
        $user_profile_id = $this->params()->fromRoute('id');
1012
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1013
 
1014
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1015
        if(!$userProfile) {
1016
            $response = [
1017
                'success' => false,
1018
                'data' => 'ERROR_INVALID_PARAMETER'
1019
            ];
1020
 
1021
            return new JsonModel($response);
1022
 
1023
        }
1024
 
1025
        $currentUser = $this->plugin('currentUserPlugin');
1026
        $currentUser = $currentUser->getUser();
1027
        if($currentUser->id != $userProfile->user_id) {
1028
            $response = [
1029
                'success' => false,
1030
                'data' => 'ERROR_UNAUTHORIZED'
1031
            ];
1032
 
1033
            return new JsonModel($response);
1034
        }
1035
 
1036
 
1037
 
1038
        $request = $this->getRequest();
1039
        if($request->isPost()) {
1040
 
1041
            $form = new LocationForm();
1042
            $dataPost = $request->getPost()->toArray();
1043
 
1044
            $form->setData($dataPost);
1045
 
1046
            if($form->isValid()) {
1047
                $this->logger->info('Se actualizaron la ubicación del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1048
 
1049
                $dataPost = (array) $form->getData();
1050
 
1051
                $location = new Location();
1052
                $hydrator = new ObjectPropertyHydrator();
1053
                $hydrator->hydrate($dataPost, $location);
1054
 
1055
                $location->id = $userProfile->location_id ? $userProfile->location_id : null;
1056
 
1057
 
1058
 
1059
                $locationMapper = LocationMapper::getInstance($this->adapter);
1060
                if($userProfile->location_id) {
1061
                    $result = $locationMapper->update($location);
1062
                } else {
1063
                    $result = $locationMapper->insert($location);
1064
 
1065
                    if($result) {
1066
                        $userProfile->location_id = $location->id;
1067
                        $userProfileMapper->updateLocation($userProfile);
1068
                    }
1069
                }
1070
 
1071
                if($result) {
1072
                    if($userProfile->public == UserProfile::PUBLIC_YES) {
1073
                        $currentUser->location_id = $location->id;
1074
 
1075
                        $userMapper = UserMapper::getInstance($this->adapter);
1076
                        $userMapper->updateLocation($currentUser);
1077
                    }
1078
 
1079
 
1080
                    $response = [
1081
                        'success'   => true,
1082
                        'data' => [
1083
                            'formatted_address' => $location->formatted_address,
1084
                            'country' => $location->country,
1085
                        ]
1086
                    ];
1087
                } else {
1088
                    $response = [
1089
                        'success'   => false,
1090
                        'data' => 'ERROR_THERE_WAS_AN_ERROR'
1091
                    ];
1092
                }
1093
 
1094
 
1095
 
1096
                return new JsonModel($response);
1097
 
1098
            } else {
1099
                return new JsonModel([
1100
                    'success'   => false,
1101
                    'data'   =>   'ERROR_PLACED_AUTOCOMPLETE_DOES_NOT_CONTAIN_GEOMETRY'
1102
                ]);
1103
            }
1104
        }
1105
 
1106
 
1107
        $data = [
1108
            'success' => false,
1109
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1110
        ];
1111
 
1112
 
1113
        return new JsonModel($data);
1114
    }
1115
 
1116
    /**
1117
     * Actualización de las redes sociales
1118
     * @return \Laminas\View\Model\JsonModel
1119
     */
1120
    public function socialNetworkAction()
1121
    {
1122
 
1123
        $user_profile_id = $this->params()->fromRoute('id');
1124
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1125
 
1126
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1127
        if(!$userProfile) {
1128
            $response = [
1129
                'success' => false,
1130
                'data' => 'ERROR_INVALID_PARAMETER'
1131
            ];
1132
 
1133
            return new JsonModel($response);
1134
 
1135
        }
1136
 
1137
        $currentUser = $this->plugin('currentUserPlugin');
1138
        if($currentUser->getUserId() != $userProfile->user_id) {
1139
            $response = [
1140
                'success' => false,
1141
                'data' => 'ERROR_UNAUTHORIZED'
1142
            ];
1143
 
1144
            return new JsonModel($response);
1145
        }
1146
 
1147
 
1148
 
1149
        $request = $this->getRequest();
1150
        if($request->isGet()) {
1151
            $data = [
1152
                'success' => true,
1153
                'data' => [
1154
                    'facebook' => $userProfile->facebook,
1155
                    'instagram' => $userProfile->instagram,
1156
                    'twitter' => $userProfile->twitter
1157
                ]
1158
            ];
1159
 
1160
            return new JsonModel($data);
1161
 
1162
 
1163
        } else if($request->isPost()) {
1164
 
1165
            $form = new SocialNetworkForm();
1166
            $dataPost = $request->getPost()->toArray();
1167
 
1168
            $form->setData($dataPost);
1169
 
1170
            if($form->isValid()) {
1171
                $this->logger->info('Se actualizaron las redes sociales del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1172
 
1173
                $dataPost = (array) $form->getData();
1174
 
1175
                $hydrator = new ObjectPropertyHydrator();
1176
                $hydrator->hydrate($dataPost, $userProfile);
1177
 
1178
                $userProfileMapper->updateSocialNetwork($userProfile);
1179
                return new JsonModel([
1180
                    'success'   => true,
1181
                    'data' => [
1182
                        'facebook' => $userProfile->facebook,
1183
                        'instagram' => $userProfile->instagram,
1184
                        'twitter' => $userProfile->twitter
1185
                    ]
1186
                ]);
1187
 
1188
            } else {
1189
                $messages = [];
1190
                $form_messages = (array) $form->getMessages();
1191
                foreach($form_messages  as $fieldname => $field_messages)
1192
                {
1193
                    $messages[$fieldname] = array_values($field_messages);
1194
                }
1195
 
1196
                return new JsonModel([
1197
                    'success'   => false,
1198
                    'data'   => $messages
1199
                ]);
1200
            }
1201
        }
1202
 
1203
 
1204
        $data = [
1205
            'success' => false,
1206
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1207
        ];
1208
 
1209
 
1210
        return new JsonModel($data);
1211
    }
1212
 
1213
    /**
1214
     * Actualización de los registros de estudios realizados
1215
     * @return \Laminas\View\Model\JsonModel
1216
     */
1217
    public function  educationAction()
1218
    {
1219
 
1220
        $user_profile_id    = $this->params()->fromRoute('id');
1221
        $user_education_id  = $this->params()->fromRoute('user_education_id');
1222
        $operation          = $this->params()->fromRoute('operation');
1223
 
1224
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1225
 
1226
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1227
        if(!$userProfile) {
1228
            $response = [
1229
                'success' => false,
1230
                'data' => 'ERROR_INVALID_PARAMETER'
1231
            ];
1232
 
1233
            return new JsonModel($response);
1234
 
1235
        }
1236
 
1237
        $currentUser = $this->plugin('currentUserPlugin');
1238
        if($currentUser->getUserId() != $userProfile->user_id) {
1239
            $response = [
1240
                'success' => false,
1241
                'data' => 'ERROR_UNAUTHORIZED'
1242
            ];
1243
 
1244
            return new JsonModel($response);
1245
        }
1246
 
1247
 
1248
 
1249
        $request = $this->getRequest();
1250
        if($request->isPost()) {
1251
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
1252
 
1253
            if($operation == 'delete' || $operation == 'edit') {
1254
                $userEducation = $userEducationMapper->fetchOneByUuid($user_education_id);
1255
 
1256
 
1257
                if(!$userEducation) {
1258
 
1259
                    $response = [
1260
                        'success' => false,
1261
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1262
                    ];
1263
 
1264
                    return new JsonModel($response);
1265
                } else if($userProfile->id != $userEducation->user_profile_id) {
1266
                    $response = [
1267
                        'success' => false,
1268
                        'data' => 'ERROR_UNAUTHORIZED'
1269
                    ];
1270
 
1271
                    return new JsonModel($response);
1272
                }
1273
            } else {
1274
                $userEducation = null;
1275
            }
1276
 
1277
            $locationMapper = LocationMapper::getInstance($this->adapter);
1278
            if($operation == 'delete') {
1279
                $this->logger->info('Se borro un registro de educación del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1280
 
1281
                $result = $userEducationMapper->delete($userEducation);
1282
                if($result) {
1283
                    $locationMapper->delete($userEducation->location_id);
1284
                }
1285
            } else {
1286
 
1287
 
1288
                $form = new EducationForm($this->adapter);
1289
                $dataPost = $request->getPost()->toArray();
1290
 
1291
                $form->setData($dataPost);
1292
 
1293
                if($form->isValid()) {
1294
 
1295
                    if(!$userEducation) {
1296
                        $userEducation = new UserEducation();
1297
                        $userEducation->user_id = $userProfile->user_id;
1298
                        $userEducation->user_profile_id = $userProfile->id;
1299
                    }
1300
 
1301
                    $dataPost = (array) $form->getData();
1302
 
1303
                    $hydrator = new ObjectPropertyHydrator();
1304
                    $hydrator->hydrate($dataPost, $userEducation);
1305
 
1306
                    $degreeMapper = DegreeMapper::getInstance($this->adapter);
1307
                    $degree = $degreeMapper->fetchOneByUuid($dataPost['degree_id']);
1308
                    $userEducation->degree_id = $degree->id;
1309
 
1310
 
1311
 
1312
 
1313
                    if($userEducation->location_id) {
1314
                        $location = $locationMapper->fetchOne($userEducation->location_id);
1315
                    } else {
1316
                        $location = new Location();
1317
                    }
1318
 
1319
                    $hydrator->hydrate($dataPost,$location);
1320
                    if($userEducation->location_id) {
1321
                        $this->logger->info('Se actualizo un registro de educación del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1322
 
1323
                        $result = $locationMapper->update($location);
1324
                    } else {
1325
                        $this->logger->info('Se agrego un registro de educación del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1326
 
1327
                        $result = $locationMapper->insert($location);
1328
 
1329
                        if($result) {
1330
                            $userEducation->location_id = $location->id;
1331
                        }
1332
                    }
1333
                    if($result) {
1334
                        if($userEducation->id) {
1335
                            $result = $userEducationMapper->update($userEducation);
1336
                        } else {
1337
                            $result =  $userEducationMapper->insert($userEducation);
1338
                        }
1339
                    }
1340
 
1341
 
1342
 
1343
                } else {
1344
                    $messages = [];
1345
                    $form_messages = (array) $form->getMessages();
1346
                    foreach($form_messages  as $fieldname => $field_messages)
1347
                    {
1348
                        $messages[$fieldname] = array_values($field_messages);
1349
                    }
1350
 
1351
                    return new JsonModel([
1352
                        'success'   => false,
1353
                        'data'   => $messages
1354
                    ]);
1355
                }
1356
            }
1357
 
1358
            if($result) {
1359
                $degreeMapper = DegreeMapper::getInstance($this->adapter);
1360
                $userEducations = $userEducationMapper->fetchAllByUserProfileId($userProfile->id);
1361
 
1362
                foreach($userEducations  as &$userEducation)
1363
                {
1364
                    $location = $locationMapper->fetchOne($userEducation->location_id);
1365
                    $degree = $degreeMapper->fetchOne($userEducation->degree_id);
1366
 
1367
                    $userEducation = [
1368
                        'university' => $userEducation->university,
1369
                        'degree' => $degree->name,
1370
                        'field_of_study' => $userEducation->field_of_study,
1371
                        'grade_or_percentage' => $userEducation->grade_or_percentage,
1372
                        'formatted_address' => $location->formatted_address,
1373
                        'from_year' => $userEducation->from_year,
1374
                        'to_year' => $userEducation->to_year,
1375
                        'description' => $userEducation->description,
1376
                        'link_edit' => $this->url()->fromRoute('profile/my-profiles/education', [ 'id' => $userProfile->uuid, 'operation' => 'edit', 'user_education_id' => $userEducation->uuid ]),
1377
                        'link_delete' => $this->url()->fromRoute('profile/my-profiles/education', [ 'id' => $userProfile->uuid, 'operation' => 'delete', 'user_education_id' => $userEducation->uuid ]),
1378
                    ];
1379
                }
1380
 
1381
                $response = [
1382
                    'success'   => true,
1383
                    'data' => $userEducations
1384
                ];
1385
            } else {
1386
                $response = [
1387
                    'success'   => false,
1388
                    'data' => 'ERROR_THERE_WAS_AN_ERROR'
1389
                ];
1390
            }
1391
 
1392
 
1393
 
1394
            return new JsonModel($response);
1395
        }
1396
        else if ($request->isGet() && $operation == 'edit') {
1397
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
1398
            $userEducation = $userEducationMapper->fetchOneByUuid($user_education_id);
1399
 
1400
            if(!$userEducation) {
1401
 
1402
                $response = [
1403
                    'success' => false,
1404
                    'data' => 'ERROR_RECORD_NOT_FOUND'
1405
                ];
1406
 
1407
 
1408
            } else if($userProfile->id != $userEducation->user_profile_id) {
1409
                $response = [
1410
                    'success' => false,
1411
                    'data' => 'ERROR_UNAUTHORIZED'
1412
                ];
1413
 
1414
 
1415
            } else {
1416
                $locationMapper = LocationMapper::getInstance($this->adapter);
1417
                $location = $locationMapper->fetchOne($userEducation->location_id);
1418
 
1419
                $hydrator = new ObjectPropertyHydrator();
1420
                $education = $hydrator->extract($userEducation);
1421
 
1422
                $degree = $degreeMapper = DegreeMapper::getInstance($this->adapter);
1423
                $degree = $degreeMapper->fetchOne($education['degree_id']);
1424
                $education['degree_id'] = $degree->uuid;
1425
 
1426
                $location = [
1427
                    'address1' => $location->address1,
1428
                    'address2' => $location->address2,
1429
                    'city1' => $location->city1,
1430
                    'city2' => $location->city2,
1431
                    'country' => $location->country,
1432
                    'formatted_address' => $location->formatted_address,
1433
                    'latitude' => $location->latitude,
1434
                    'longitude' => $location->longitude,
1435
                    'postal_code' => $location->postal_code,
1436
                    'state' => $location->state,
1437
                ];
1438
 
1439
                $response = [
1440
                    'success' => true,
1441
                    'data' => [
1442
                        'location' => $location,
1443
                        'education' => $education,
1444
                    ]
1445
                ];
1446
 
1447
            }
1448
 
1449
            return new JsonModel($response);
1450
        }
1451
 
1452
 
1453
 
1454
 
1455
        $data = [
1456
            'success' => false,
1457
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1458
        ];
1459
 
1460
 
1461
        return new JsonModel($data);
1462
    }
1463
 
1464
    /**
1465
     * Actualización de los registros de la experiencia laboral
1466
     * @return \Laminas\View\Model\JsonModel
1467
     */
1468
    public function  experienceAction()
1469
    {
1470
 
1471
        $user_profile_id    = $this->params()->fromRoute('id');
1472
        $user_experience_id = $this->params()->fromRoute('user_experience_id');
1473
        $operation          = $this->params()->fromRoute('operation');
1474
 
1475
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1476
 
1477
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1478
        if(!$userProfile) {
1479
            $response = [
1480
                'success' => false,
1481
                'data' => 'ERROR_INVALID_PARAMETER'
1482
            ];
1483
 
1484
            return new JsonModel($response);
1485
 
1486
        }
1487
 
1488
        $currentUser = $this->plugin('currentUserPlugin');
1489
        if($currentUser->getUserId() != $userProfile->user_id) {
1490
            $response = [
1491
                'success' => false,
1492
                'data' => 'ERROR_UNAUTHORIZED'
1493
            ];
1494
 
1495
            return new JsonModel($response);
1496
        }
1497
 
1498
 
1499
 
1500
        $request = $this->getRequest();
1501
        if($request->isPost()) {
1502
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
1503
 
1504
            if($operation == 'delete' || $operation == 'edit') {
1505
                $userExperience = $userExperienceMapper->fetchOneByUuid($user_experience_id);
1506
 
1507
                if(!$userExperience) {
1508
 
1509
                    $response = [
1510
                        'success' => false,
1511
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1512
                    ];
1513
 
1514
                    return new JsonModel($response);
1515
                } else if($userProfile->id != $userExperience->user_profile_id) {
1516
                    $response = [
1517
                        'success' => false,
1518
                        'data' => 'ERROR_UNAUTHORIZED'
1519
                    ];
1520
 
1521
                    return new JsonModel($response);
1522
                }
1523
            } else {
1524
                $userExperience = null;
1525
            }
1526
 
1527
            $locationMapper = LocationMapper::getInstance($this->adapter);
1528
            if($operation == 'delete') {
1529
                $this->logger->info('Se borro un registro de experiencia del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1530
 
1531
                $result = $userExperienceMapper->delete($userExperience);
1532
                if($result) {
1533
                    $locationMapper->delete($userExperience->location_id);
1534
                }
1535
 
1536
 
1537
            } else {
1538
 
1539
 
1540
                $form = new ExperienceForm($this->adapter);
1541
                $dataPost = $request->getPost()->toArray();
1542
 
1543
 
1544
                $dataPost['is_current'] = isset($dataPost['is_current']) ? $dataPost['is_current'] : UserExperience::IS_CURRENT_NO;
1545
                if( $dataPost['is_current']  == UserExperience::IS_CURRENT_YES) {
1546
                    $dataPost['to_month'] = 12;
1547
                    $dataPost['to_year'] = date('Y');
1548
                }
1549
 
1550
 
1551
                $form->setData($dataPost);
1552
 
1553
                if($form->isValid()) {
1554
 
1555
 
1556
 
1557
 
1558
                    if(!$userExperience) {
1559
                        $userExperience = new UserExperience();
1560
                        $userExperience->user_id = $userProfile->user_id;
1561
                        $userExperience->user_profile_id = $userProfile->id;
1562
                    }
1563
 
1564
                    $dataPost = (array) $form->getData();
1565
                    $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1566
                    $companySize = $companySizeMapper->fetchOneByUuid($dataPost['company_size_id']);
1567
 
1568
                    $industryMapper = IndustryMapper::getInstance($this->adapter);
1569
                    $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
1570
 
1571
                    $hydrator = new ObjectPropertyHydrator();
1572
                    $hydrator->hydrate($dataPost, $userExperience);
1573
 
1574
                    $userExperience->company_size_id = $companySize->id;
1575
                    $userExperience->industry_id = $industry->id;
1576
 
1577
                    if($userExperience->is_current == UserExperience::IS_CURRENT_YES) {
1578
                        $userExperience->to_month = null;
1579
                        $userExperience->to_year = null;
1580
                    }
1581
 
1582
                    if($userExperience->location_id) {
1583
                        $location = $locationMapper->fetchOne($userExperience->location_id);
1584
                    } else {
1585
                        $location = new Location();
1586
                    }
1587
                    $hydrator->hydrate($dataPost,$location);
1588
 
1589
                    if($userExperience->location_id) {
1590
                        $this->logger->info('Se actualizo un registro de experiencia del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1591
 
1592
                        $result = $locationMapper->update($location);
1593
                    } else {
1594
                        $this->logger->info('Se agrego un registro de experiencia del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1595
 
1596
                        $result = $locationMapper->insert($location);
1597
 
1598
                        if($result) {
1599
                            $userExperience->location_id = $location->id;
1600
                        }
1601
                    }
1602
                    if($result) {
1603
                        if($userExperience->id) {
1604
                            $result = $userExperienceMapper->update($userExperience);
1605
                        } else {
1606
                            $result =  $userExperienceMapper->insert($userExperience);
1607
                        }
1608
                    }
1609
 
1610
 
1611
 
1612
                } else {
1613
                    $messages = [];
1614
                    $form_messages = (array) $form->getMessages();
1615
                    foreach($form_messages  as $fieldname => $field_messages)
1616
                    {
1617
                        $messages[$fieldname] = array_values($field_messages);
1618
                    }
1619
 
1620
                    return new JsonModel([
1621
                        'success'   => false,
1622
                        'data'   => $messages,
1623
                    ]);
1624
                }
1625
            }
1626
 
1627
            if($result) {
1628
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1629
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1630
                $userExperiences = $userExperienceMapper->fetchAllByUserProfileId($userProfile->id);
1631
 
1632
                foreach($userExperiences  as &$userExperience)
1633
                {
1634
                    $location = $locationMapper->fetchOne($userExperience->location_id);
1635
                    $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
1636
                    $industry = $industryMapper->fetchOne($userExperience->industry_id);
1637
 
1638
                    $userExperience = [
1639
                        'company' => $userExperience->company,
1640
                        'industry' => $industry,
1641
                        'size' => $companySize->name . ' (' . $companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee . ') ',
1642
                        'title' => $userExperience->title,
1643
                        'formatted_address' => $location->formatted_address,
1644
                        'from_year' => $userExperience->from_year,
1645
                        'from_month' => $userExperience->from_month,
1646
                        'to_year' => $userExperience->to_year,
1647
                        'to_month' => $userExperience->to_month,
1648
                        'description' => $userExperience->description,
1649
                        'is_current' => $userExperience->is_current,
1650
                        'link_edit' => $this->url()->fromRoute('profile/my-profiles/experience', [ 'id' => $userProfile->uuid, 'operation' => 'edit', 'user_experience_id' => $userExperience->uuid ]),
1651
                        'link_delete' => $this->url()->fromRoute('profile/my-profiles/experience', [ 'id' => $userProfile->uuid, 'operation' => 'delete', 'user_experience_id' => $userExperience->uuid ]),
1652
                    ];
1653
                }
1654
 
1655
                $response = [
1656
                    'success'   => true,
1657
                    'data' => $userExperiences
1658
                ];
1659
            } else {
1660
                $response = [
1661
                    'success'   => false,
1662
                    'data' => 'ERROR_THERE_WAS_AN_ERROR'
1663
                ];
1664
            }
1665
 
1666
            return new JsonModel($response);
1667
        }
1668
        else if ($request->isGet() && $operation == 'edit') {
1669
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
1670
            $userExperience = $userExperienceMapper->fetchOneByUuid($user_experience_id);
1671
 
1672
            if(!$userExperience) {
1673
                $response = [
1674
                    'success' => false,
1675
                    'data' => 'ERROR_RECORD_NOT_FOUND'
1676
                ];
1677
            } else if($userProfile->id != $userExperience->user_profile_id) {
1678
                $response = [
1679
                    'success' => false,
1680
                    'data' => 'ERROR_UNAUTHORIZED'
1681
                ];
1682
            } else {
1683
                $hydrator = new ObjectPropertyHydrator();
1684
                $experience = $hydrator->extract($userExperience);
1685
 
1686
 
1687
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1688
                $industry = $industryMapper->fetchOne($userExperience->industry_id);
1689
 
1690
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1691
                $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
1692
 
1693
                $experience['industry_id'] = $industry->uuid;
1694
                $experience['company_size_id'] = $companySize->uuid;
1695
 
1696
                $locationMapper = LocationMapper::getInstance($this->adapter);
1697
                $location = $locationMapper->fetchOne($userExperience->location_id);
1698
 
1699
                $response = [
1700
                    'success' => true,
1701
                    'data' => [
1702
                        'location' => $hydrator->extract($location),
1703
                        'experience' => $experience
1704
                    ]
1705
                ];
1706
            }
1707
            return new JsonModel($response);
1708
        }
1709
 
1710
        $data = [
1711
            'success' => false,
1712
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1713
        ];
1714
 
1715
 
1716
        return new JsonModel($data);
1717
    }
1718
 
1719
    /**
1720
     * Cambio de la imagen del image del perfil
1721
     * @return \Laminas\View\Model\JsonModel
1722
     */
1723
    public function imageAction()
1724
    {
1725
        $user_profile_id    = $this->params()->fromRoute('id');
1726
        $operation          = $this->params()->fromRoute('operation');
1727
 
1728
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1729
 
1730
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1731
        if(!$userProfile) {
1732
            $response = [
1733
                'success' => false,
1734
                'data' => 'ERROR_INVALID_PARAMETER'
1735
            ];
1736
 
1737
            return new JsonModel($response);
1738
 
1739
        }
1740
 
1741
        $currentUser = $this->plugin('currentUserPlugin');
1742
        $currentUser = $currentUser->getUser();
1743
 
1744
        if($currentUser->id != $userProfile->user_id) {
1745
            $response = [
1746
                'success' => false,
1747
                'data' => 'ERROR_UNAUTHORIZED'
1748
            ];
1749
 
1750
            return new JsonModel($response);
1751
        }
1752
 
1753
 
1754
 
1755
        $request = $this->getRequest();
1756
        if($request->isPost()) {
1757
            $target_path = $this->config['leaderslinked.fullpath.user'] . $currentUser->uuid;
1758
            if($operation == 'delete') {
1759
                $this->logger->info('Se borro el image del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1760
 
1761
                if($userProfile->image) {
1762
                    if(!Image::delete($target_path, $userProfile->image)) {
1763
                        return new JsonModel([
1764
                            'success'   => false,
1765
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1766
                        ]);
1767
                    }
1768
                }
1769
 
1770
                $userProfile->image = '';
1771
                if(!$userProfileMapper->updateImage($userProfile)) {
1772
                    return new JsonModel([
1773
                        'success'   => false,
1774
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1775
                    ]);
1776
                }
1777
 
1778
            } else {
1779
                $form = new ImageForm($this->config);
1780
                $data 	= array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1781
 
1782
                $form->setData($data);
1783
 
1784
                if($form->isValid()) {
1785
 
1786
                    $files = $request->getFiles()->toArray();
1787
                    if(!empty($files['image']['error'])) {
1788
 
1789
                        return new JsonModel([
1790
                            'success'   => false,
1791
                            'data'   =>  'ERROR_UPLOAD_FILE'
1792
                        ]);
1793
 
1794
 
1795
                    }
1796
 
1797
                    if($userProfile->image) {
1798
                        if(!Image::delete($target_path, $userProfile->image)) {
1799
                            return new JsonModel([
1800
                                'success'   => false,
1801
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1802
                            ]);
1803
                        }
1804
                    }
1805
 
1806
                    list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
1807
                    $source             = $files['image']['tmp_name'];
1808
                    $target_filename    = 'user-profile-' . uniqid() . '.png';
1809
                    $crop_to_dimensions = true;
1810
 
1811
                    if(!Image::uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions)) {
1812
                        return new JsonModel([
1813
                            'success'   => false,
1814
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1815
                        ]);
1816
                    }
1817
 
1818
                    $userProfile->image = $target_filename;
1819
                    if(!$userProfileMapper->updateImage($userProfile)) {
1820
                        return new JsonModel([
1821
                            'success'   => false,
1822
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1823
                        ]);
1824
                    }
1825
 
3163 efrain 1826
                    if($userProfile->public == UserProfile::PUBLIC_YES) {
1 www 1827
 
1828
                        $currentUser->image = $target_filename;
1829
 
1830
                        $userMapper = UserMapper::getInstance($this->adapter);
3163 efrain 1831
                        $userMapper->updateImage($currentUser);
1832
 
1 www 1833
                    }
1834
 
1835
                    $this->logger->info('Se actualizo el image del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1836
 
1837
                } else {
1838
                    $messages = [];
1839
                    $form_messages = (array) $form->getMessages();
1840
                    foreach($form_messages  as $fieldname => $field_messages)
1841
                    {
1842
                        $messages[$fieldname] = array_values($field_messages);
1843
                    }
1844
 
1845
                    return new JsonModel([
1846
                        'success'   => false,
1847
                        'data'   => $messages
1848
                    ]);
1849
                }
1850
            }
1851
            return new JsonModel([
1852
                'success'   => true,
1853
                'data' => [
1854
                    'user' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $currentUser->uuid, 'filename' => $currentUser->image]),
1855
                    'profile' => $this->url()->fromRoute('storage', ['type' => 'user-profile', 'code' => $currentUser->uuid, 'filename' => $userProfile->image]),
3163 efrain 1856
                    'update_navbar' =>  $userProfile->public == UserProfile::PUBLIC_YES ? 1 : 0,
1 www 1857
 
1858
                ]
1859
            ]);
1860
        }
1861
 
1862
 
1863
        $data = [
1864
            'success' => false,
1865
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1866
        ];
1867
 
1868
 
1869
        return new JsonModel($data);
1870
    }
1871
 
1872
    /**
1873
     * Cambio de la imagen de fondo superior (cover) del perfil
1874
     * @return \Laminas\View\Model\JsonModel
1875
     */
1876
    public function coverAction()
1877
    {
1878
        $user_profile_id    = $this->params()->fromRoute('id');
1879
        $operation          = $this->params()->fromRoute('operation');
1880
 
1881
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1882
 
1883
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1884
        if(!$userProfile) {
1885
            $response = [
1886
                'success' => false,
1887
                'data' => 'ERROR_INVALID_PARAMETER'
1888
            ];
1889
 
1890
            return new JsonModel($response);
1891
 
1892
        }
1893
 
1894
 
1895
        $currentUser = $this->plugin('currentUserPlugin');
1896
        $currentUser = $currentUser->getUser();
1897
 
1898
        if($currentUser->id != $userProfile->user_id) {
1899
            $response = [
1900
                'success' => false,
1901
                'data' => 'ERROR_UNAUTHORIZED'
1902
            ];
1903
 
1904
            return new JsonModel($response);
1905
        }
1906
 
1907
 
1908
 
1909
        $request = $this->getRequest();
1910
        if($request->isPost()) {
1911
            $target_path = $this->config['leaderslinked.fullpath.user'] . $currentUser->uuid;
1912
 
1913
            if($operation == 'delete') {
1914
                if($userProfile->cover) {
1915
                    if(!Image::delete($target_path, $userProfile->cover)) {
1916
                        return new JsonModel([
1917
                            'success'   => false,
1918
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1919
                        ]);
1920
                    }
1921
                }
1922
 
1923
                $this->logger->info('Se borro el cover del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1924
                $userProfile->cover = '';
1925
 
1926
            } else {
1927
                $form = new CoverForm($this->config);
1928
                $data 	= array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1929
 
1930
 
1931
 
1932
                $form->setData($data);
1933
 
1934
                if($form->isValid()) {
1935
 
1936
                    $files = $request->getFiles()->toArray();
1937
                    if(!empty($files['cover']['error'])) {
1938
 
1939
                        return new JsonModel([
1940
                            'success'   => false,
1941
                            'data'   =>  'ERROR_UPLOAD_FILE'
1942
                        ]);
1943
 
1944
 
1945
                    }
1946
 
1947
 
1948
                    if($userProfile->cover) {
1949
                        if(!Image::delete($target_path, $userProfile->cover)) {
1950
                            return new JsonModel([
1951
                                'success'   => false,
1952
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1953
                            ]);
1954
                        }
1955
                    }
1956
 
1957
                    //echo '$target_path = ' . $target_path . ' cover =  ' . $userProfile->cover;
1958
                    //exit;
1959
 
1960
                    list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.user_cover_size']);
1961
                    $target_filename = 'user-cover-' . uniqid() . '.png';
1962
                    $source = $files['cover']['tmp_name'];
1963
                    $crop_to_dimensions = false;
1964
 
1965
                    if(!Image::uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions )) {
1966
                        return new JsonModel([
1967
                            'success'   => false,
1968
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1969
                        ]);
1970
                    }
1971
 
1972
                    $userProfile->cover = $target_filename;
1973
 
1974
 
1975
                    if(!$userProfileMapper->updateCover($userProfile)) {
1976
                        return new JsonModel([
1977
                            'success'   => false,
1978
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1979
                        ]);
1980
                    }
1981
 
1982
 
1983
                    $this->logger->info('Se actualizo el cover del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1984
 
1985
                } else {
1986
                    $messages = [];
1987
                    $form_messages = (array) $form->getMessages();
1988
                    foreach($form_messages  as $fieldname => $field_messages)
1989
                    {
1990
                        $messages[$fieldname] = array_values($field_messages);
1991
                    }
1992
 
1993
                    return new JsonModel([
1994
                        'success'   => false,
1995
                        'data'   => $messages
1996
                    ]);
1997
                }
1998
            }
1999
            return new JsonModel([
2000
                'success'   => true,
2001
                'data' => $this->url()->fromRoute('storage', ['type' => 'user-cover', 'code' => $currentUser->uuid, 'filename' => $userProfile->cover])
2002
 
2003
            ]);
2004
        }
2005
 
2006
 
2007
        $data = [
2008
            'success' => false,
2009
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
2010
        ];
2011
 
2012
 
2013
        return new JsonModel($data);
2014
    }
2015
 
2016
}