| Línea 878... |
Línea 878... |
| 878 |
}
|
878 |
}
|
| 879 |
}
|
879 |
}
|
| 880 |
} else {
|
880 |
} else {
|
| 881 |
// Manejar la operación de carga de imagen
|
881 |
// Manejar la operación de carga de imagen
|
| 882 |
$form = new ProfileImageForm($this->config);
|
882 |
$form = new ProfileImageForm($this->config);
|
| 883 |
$data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
|
883 |
$data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
|
| Línea 884... |
Línea 884... |
| 884 |
|
884 |
|
| Línea 885... |
Línea 885... |
| 885 |
$form->setData($data);
|
885 |
$form->setData($data);
|
| 886 |
|
886 |
|
| 887 |
// Validar los datos del formulario
|
- |
|
| 888 |
if ($form->isValid()) {
|
- |
|
| 889 |
$storage->setFiles($request->getFiles()->toArray());
|
- |
|
| 890 |
|
- |
|
| 891 |
// Verificar y establecer el nombre de archivo actual
|
- |
|
| 892 |
if (!$storage->setCurrentFilename('image')) {
|
- |
|
| 893 |
return new JsonModel([
|
- |
|
| 894 |
'success' => false,
|
887 |
// Validar los datos del formulario
|
| 895 |
'data' => 'ERROR_UPLOAD_FILE'
|
- |
|
| 896 |
]);
|
- |
|
| 897 |
}
|
- |
|
| 898 |
|
888 |
if (!$form->isValid()) {
|
| 899 |
// Eliminar la imagen existente de la empresa si está presente
|
- |
|
| 900 |
if ($company->image) {
|
- |
|
| 901 |
if (!$storage->deleteFile($target_company_path, $company->uuid, $company->image)) {
|
889 |
$messages = [];
|
| 902 |
return new JsonModel([
|
890 |
$form_messages = (array) $form->getMessages();
|
| 903 |
'success' => false,
|
- |
|
| 904 |
'data' => 'ERROR_THERE_WAS_AN_ERROR'
|
- |
|
| 905 |
]);
|
891 |
foreach ($form_messages as $fieldname => $field_messages) {
|
| - |
|
892 |
$messages[$fieldname] = array_values($field_messages);
|
| - |
|
893 |
}
|
| - |
|
894 |
return new JsonModel([
|
| - |
|
895 |
'success' => false,
|
| - |
|
896 |
'data' => $messages
|
| Línea 906... |
Línea -... |
| 906 |
}
|
- |
|
| 907 |
}
|
- |
|
| 908 |
|
- |
|
| 909 |
// Obtener dimensiones objetivo para el redimensionamiento
|
- |
|
| 910 |
list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.company_image_size']);
|
- |
|
| 911 |
$target_width = (int)$target_width_str;
|
- |
|
| 912 |
$target_height = (int)$target_height_str;
|
897 |
]);
|
| 913 |
|
- |
|
| 914 |
// Generar nombres de archivo únicos
|
- |
|
| Línea 915... |
Línea 898... |
| 915 |
$source = $storage->getTmpFilename();
|
898 |
}
|
| 916 |
$filename = 'company-image-' . $company->uuid . '.png';
|
899 |
|
| - |
|
900 |
$storage->setFiles($request->getFiles()->toArray());
|
| - |
|
901 |
|
| 917 |
$target_filename = $storage->composePathToFilename(Storage::TYPE_COMPANY, $company->uuid, $filename);
|
902 |
// Verificar y establecer el nombre de archivo actual
|
| 918 |
|
903 |
if (!$storage->setCurrentFilename('image')) {
|
| - |
|
904 |
return new JsonModel([
|
| Línea -... |
Línea 905... |
| - |
|
905 |
'success' => false,
|
| - |
|
906 |
'data' => 'ERROR_UPLOAD_FILE'
|
| - |
|
907 |
]);
|
| - |
|
908 |
}
|
| - |
|
909 |
|
| 919 |
// Subir y redimensionar la imagen
|
910 |
// Obtener dimensiones objetivo para el redimensionamiento
|
| - |
|
911 |
list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.company_image_size']);
|
| - |
|
912 |
$target_width = (int)$target_width_str;
|
| - |
|
913 |
$target_height = (int)$target_height_str;
|
| - |
|
914 |
|
| 920 |
if (!$storage->uploadImageResize($source, $target_filename, $target_width, $target_height)) {
|
915 |
// Generar nombres de archivo únicos
|
| 921 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
916 |
$source = $storage->getTmpFilename();
|
| 922 |
}
|
917 |
$filename = 'company-image-' . $company->uuid . '.png';
|
| 923 |
|
- |
|
| 924 |
// Eliminar la imagen anterior si existe
|
918 |
$target_filename = $storage->composePathToFilename(Storage::TYPE_COMPANY, $company->uuid, $filename);
|
| Línea 925... |
Línea 919... |
| 925 |
if ($company->image) {
|
919 |
|
| 926 |
if (!$storage->deleteFile($target_company_path, $company->uuid, $company->image)) {
|
920 |
// Subir y redimensionar la imagen
|
| 927 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
921 |
if (!$storage->uploadImageResize($source, $target_filename, $target_width, $target_height)) {
|
| 928 |
}
|
922 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
| 929 |
}
|
923 |
}
|
| - |
|
924 |
|
| Línea 930... |
Línea 925... |
| 930 |
|
925 |
// Eliminar la imagen anterior si existe
|
| 931 |
// Actualizar el perfil con la nueva imagen
|
- |
|
| 932 |
$company->image = $filename;
|
- |
|
| 933 |
if (!$companyMapper->updateImage($company)) {
|
- |
|
| 934 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
- |
|
| 935 |
}
|
- |
|
| 936 |
|
- |
|
| 937 |
// Actualizar la imagen del propietario si es necesario
|
- |
|
| 938 |
$companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
|
- |
|
| 939 |
$companyUser = $companyUserMapper->fetchOwnerByCompanyId($company->id);
|
- |
|
| 940 |
|
- |
|
| 941 |
if ($companyUser) {
|
926 |
if ($company->image) {
|
| 942 |
$userMapper = UserMapper::getInstance($this->adapter);
|
- |
|
| 943 |
$user = $userMapper->fetchOne($companyUser->user_id);
|
- |
|
| 944 |
|
927 |
if (!$storage->deleteFile($target_company_path, $company->uuid, $company->image)) {
|
| 945 |
$main_company_filename = 'comany-' . uniqid() . '.png';
|
928 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
| 946 |
$target_filename_main_company = $storage->composePathToFilename(Storage::TYPE_USER, $user->uuid, $main_company_filename);
|
929 |
}
|
| Línea 947... |
Línea 930... |
| 947 |
|
930 |
}
|
| - |
|
931 |
|
| - |
|
932 |
// Actualizar el perfil con la nueva imagen
|
| - |
|
933 |
$company->image = $filename;
|
| - |
|
934 |
if (!$companyMapper->update($company)) {
|
| - |
|
935 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
| - |
|
936 |
}
|
| - |
|
937 |
|
| - |
|
938 |
// Actualizar la imagen del propietario si es necesario
|
| 948 |
if ($user) {
|
939 |
$companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
|
| - |
|
940 |
$companyUser = $companyUserMapper->fetchOwnerByCompanyId($company->id);
|
| - |
|
941 |
|
| - |
|
942 |
if ($companyUser) {
|
| - |
|
943 |
$userMapper = UserMapper::getInstance($this->adapter);
|
| - |
|
944 |
$user = $userMapper->fetchOne($companyUser->user_id);
|
| 949 |
|
945 |
|
| 950 |
// Eliminar la imagen existente del usuario si está presente
|
946 |
$main_company_filename = 'comany-' . uniqid() . '.png';
|
| Línea 951... |
Línea 947... |
| 951 |
if ($user->image) {
|
947 |
$target_filename_main_company = $storage->composePathToFilename(Storage::TYPE_USER, $user->uuid, $main_company_filename);
|
| 952 |
$storage->deleteFile($target_user_path, $user->uuid, $user->image);
|
- |
|
| 953 |
}
|
948 |
|
| 954 |
|
949 |
if ($user) {
|
| 955 |
// Copiar la imagen del perfil para la imagen principal
|
- |
|
| 956 |
if (!$storage->copyFile($target_filename, $target_filename_main_company)) {
|
950 |
|
| Línea 957... |
Línea 951... |
| 957 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
951 |
// Eliminar la imagen existente del usuario si está presente
|
| 958 |
}
|
952 |
if ($user->image) {
|
| 959 |
|
953 |
$storage->deleteFile($target_user_path, $user->uuid, $user->image);
|
| 960 |
// Eliminar la imagen principal anterior si existe
|
954 |
}
|
| 961 |
if ($currentUser->image) {
|
955 |
|
| - |
|
956 |
// Copiar la imagen del perfil para la imagen principal
|
| Línea 962... |
Línea 957... |
| 962 |
if (!$storage->deleteFile($target_company_path, $user->uuid, $currentUser->image)) {
|
957 |
if (!$storage->copyFile($target_filename, $target_filename_main_company)) {
|
| 963 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
- |
|
| 964 |
}
|
958 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
| 965 |
}
|
959 |
}
|
| 966 |
|
960 |
|
| 967 |
// Actualizar la imagen principal del usuario
|
- |
|
| 968 |
$currentUser->image = $main_company_filename;
|
961 |
// Eliminar la imagen principal anterior si existe
|
| 969 |
if (!$userMapper->updateImage($currentUser)) {
|
- |
|
| Línea 970... |
Línea 962... |
| 970 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
962 |
if ($currentUser->image) {
|
| 971 |
}
|
- |
|
| 972 |
|
- |
|
| 973 |
// Actualizar el nombre del usuario si difiere del nombre de la empresa
|
963 |
if (!$storage->deleteFile($target_company_path, $user->uuid, $currentUser->image)) {
|
| 974 |
if ($company->name != $user->first_name) {
|
964 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
| 975 |
$user->first_name = $company->name;
|
965 |
}
|
| 976 |
$user->last_name = '';
|
966 |
}
|
| 977 |
$userMapper->updateFirstNameAndLastName($user);
|
967 |
|
| 978 |
}
|
968 |
// Actualizar la imagen principal del usuario
|
| 979 |
}
|
- |
|
| 980 |
}
|
- |
|
| 981 |
|
- |
|
| 982 |
// Registrar la actualización de la imagen
|
- |
|
| 983 |
$this->logger->info('Se actualizo la imagen de la empresa: ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
- |
|
| 984 |
} else {
|
969 |
$currentUser->image = $main_company_filename;
|
| - |
|
970 |
if (!$userMapper->updateImage($currentUser)) {
|
| - |
|
971 |
return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
|
| - |
|
972 |
}
|
| 985 |
// Manejar errores de validación del formulario
|
973 |
|
| Línea 986... |
Línea 974... |
| 986 |
$messages = [];
|
974 |
// Actualizar el nombre del usuario si difiere del nombre de la empresa
|
| 987 |
$form_messages = (array) $form->getMessages();
|
975 |
if ($company->name != $user->first_name) {
|
| 988 |
foreach ($form_messages as $fieldname => $field_messages) {
|
976 |
$user->first_name = $company->name;
|