Proyectos de Subversion LeadersLinked - Services

Rev

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