Proyectos de Subversion LeadersLinked - Services

Rev

Rev 350 | Rev 383 | 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),
350 www 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]) : '',
1 efrain 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,
350 www 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]),
1 efrain 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,
350 www 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])
1 efrain 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,
348 www 531
                    'image_size_profile' => $image_size_profile,
532
 
350 www 533
                    'link_extended' => $this->url()->fromRoute('profile/my-profiles/extended', ['id' => $userProfile->uuid] ),
534
                    'link_image_upload' => $this->url()->fromRoute('profile/my-profiles/image', ['id' => $userProfile->uuid, 'operation' => 'upload']),
351 www 535
                    'link_image_delete' => $this->url()->fromRoute('profile/my-profiles/image', ['id' => $userProfile->uuid, 'operation' => 'delete']),
350 www 536
                    'link_cover_upload' => $this->url()->fromRoute('profile/my-profiles/cover', ['id' => $userProfile->uuid, 'operation' => 'upload']),
351 www 537
                    'link_cover_delete' => $this->url()->fromRoute('profile/my-profiles/cover', ['id' => $userProfile->uuid, 'operation' => 'delete']),
350 www 538
                    'link_experience_add' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'add']),
539
                    'link_education_add' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'add']),
540
                    'link_language' => $this->url()->fromRoute('profile/my-profiles/language', ['id' => $userProfile->uuid]),
541
                    'link_location' => $this->url()->fromRoute('profile/my-profiles/location', ['id' => $userProfile->uuid]),
542
                    'link_skill' => $this->url()->fromRoute('profile/my-profiles/skill', ['id' => $userProfile->uuid] ),
543
                    'link_social_network' => $this->url()->fromRoute('profile/my-profiles/social-network', ['id' => $userProfile->uuid]),
544
                    'link_aptitude' => $this->url()->fromRoute('profile/my-profiles/aptitude', ['id' => $userProfile->uuid] ),
545
                    'link_hobby_and_interest' => $this->url()->fromRoute('profile/my-profiles/hobby-and-interest', ['id' => $userProfile->uuid]),
348 www 546
 
1 efrain 547
                ];
548
 
549
                return new JsonModel($data);
550
 
551
        } else {
552
            $data = [
553
                'success' => false,
554
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
555
            ];
556
 
557
            return new JsonModel($data);
558
        }
559
 
560
        return new JsonModel($data);
561
    }
562
 
563
    /**
564
     * Actualización de las habilidades
565
     * @return \Laminas\View\Model\JsonModel
566
     */
567
    public function skillAction()
568
    {
569
        $currentUserPlugin = $this->plugin('currentUserPlugin');
570
        $currentUser = $currentUserPlugin->getUser();
571
 
572
        $user_profile_id = $this->params()->fromRoute('id');
573
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
574
 
575
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
576
        if (!$userProfile) {
577
            $response = [
578
                'success' => false,
579
                'data' => 'ERROR_INVALID_PARAMETER'
580
            ];
581
 
582
            return new JsonModel($response);
583
        }
584
 
585
        if ($currentUser->id != $userProfile->user_id) {
586
            $response = [
587
                'success' => false,
588
                'data' => 'ERROR_UNAUTHORIZED'
589
            ];
590
 
591
            return new JsonModel($response);
592
        }
593
 
594
 
595
 
596
        $request = $this->getRequest();
597
        if ($request->isGet()) {
598
            $skillMapper = SkillMapper::getInstance($this->adapter);
599
 
600
 
601
            $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
602
            $userSkills  = $userSkillMapper->fetchAllByUserProfileId($userProfile->id);
603
 
604
            $items = [];
605
            foreach ($userSkills as $userSkill) {
606
                $skill = $skillMapper->fetchOne($userSkill->skill_id);
607
                array_push($items, $skill->uuid);
608
            }
609
 
610
            $data = [
611
                'success' => true,
612
                'data' => $items
613
            ];
614
 
615
            return new JsonModel($data);
616
        } else if ($request->isPost()) {
617
 
618
            $form = new SkillForm($this->adapter);
619
            $dataPost = $request->getPost()->toArray();
620
 
621
            $form->setData($dataPost);
622
 
623
            if ($form->isValid()) {
624
                $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()]);
625
 
626
                $skillMapper = SkillMapper::getInstance($this->adapter);
627
 
628
 
629
                $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
630
                $userSkillMapper->deleteByUserProfileId($userProfile->id);
631
 
632
                $dataPost = (array) $form->getData();
633
                $skills = $dataPost['skills'];
634
                foreach ($skills as $skill_uuid) {
635
 
636
                    $skill = $skillMapper->fetchOneByUuid($skill_uuid);
637
 
638
 
639
 
640
                    $userSkill = new UserSkill();
641
                    $userSkill->user_id = $userProfile->user_id;
642
                    $userSkill->user_profile_id = $userProfile->id;
643
                    $userSkill->skill_id =  $skill->id;
644
 
645
                    $userSkillMapper->insert($userSkill);
646
                }
647
 
648
                $items = [];
649
 
650
                $records = $userSkillMapper->fetchAllByUserProfileId($userProfile->id);
651
                foreach ($records as $record) {
652
                    $skill = $skillMapper->fetchOne($record->skill_id);
653
                    array_push($items,  ['value' => $skill->uuid, 'label' => $skill->name]);
654
                }
655
 
656
                return new JsonModel([
657
                    'success'   => true,
658
                    'data'   => $items
659
                ]);
660
            } else {
661
                $messages = [];
662
                $form_messages = (array) $form->getMessages();
663
                foreach ($form_messages  as $fieldname => $field_messages) {
664
                    $messages[$fieldname] = array_values($field_messages);
665
                }
666
 
667
                return new JsonModel([
668
                    'success'   => false,
669
                    'data'   => $messages
670
                ]);
671
            }
672
        }
673
 
674
 
675
        $data = [
676
            'success' => false,
677
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
678
        ];
679
 
680
 
681
        return new JsonModel($data);
682
    }
683
 
684
    /**
685
     * Actualización de los idiomas
686
     * @return \Laminas\View\Model\JsonModel
687
     */
688
    public function languageAction()
689
    {
690
 
691
        $currentUserPlugin = $this->plugin('currentUserPlugin');
692
        $currentUser = $currentUserPlugin->getUser();
693
 
694
        $user_profile_id = $this->params()->fromRoute('id');
695
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
696
 
697
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
698
        if (!$userProfile) {
699
            $response = [
700
                'success' => false,
701
                'data' => 'ERROR_INVALID_PARAMETER'
702
            ];
703
 
704
            return new JsonModel($response);
705
        }
706
 
707
 
708
        if ($currentUser->id != $userProfile->user_id) {
709
            $response = [
710
                'success' => false,
711
                'data' => 'ERROR_UNAUTHORIZED'
712
            ];
713
 
714
            return new JsonModel($response);
715
        }
716
 
717
 
718
 
719
        $request = $this->getRequest();
720
        if ($request->isGet()) {
721
            $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()]);
722
 
723
            $userLanguageMapper = UserLanguageMapper::getInstance($this->adapter);
724
            $languages  = $userLanguageMapper->fetchAllByUserProfileId($userProfile->id);
725
 
726
            $items = [];
727
            foreach ($languages as $language) {
728
                array_push($items, $language->language_id);
729
            }
730
 
731
            $data = [
732
                'success' => true,
733
                'data' => $items
734
            ];
735
 
736
            return new JsonModel($data);
737
        } else if ($request->isPost()) {
738
 
739
            $form = new LanguageForm($this->adapter);
740
            $dataPost = $request->getPost()->toArray();
741
 
742
            $form->setData($dataPost);
743
 
744
            if ($form->isValid()) {
745
 
746
                $languageMapper = LanguageMapper::getInstance($this->adapter);
747
                $userLanguageMapper = UserLanguageMapper::getInstance($this->adapter);
748
                $userLanguageMapper->deleteByUserProfileId($userProfile->id);
749
 
750
                $dataPost = (array) $form->getData();
751
                $languages = $dataPost['languages'];
752
                foreach ($languages as $language_id) {
753
                    $language = $languageMapper->fetchOne($language_id);
754
 
755
                    $userLanguage = new UserLanguage();
756
                    $userLanguage->user_id = $userProfile->user_id;
757
                    $userLanguage->user_profile_id = $userProfile->id;
758
                    $userLanguage->language_id = $language->id;
759
 
760
                    $userLanguageMapper->insert($userLanguage);
761
                }
762
 
763
                $items = [];
764
                $records = $userLanguageMapper->fetchAllByUserProfileId($userProfile->id);
765
                foreach ($records as $record) {
766
                    $language = $languageMapper->fetchOne($record->language_id);
767
                    array_push($items,  ['value' => $language->id, 'label' => $language->name]);
768
                }
769
 
770
                return new JsonModel([
771
                    'success'   => true,
772
                    'data'   => $items
773
                ]);
774
            } else {
775
                $messages = [];
776
                $form_messages = (array) $form->getMessages();
777
                foreach ($form_messages  as $fieldname => $field_messages) {
778
                    $messages[$fieldname] = array_values($field_messages);
779
                }
780
 
781
                return new JsonModel([
782
                    'success'   => false,
783
                    'data'   => $messages
784
                ]);
785
            }
786
        }
787
 
788
 
789
        $data = [
790
            'success' => false,
791
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
792
        ];
793
 
794
 
795
        return new JsonModel($data);
796
    }
797
 
798
    /**
799
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
800
     * @return \Laminas\View\Model\JsonModel
801
     */
802
    public function extendedAction()
803
    {
804
        $currentUserPlugin = $this->plugin('currentUserPlugin');
805
        $currentUser = $currentUserPlugin->getUser();
806
 
807
 
808
        $user_profile_id = $this->params()->fromRoute('id');
809
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
810
 
811
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
812
        if (!$userProfile) {
813
            $response = [
814
                'success' => false,
815
                'data' => 'ERROR_INVALID_PARAMETER'
816
            ];
817
 
818
            return new JsonModel($response);
819
        }
820
 
821
        if ($currentUser->id != $userProfile->user_id) {
822
            $response = [
823
                'success' => false,
824
                'data' => 'ERROR_UNAUTHORIZED'
825
            ];
826
 
827
            return new JsonModel($response);
828
        }
829
 
830
 
831
 
832
        $request = $this->getRequest();
833
        if ($request->isGet()) {
834
            $data = [
835
                'success' => true,
836
                'data' => [
837
                    'description' => $userProfile->description,
838
                ]
839
            ];
840
 
841
            return new JsonModel($data);
842
        } else if ($request->isPost()) {
843
 
844
 
845
            $form = new ExtendedForm();
846
            $dataPost = $request->getPost()->toArray();
847
 
848
            $form->setData($dataPost);
849
 
850
            if ($form->isValid()) {
851
                $dataPost = (array) $form->getData();
852
 
853
                $hydrator = new ObjectPropertyHydrator();
854
                $hydrator->hydrate($dataPost, $userProfile);
855
 
856
                $userProfileMapper->updateExtended($userProfile);
857
 
858
                $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()]);
859
 
860
                return new JsonModel([
861
                    'success'   => true,
862
                    'data' => [
863
                        'description' => $userProfile->description,
864
                    ]
865
                ]);
866
            } else {
867
                $messages = [];
868
                $form_messages = (array) $form->getMessages();
869
                foreach ($form_messages  as $fieldname => $field_messages) {
870
                    $messages[$fieldname] = array_values($field_messages);
871
                }
872
 
873
                return new JsonModel([
874
                    'success'   => false,
875
                    'data'   => $messages
876
                ]);
877
            }
878
        }
879
 
880
 
881
        $data = [
882
            'success' => false,
883
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
884
        ];
885
 
886
 
887
        return new JsonModel($data);
888
    }
889
 
890
    /**
891
     * Actualización de la ubucación
892
     * @return \Laminas\View\Model\JsonModel
893
     */
894
    public function locationAction()
895
    {
896
        $currentUserPlugin = $this->plugin('currentUserPlugin');
897
        $currentUser = $currentUserPlugin->getUser();
898
 
899
        $user_profile_id = $this->params()->fromRoute('id');
900
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
901
 
902
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
903
        if (!$userProfile) {
904
            $response = [
905
                'success' => false,
906
                'data' => 'ERROR_INVALID_PARAMETER'
907
            ];
908
 
909
            return new JsonModel($response);
910
        }
911
 
912
 
913
        if ($currentUser->id != $userProfile->user_id) {
914
            $response = [
915
                'success' => false,
916
                'data' => 'ERROR_UNAUTHORIZED'
917
            ];
918
 
919
            return new JsonModel($response);
920
        }
921
 
922
 
923
 
924
        $request = $this->getRequest();
925
        if ($request->isPost()) {
926
 
927
            $form = new LocationForm();
928
            $dataPost = $request->getPost()->toArray();
929
 
930
            $form->setData($dataPost);
931
 
932
            if ($form->isValid()) {
933
                $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()]);
934
 
935
                $dataPost = (array) $form->getData();
936
 
937
                $location = new Location();
938
                $hydrator = new ObjectPropertyHydrator();
939
                $hydrator->hydrate($dataPost, $location);
940
 
941
                $location->id = $userProfile->location_id ? $userProfile->location_id : null;
942
 
943
 
944
 
945
                $locationMapper = LocationMapper::getInstance($this->adapter);
946
                if ($userProfile->location_id) {
947
                    $result = $locationMapper->update($location);
948
                } else {
949
                    $result = $locationMapper->insert($location);
950
 
951
                    if ($result) {
952
                        $userProfile->location_id = $location->id;
953
                        $userProfileMapper->updateLocation($userProfile);
954
                    }
955
                }
956
 
957
                if ($result) {
958
                    if ($userProfile->public == UserProfile::PUBLIC_YES) {
959
                        $currentUser->location_id = $location->id;
960
 
961
                        $userMapper = UserMapper::getInstance($this->adapter);
962
                        $userMapper->updateLocation($currentUser);
963
                    }
964
 
965
 
966
                    $response = [
967
                        'success'   => true,
968
                        'data' => [
969
                            'formatted_address' => $location->formatted_address,
970
                            'country' => $location->country,
971
                        ]
972
                    ];
973
                } else {
974
                    $response = [
975
                        'success'   => false,
976
                        'data' => 'ERROR_THERE_WAS_AN_ERROR'
977
                    ];
978
                }
979
 
980
 
981
 
982
                return new JsonModel($response);
983
            } else {
984
                return new JsonModel([
985
                    'success'   => false,
986
                    'data'   =>   'ERROR_PLACED_AUTOCOMPLETE_DOES_NOT_CONTAIN_GEOMETRY'
987
                ]);
988
            }
989
        }
990
 
991
 
992
        $data = [
993
            'success' => false,
994
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
995
        ];
996
 
997
 
998
        return new JsonModel($data);
999
    }
1000
 
1001
    /**
1002
     * Actualización de las redes sociales
1003
     * @return \Laminas\View\Model\JsonModel
1004
     */
1005
    public function socialNetworkAction()
1006
    {
1007
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1008
        $currentUser = $currentUserPlugin->getUser();
1009
 
1010
        $user_profile_id = $this->params()->fromRoute('id');
1011
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1012
 
1013
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1014
        if (!$userProfile) {
1015
            $response = [
1016
                'success' => false,
1017
                'data' => 'ERROR_INVALID_PARAMETER'
1018
            ];
1019
 
1020
            return new JsonModel($response);
1021
        }
1022
 
1023
 
1024
        if ($currentUser->id != $userProfile->user_id) {
1025
            $response = [
1026
                'success' => false,
1027
                'data' => 'ERROR_UNAUTHORIZED'
1028
            ];
1029
 
1030
            return new JsonModel($response);
1031
        }
1032
 
1033
 
1034
 
1035
        $request = $this->getRequest();
1036
        if ($request->isGet()) {
1037
            $data = [
1038
                'success' => true,
1039
                'data' => [
1040
                    'facebook' => $userProfile->facebook,
1041
                    'instagram' => $userProfile->instagram,
1042
                    'twitter' => $userProfile->twitter
1043
                ]
1044
            ];
1045
 
1046
            return new JsonModel($data);
1047
        } else if ($request->isPost()) {
1048
 
1049
            $form = new SocialNetworkForm();
1050
            $dataPost = $request->getPost()->toArray();
1051
 
1052
            $form->setData($dataPost);
1053
 
1054
            if ($form->isValid()) {
1055
                $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()]);
1056
 
1057
                $dataPost = (array) $form->getData();
1058
 
1059
                $hydrator = new ObjectPropertyHydrator();
1060
                $hydrator->hydrate($dataPost, $userProfile);
1061
 
1062
                $userProfileMapper->updateSocialNetwork($userProfile);
1063
                return new JsonModel([
1064
                    'success'   => true,
1065
                    'data' => [
1066
                        'facebook' => $userProfile->facebook,
1067
                        'instagram' => $userProfile->instagram,
1068
                        'twitter' => $userProfile->twitter
1069
                    ]
1070
                ]);
1071
            } else {
1072
                $messages = [];
1073
                $form_messages = (array) $form->getMessages();
1074
                foreach ($form_messages  as $fieldname => $field_messages) {
1075
                    $messages[$fieldname] = array_values($field_messages);
1076
                }
1077
 
1078
                return new JsonModel([
1079
                    'success'   => false,
1080
                    'data'   => $messages
1081
                ]);
1082
            }
1083
        }
1084
 
1085
 
1086
        $data = [
1087
            'success' => false,
1088
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1089
        ];
1090
 
1091
 
1092
        return new JsonModel($data);
1093
    }
1094
 
1095
    /**
1096
     * Actualización de los registros de estudios realizados
1097
     * @return \Laminas\View\Model\JsonModel
1098
     */
1099
    public function  educationAction()
1100
    {
1101
 
1102
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1103
        $currentUser = $currentUserPlugin->getUser();
1104
 
1105
        $user_profile_id    = $this->params()->fromRoute('id');
1106
        $user_education_id  = $this->params()->fromRoute('user_education_id');
1107
        $operation          = $this->params()->fromRoute('operation');
1108
 
1109
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1110
 
1111
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1112
        if (!$userProfile) {
1113
            $response = [
1114
                'success' => false,
1115
                'data' => 'ERROR_INVALID_PARAMETER'
1116
            ];
1117
 
1118
            return new JsonModel($response);
1119
        }
1120
 
1121
 
1122
        if ($currentUser->id != $userProfile->user_id) {
1123
            $response = [
1124
                'success' => false,
1125
                'data' => 'ERROR_UNAUTHORIZED'
1126
            ];
1127
 
1128
            return new JsonModel($response);
1129
        }
1130
 
1131
 
1132
 
1133
        $request = $this->getRequest();
1134
        if ($request->isPost()) {
1135
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
1136
 
1137
            if ($operation == 'delete' || $operation == 'edit') {
1138
                $userEducation = $userEducationMapper->fetchOneByUuid($user_education_id);
1139
 
1140
 
1141
                if (!$userEducation) {
1142
 
1143
                    $response = [
1144
                        'success' => false,
1145
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1146
                    ];
1147
 
1148
                    return new JsonModel($response);
1149
                } else if ($userProfile->id != $userEducation->user_profile_id) {
1150
                    $response = [
1151
                        'success' => false,
1152
                        'data' => 'ERROR_UNAUTHORIZED'
1153
                    ];
1154
 
1155
                    return new JsonModel($response);
1156
                }
1157
            } else {
1158
                $userEducation = null;
1159
            }
1160
 
1161
            $locationMapper = LocationMapper::getInstance($this->adapter);
1162
            if ($operation == 'delete') {
1163
                $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()]);
1164
 
1165
                $result = $userEducationMapper->delete($userEducation);
1166
                if ($result) {
1167
                    $locationMapper->delete($userEducation->location_id);
1168
                }
1169
            } else {
1170
 
1171
 
1172
                $form = new EducationForm($this->adapter);
1173
                $dataPost = $request->getPost()->toArray();
1174
 
1175
                $form->setData($dataPost);
1176
 
1177
                if ($form->isValid()) {
1178
 
1179
                    if (!$userEducation) {
1180
                        $userEducation = new UserEducation();
1181
                        $userEducation->user_id = $userProfile->user_id;
1182
                        $userEducation->user_profile_id = $userProfile->id;
1183
                    }
1184
 
1185
                    $dataPost = (array) $form->getData();
1186
 
1187
                    $hydrator = new ObjectPropertyHydrator();
1188
                    $hydrator->hydrate($dataPost, $userEducation);
1189
 
1190
                    $degreeMapper = DegreeMapper::getInstance($this->adapter);
1191
                    $degree = $degreeMapper->fetchOneByUuid($dataPost['degree_id']);
1192
                    $userEducation->degree_id = $degree->id;
1193
 
1194
 
1195
 
1196
 
1197
                    if ($userEducation->location_id) {
1198
                        $location = $locationMapper->fetchOne($userEducation->location_id);
1199
                    } else {
1200
                        $location = new Location();
1201
                    }
1202
 
1203
                    $hydrator->hydrate($dataPost, $location);
1204
                    if ($userEducation->location_id) {
1205
                        $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()]);
1206
 
1207
                        $result = $locationMapper->update($location);
1208
                    } else {
1209
                        $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()]);
1210
 
1211
                        $result = $locationMapper->insert($location);
1212
 
1213
                        if ($result) {
1214
                            $userEducation->location_id = $location->id;
1215
                        }
1216
                    }
1217
                    if ($result) {
1218
                        if ($userEducation->id) {
1219
                            $result = $userEducationMapper->update($userEducation);
1220
                        } else {
1221
                            $result =  $userEducationMapper->insert($userEducation);
1222
                        }
1223
                    }
1224
                } else {
1225
                    $messages = [];
1226
                    $form_messages = (array) $form->getMessages();
1227
                    foreach ($form_messages  as $fieldname => $field_messages) {
1228
                        $messages[$fieldname] = array_values($field_messages);
1229
                    }
1230
 
1231
                    return new JsonModel([
1232
                        'success'   => false,
1233
                        'data'   => $messages
1234
                    ]);
1235
                }
1236
            }
1237
 
1238
            if ($result) {
1239
                $degreeMapper = DegreeMapper::getInstance($this->adapter);
1240
                $userEducations = $userEducationMapper->fetchAllByUserProfileId($userProfile->id);
1241
 
1242
                foreach ($userEducations  as &$userEducation) {
1243
                    $location = $locationMapper->fetchOne($userEducation->location_id);
1244
                    $degree = $degreeMapper->fetchOne($userEducation->degree_id);
1245
 
1246
                    $userEducation = [
1247
                        'university' => $userEducation->university,
1248
                        'degree' => $degree->name,
1249
                        'field_of_study' => $userEducation->field_of_study,
1250
                        'grade_or_percentage' => $userEducation->grade_or_percentage,
1251
                        'formatted_address' => $location->formatted_address,
1252
                        'from_year' => $userEducation->from_year,
1253
                        'to_year' => $userEducation->to_year,
1254
                        'description' => $userEducation->description,
350 www 1255
                        'link_edit' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'edit', 'user_education_id' => $userEducation->uuid]),
1256
                        'link_delete' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'delete', 'user_education_id' => $userEducation->uuid]),
1 efrain 1257
                    ];
1258
                }
1259
 
1260
                $response = [
1261
                    'success'   => true,
1262
                    'data' => $userEducations
1263
                ];
1264
            } else {
1265
                $response = [
1266
                    'success'   => false,
1267
                    'data' => 'ERROR_THERE_WAS_AN_ERROR'
1268
                ];
1269
            }
1270
 
1271
 
1272
 
1273
            return new JsonModel($response);
1274
        } else if ($request->isGet() && $operation == 'edit') {
1275
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
1276
            $userEducation = $userEducationMapper->fetchOneByUuid($user_education_id);
1277
 
1278
            if (!$userEducation) {
1279
 
1280
                $response = [
1281
                    'success' => false,
1282
                    'data' => 'ERROR_RECORD_NOT_FOUND'
1283
                ];
1284
            } else if ($userProfile->id != $userEducation->user_profile_id) {
1285
                $response = [
1286
                    'success' => false,
1287
                    'data' => 'ERROR_UNAUTHORIZED'
1288
                ];
1289
            } else {
1290
                $locationMapper = LocationMapper::getInstance($this->adapter);
1291
                $location = $locationMapper->fetchOne($userEducation->location_id);
1292
 
1293
                $hydrator = new ObjectPropertyHydrator();
1294
                $education = $hydrator->extract($userEducation);
1295
 
1296
                $degree = $degreeMapper = DegreeMapper::getInstance($this->adapter);
1297
                $degree = $degreeMapper->fetchOne($education['degree_id']);
1298
                $education['degree_id'] = $degree->uuid;
1299
 
1300
                $location = [
1301
                    'address1' => $location->address1,
1302
                    'address2' => $location->address2,
1303
                    'city1' => $location->city1,
1304
                    'city2' => $location->city2,
1305
                    'country' => $location->country,
1306
                    'formatted_address' => $location->formatted_address,
1307
                    'latitude' => $location->latitude,
1308
                    'longitude' => $location->longitude,
1309
                    'postal_code' => $location->postal_code,
1310
                    'state' => $location->state,
1311
                ];
1312
 
1313
                $response = [
1314
                    'success' => true,
1315
                    'data' => [
1316
                        'location' => $location,
1317
                        'education' => $education,
1318
                    ]
1319
                ];
1320
            }
1321
 
1322
            return new JsonModel($response);
1323
        }
1324
 
1325
 
1326
 
1327
 
1328
        $data = [
1329
            'success' => false,
1330
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1331
        ];
1332
 
1333
 
1334
        return new JsonModel($data);
1335
    }
1336
 
1337
    /**
1338
     * Actualización de los registros de la experiencia laboral
1339
     * @return \Laminas\View\Model\JsonModel
1340
     */
1341
    public function  experienceAction()
1342
    {
1343
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1344
        $currentUser = $currentUserPlugin->getUser();
1345
 
1346
        $user_profile_id    = $this->params()->fromRoute('id');
1347
        $user_experience_id = $this->params()->fromRoute('user_experience_id');
1348
        $operation          = $this->params()->fromRoute('operation');
1349
 
1350
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1351
 
1352
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1353
        if (!$userProfile) {
1354
            $response = [
1355
                'success' => false,
1356
                'data' => 'ERROR_INVALID_PARAMETER'
1357
            ];
1358
 
1359
            return new JsonModel($response);
1360
        }
1361
 
1362
        if ($currentUser->id != $userProfile->user_id) {
1363
            $response = [
1364
                'success' => false,
1365
                'data' => 'ERROR_UNAUTHORIZED'
1366
            ];
1367
 
1368
            return new JsonModel($response);
1369
        }
1370
 
1371
 
1372
 
1373
        $request = $this->getRequest();
1374
        if ($request->isPost()) {
1375
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
1376
 
1377
            if ($operation == 'delete' || $operation == 'edit') {
1378
                $userExperience = $userExperienceMapper->fetchOneByUuid($user_experience_id);
1379
 
1380
                if (!$userExperience) {
1381
 
1382
                    $response = [
1383
                        'success' => false,
1384
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1385
                    ];
1386
 
1387
                    return new JsonModel($response);
1388
                } else if ($userProfile->id != $userExperience->user_profile_id) {
1389
                    $response = [
1390
                        'success' => false,
1391
                        'data' => 'ERROR_UNAUTHORIZED'
1392
                    ];
1393
 
1394
                    return new JsonModel($response);
1395
                }
1396
            } else {
1397
                $userExperience = null;
1398
            }
1399
 
1400
            $locationMapper = LocationMapper::getInstance($this->adapter);
1401
            if ($operation == 'delete') {
1402
                $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()]);
1403
 
1404
                $result = $userExperienceMapper->delete($userExperience);
1405
                if ($result) {
1406
                    $locationMapper->delete($userExperience->location_id);
1407
                }
1408
            } else {
1409
 
1410
 
1411
                $form = new ExperienceForm($this->adapter);
1412
                $dataPost = $request->getPost()->toArray();
1413
 
1414
 
1415
                $dataPost['is_current'] = isset($dataPost['is_current']) ? $dataPost['is_current'] : UserExperience::IS_CURRENT_NO;
1416
                if ($dataPost['is_current']  == UserExperience::IS_CURRENT_YES) {
1417
                    $dataPost['to_month'] = 12;
1418
                    $dataPost['to_year'] = date('Y');
1419
                }
1420
 
1421
 
1422
                $form->setData($dataPost);
1423
 
1424
                if ($form->isValid()) {
1425
 
1426
 
1427
 
1428
 
1429
                    if (!$userExperience) {
1430
                        $userExperience = new UserExperience();
1431
                        $userExperience->user_id = $userProfile->user_id;
1432
                        $userExperience->user_profile_id = $userProfile->id;
1433
                    }
1434
 
1435
                    $dataPost = (array) $form->getData();
1436
                    $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1437
                    $companySize = $companySizeMapper->fetchOneByUuid($dataPost['company_size_id']);
1438
 
1439
                    $industryMapper = IndustryMapper::getInstance($this->adapter);
1440
                    $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
1441
 
1442
                    $hydrator = new ObjectPropertyHydrator();
1443
                    $hydrator->hydrate($dataPost, $userExperience);
1444
 
1445
                    $userExperience->company_size_id = $companySize->id;
1446
                    $userExperience->industry_id = $industry->id;
1447
 
1448
                    if ($userExperience->is_current == UserExperience::IS_CURRENT_YES) {
1449
                        $userExperience->to_month = null;
1450
                        $userExperience->to_year = null;
1451
                    }
1452
 
1453
                    if ($userExperience->location_id) {
1454
                        $location = $locationMapper->fetchOne($userExperience->location_id);
1455
                    } else {
1456
                        $location = new Location();
1457
                    }
1458
                    $hydrator->hydrate($dataPost, $location);
1459
 
1460
                    if ($userExperience->location_id) {
1461
                        $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()]);
1462
 
1463
                        $result = $locationMapper->update($location);
1464
                    } else {
1465
                        $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()]);
1466
 
1467
                        $result = $locationMapper->insert($location);
1468
 
1469
                        if ($result) {
1470
                            $userExperience->location_id = $location->id;
1471
                        }
1472
                    }
1473
                    if ($result) {
1474
                        if ($userExperience->id) {
1475
                            $result = $userExperienceMapper->update($userExperience);
1476
                        } else {
1477
                            $result =  $userExperienceMapper->insert($userExperience);
1478
                        }
1479
                    }
1480
                } else {
1481
                    $messages = [];
1482
                    $form_messages = (array) $form->getMessages();
1483
                    foreach ($form_messages  as $fieldname => $field_messages) {
1484
                        $messages[$fieldname] = array_values($field_messages);
1485
                    }
1486
 
1487
                    return new JsonModel([
1488
                        'success'   => false,
1489
                        'data'   => $messages,
1490
                    ]);
1491
                }
1492
            }
1493
 
1494
            if ($result) {
1495
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1496
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1497
                $userExperiences = $userExperienceMapper->fetchAllByUserProfileId($userProfile->id);
1498
 
1499
                foreach ($userExperiences  as &$userExperience) {
1500
                    $location = $locationMapper->fetchOne($userExperience->location_id);
1501
                    $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
1502
                    $industry = $industryMapper->fetchOne($userExperience->industry_id);
1503
 
1504
                    $userExperience = [
1505
                        'company' => $userExperience->company,
1506
                        'industry' => $industry,
1507
                        'size' => $companySize->name . ' (' . $companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee . ') ',
1508
                        'title' => $userExperience->title,
1509
                        'formatted_address' => $location->formatted_address,
1510
                        'from_year' => $userExperience->from_year,
1511
                        'from_month' => $userExperience->from_month,
1512
                        'to_year' => $userExperience->to_year,
1513
                        'to_month' => $userExperience->to_month,
1514
                        'description' => $userExperience->description,
1515
                        'is_current' => $userExperience->is_current,
350 www 1516
                        'link_edit' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'edit', 'user_experience_id' => $userExperience->uuid]),
1517
                        'link_delete' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'delete', 'user_experience_id' => $userExperience->uuid]),
1 efrain 1518
                    ];
1519
                }
1520
 
1521
                $response = [
1522
                    'success'   => true,
1523
                    'data' => $userExperiences
1524
                ];
1525
            } else {
1526
                $response = [
1527
                    'success'   => false,
1528
                    'data' => 'ERROR_THERE_WAS_AN_ERROR'
1529
                ];
1530
            }
1531
 
1532
            return new JsonModel($response);
1533
        } else if ($request->isGet() && $operation == 'edit') {
1534
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
1535
            $userExperience = $userExperienceMapper->fetchOneByUuid($user_experience_id);
1536
 
1537
            if (!$userExperience) {
1538
                $response = [
1539
                    'success' => false,
1540
                    'data' => 'ERROR_RECORD_NOT_FOUND'
1541
                ];
1542
            } else if ($userProfile->id != $userExperience->user_profile_id) {
1543
                $response = [
1544
                    'success' => false,
1545
                    'data' => 'ERROR_UNAUTHORIZED'
1546
                ];
1547
            } else {
1548
                $hydrator = new ObjectPropertyHydrator();
1549
                $experience = $hydrator->extract($userExperience);
1550
 
1551
 
1552
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1553
                $industry = $industryMapper->fetchOne($userExperience->industry_id);
1554
 
1555
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1556
                $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
1557
 
1558
                $experience['industry_id'] = $industry->uuid;
1559
                $experience['company_size_id'] = $companySize->uuid;
1560
 
1561
                $locationMapper = LocationMapper::getInstance($this->adapter);
1562
                $location = $locationMapper->fetchOne($userExperience->location_id);
1563
 
1564
                $response = [
1565
                    'success' => true,
1566
                    'data' => [
1567
                        'location' => $hydrator->extract($location),
1568
                        'experience' => $experience
1569
                    ]
1570
                ];
1571
            }
1572
            return new JsonModel($response);
1573
        }
1574
 
1575
        $data = [
1576
            'success' => false,
1577
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1578
        ];
1579
 
1580
 
1581
        return new JsonModel($data);
1582
    }
1583
 
1584
    /**
1585
     * Cambio de la imagen del image del perfil
1586
     * @return \Laminas\View\Model\JsonModel
1587
     */
1588
    public function imageAction()
1589
    {
1590
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1591
        $currentUser = $currentUserPlugin->getUser();
1592
 
1593
        $user_profile_id    = $this->params()->fromRoute('id');
1594
        $operation          = $this->params()->fromRoute('operation');
1595
 
1596
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1597
 
1598
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1599
        if (!$userProfile) {
1600
            $response = [
1601
                'success' => false,
1602
                'data' => 'ERROR_INVALID_PARAMETER'
1603
            ];
1604
 
1605
            return new JsonModel($response);
1606
        }
1607
 
1608
        if ($currentUser->id != $userProfile->user_id) {
1609
            $response = [
1610
                'success' => false,
1611
                'data' => 'ERROR_UNAUTHORIZED'
1612
            ];
1613
 
1614
            return new JsonModel($response);
1615
        }
1616
 
1617
 
1618
 
1619
        $request = $this->getRequest();
1620
        if ($request->isPost()) {
283 www 1621
 
346 www 1622
            $storage = Storage::getInstance($this->config, $this->adapter);
1623
            $target_path = $storage->getPathUser();
1624
 
1 efrain 1625
            if ($operation == 'delete') {
1626
                $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()]);
1627
 
1628
                if ($userProfile->image) {
346 www 1629
                    if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->image)) {
1 efrain 1630
                        return new JsonModel([
1631
                            'success'   => false,
1632
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1633
                        ]);
1634
                    }
1635
                }
1636
 
1637
                $userProfile->image = '';
1638
                if (!$userProfileMapper->updateImage($userProfile)) {
1639
                    return new JsonModel([
1640
                        'success'   => false,
1641
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1642
                    ]);
1643
                }
1644
            } else {
1645
                $form = new ImageForm($this->config);
1646
                $data     = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1647
 
1648
                $form->setData($data);
1649
 
1650
                if ($form->isValid()) {
1651
 
346 www 1652
                    $storage->setFiles($request->getFiles()->toArray());
1653
 
1654
                    if (!$storage->setCurrentFilename('error')) {
1 efrain 1655
 
1656
                        return new JsonModel([
1657
                            'success'   => false,
1658
                            'data'   =>  'ERROR_UPLOAD_FILE'
1659
                        ]);
1660
                    }
1661
 
346 www 1662
 
1 efrain 1663
 
1664
                    list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
346 www 1665
                    $source_filename    = $storage->getTmpFilename();
1666
                    $filename           = 'user-profile-' . uniqid() . '.png';
1667
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->uuid, $filename);
1668
 
1 efrain 1669
 
346 www 1670
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
1 efrain 1671
                        return new JsonModel([
1672
                            'success'   => false,
1673
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1674
                        ]);
1675
                    }
346 www 1676
 
1677
                    if ($userProfile->image) {
1678
                        if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->image)) {
1679
                            return new JsonModel([
1680
                                'success'   => false,
1681
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1682
                            ]);
1683
                        }
1684
                    }
1 efrain 1685
 
346 www 1686
                    $userProfile->image = $filename;
1 efrain 1687
                    if (!$userProfileMapper->updateImage($userProfile)) {
1688
                        return new JsonModel([
1689
                            'success'   => false,
1690
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1691
                        ]);
1692
                    }
1693
 
1694
                    if ($userProfile->public == UserProfile::PUBLIC_YES) {
283 www 1695
 
346 www 1696
                        $filename    = 'user-' . uniqid() . '.png';
1697
                        $source_filename = $target_filename;
1698
                        $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->uuid, $filename);
1699
 
283 www 1700
 
346 www 1701
                        if (!$storage->copyFile($source_filename, $target_filename)) {
283 www 1702
                            return new JsonModel([
1703
                                'success'   => false,
1704
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1705
                            ]);
1706
                        }
346 www 1707
 
1708
                        if ($currentUser->image) {
1709
                            if (!$storage->deleteFile($target_path, $currentUser->uuid, $currentUser->image)) {
1710
                                return new JsonModel([
1711
                                    'success'   => false,
1712
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1713
                                ]);
1714
                            }
1715
                        }
1 efrain 1716
 
346 www 1717
                        $currentUser->image = $filename;
1 efrain 1718
 
1719
                        $userMapper = UserMapper::getInstance($this->adapter);
1720
                        $userMapper->updateImage($currentUser);
1721
                    }
1722
 
1723
                    $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()]);
1724
                } else {
1725
                    $messages = [];
1726
                    $form_messages = (array) $form->getMessages();
1727
                    foreach ($form_messages  as $fieldname => $field_messages) {
1728
                        $messages[$fieldname] = array_values($field_messages);
1729
                    }
1730
 
1731
                    return new JsonModel([
1732
                        'success'   => false,
1733
                        'data'   => $messages
1734
                    ]);
1735
                }
1736
            }
283 www 1737
 
333 www 1738
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1739
 
1 efrain 1740
            return new JsonModel([
1741
                'success'   => true,
1742
                'data' => [
283 www 1743
                    'user' => $storage->getUserImage($currentUser),
1744
                    'profile' => $storage->getUserProfileImage($currentUser, $userProfile),
1 efrain 1745
                    'update_navbar' =>  $userProfile->public == UserProfile::PUBLIC_YES ? 1 : 0,
1746
 
1747
                ]
1748
            ]);
1749
        }
1750
 
1751
 
1752
        $data = [
1753
            'success' => false,
1754
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1755
        ];
1756
 
1757
 
1758
        return new JsonModel($data);
1759
    }
1760
 
1761
    /**
1762
     * Cambio de la imagen de fondo superior (cover) del perfil
1763
     * @return \Laminas\View\Model\JsonModel
1764
     */
1765
    public function coverAction()
1766
    {
1767
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1768
        $currentUser = $currentUserPlugin->getUser();
1769
 
1770
        $user_profile_id    = $this->params()->fromRoute('id');
1771
        $operation          = $this->params()->fromRoute('operation');
1772
 
1773
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1774
 
1775
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1776
        if (!$userProfile) {
1777
            $response = [
1778
                'success' => false,
1779
                'data' => 'ERROR_INVALID_PARAMETER'
1780
            ];
1781
 
1782
            return new JsonModel($response);
1783
        }
1784
 
1785
 
1786
 
1787
        if ($currentUser->id != $userProfile->user_id) {
1788
            $response = [
1789
                'success' => false,
1790
                'data' => 'ERROR_UNAUTHORIZED'
1791
            ];
1792
 
1793
            return new JsonModel($response);
1794
        }
1795
 
1796
 
1797
 
1798
        $request = $this->getRequest();
1799
        if ($request->isPost()) {
346 www 1800
            $storage = Storage::getInstance($this->config, $this->adapter);
1801
            $target_path = $storage->getPathUser();
283 www 1802
 
1 efrain 1803
            if ($operation == 'delete') {
1804
                if ($userProfile->cover) {
346 www 1805
                    if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->cover)) {
1 efrain 1806
                        return new JsonModel([
1807
                            'success'   => false,
1808
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1809
                        ]);
1810
                    }
1811
                }
1812
 
1813
                $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()]);
1814
                $userProfile->cover = '';
1815
            } else {
1816
                $form = new CoverForm($this->config);
1817
                $data     = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1818
 
1819
 
1820
 
1821
                $form->setData($data);
1822
 
1823
                if ($form->isValid()) {
1824
 
346 www 1825
                    $storage->setFiles($request->getFiles()->toArray());
1826
                    if (!$storage->setCurrentFilename('error')) {
1 efrain 1827
 
1828
                        return new JsonModel([
1829
                            'success'   => false,
1830
                            'data'   =>  'ERROR_UPLOAD_FILE'
1831
                        ]);
1832
                    }
1833
 
1834
 
346 www 1835
 
1 efrain 1836
 
1837
                    //echo '$target_path = ' . $target_path . ' cover =  ' . $userProfile->cover;
1838
                    //exit;
1839
 
1840
                    list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.user_cover_size']);
346 www 1841
                    $filename           = 'user-cover-' . uniqid() . '.png';
1842
                    $source_filename    = $storage->getTmpFilename();
1843
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->id, $filename);
1 efrain 1844
 
346 www 1845
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
1 efrain 1846
                        return new JsonModel([
1847
                            'success'   => false,
1848
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1849
                        ]);
1850
                    }
346 www 1851
 
1852
                    if ($userProfile->cover) {
1853
                        if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->cover)) {
1854
                            return new JsonModel([
1855
                                'success'   => false,
1856
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1857
                            ]);
1858
                        }
1859
                    }
1 efrain 1860
 
346 www 1861
                    $userProfile->cover = $filename;
1 efrain 1862
                    if (!$userProfileMapper->updateCover($userProfile)) {
1863
                        return new JsonModel([
1864
                            'success'   => false,
1865
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1866
                        ]);
1867
                    }
1868
 
1869
 
1870
                    $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()]);
1871
                } else {
1872
                    $messages = [];
1873
                    $form_messages = (array) $form->getMessages();
1874
                    foreach ($form_messages  as $fieldname => $field_messages) {
1875
                        $messages[$fieldname] = array_values($field_messages);
1876
                    }
1877
 
1878
                    return new JsonModel([
1879
                        'success'   => false,
1880
                        'data'   => $messages
1881
                    ]);
1882
                }
1883
            }
283 www 1884
 
333 www 1885
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1886
 
1 efrain 1887
            return new JsonModel([
1888
                'success'   => true,
283 www 1889
                'data' => $storage->getUserProfileCover($currentUser, $userProfile)
1 efrain 1890
 
1891
            ]);
1892
        }
1893
 
1894
 
1895
        $data = [
1896
            'success' => false,
1897
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1898
        ];
1899
 
1900
 
1901
        return new JsonModel($data);
1902
    }
1903
 
1904
    /**
1905
     * Actualización de las habilidades
1906
     * @return \Laminas\View\Model\JsonModel
1907
     */
1908
    public function aptitudeAction()
1909
    {
1910
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1911
        $currentUser = $currentUserPlugin->getUser();
1912
 
1913
        $user_profile_id = $this->params()->fromRoute('id');
1914
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1915
 
1916
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1917
        if (!$userProfile) {
1918
            $response = [
1919
                'success' => false,
1920
                'data' => 'ERROR_INVALID_PARAMETER'
1921
            ];
1922
 
1923
            return new JsonModel($response);
1924
        }
1925
 
1926
        if ($currentUser->id != $userProfile->user_id) {
1927
            $response = [
1928
                'success' => false,
1929
                'data' => 'ERROR_UNAUTHORIZED'
1930
            ];
1931
 
1932
            return new JsonModel($response);
1933
        }
1934
 
1935
 
1936
 
1937
        $request = $this->getRequest();
1938
        if ($request->isGet()) {
1939
            $aptitudeMapper = AptitudeMapper::getInstance($this->adapter);
1940
 
1941
 
1942
            $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
1943
            $userAptitudes  = $userAptitudeMapper->fetchAllByUserProfileId($userProfile->id);
1944
 
1945
            $items = [];
1946
            foreach ($userAptitudes as $userAptitude) {
1947
                $aptitude = $aptitudeMapper->fetchOne($userAptitude->aptitude_id);
1948
                array_push($items, $aptitude->uuid);
1949
            }
1950
 
1951
            $data = [
1952
                'success' => true,
1953
                'data' => $items
1954
            ];
1955
 
1956
            return new JsonModel($data);
1957
        } else if ($request->isPost()) {
1958
 
1959
            $form = new AptitudeForm($this->adapter);
1960
            $dataPost = $request->getPost()->toArray();
1961
 
1962
            $form->setData($dataPost);
1963
 
1964
            if ($form->isValid()) {
1965
                $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()]);
1966
 
1967
                $aptitudeMapper = AptitudeMapper::getInstance($this->adapter);
1968
 
1969
 
1970
                $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
1971
                $userAptitudeMapper->deleteByUserProfileId($userProfile->id);
1972
 
1973
                $dataPost = (array) $form->getData();
1974
                $aptitudes = $dataPost['aptitudes'];
1975
                foreach ($aptitudes as $aptitude_uuid) {
1976
 
1977
                    $aptitude = $aptitudeMapper->fetchOneByUuid($aptitude_uuid);
1978
 
1979
 
1980
                    $userAptitude = new UserAptitude();
1981
                    $userAptitude->user_id = $userProfile->user_id;
1982
                    $userAptitude->user_profile_id = $userProfile->id;
1983
                    $userAptitude->aptitude_id =  $aptitude->id;
1984
 
1985
                    $userAptitudeMapper->insert($userAptitude);
1986
                }
1987
 
1988
                $items = [];
1989
 
1990
                $records = $userAptitudeMapper->fetchAllByUserProfileId($userProfile->id);
1991
                foreach ($records as $record) {
1992
                    $aptitude = $aptitudeMapper->fetchOne($record->aptitude_id);
1993
                    array_push($items,  ['value' => $aptitude->uuid, 'label' => $aptitude->name]);
1994
                }
1995
 
1996
                return new JsonModel([
1997
                    'success'   => true,
1998
                    'data'   => $items
1999
                ]);
2000
            } else {
2001
                $messages = [];
2002
                $form_messages = (array) $form->getMessages();
2003
                foreach ($form_messages  as $fieldname => $field_messages) {
2004
                    $messages[$fieldname] = array_values($field_messages);
2005
                }
2006
 
2007
                return new JsonModel([
2008
                    'success'   => false,
2009
                    'data'   => $messages
2010
                ]);
2011
            }
2012
 
2013
            $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
2014
        }
2015
 
2016
 
2017
        $data = [
2018
            'success' => false,
2019
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
2020
        ];
2021
 
2022
 
2023
        return new JsonModel($data);
2024
    }
2025
 
2026
    /**
2027
     * Actualización de las habilidades
2028
     * @return \Laminas\View\Model\JsonModel
2029
     */
2030
    public function hobbyAndInterestAction()
2031
    {
2032
        $currentUserPlugin = $this->plugin('currentUserPlugin');
2033
        $currentUser = $currentUserPlugin->getUser();
2034
 
2035
        $user_profile_id = $this->params()->fromRoute('id');
2036
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
2037
 
2038
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
2039
        if (!$userProfile) {
2040
            $response = [
2041
                'success' => false,
2042
                'data' => 'ERROR_INVALID_PARAMETER'
2043
            ];
2044
 
2045
            return new JsonModel($response);
2046
        }
2047
 
2048
        if ($currentUser->id != $userProfile->user_id) {
2049
            $response = [
2050
                'success' => false,
2051
                'data' => 'ERROR_UNAUTHORIZED'
2052
            ];
2053
 
2054
            return new JsonModel($response);
2055
        }
2056
 
2057
 
2058
 
2059
        $request = $this->getRequest();
2060
        if ($request->isGet()) {
2061
            $hobbyAndInterestMapper = HobbyAndInterestMapper::getInstance($this->adapter);
2062
 
2063
 
2064
            $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
2065
            $userHobbyAndInterests  = $userHobbyAndInterestMapper->fetchAllByUserProfileId($userProfile->id);
2066
 
2067
            $items = [];
2068
            foreach ($userHobbyAndInterests as $userHobbyAndInterest) {
2069
                $hobbyAndInterest = $hobbyAndInterestMapper->fetchOne($userHobbyAndInterest->hobbyAndInterest_id);
2070
                array_push($items, $hobbyAndInterest->uuid);
2071
            }
2072
 
2073
            $data = [
2074
                'success' => true,
2075
                'data' => $items
2076
            ];
2077
 
2078
            return new JsonModel($data);
2079
        } else if ($request->isPost()) {
2080
 
2081
            $form = new HobbyAndInterestForm($this->adapter);
2082
            $dataPost = $request->getPost()->toArray();
2083
 
2084
 
2085
            $form->setData($dataPost);
2086
 
2087
            if ($form->isValid()) {
2088
                $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()]);
2089
 
2090
                $hobbyAndInterestMapper = HobbyAndInterestMapper::getInstance($this->adapter);
2091
 
2092
 
2093
                $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
2094
                $userHobbyAndInterestMapper->deleteByUserProfileId($userProfile->id);
2095
 
2096
                $dataPost = (array) $form->getData();
2097
                $hobbyAndInterests = $dataPost['hobbies_and_interests'];
2098
                foreach ($hobbyAndInterests as $hobbyAndInterest_uuid) {
2099
 
2100
                    $hobbyAndInterest = $hobbyAndInterestMapper->fetchOneByUuid($hobbyAndInterest_uuid);
2101
 
2102
 
2103
                    $userHobbyAndInterest = new UserHobbyAndInterest();
2104
                    $userHobbyAndInterest->user_id = $userProfile->user_id;
2105
                    $userHobbyAndInterest->user_profile_id = $userProfile->id;
2106
                    $userHobbyAndInterest->hobby_and_interest_id =  $hobbyAndInterest->id;
2107
 
2108
                    $userHobbyAndInterestMapper->insert($userHobbyAndInterest);
2109
                }
2110
 
2111
                $items = [];
2112
 
2113
                $records = $userHobbyAndInterestMapper->fetchAllByUserProfileId($userProfile->id);
2114
                foreach ($records as $record) {
2115
                    $hobbyAndInterest = $hobbyAndInterestMapper->fetchOne($record->hobby_and_interest_id);
2116
                    array_push($items,  ['value' => $hobbyAndInterest->uuid, 'label' => $hobbyAndInterest->name]);
2117
                }
2118
 
2119
                return new JsonModel([
2120
                    'success'   => true,
2121
                    'data'   => $items
2122
                ]);
2123
            } else {
2124
                $messages = [];
2125
                $form_messages = (array) $form->getMessages();
2126
                foreach ($form_messages  as $fieldname => $field_messages) {
2127
                    $messages[$fieldname] = array_values($field_messages);
2128
                }
2129
 
2130
                return new JsonModel([
2131
                    'success'   => false,
2132
                    'data'   => $messages
2133
                ]);
2134
            }
2135
 
2136
            $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
2137
        }
2138
 
2139
 
2140
        $data = [
2141
            'success' => false,
2142
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
2143
        ];
2144
 
2145
 
2146
        return new JsonModel($data);
2147
    }
2148
}