Proyectos de Subversion LeadersLinked - Services

Rev

Rev 622 | | Comparar con el anterior | Ultima modificación | Ver Log |

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