Proyectos de Subversion LeadersLinked - Services

Rev

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

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