[Autocompleta] el formulario de edición de perfil

This commit is contained in:
Ricardo García Jiménez 2022-01-22 16:56:34 -06:00
parent cac451f295
commit 172f8cc49d
5 changed files with 86 additions and 11 deletions

View file

@ -18,6 +18,8 @@
defined('NABU') || exit();
require_once 'models/profilesModel.php';
class profilesController {
static public function sent_articles() {
//
@ -33,7 +35,21 @@ class profilesController {
if (empty($_POST['edit-profile-form'])) {
utils::check_session(NABU_ROUTES['home']);
$user = array();
$profilesModel = new profilesModel();
// Obtiene los datos de perfil del usuario de sesión.
$profile = $profilesModel -> get('id', $_SESSION['user']['id']);
unset($profilesModel);
if (empty($profile))
utils::redirect(NABU_ROUTES['home']);
$profile['avatar'] = utils::url_image('avatar', $profile['avatar']);
$profile['background'] = utils::url_image('background', $profile['background']);
if (empty($profile['description']))
$profile['description'] = '';
$token = csrf::generate();
$messages = messages::get();

View file

@ -52,4 +52,15 @@ class utils {
public static function url_slug(string $title) {
return date('Ymd') . '-' . preg_replace('/[^A-Za-z0-9-]+/', '-', strtolower($title));
}
// @return la URL completa de una imagen.
public static function url_image(string $category, $filename) {
$url = NABU_DEFAULT[$category];
if (!empty($filename))
if (file_exists(NABU_DIRECTORY['storage_' . $category . 's'] . '/' . $filename))
$url = NABU_DIRECTORY[$category . 's'] . '/' . $filename;
return $url;
}
}

View file

@ -0,0 +1,48 @@
<?php
/*
* Este archivo es parte de Nabu.
*
* Nabu es software libre: puedes redistribuirlo y/o modificarlo
* bajo los términos de la Licencia Pública General de GNU Affero publicada por
* la Free Software Foundation, ya sea la versión 3 de la Licencia, o
* (a su elección) cualquier versión posterior.
*
* Nabu se distribuye con la esperanza de que sea de utilidad,
* pero SIN NINGUNA GARANTÍA; incluso sin la garantía implícita de
* COMERCIABILIDAD o APTITUD PARA UN PROPÓSITO PARTICULAR. Consulte la
* Licencia Pública General de GNU Affero para obtener más detalles.
*
* Debería haber recibido una copia de la Licencia Pública General de GNU Affero
* junto con este programa. De lo contrario, consulte <https://www.gnu.org/licenses/>.
*/
defined('NABU') || exit();
class profilesModel extends dbConnection {
public function __construct() {
parent::__construct();
}
// @return un array asociativo con los datos de perfil de un usuario.
public function get(string $column, $pattern) {
$query = 'SELECT u.name, u.username, p.avatar, p.background, p.description '.
'FROM users AS u INNER JOIN profiles AS p ON u.id = p.id ' .
'WHERE u.' . $column . ' = ? LIMIT 1';
try {
$prepare = $this -> pdo -> prepare($query);
$prepare -> execute(array($pattern));
return $prepare -> fetch();
}
catch (PDOException $e) {
$this -> errors($e -> getMessage(), 'tuvimos un problema para mostrar los datos de tu perfil');
}
}
public function __destruct() {
parent::__destruct();
$this -> pdo = null;
}
}

View file

@ -36,8 +36,8 @@
<span></span><a href="<?= NABU_ROUTES['all-articles'] ?>">Artículos</a>
</li>
<?php if (isset($_SESSION['user'])): ?>
<?php $nav_user = $_SESSION['user'] ?>
<?php if ($nav_user['role'] == 'admin'): ?>
<?php $user = $_SESSION['user'] ?>
<?php if ($user['role'] == 'admin'): ?>
<li class="nav__item">
<a href="<?= NABU_ROUTES['admin'] ?>">Administración</a>
</li>
@ -52,7 +52,7 @@
<a href="<?= NABU_ROUTES['post-article'] ?>">Publicar un post</a>
</li>
<li class="nav__item">
<a href="<?= NABU_ROUTES['profile'] . '&user=' . urlencode($nav_user['username']) ?>"><?= utils::escape($nav_user['username']) ?></a>
<a href="<?= NABU_ROUTES['profile'] . '&user=' . urlencode($user['username']) ?>"><?= utils::escape($user['username']) ?></a>
</li>
<li class="nav__item">
<span></span><a href="<?= NABU_ROUTES['logout'] ?>">Cerrar sesión</a>

View file

@ -21,12 +21,12 @@
<!-- Estilos a cargar -->
<?php $styles = array(
'pages/post-article/post-article.css',
'pages/edit-profile/edit-profile.css',
) ?>
<!-- Estilos a cargar para el responsive design -->
<?php $desktop_styles = array(
array('file' => 'pages/post-article/post-article-desktop.css', 'attributes' => ''),
array('file' => 'pages/edit-profile/edit-profile-desktop.css', 'attributes' => ''),
) ?>
<?php require_once 'views/components/head.php' ?>
@ -45,7 +45,7 @@
<label for="avatar"><b>Editar foto de perfil</b></label>
</div>
<div>
<img src="<?= $user['avatar'] ?>" alt="Foto de perfil" width="8%">
<img src="<?= $profile['avatar'] ?>" alt="Foto de perfil" width="8%">
</div>
<div>
<input type="file" id="avatar" name="avatar" accept="<?= NABU_DEFAULT['image-formats'] ?>">
@ -56,7 +56,7 @@
<label for="background"><b>Editar fondo de perfil</b></label>
</div>
<div>
<img src="<?= $user['background'] ?>" alt="Fondo de perfil" width="32%">
<img src="<?= $profile['background'] ?>" alt="Fondo de perfil" width="32%">
</div>
<div>
<input type="file" id="background" name="background" accept="<?= NABU_DEFAULT['image-formats'] ?>">
@ -67,7 +67,7 @@
<label for="description"><b>Descripción</b></label>
</div>
<div>
<textarea id="description" name="description" maxlength="255" rows="5" cols="51"><?= $user['description'] ?></textarea>
<textarea id="description" name="description" maxlength="255" rows="5" cols="51"><?= $profile['description'] ?></textarea>
</div>
</div>
</fieldset>
@ -75,11 +75,11 @@
<legend>Datos personales</legend>
<div>
<label for="name"><b>Nombre completo</b></label>
<input type="text" id="name" name="name" minlength="5" maxlength="255" value="<?= $user['name'] ?>">
<input type="text" id="name" name="name" minlength="5" maxlength="255" value="<?= $profile['name'] ?>">
</div>
<div>
<label for="username"><b>Apodo</b></label>
<input type="text" id="username" name="username" minlength="1" value="<?= $user['username'] ?>" maxlength="255">
<input type="text" id="username" name="username" minlength="1" maxlength="255" value="<?= $profile['username'] ?>">
</div>
<div>
<label for="password"><b>Nueva constraseña</b></label>