Proyectos de Subversion LeadersLinked - Services

Rev

Rev 550 | Rev 553 | 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
 
383 www 1095
 
385 www 1096
 
383 www 1097
 
1 efrain 1098
    /**
1099
     * Actualización de los registros de estudios realizados
1100
     * @return \Laminas\View\Model\JsonModel
1101
     */
1102
    public function  educationAction()
1103
    {
1104
 
1105
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1106
        $currentUser = $currentUserPlugin->getUser();
1107
 
1108
        $user_profile_id    = $this->params()->fromRoute('id');
1109
        $user_education_id  = $this->params()->fromRoute('user_education_id');
1110
        $operation          = $this->params()->fromRoute('operation');
1111
 
1112
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1113
 
1114
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1115
        if (!$userProfile) {
1116
            $response = [
1117
                'success' => false,
1118
                'data' => 'ERROR_INVALID_PARAMETER'
1119
            ];
1120
 
1121
            return new JsonModel($response);
1122
        }
1123
 
1124
 
1125
        if ($currentUser->id != $userProfile->user_id) {
1126
            $response = [
1127
                'success' => false,
1128
                'data' => 'ERROR_UNAUTHORIZED'
1129
            ];
1130
 
1131
            return new JsonModel($response);
1132
        }
1133
 
1134
 
1135
 
1136
        $request = $this->getRequest();
1137
        if ($request->isPost()) {
1138
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
1139
 
1140
            if ($operation == 'delete' || $operation == 'edit') {
1141
                $userEducation = $userEducationMapper->fetchOneByUuid($user_education_id);
1142
 
1143
 
1144
                if (!$userEducation) {
1145
 
1146
                    $response = [
1147
                        'success' => false,
1148
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1149
                    ];
1150
 
1151
                    return new JsonModel($response);
1152
                } else if ($userProfile->id != $userEducation->user_profile_id) {
1153
                    $response = [
1154
                        'success' => false,
1155
                        'data' => 'ERROR_UNAUTHORIZED'
1156
                    ];
1157
 
1158
                    return new JsonModel($response);
1159
                }
1160
            } else {
1161
                $userEducation = null;
1162
            }
1163
 
1164
            $locationMapper = LocationMapper::getInstance($this->adapter);
1165
            if ($operation == 'delete') {
1166
                $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()]);
1167
 
1168
                $result = $userEducationMapper->delete($userEducation);
1169
                if ($result) {
1170
                    $locationMapper->delete($userEducation->location_id);
1171
                }
1172
            } else {
1173
 
1174
 
1175
                $form = new EducationForm($this->adapter);
1176
                $dataPost = $request->getPost()->toArray();
1177
 
1178
                $form->setData($dataPost);
1179
 
1180
                if ($form->isValid()) {
1181
 
1182
                    if (!$userEducation) {
1183
                        $userEducation = new UserEducation();
1184
                        $userEducation->user_id = $userProfile->user_id;
1185
                        $userEducation->user_profile_id = $userProfile->id;
1186
                    }
1187
 
1188
                    $dataPost = (array) $form->getData();
1189
 
1190
                    $hydrator = new ObjectPropertyHydrator();
1191
                    $hydrator->hydrate($dataPost, $userEducation);
1192
 
1193
                    $degreeMapper = DegreeMapper::getInstance($this->adapter);
1194
                    $degree = $degreeMapper->fetchOneByUuid($dataPost['degree_id']);
1195
                    $userEducation->degree_id = $degree->id;
1196
 
1197
 
1198
 
1199
 
1200
                    if ($userEducation->location_id) {
1201
                        $location = $locationMapper->fetchOne($userEducation->location_id);
1202
                    } else {
1203
                        $location = new Location();
1204
                    }
1205
 
1206
                    $hydrator->hydrate($dataPost, $location);
1207
                    if ($userEducation->location_id) {
1208
                        $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()]);
1209
 
1210
                        $result = $locationMapper->update($location);
1211
                    } else {
1212
                        $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()]);
1213
 
1214
                        $result = $locationMapper->insert($location);
1215
 
1216
                        if ($result) {
1217
                            $userEducation->location_id = $location->id;
1218
                        }
1219
                    }
1220
                    if ($result) {
1221
                        if ($userEducation->id) {
1222
                            $result = $userEducationMapper->update($userEducation);
1223
                        } else {
1224
                            $result =  $userEducationMapper->insert($userEducation);
1225
                        }
1226
                    }
1227
                } else {
1228
                    $messages = [];
1229
                    $form_messages = (array) $form->getMessages();
1230
                    foreach ($form_messages  as $fieldname => $field_messages) {
1231
                        $messages[$fieldname] = array_values($field_messages);
1232
                    }
1233
 
1234
                    return new JsonModel([
1235
                        'success'   => false,
1236
                        'data'   => $messages
1237
                    ]);
1238
                }
1239
            }
1240
 
1241
            if ($result) {
1242
                $degreeMapper = DegreeMapper::getInstance($this->adapter);
1243
                $userEducations = $userEducationMapper->fetchAllByUserProfileId($userProfile->id);
1244
 
1245
                foreach ($userEducations  as &$userEducation) {
1246
                    $location = $locationMapper->fetchOne($userEducation->location_id);
1247
                    $degree = $degreeMapper->fetchOne($userEducation->degree_id);
1248
 
1249
                    $userEducation = [
1250
                        'university' => $userEducation->university,
1251
                        'degree' => $degree->name,
1252
                        'field_of_study' => $userEducation->field_of_study,
1253
                        'grade_or_percentage' => $userEducation->grade_or_percentage,
1254
                        'formatted_address' => $location->formatted_address,
1255
                        'from_year' => $userEducation->from_year,
1256
                        'to_year' => $userEducation->to_year,
1257
                        'description' => $userEducation->description,
350 www 1258
                        'link_edit' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'edit', 'user_education_id' => $userEducation->uuid]),
1259
                        'link_delete' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'delete', 'user_education_id' => $userEducation->uuid]),
1 efrain 1260
                    ];
1261
                }
1262
 
1263
                $response = [
1264
                    'success'   => true,
1265
                    'data' => $userEducations
1266
                ];
1267
            } else {
1268
                $response = [
1269
                    'success'   => false,
1270
                    'data' => 'ERROR_THERE_WAS_AN_ERROR'
1271
                ];
1272
            }
1273
 
1274
 
1275
 
1276
            return new JsonModel($response);
1277
        } else if ($request->isGet() && $operation == 'edit') {
1278
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
1279
            $userEducation = $userEducationMapper->fetchOneByUuid($user_education_id);
1280
 
1281
            if (!$userEducation) {
1282
 
1283
                $response = [
1284
                    'success' => false,
1285
                    'data' => 'ERROR_RECORD_NOT_FOUND'
1286
                ];
1287
            } else if ($userProfile->id != $userEducation->user_profile_id) {
1288
                $response = [
1289
                    'success' => false,
1290
                    'data' => 'ERROR_UNAUTHORIZED'
1291
                ];
1292
            } else {
1293
                $locationMapper = LocationMapper::getInstance($this->adapter);
1294
                $location = $locationMapper->fetchOne($userEducation->location_id);
1295
 
1296
                $hydrator = new ObjectPropertyHydrator();
1297
                $education = $hydrator->extract($userEducation);
1298
 
1299
                $degree = $degreeMapper = DegreeMapper::getInstance($this->adapter);
1300
                $degree = $degreeMapper->fetchOne($education['degree_id']);
1301
                $education['degree_id'] = $degree->uuid;
1302
 
1303
                $location = [
1304
                    'address1' => $location->address1,
1305
                    'address2' => $location->address2,
1306
                    'city1' => $location->city1,
1307
                    'city2' => $location->city2,
1308
                    'country' => $location->country,
1309
                    'formatted_address' => $location->formatted_address,
1310
                    'latitude' => $location->latitude,
1311
                    'longitude' => $location->longitude,
1312
                    'postal_code' => $location->postal_code,
1313
                    'state' => $location->state,
1314
                ];
1315
 
1316
                $response = [
1317
                    'success' => true,
1318
                    'data' => [
1319
                        'location' => $location,
1320
                        'education' => $education,
1321
                    ]
1322
                ];
1323
            }
1324
 
1325
            return new JsonModel($response);
1326
        }
1327
 
1328
 
1329
 
1330
 
1331
        $data = [
1332
            'success' => false,
1333
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1334
        ];
1335
 
1336
 
1337
        return new JsonModel($data);
1338
    }
1339
 
1340
    /**
1341
     * Actualización de los registros de la experiencia laboral
1342
     * @return \Laminas\View\Model\JsonModel
1343
     */
1344
    public function  experienceAction()
1345
    {
1346
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1347
        $currentUser = $currentUserPlugin->getUser();
1348
 
1349
        $user_profile_id    = $this->params()->fromRoute('id');
1350
        $user_experience_id = $this->params()->fromRoute('user_experience_id');
1351
        $operation          = $this->params()->fromRoute('operation');
1352
 
1353
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1354
 
1355
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1356
        if (!$userProfile) {
1357
            $response = [
1358
                'success' => false,
1359
                'data' => 'ERROR_INVALID_PARAMETER'
1360
            ];
1361
 
1362
            return new JsonModel($response);
1363
        }
1364
 
1365
        if ($currentUser->id != $userProfile->user_id) {
1366
            $response = [
1367
                'success' => false,
1368
                'data' => 'ERROR_UNAUTHORIZED'
1369
            ];
1370
 
1371
            return new JsonModel($response);
1372
        }
1373
 
1374
 
1375
 
1376
        $request = $this->getRequest();
1377
        if ($request->isPost()) {
1378
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
1379
 
1380
            if ($operation == 'delete' || $operation == 'edit') {
1381
                $userExperience = $userExperienceMapper->fetchOneByUuid($user_experience_id);
1382
 
1383
                if (!$userExperience) {
1384
 
1385
                    $response = [
1386
                        'success' => false,
1387
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1388
                    ];
1389
 
1390
                    return new JsonModel($response);
1391
                } else if ($userProfile->id != $userExperience->user_profile_id) {
1392
                    $response = [
1393
                        'success' => false,
1394
                        'data' => 'ERROR_UNAUTHORIZED'
1395
                    ];
1396
 
1397
                    return new JsonModel($response);
1398
                }
1399
            } else {
1400
                $userExperience = null;
1401
            }
1402
 
1403
            $locationMapper = LocationMapper::getInstance($this->adapter);
1404
            if ($operation == 'delete') {
1405
                $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()]);
1406
 
1407
                $result = $userExperienceMapper->delete($userExperience);
1408
                if ($result) {
1409
                    $locationMapper->delete($userExperience->location_id);
1410
                }
1411
            } else {
1412
 
1413
 
1414
                $form = new ExperienceForm($this->adapter);
1415
                $dataPost = $request->getPost()->toArray();
1416
 
1417
 
1418
                $dataPost['is_current'] = isset($dataPost['is_current']) ? $dataPost['is_current'] : UserExperience::IS_CURRENT_NO;
1419
                if ($dataPost['is_current']  == UserExperience::IS_CURRENT_YES) {
1420
                    $dataPost['to_month'] = 12;
1421
                    $dataPost['to_year'] = date('Y');
1422
                }
1423
 
1424
 
1425
                $form->setData($dataPost);
1426
 
1427
                if ($form->isValid()) {
1428
 
1429
 
1430
 
1431
 
1432
                    if (!$userExperience) {
1433
                        $userExperience = new UserExperience();
1434
                        $userExperience->user_id = $userProfile->user_id;
1435
                        $userExperience->user_profile_id = $userProfile->id;
1436
                    }
1437
 
1438
                    $dataPost = (array) $form->getData();
1439
                    $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1440
                    $companySize = $companySizeMapper->fetchOneByUuid($dataPost['company_size_id']);
1441
 
1442
                    $industryMapper = IndustryMapper::getInstance($this->adapter);
1443
                    $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
1444
 
1445
                    $hydrator = new ObjectPropertyHydrator();
1446
                    $hydrator->hydrate($dataPost, $userExperience);
1447
 
1448
                    $userExperience->company_size_id = $companySize->id;
1449
                    $userExperience->industry_id = $industry->id;
1450
 
1451
                    if ($userExperience->is_current == UserExperience::IS_CURRENT_YES) {
1452
                        $userExperience->to_month = null;
1453
                        $userExperience->to_year = null;
1454
                    }
1455
 
1456
                    if ($userExperience->location_id) {
1457
                        $location = $locationMapper->fetchOne($userExperience->location_id);
1458
                    } else {
1459
                        $location = new Location();
1460
                    }
1461
                    $hydrator->hydrate($dataPost, $location);
1462
 
1463
                    if ($userExperience->location_id) {
1464
                        $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()]);
1465
 
1466
                        $result = $locationMapper->update($location);
1467
                    } else {
1468
                        $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()]);
1469
 
1470
                        $result = $locationMapper->insert($location);
1471
 
1472
                        if ($result) {
1473
                            $userExperience->location_id = $location->id;
1474
                        }
1475
                    }
1476
                    if ($result) {
1477
                        if ($userExperience->id) {
1478
                            $result = $userExperienceMapper->update($userExperience);
1479
                        } else {
1480
                            $result =  $userExperienceMapper->insert($userExperience);
1481
                        }
1482
                    }
1483
                } else {
1484
                    $messages = [];
1485
                    $form_messages = (array) $form->getMessages();
1486
                    foreach ($form_messages  as $fieldname => $field_messages) {
1487
                        $messages[$fieldname] = array_values($field_messages);
1488
                    }
1489
 
1490
                    return new JsonModel([
1491
                        'success'   => false,
1492
                        'data'   => $messages,
1493
                    ]);
1494
                }
1495
            }
1496
 
1497
            if ($result) {
1498
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1499
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1500
                $userExperiences = $userExperienceMapper->fetchAllByUserProfileId($userProfile->id);
1501
 
1502
                foreach ($userExperiences  as &$userExperience) {
1503
                    $location = $locationMapper->fetchOne($userExperience->location_id);
1504
                    $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
1505
                    $industry = $industryMapper->fetchOne($userExperience->industry_id);
1506
 
1507
                    $userExperience = [
1508
                        'company' => $userExperience->company,
1509
                        'industry' => $industry,
1510
                        'size' => $companySize->name . ' (' . $companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee . ') ',
1511
                        'title' => $userExperience->title,
1512
                        'formatted_address' => $location->formatted_address,
1513
                        'from_year' => $userExperience->from_year,
1514
                        'from_month' => $userExperience->from_month,
1515
                        'to_year' => $userExperience->to_year,
1516
                        'to_month' => $userExperience->to_month,
1517
                        'description' => $userExperience->description,
1518
                        'is_current' => $userExperience->is_current,
350 www 1519
                        'link_edit' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'edit', 'user_experience_id' => $userExperience->uuid]),
1520
                        'link_delete' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'delete', 'user_experience_id' => $userExperience->uuid]),
1 efrain 1521
                    ];
1522
                }
1523
 
1524
                $response = [
1525
                    'success'   => true,
1526
                    'data' => $userExperiences
1527
                ];
1528
            } else {
1529
                $response = [
1530
                    'success'   => false,
1531
                    'data' => 'ERROR_THERE_WAS_AN_ERROR'
1532
                ];
1533
            }
1534
 
1535
            return new JsonModel($response);
1536
        } else if ($request->isGet() && $operation == 'edit') {
1537
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
1538
            $userExperience = $userExperienceMapper->fetchOneByUuid($user_experience_id);
1539
 
1540
            if (!$userExperience) {
1541
                $response = [
1542
                    'success' => false,
1543
                    'data' => 'ERROR_RECORD_NOT_FOUND'
1544
                ];
1545
            } else if ($userProfile->id != $userExperience->user_profile_id) {
1546
                $response = [
1547
                    'success' => false,
1548
                    'data' => 'ERROR_UNAUTHORIZED'
1549
                ];
1550
            } else {
1551
                $hydrator = new ObjectPropertyHydrator();
1552
                $experience = $hydrator->extract($userExperience);
1553
 
1554
 
1555
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1556
                $industry = $industryMapper->fetchOne($userExperience->industry_id);
1557
 
1558
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1559
                $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
1560
 
1561
                $experience['industry_id'] = $industry->uuid;
1562
                $experience['company_size_id'] = $companySize->uuid;
1563
 
1564
                $locationMapper = LocationMapper::getInstance($this->adapter);
1565
                $location = $locationMapper->fetchOne($userExperience->location_id);
1566
 
1567
                $response = [
1568
                    'success' => true,
1569
                    'data' => [
1570
                        'location' => $hydrator->extract($location),
1571
                        'experience' => $experience
1572
                    ]
1573
                ];
1574
            }
1575
            return new JsonModel($response);
1576
        }
1577
 
1578
        $data = [
1579
            'success' => false,
1580
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1581
        ];
1582
 
1583
 
1584
        return new JsonModel($data);
1585
    }
1586
 
1587
    /**
1588
     * Cambio de la imagen del image del perfil
1589
     * @return \Laminas\View\Model\JsonModel
1590
     */
1591
    public function imageAction()
1592
    {
1593
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1594
        $currentUser = $currentUserPlugin->getUser();
1595
 
1596
        $user_profile_id    = $this->params()->fromRoute('id');
1597
        $operation          = $this->params()->fromRoute('operation');
1598
 
1599
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1600
 
1601
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1602
        if (!$userProfile) {
1603
            $response = [
1604
                'success' => false,
1605
                'data' => 'ERROR_INVALID_PARAMETER'
1606
            ];
1607
 
1608
            return new JsonModel($response);
1609
        }
1610
 
1611
        if ($currentUser->id != $userProfile->user_id) {
1612
            $response = [
1613
                'success' => false,
1614
                'data' => 'ERROR_UNAUTHORIZED'
1615
            ];
1616
 
1617
            return new JsonModel($response);
1618
        }
1619
 
1620
 
1621
 
1622
        $request = $this->getRequest();
1623
        if ($request->isPost()) {
283 www 1624
 
346 www 1625
            $storage = Storage::getInstance($this->config, $this->adapter);
1626
            $target_path = $storage->getPathUser();
1627
 
1 efrain 1628
            if ($operation == 'delete') {
1629
                $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()]);
1630
 
1631
                if ($userProfile->image) {
346 www 1632
                    if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->image)) {
1 efrain 1633
                        return new JsonModel([
1634
                            'success'   => false,
1635
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1636
                        ]);
1637
                    }
1638
                }
1639
 
1640
                $userProfile->image = '';
1641
                if (!$userProfileMapper->updateImage($userProfile)) {
1642
                    return new JsonModel([
1643
                        'success'   => false,
1644
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1645
                    ]);
1646
                }
1647
            } else {
1648
                $form = new ImageForm($this->config);
1649
                $data     = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1650
 
1651
                $form->setData($data);
1652
 
1653
                if ($form->isValid()) {
1654
 
346 www 1655
                    $storage->setFiles($request->getFiles()->toArray());
1656
 
1657
                    if (!$storage->setCurrentFilename('error')) {
1 efrain 1658
 
1659
                        return new JsonModel([
1660
                            'success'   => false,
1661
                            'data'   =>  'ERROR_UPLOAD_FILE'
1662
                        ]);
1663
                    }
1664
 
346 www 1665
 
1 efrain 1666
 
1667
                    list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
346 www 1668
                    $source_filename    = $storage->getTmpFilename();
1669
                    $filename           = 'user-profile-' . uniqid() . '.png';
1670
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->uuid, $filename);
1671
 
1 efrain 1672
 
346 www 1673
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
1 efrain 1674
                        return new JsonModel([
1675
                            'success'   => false,
1676
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1677
                        ]);
1678
                    }
346 www 1679
 
1680
                    if ($userProfile->image) {
1681
                        if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->image)) {
1682
                            return new JsonModel([
1683
                                'success'   => false,
1684
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1685
                            ]);
1686
                        }
1687
                    }
1 efrain 1688
 
346 www 1689
                    $userProfile->image = $filename;
1 efrain 1690
                    if (!$userProfileMapper->updateImage($userProfile)) {
1691
                        return new JsonModel([
1692
                            'success'   => false,
1693
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1694
                        ]);
1695
                    }
1696
 
1697
                    if ($userProfile->public == UserProfile::PUBLIC_YES) {
283 www 1698
 
346 www 1699
                        $filename    = 'user-' . uniqid() . '.png';
1700
                        $source_filename = $target_filename;
1701
                        $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->uuid, $filename);
1702
 
283 www 1703
 
346 www 1704
                        if (!$storage->copyFile($source_filename, $target_filename)) {
283 www 1705
                            return new JsonModel([
1706
                                'success'   => false,
1707
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1708
                            ]);
1709
                        }
346 www 1710
 
1711
                        if ($currentUser->image) {
1712
                            if (!$storage->deleteFile($target_path, $currentUser->uuid, $currentUser->image)) {
1713
                                return new JsonModel([
1714
                                    'success'   => false,
1715
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1716
                                ]);
1717
                            }
1718
                        }
1 efrain 1719
 
346 www 1720
                        $currentUser->image = $filename;
1 efrain 1721
 
1722
                        $userMapper = UserMapper::getInstance($this->adapter);
1723
                        $userMapper->updateImage($currentUser);
1724
                    }
1725
 
1726
                    $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()]);
1727
                } else {
1728
                    $messages = [];
1729
                    $form_messages = (array) $form->getMessages();
1730
                    foreach ($form_messages  as $fieldname => $field_messages) {
1731
                        $messages[$fieldname] = array_values($field_messages);
1732
                    }
1733
 
1734
                    return new JsonModel([
1735
                        'success'   => false,
1736
                        'data'   => $messages
1737
                    ]);
1738
                }
1739
            }
283 www 1740
 
333 www 1741
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1742
 
1 efrain 1743
            return new JsonModel([
1744
                'success'   => true,
1745
                'data' => [
283 www 1746
                    'user' => $storage->getUserImage($currentUser),
1747
                    'profile' => $storage->getUserProfileImage($currentUser, $userProfile),
1 efrain 1748
                    'update_navbar' =>  $userProfile->public == UserProfile::PUBLIC_YES ? 1 : 0,
1749
 
1750
                ]
1751
            ]);
1752
        }
1753
 
1754
 
1755
        $data = [
1756
            'success' => false,
1757
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1758
        ];
1759
 
1760
 
1761
        return new JsonModel($data);
1762
    }
1763
 
1764
    /**
550 stevensc 1765
     * Handles changing the user profile's cover image.
1766
     * Supports uploading a new cover or deleting the existing one.
1767
     * @return \\Laminas\\View\\Model\\JsonModel
1 efrain 1768
     */
1769
    public function coverAction()
1770
    {
550 stevensc 1771
        // Get current user
1 efrain 1772
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1773
        $currentUser = $currentUserPlugin->getUser();
1774
 
550 stevensc 1775
        // Get parameters from route
1 efrain 1776
        $user_profile_id    = $this->params()->fromRoute('id');
550 stevensc 1777
        $operation          = $this->params()->fromRoute('operation'); // Expected: 'upload' or 'delete'
1 efrain 1778
 
550 stevensc 1779
        // Fetch user profile
1 efrain 1780
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
550 stevensc 1781
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1 efrain 1782
 
550 stevensc 1783
        // Validate profile existence
1 efrain 1784
        if (!$userProfile) {
550 stevensc 1785
            return $this->_createSimpleErrorResponse('ERROR_INVALID_PARAMETER');
1 efrain 1786
        }
1787
 
550 stevensc 1788
        // Authorize current user against the profile: only the profile owner can modify it
1 efrain 1789
        if ($currentUser->id != $userProfile->user_id) {
550 stevensc 1790
            return $this->_createSimpleErrorResponse('ERROR_UNAUTHORIZED');
1 efrain 1791
        }
1792
 
1793
        $request = $this->getRequest();
1794
        if ($request->isPost()) {
346 www 1795
            $storage = Storage::getInstance($this->config, $this->adapter);
550 stevensc 1796
            $target_path = $storage->getPathUser(); // Base path for user-specific files
283 www 1797
 
550 stevensc 1798
            // Handle cover deletion operation
1 efrain 1799
            if ($operation == 'delete') {
1800
                if ($userProfile->cover) {
550 stevensc 1801
                    // Attempt to delete the existing cover file from storage
346 www 1802
                    if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->cover)) {
550 stevensc 1803
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1 efrain 1804
                    }
1805
                }
550 stevensc 1806
                // Log the deletion action
1 efrain 1807
                $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()]);
550 stevensc 1808
                $userProfile->cover = ''; // Clear the cover field in the profile model
1809
 
1810
                // Persist the cleared cover information to the database
1811
                if (!$userProfileMapper->updateCover($userProfile)) {
1812
                    // Handle error if database update fails for deletion
1813
                    return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1814
                }
1815
            } else { // Handle cover upload operation
1 efrain 1816
                $form = new CoverForm($this->config);
550 stevensc 1817
                // Merge POST data and FILES data for the form
1818
                $data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1 efrain 1819
                $form->setData($data);
1820
 
1821
                if ($form->isValid()) {
550 stevensc 1822
                    // Set files in storage and select the 'cover' file by its input name
346 www 1823
                    $storage->setFiles($request->getFiles()->toArray());
550 stevensc 1824
                    if (!$storage->setCurrentFilename('cover')) { // Ensure 'cover' is the correct file input name from CoverForm
1825
                        return $this->_createSimpleErrorResponse('ERROR_UPLOAD_FILE');
1 efrain 1826
                    }
550 stevensc 1827
 
1828
                    // Prepare for image resize and save: Get target dimensions from config
1829
                    // Example config value: '1200x400'
1830
                    list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.user_cover_size']);
1831
                    $target_width = (int)$target_width_str;
1832
                    $target_height = (int)$target_height_str;
1 efrain 1833
 
550 stevensc 1834
                    $filename           = 'user-cover-' . uniqid() . '.png'; // Generate unique filename for the new cover
1835
                    $source_filename    = $storage->getTmpFilename(); // Temporary path of the uploaded file
346 www 1836
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->id, $filename);
1 efrain 1837
 
550 stevensc 1838
                    // Upload, resize, and save the image
346 www 1839
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
550 stevensc 1840
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1 efrain 1841
                    }
346 www 1842
 
550 stevensc 1843
                    // If an old cover exists, delete it from storage
346 www 1844
                    if ($userProfile->cover) {
1845
                        if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->cover)) {
550 stevensc 1846
                            // Decide if this is a critical error. Original code returns error.
1847
                            // Consider logging this and proceeding if deletion of old file is non-critical.
1848
                            return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
346 www 1849
                        }
1850
                    }
1 efrain 1851
 
550 stevensc 1852
                    // Update profile model with new cover filename
346 www 1853
                    $userProfile->cover = $filename;
550 stevensc 1854
                    // Persist the new cover information to the database
1 efrain 1855
                    if (!$userProfileMapper->updateCover($userProfile)) {
550 stevensc 1856
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1 efrain 1857
                    }
1858
 
550 stevensc 1859
                    // Log successful update
1 efrain 1860
                    $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()]);
1861
                } else {
550 stevensc 1862
                    // Form is invalid, return validation messages
1863
                    return $this->_createFormErrorResponse($form);
1 efrain 1864
                }
550 stevensc 1865
            } // End of upload/delete specific logic
283 www 1866
 
550 stevensc 1867
            // Successful operation (upload or delete has been handled and persisted)
1868
            // Return success response with the current cover URL (new, or default after delete)
1869
            // The $storage instance from the beginning of the POST block is still valid.
1 efrain 1870
            return new JsonModel([
1871
                'success'   => true,
283 www 1872
                'data' => $storage->getUserProfileCover($currentUser, $userProfile)
1 efrain 1873
            ]);
550 stevensc 1874
        } // End of isPost()
1 efrain 1875
 
550 stevensc 1876
        // If not a POST request, return method not allowed error
1877
        return $this->_createSimpleErrorResponse('ERROR_METHOD_NOT_ALLOWED');
1 efrain 1878
    }
1879
 
1880
    /**
1881
     * Actualización de las habilidades
1882
     * @return \Laminas\View\Model\JsonModel
1883
     */
1884
    public function aptitudeAction()
1885
    {
1886
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1887
        $currentUser = $currentUserPlugin->getUser();
1888
 
1889
        $user_profile_id = $this->params()->fromRoute('id');
1890
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1891
 
1892
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1893
        if (!$userProfile) {
1894
            $response = [
1895
                'success' => false,
1896
                'data' => 'ERROR_INVALID_PARAMETER'
1897
            ];
1898
 
1899
            return new JsonModel($response);
1900
        }
1901
 
1902
        if ($currentUser->id != $userProfile->user_id) {
1903
            $response = [
1904
                'success' => false,
1905
                'data' => 'ERROR_UNAUTHORIZED'
1906
            ];
1907
 
1908
            return new JsonModel($response);
1909
        }
1910
 
1911
 
1912
 
1913
        $request = $this->getRequest();
1914
        if ($request->isGet()) {
1915
            $aptitudeMapper = AptitudeMapper::getInstance($this->adapter);
1916
 
1917
 
1918
            $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
1919
            $userAptitudes  = $userAptitudeMapper->fetchAllByUserProfileId($userProfile->id);
1920
 
1921
            $items = [];
1922
            foreach ($userAptitudes as $userAptitude) {
1923
                $aptitude = $aptitudeMapper->fetchOne($userAptitude->aptitude_id);
1924
                array_push($items, $aptitude->uuid);
1925
            }
1926
 
1927
            $data = [
1928
                'success' => true,
1929
                'data' => $items
1930
            ];
1931
 
1932
            return new JsonModel($data);
1933
        } else if ($request->isPost()) {
1934
 
1935
            $form = new AptitudeForm($this->adapter);
1936
            $dataPost = $request->getPost()->toArray();
1937
 
1938
            $form->setData($dataPost);
1939
 
1940
            if ($form->isValid()) {
1941
                $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()]);
1942
 
1943
                $aptitudeMapper = AptitudeMapper::getInstance($this->adapter);
1944
 
1945
 
1946
                $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
1947
                $userAptitudeMapper->deleteByUserProfileId($userProfile->id);
1948
 
1949
                $dataPost = (array) $form->getData();
1950
                $aptitudes = $dataPost['aptitudes'];
1951
                foreach ($aptitudes as $aptitude_uuid) {
1952
 
1953
                    $aptitude = $aptitudeMapper->fetchOneByUuid($aptitude_uuid);
1954
 
1955
 
1956
                    $userAptitude = new UserAptitude();
1957
                    $userAptitude->user_id = $userProfile->user_id;
1958
                    $userAptitude->user_profile_id = $userProfile->id;
1959
                    $userAptitude->aptitude_id =  $aptitude->id;
1960
 
1961
                    $userAptitudeMapper->insert($userAptitude);
1962
                }
1963
 
1964
                $items = [];
1965
 
1966
                $records = $userAptitudeMapper->fetchAllByUserProfileId($userProfile->id);
1967
                foreach ($records as $record) {
1968
                    $aptitude = $aptitudeMapper->fetchOne($record->aptitude_id);
1969
                    array_push($items,  ['value' => $aptitude->uuid, 'label' => $aptitude->name]);
1970
                }
1971
 
1972
                return new JsonModel([
1973
                    'success'   => true,
1974
                    'data'   => $items
1975
                ]);
1976
            } else {
1977
                $messages = [];
1978
                $form_messages = (array) $form->getMessages();
1979
                foreach ($form_messages  as $fieldname => $field_messages) {
1980
                    $messages[$fieldname] = array_values($field_messages);
1981
                }
1982
 
1983
                return new JsonModel([
1984
                    'success'   => false,
1985
                    'data'   => $messages
1986
                ]);
1987
            }
1988
 
1989
            $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
1990
        }
1991
 
1992
 
1993
        $data = [
1994
            'success' => false,
1995
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1996
        ];
1997
 
1998
 
1999
        return new JsonModel($data);
2000
    }
2001
 
2002
    /**
2003
     * Actualización de las habilidades
2004
     * @return \Laminas\View\Model\JsonModel
2005
     */
2006
    public function hobbyAndInterestAction()
2007
    {
2008
        $currentUserPlugin = $this->plugin('currentUserPlugin');
2009
        $currentUser = $currentUserPlugin->getUser();
2010
 
2011
        $user_profile_id = $this->params()->fromRoute('id');
2012
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
2013
 
2014
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
2015
        if (!$userProfile) {
2016
            $response = [
2017
                'success' => false,
2018
                'data' => 'ERROR_INVALID_PARAMETER'
2019
            ];
2020
 
2021
            return new JsonModel($response);
2022
        }
2023
 
2024
        if ($currentUser->id != $userProfile->user_id) {
2025
            $response = [
2026
                'success' => false,
2027
                'data' => 'ERROR_UNAUTHORIZED'
2028
            ];
2029
 
2030
            return new JsonModel($response);
2031
        }
2032
 
2033
 
2034
 
2035
        $request = $this->getRequest();
2036
        if ($request->isGet()) {
2037
            $hobbyAndInterestMapper = HobbyAndInterestMapper::getInstance($this->adapter);
2038
 
2039
 
2040
            $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
2041
            $userHobbyAndInterests  = $userHobbyAndInterestMapper->fetchAllByUserProfileId($userProfile->id);
2042
 
2043
            $items = [];
2044
            foreach ($userHobbyAndInterests as $userHobbyAndInterest) {
2045
                $hobbyAndInterest = $hobbyAndInterestMapper->fetchOne($userHobbyAndInterest->hobbyAndInterest_id);
2046
                array_push($items, $hobbyAndInterest->uuid);
2047
            }
2048
 
2049
            $data = [
2050
                'success' => true,
2051
                'data' => $items
2052
            ];
2053
 
2054
            return new JsonModel($data);
2055
        } else if ($request->isPost()) {
2056
 
2057
            $form = new HobbyAndInterestForm($this->adapter);
2058
            $dataPost = $request->getPost()->toArray();
2059
 
2060
 
2061
            $form->setData($dataPost);
2062
 
2063
            if ($form->isValid()) {
2064
                $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()]);
2065
 
2066
                $hobbyAndInterestMapper = HobbyAndInterestMapper::getInstance($this->adapter);
2067
 
2068
 
2069
                $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
2070
                $userHobbyAndInterestMapper->deleteByUserProfileId($userProfile->id);
2071
 
2072
                $dataPost = (array) $form->getData();
2073
                $hobbyAndInterests = $dataPost['hobbies_and_interests'];
2074
                foreach ($hobbyAndInterests as $hobbyAndInterest_uuid) {
2075
 
2076
                    $hobbyAndInterest = $hobbyAndInterestMapper->fetchOneByUuid($hobbyAndInterest_uuid);
2077
 
2078
 
2079
                    $userHobbyAndInterest = new UserHobbyAndInterest();
2080
                    $userHobbyAndInterest->user_id = $userProfile->user_id;
2081
                    $userHobbyAndInterest->user_profile_id = $userProfile->id;
2082
                    $userHobbyAndInterest->hobby_and_interest_id =  $hobbyAndInterest->id;
2083
 
2084
                    $userHobbyAndInterestMapper->insert($userHobbyAndInterest);
2085
                }
2086
 
2087
                $items = [];
2088
 
2089
                $records = $userHobbyAndInterestMapper->fetchAllByUserProfileId($userProfile->id);
2090
                foreach ($records as $record) {
2091
                    $hobbyAndInterest = $hobbyAndInterestMapper->fetchOne($record->hobby_and_interest_id);
2092
                    array_push($items,  ['value' => $hobbyAndInterest->uuid, 'label' => $hobbyAndInterest->name]);
2093
                }
2094
 
2095
                return new JsonModel([
2096
                    'success'   => true,
2097
                    'data'   => $items
2098
                ]);
2099
            } else {
2100
                $messages = [];
2101
                $form_messages = (array) $form->getMessages();
2102
                foreach ($form_messages  as $fieldname => $field_messages) {
2103
                    $messages[$fieldname] = array_values($field_messages);
2104
                }
2105
 
2106
                return new JsonModel([
2107
                    'success'   => false,
2108
                    'data'   => $messages
2109
                ]);
2110
            }
2111
 
2112
            $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
2113
        }
2114
 
2115
 
2116
        $data = [
2117
            'success' => false,
2118
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
2119
        ];
2120
 
2121
 
2122
        return new JsonModel($data);
2123
    }
550 stevensc 2124
 
2125
    // Helper methods for creating standardized JSON responses
2126
    // These can be used across various actions in this controller
2127
 
2128
    /**
2129
     * Creates a standardized JSON error response with a simple message.
2130
     * @param string $errorMessageKey Identifier for the error message (e.g., a translation key or constant).
2131
     * @return JsonModel
2132
     */
551 stevensc 2133
    private function _createSimpleErrorResponse($errorMessageKey)
550 stevensc 2134
    {
2135
        return new JsonModel([
2136
            'success' => false,
2137
            'data'    => $errorMessageKey
2138
        ]);
2139
    }
2140
 
2141
    /**
2142
     * Creates a JSON response for form validation errors.
2143
     * Extracts messages from the form and structures them for the client.
551 stevensc 2144
     * @param \Laminas\Form\FormInterface $form The form instance containing validation messages.
550 stevensc 2145
     * @return JsonModel
2146
     */
551 stevensc 2147
    private function _createFormErrorResponse($form)
550 stevensc 2148
    {
2149
        $messages = [];
2150
        $form_messages = (array) $form->getMessages(); // Get all error messages from the form
2151
        foreach ($form_messages as $fieldname => $field_messages_for_field) {
2152
            // Ensure messages for each field are in a simple array format for the frontend
2153
            $messages[$fieldname] = array_values((array)$field_messages_for_field);
2154
        }
2155
        return new JsonModel([
2156
            'success' => false,
2157
            'data'    => $messages
2158
        ]);
2159
    }
1 efrain 2160
}