Proyectos de Subversion LeadersLinked - Services

Rev

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