From 2b91fb80acd68f501c8f0d08118533cfd971dee9 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Thu, 11 Feb 2016 16:19:24 +0100 Subject: [PATCH] ajout commentaire --- README.md | 4 +-- lib/project.php | 37 ++++++++++++++++++++-- project.php | 84 +++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 111 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6253731..cafa498 100644 --- a/README.md +++ b/README.md @@ -58,14 +58,14 @@ Petit plus Les étudiants qui me proposeront une correction via des `pull-request` auront 1 point supplémentaire sur la note finale. Les intéressés devront -créer un compte sur [Gitlab](https://gitlab.deblan.org). Je les ajouterai +créer un compte sur [Gitlab](https://gitnet.fr). Je les ajouterai au dépôt et vous me transmettrez votre travail dans une branche portant votre nom : `nom-prenom`. #### Installation du projet ``` -$ git clone git@gitlab.deblan.org:deblan/lpwd-php-portfolio.git +$ git clone git@gitnet.fr:deblan/lpwd-php-portfolio.git $ git branch nom-prenom # à modifier $ git checkout nom-prenom $ ... modifications ... diff --git a/lib/project.php b/lib/project.php index b6cac57..caaeb3f 100644 --- a/lib/project.php +++ b/lib/project.php @@ -78,12 +78,29 @@ function getProjectNumberOfPages($maxPerPage) /** * Returne les commentaires d'un projet * - * @param int $id L'id du projet + * @param int $projectId L'id du projet * @return array Les commentaires du projet */ -function getCommentsByProject($id) +function getCommentsByProject($projectId) { - // À réaliser + $pdo = getDatabaseConnection(); + + $query = $pdo->prepare('select * from comment where project_id=:project_id'); + + $query->bindParam(':project_id', $projectId, PDO::PARAM_INT); + + $query->execute(); + + $comments = $query->fetchAll(); + + foreach ($comments as $index => $comment) { + $comment['avatar'] = getGravatar($comment['email']); + $comment['date'] = new DateTime($comment['date']); + + $comments[$index] = $comment; + } + + return $comments; } /** @@ -178,3 +195,17 @@ function getProjectsPager($page, $numberOfPages) return $pages; } + +/** + * Retourne une url gravatar en fonction d'un email + * + * @param string $email + * @param int $size + * @return string + */ +function getGravatar($email, $size = 50) +{ + $hash = md5(strtolower(trim($email))); + + return 'http://www.gravatar.com/avatar/'.$hash.'?r=g&s='.$size; +} diff --git a/project.php b/project.php index fdf96be..1e7d0d2 100644 --- a/project.php +++ b/project.php @@ -17,14 +17,40 @@ if (false === $project) { die; } -// createComment( -// 'Simon', -// 'simon@deblan.fr', -// 'https://www.deblan.io/', -// 'Mon second super commentaire !', -// new DateTime('now'), -// $id -// ); +if (isset($_POST['name'], $_POST['email'], $_POST['website'], $_POST['content'])) { + $name = trim($_POST['name']); + $email = trim($_POST['email']); + $website = trim($_POST['website']); + $content = trim($_POST['content']); + + $valid = true; + + foreach ([$name, $email, $content] as $field) { + if (empty($field)) { + $valid = false; + } + } + + if ($valid) { + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + $valid = false; + } + } + + if ($valid) { + createComment( + $name, + $email, + $website, + $content, + new DateTime('now'), + $id + ); + + header('Location: project.php?id='.$id); + die; + } +} $comments = getCommentsByProject($id); @@ -63,7 +89,47 @@ $comments = getCommentsByProject($id); -

Commentaires

+
+

Commentaires

+ +
+
+

+ + +

+

+ + +

+

+ + +

+

+ +

+ +

+ +

+
+
+ + +
+

+ + , + posté le format('d/m/Y à H:i') ?> +

+ +

+ +

+
+ +