[Muestra] la foto de perfil de un usuario de sesión para escribir un comentario
This commit is contained in:
parent
57ee90f0b2
commit
977d912c15
|
@ -108,7 +108,6 @@ class articlesController {
|
|||
// Renderiza un artículo publicado
|
||||
// y publica un comentario con el método POST.
|
||||
static public function article() {
|
||||
$token = csrf::generate();
|
||||
$messages = messages::get();
|
||||
|
||||
$validations = new validations(NABU_ROUTES['home']);
|
||||
|
@ -120,88 +119,107 @@ class articlesController {
|
|||
|
||||
$articlesModel = new articlesModel();
|
||||
|
||||
// Obtiene el contenido del artículo.
|
||||
$article = $articlesModel -> get_article($data['slug']);
|
||||
|
||||
$articles = array();
|
||||
$article = $articlesModel -> find_article($data['slug']);
|
||||
|
||||
if (empty($article))
|
||||
utils::redirect(NABU_ROUTES['home']);
|
||||
|
||||
unset($validatiosn, $data, $articlesModel);
|
||||
|
||||
$login = array('avatar' => null);
|
||||
|
||||
$login['avatar'] = utils::url_image('avatar', $login['avatar']);
|
||||
|
||||
require_once 'libs/parsedown-1.7.4/Parsedown.php';
|
||||
|
||||
// Formatea los datos del artículo.
|
||||
$article['title'] = utils::escape($article['title']);
|
||||
$article['cover'] = utils::url_image('cover', $article['cover']);
|
||||
$article['author'] = utils::escape($article['author']);
|
||||
$article['avatar'] = utils::url_image('avatar', $article['avatar']);
|
||||
$article['profile'] = NABU_ROUTES['profile'] . '&user=' . urlencode($article['username']);
|
||||
$article['username'] = utils::escape($article['username']);
|
||||
|
||||
$parsedown = new Parsedown;
|
||||
$parsedown -> setSafeMode(true);
|
||||
|
||||
// Convierte el artículo Markdown en HTML.
|
||||
$article['body'] = $parsedown -> text($article['body']);
|
||||
|
||||
if (empty($article['description']))
|
||||
$article['description'] = NABU_DEFAULT['description'];
|
||||
|
||||
$article['description'] = utils::escape($article['description']);
|
||||
|
||||
$date = date_parse($article['date']);
|
||||
|
||||
switch ($date['month']) {
|
||||
case 1:
|
||||
$month = 'Enero';
|
||||
break;
|
||||
case 2:
|
||||
$month = 'Febrero';
|
||||
break;
|
||||
case 3:
|
||||
$month = 'Marzo';
|
||||
break;
|
||||
case 4:
|
||||
$month = 'Abril';
|
||||
break;
|
||||
case 5:
|
||||
$month = 'Mayo';
|
||||
break;
|
||||
case 6:
|
||||
$month = 'Junio';
|
||||
break;
|
||||
case 7:
|
||||
$month = 'Julio';
|
||||
break;
|
||||
case 8:
|
||||
$month = 'Agosto';
|
||||
break;
|
||||
case 9:
|
||||
$month = 'Septiembre';
|
||||
break;
|
||||
case 10:
|
||||
$month = 'Octubre';
|
||||
break;
|
||||
case 11:
|
||||
$month = 'Noviembre';
|
||||
break;
|
||||
case 12:
|
||||
$month = 'Diciembre';
|
||||
break;
|
||||
default:
|
||||
$month = '';
|
||||
}
|
||||
|
||||
$article['date'] = $date['day'] . ' de ' . $month . ' del ' . $date['year'];
|
||||
|
||||
$view = NABU_ROUTES['article'] . '&slug=' . $article['slug'];
|
||||
|
||||
require_once 'views/pages/article.php';
|
||||
if (empty($_POST['comments-form'])) {
|
||||
// Obtiene el contenido del artículo.
|
||||
$article = $articlesModel -> get_article($article['id']);
|
||||
|
||||
$articles = array();
|
||||
|
||||
$login = array('avatar' => null);
|
||||
|
||||
// Obtiene la foto de perfil del usuario de sesión para mostrar en los comentarios.
|
||||
if (isset($_SESSION['user'])) {
|
||||
$login = $articlesModel -> get_avatar($_SESSION['user']['id']);
|
||||
|
||||
if (empty($login))
|
||||
utils::redirect(NABU_ROUTES['logout']);
|
||||
}
|
||||
|
||||
$login['avatar'] = utils::url_image('avatar', $login['avatar']);
|
||||
|
||||
require_once 'libs/parsedown-1.7.4/Parsedown.php';
|
||||
|
||||
// Formatea los datos del artículo.
|
||||
$article['title'] = utils::escape($article['title']);
|
||||
$article['cover'] = utils::url_image('cover', $article['cover']);
|
||||
$article['author'] = utils::escape($article['author']);
|
||||
$article['avatar'] = utils::url_image('avatar', $article['avatar']);
|
||||
$article['profile'] = NABU_ROUTES['profile'] . '&user=' . urlencode($article['username']);
|
||||
$article['username'] = utils::escape($article['username']);
|
||||
|
||||
$parsedown = new Parsedown;
|
||||
$parsedown -> setSafeMode(true);
|
||||
|
||||
// Convierte el artículo Markdown en HTML.
|
||||
$article['body'] = $parsedown -> text($article['body']);
|
||||
|
||||
if (empty($article['description']))
|
||||
$article['description'] = NABU_DEFAULT['description'];
|
||||
|
||||
$article['description'] = utils::escape($article['description']);
|
||||
|
||||
// Segmenta la fecha en un array asociativo.
|
||||
$date = date_parse($article['date']);
|
||||
|
||||
switch ($date['month']) {
|
||||
case 1:
|
||||
$month = 'Enero';
|
||||
break;
|
||||
case 2:
|
||||
$month = 'Febrero';
|
||||
break;
|
||||
case 3:
|
||||
$month = 'Marzo';
|
||||
break;
|
||||
case 4:
|
||||
$month = 'Abril';
|
||||
break;
|
||||
case 5:
|
||||
$month = 'Mayo';
|
||||
break;
|
||||
case 6:
|
||||
$month = 'Junio';
|
||||
break;
|
||||
case 7:
|
||||
$month = 'Julio';
|
||||
break;
|
||||
case 8:
|
||||
$month = 'Agosto';
|
||||
break;
|
||||
case 9:
|
||||
$month = 'Septiembre';
|
||||
break;
|
||||
case 10:
|
||||
$month = 'Octubre';
|
||||
break;
|
||||
case 11:
|
||||
$month = 'Noviembre';
|
||||
break;
|
||||
case 12:
|
||||
$month = 'Diciembre';
|
||||
break;
|
||||
default:
|
||||
$month = '';
|
||||
}
|
||||
|
||||
$article['date'] = $date['day'] . ' de ' . $month . ' del ' . $date['year'];
|
||||
|
||||
unset($validations, $data, $articlesModel, $parsedown, $date, $month);
|
||||
|
||||
$token = csrf::generate();
|
||||
|
||||
require_once 'views/pages/article.php';
|
||||
|
||||
exit();
|
||||
}
|
||||
|
||||
csrf::validate($_POST['csrf']);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ class articlesModel extends dbConnection {
|
|||
|
||||
// @return un array asociativo con los datos de un artículo.
|
||||
public function find_article(string $slug) {
|
||||
$query = 'SELECT id FROM articles WHERE slug = ? LIMIT 1';
|
||||
$query = 'SELECT id, slug FROM articles WHERE slug = ? LIMIT 1';
|
||||
|
||||
try {
|
||||
$prepare = $this -> pdo -> prepare($query);
|
||||
|
@ -115,18 +115,18 @@ class articlesModel extends dbConnection {
|
|||
}
|
||||
|
||||
// @return el contenido de un artículo.
|
||||
public function get_article(string $slug) {
|
||||
public function get_article(int $id) {
|
||||
$query = 'SELECT a.title, a.body, a.slug, a.cover, a.modification_date AS date, ' .
|
||||
'u.id AS user_id, u.name AS author, u.username, p.avatar, p.description ' .
|
||||
'FROM articles AS a ' .
|
||||
'INNER JOIN users AS u ON a.user_id = u.id ' .
|
||||
'LEFT JOIN profiles AS p ON u.id = p.id ' .
|
||||
'WHERE a.authorized = TRUE AND a.slug = ? LIMIT 1';
|
||||
'WHERE a.authorized = TRUE AND a.id = ? LIMIT 1';
|
||||
|
||||
try {
|
||||
$prepare = $this -> pdo -> prepare($query);
|
||||
|
||||
$prepare -> execute(array($slug));
|
||||
$prepare -> execute(array($id));
|
||||
|
||||
return $prepare -> fetch();
|
||||
}
|
||||
|
@ -135,6 +135,22 @@ class articlesModel extends dbConnection {
|
|||
}
|
||||
}
|
||||
|
||||
// @return la foto de perfil de un usuario.
|
||||
public function get_avatar(int $id) {
|
||||
$query = 'SELECT avatar FROM profiles WHERE id = ? LIMIT 1';
|
||||
|
||||
try {
|
||||
$prepare = $this -> pdo -> prepare($query);
|
||||
|
||||
$prepare -> execute(array($id));
|
||||
|
||||
return $prepare -> fetch();
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
$this -> errors($e -> getMessage(), 'tuvimos un problema para obtener la foto de perfil de un usuario');
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
parent::__destruct();
|
||||
$this -> pdo = null;
|
||||
|
|
|
@ -107,6 +107,8 @@
|
|||
</div>
|
||||
</section>
|
||||
|
||||
<?php require_once 'views/components/messages.php' ?>
|
||||
|
||||
<section class="comments">
|
||||
<div class="comments__container">
|
||||
<h2 class="comments__title">Deja tu opinión al autor</h2>
|
||||
|
@ -117,13 +119,13 @@
|
|||
<picture class="author-info__image comment__user-image-container">
|
||||
<img class="author__image comment__user-image" src="<?= $login['avatar'] ?>">
|
||||
</picture>
|
||||
<form class="comments__form">
|
||||
<form class="comments__form" method="POST" action="<?= $view ?>">
|
||||
<input type="hidden" name="csrf" value="<?= $token ?>">
|
||||
<textarea class="comments__textarea" placeholder="Hazle saber que estuviste aqui" maxlength="255" name="textarea"></textarea>
|
||||
<input type="submit" class="comments__button" value="Enviar">
|
||||
<input type="submit" name="comments-form" class="comments__button" value="Enviar">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<?php require_once 'views/components/messages.php' ?>
|
||||
<?php require_once 'views/components/footer.php' ?>
|
||||
|
|
Loading…
Reference in a new issue