Proyectos de Subversion LeadersLinked - Services

Rev

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