Proyectos de Subversion LeadersLinked - Services

Rev

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