diff --git a/ci4/app/Controllers/Profile.php b/ci4/app/Controllers/Profile.php index d18708e8..71772a74 100755 --- a/ci4/app/Controllers/Profile.php +++ b/ci4/app/Controllers/Profile.php @@ -7,52 +7,25 @@ use App\Models\UserModel; class Profile extends BaseController { - private $user_model; - private $id_user; - function __construct() { - $this->user_model = new UserModel(); - $this->id_user = auth()->user()->id; } public function index() { - helper('file'); - helper('form'); - helper('text'); - - $data['title'] = [ - 'module' => lang("App.profile_title"), - 'page' => lang("App.profile_subtitle"), - 'icon' => 'fas fa-user' - ]; $data['breadcrumb'] = [ ['title' => lang("App.menu_dashboard"), 'route' => "/home", 'active' => false], ['title' => lang("App.profile_title"), 'route' => "", 'active' => true] ]; - $data['btn_return'] = [ - 'title' => lang("App.global_come_back"), - 'route' => '/', - 'class' => 'btn btn-dark mr-1', - 'icon' => 'fas fa-angle-left' - ]; - - $data['btn_submit'] = [ - 'title' => lang("App.global_save"), - 'route' => '', - 'class' => 'btn btn-primary mr-1', - 'icon' => 'fas fa-save' - ]; - - $session = session(); - - $data['obj'] = $this->user_model->where('id', $this->id_user)->first(); + // Get the User Provider (UserModel by default) + $users = auth()->getProvider(); + // Find by the user_id + $data['obj'] = $users->findById(auth()->id()); echo view(getenv('theme.path') . 'form/profile/index', $data); @@ -60,38 +33,55 @@ class Profile extends BaseController public function store() { - $session = session(); helper('form'); $rules = [ 'first_name' => 'required', 'last_name' => 'required', + 'new_pwd' => 'permit_empty|min_length[8]', + 'new_pwd_confirm' => 'required_with[new_pwd]|matches[new_pwd]' ]; + // Definir los mensajes de error personalizados $rules_error = [ 'first_name' => ['required' => lang("App.profile_rules_first_name_r")], 'last_name' => ['required' => lang("App.profile_rules_last_name_r")], + 'new_pwd' => ['min_length' => lang("App.profile_rules_password_m")], + 'new_pwd_confirm' => [ + 'matches' => lang("App.profile_rules_password_confirm_m") + ] ]; - - if ($this->validate($rules ?? [], $rules_error ?? [])) { - if (!empty($this->id_user)) { - $this->user_model->save([ - 'id' => $this->id_user, - 'first_name' => $this->request->getPost('first_name'), - 'last_name' => $this->request->getPost('last_name') - ]); - $session->setFlashdata('sweet', ['success', lang("App.global_alert_save_success")]); - } else { - $session->setFlashdata('sweet', ['error', lang("App.global_alert_save_error")]); - } - } else { - - $session->setFlashdata('error', 'error'); - return $this->index(); + // Validar la entrada + if (!$this->validate($rules, $rules_error)) { + // Si la validación falla, redirigir de vuelta con errores + return redirect()->back()->withInput()->with('errors', $this->validator->getErrors()); } - return redirect()->to('/profile'); + // Obtener los valores de los campos + $firstName = $this->request->getPost('first_name'); + $lastName = $this->request->getPost('last_name'); + $newPwd = $this->request->getPost('new_pwd'); + + $update_data = [ + 'first_name' => $firstName, + 'last_name' => $lastName, + 'password' => $newPwd, + ]; + + if(empty($newPwd)){ + unset($update_data['password']); + } + + // Aquí debes obtener el usuario actual, por ejemplo, desde la sesión + $users = auth()->getProvider(); + $user = $users->findById(auth()->id()); + $user->fill($update_data); + $users->save($user); + + // Redirigir con un mensaje de éxito + return redirect()->back()->with('success', lang('App.profile_updated_successfully')); + } } diff --git a/ci4/app/Language/es/App.php b/ci4/app/Language/es/App.php index 5e607b58..e03fa10d 100755 --- a/ci4/app/Language/es/App.php +++ b/ci4/app/Language/es/App.php @@ -137,9 +137,9 @@ return [ "profile_mobile" => "Teléfono Móvil", "profile_mobile_ph" => "Escriba su número de celular", "profile_password" => "Cambiar Contraseña", - "profile_password_ph" => "Escribe tu contraseña", - "profile_confirm_password" => "Confirmar seña", - "profile_confirm_password_ph" => "Confirma tu contraseña anterior", + "profile_password_ph" => "Escribe nueva contraseña para cambiarla", + "profile_confirm_password" => "Confirmar contraseña", + "profile_confirm_password_ph" => "Confirma tu contraseña anterior para cambiarla", "profile_date_birth" => "Fecha de Nacimiento", "profile_date_birth_ph" => "Seleccionar fecha de nacimiento", "profile_address" => "Dirección", diff --git a/ci4/app/Views/themes/vuexy/form/profile/index.php b/ci4/app/Views/themes/vuexy/form/profile/index.php index 58eee725..bff0c49f 100644 --- a/ci4/app/Views/themes/vuexy/form/profile/index.php +++ b/ci4/app/Views/themes/vuexy/form/profile/index.php @@ -1,4 +1,3 @@ -= $this->include("themes/_commonPartialsBs/select2bs5") ?> = $this->extend('themes/vuexy/main/general_settings_layout') ?> = $this->section('content'); ?> @@ -12,10 +11,11 @@