Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Juan Montilla
/
TBW2223_equipo12
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5f3f5bcf
authored
May 12, 2023
by
Juan Montilla
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Editar Usuario
parent
fac1f0e7
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
92 additions
and
95 deletions
app/Config/Routes.php
app/Controllers/User.php
app/Filters/AdminAuth.php
app/Models/UserModel.php
app/Views/pages/profile_view.php
app/Config/Routes.php
View file @
5f3f5bcf
...
...
@@ -68,17 +68,18 @@ $routes->get('/myrecipes', 'User::personalRecipes', ['filter' => 'user_auth']);
// Ruta para vista "Mi perfil"
$routes
->
get
(
'/profile'
,
'User::myprofile'
,
[
'filter'
=>
'user_auth'
]);
$routes
->
post
(
'/cambiarFoto'
,
'User::changeProfilePhoto'
);
$routes
->
post
(
'/profile'
,
'User::editProfile'
);
// Cambia la ruta para editar perfil
$routes
->
get
(
'login'
,
'Pages::viewLogin'
);
$routes
->
get
(
'users'
,
'User::list'
);
$routes
->
get
(
'users'
,
'User::list'
,
[
'filter'
=>
'admin_auth'
]);
$routes
->
get
(
'home'
,
'Pages::prueba'
);
$routes
->
get
(
'(:segment)'
,
'Home::index'
);
// Ruta para la búsqueda de re
// Ruta para la búsqueda de re
cetas
$routes
->
post
(
'/filter_recipes'
,
'RecipesController::filter_recipes'
);
...
...
app/Controllers/User.php
View file @
5f3f5bcf
...
...
@@ -192,23 +192,35 @@ class User extends BaseController
.
view
(
'templates/footer'
);
}
public
function
changeProfilePhoto
()
public
function
editProfile
()
{
$session
=
\Config\Services
::
session
();
$email
=
$session
->
get
(
'user'
)
->
email
;
$userModel
=
new
UserModel
();
$photo
=
$this
->
request
->
getFile
(
'photo'
);
if
(
$photo
->
isValid
()
&&
!
$photo
->
hasMoved
())
{
$photoBlob
=
file_get_contents
(
$photo
->
getRealPath
());
$data
[
'photo'
]
=
$photoBlob
;
$userModel
->
update
(
$email
,
$data
);
$session
=
session
();
$userModel
=
new
\App\Models\UserModel
();
if
(
$this
->
request
->
getMethod
()
===
'post'
)
{
$username
=
$this
->
request
->
getPost
(
'username'
);
// Manejar el archivo de imagen
$photo
=
$this
->
request
->
getFile
(
'photo'
);
$photoBlob
=
null
;
if
(
$photo
->
isValid
()
&&
!
$photo
->
hasMoved
())
{
$photoBlob
=
file_get_contents
(
$photo
->
getRealPath
());
$userModel
->
updateUser
(
$session
->
get
(
'user'
)
->
email
,
$username
,
$photoBlob
);
}
else
{
// Actualiza solo el nombre de usuario si no se ha subido una nueva foto
$userModel
->
updateUser
(
$session
->
get
(
'user'
)
->
email
,
$username
,
$session
->
get
(
'user'
)
->
photo
);
}
// Actualizar datos de la sesión
$session
->
set
(
'user'
,
$userModel
->
find
(
$session
->
get
(
'user'
)
->
email
));
$session
->
setFlashdata
(
'success'
,
'Profile updated successfully'
);
return
redirect
()
->
to
(
'/profile'
);
// Cambia la redirección a '/profile'
}
return
redirect
()
->
to
(
'/perfil'
);
$data
[
'user'
]
=
$session
->
get
(
'user'
);
return
view
(
'edit_profile'
,
$data
);
}
}
\ No newline at end of file
app/Filters/AdminAuth.php
View file @
5f3f5bcf
...
...
@@ -12,7 +12,7 @@ class AdminAuth implements FilterInterface
if
(
!
session
(
'logged_in'
))
return
redirect
()
->
to
(
site_url
(
'/login'
));
elseif
((
session
(
'user'
)
->
rol
&
2
)
==
0
)
return
redirect
()
->
to
(
site_url
(
'/
unauthorized
'
));
return
redirect
()
->
to
(
site_url
(
'/
login
'
));
}
public
function
after
(
RequestInterface
$request
,
ResponseInterface
$response
,
...
...
app/Models/UserModel.php
View file @
5f3f5bcf
...
...
@@ -11,7 +11,7 @@ class UserModel extends Model
protected
$returnType
=
'object'
;
# 'object' or 'array'
protected
$useSoftDeletes
=
false
;
# true if you expect to recover data
# Fields that can be set during save, insert, or update methods
protected
$allowedFields
=
[
'email'
,
'username'
,
'password'
,
'photo'
];
protected
$allowedFields
=
[
'email'
,
'username'
,
'password'
,
'photo'
];
protected
$useTimestamps
=
false
;
# no timestamps on inserts and updates
# Do not use validations rules (for the time being...)
protected
$validationRules
=
[];
...
...
@@ -36,5 +36,17 @@ class UserModel extends Model
return
$this
->
insert
(
$data
);
}
public
function
updateUser
(
$email
,
$username
,
$photo
)
{
$data
=
[
'username'
=>
$username
,
'photo'
=>
$photo
,
];
return
$this
->
update
(
$email
,
$data
);
}
}
\ No newline at end of file
app/Views/pages/profile_view.php
View file @
5f3f5bcf
<main
id=
"main"
class=
"main"
>
<section
class=
"section dashboard"
>
<?php
$session
=
session
();
?>
<div
style=
"display: flex; flex-direction: row; align-items: center;"
>
<?php
if
(
$session
->
has
(
'user'
)
&&
!
is_null
(
$session
->
get
(
'user'
)
->
photo
))
:
?>
<img
src=
"data:image/jpeg;base64,
<?=
base64_encode
(
$session
->
get
(
'user'
)
->
photo
)
?>
"
alt=
"Profile"
style=
"width: 100px; height: 100px; object-fit: cover; border-radius: 50%;"
>
<?php
else
:
?>
<img
src=
"
<?=
base_url
(
"imagenes/profile.png"
)
?>
"
style=
"width: 100px; height: 100px; object-fit: cover; border-radius: 50%;"
>
<?php
endif
;
?>
<div
style=
"display: flex; flex-direction: column; margin-left: 20px;"
>
<span
style=
"font-size: 18px; font-weight: bold;"
>
Nombre de usuario:
</span>
<span
style=
"font-size: 16px;"
>
<?=
$session
->
get
(
'user'
)
->
username
?>
</span>
<span
style=
"font-size: 18px; font-weight: bold;"
>
Correo:
</span>
<span
style=
"font-size: 16px;"
>
<?=
$session
->
get
(
'user'
)
->
email
?>
</span>
</div>
</div>
<style>
.profile-container
{
max-width
:
500px
;
margin
:
auto
;
}
.profile-photo
{
max-width
:
200px
;
display
:
block
;
margin
:
10px
0
;
}
.save-btn-container
{
margin-top
:
20px
;
}
</style>
<a
href=
"#"
id=
"cambiar-foto-btn"
class=
"btn btn-primary"
>
Cambiar foto de perfil
</a>
<div
class=
"modal fade"
id=
"cambiar-foto-modal"
tabindex=
"-1"
role=
"dialog"
>
<div
class=
"modal-dialog"
role=
"document"
>
<div
class=
"modal-content"
>
<div
class=
"modal-header"
>
<h5
class=
"modal-title"
>
Cambiar foto de perfil
</h5>
<button
type=
"button"
class=
"close"
data-dismiss=
"modal"
aria-label=
"Close"
>
<span
aria-hidden=
"true"
>
×
</span>
</button>
</div>
<div
class=
"modal-body"
>
<form
id=
"cambiar-foto-form"
enctype=
"multipart/form-data"
>
<div
class=
"form-group"
>
<label
for=
"foto"
>
Selecciona una foto:
</label>
<input
type=
"file"
class=
"form-control-file"
id=
"foto"
name=
"foto"
>
</div>
</form>
</div>
<div
class=
"modal-footer"
>
<button
type=
"button"
class=
"btn btn-secondary"
data-dismiss=
"modal"
>
Cancelar
</button>
<button
type=
"button"
class=
"btn btn-primary"
id=
"guardar-foto-btn"
>
Guardar
</button>
</div>
<main
id=
"main"
class=
"main"
>
<section
class=
"section dashboard"
>
<?php
$session
=
session
();
$user
=
$session
->
get
(
'user'
);
?>
<div
class=
"container profile-container"
>
<div
class=
"card"
>
<div
class=
"card-header"
>
Editar usuario
</div>
<div
class=
"card-body"
>
<form
method=
"post"
enctype=
"multipart/form-data"
>
<div
class=
"form-group"
>
<label
for=
"email"
>
Email
</label>
<input
type=
"email"
class=
"form-control"
id=
"email"
name=
"email"
value=
"
<?=
$user
->
email
?>
"
readonly
>
</div>
<div
class=
"form-group"
>
<label
for=
"username"
>
Nombre de usuario
</label>
<input
type=
"text"
class=
"form-control"
id=
"username"
name=
"username"
value=
"
<?=
$user
->
username
?>
"
>
</div>
<div
class=
"form-group"
>
<label
for=
"photo"
>
Foto de perfil
</label>
<?php
if
(
$user
->
photo
)
:
?>
<img
src=
"data:image/jpeg;base64,
<?=
base64_encode
(
$user
->
photo
)
?>
"
alt=
"User photo"
class=
"profile-photo"
/>
<?php
endif
;
?>
<input
type=
"file"
class=
"form-control"
id=
"photo"
name=
"photo"
>
</div>
<div
class=
"text-center save-btn-container"
>
<button
type=
"submit"
class=
"btn btn-primary"
>
Guardar cambios
</button>
</div>
</form>
</div>
</div>
</div>
</section>
</main>
<!-- End #main -->
<script>
$
(
document
).
ready
(
function
()
{
$
(
'#cambiar-foto-btn'
).
click
(
function
()
{
$
(
'#cambiar-foto-modal'
).
modal
(
'show'
);
});
});
</script>
<script>
$
(
document
).
ready
(
function
()
{
$
(
'#guardar-foto-btn'
).
click
(
function
()
{
var
formData
=
new
FormData
(
$
(
'#cambiar-foto-form'
)[
0
]);
$
.
ajax
({
url
:
'/cambiarFoto'
,
type
:
'POST'
,
data
:
formData
,
contentType
:
false
,
processData
:
false
,
success
:
function
(
data
)
{
window
.
location
.
href
=
'/perfil'
;
}
});
});
});
</script>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment