ajout commentaire

This commit is contained in:
Simon Vieille 2016-02-11 16:19:24 +01:00
parent 64cbb94829
commit 2b91fb80ac
3 changed files with 111 additions and 14 deletions

View File

@ -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 ...

View File

@ -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;
}

View File

@ -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);
</article>
</section>
<h2>Commentaires</h2>
<section>
<h2>Commentaires</h2>
<div>
<form action="" method="POST">
<p>
<label for="form-name">Nom</label>
<input name="name" required id="form-name" type="text" />
</p>
<p>
<label for="form-mail">Email</label>
<input name="email" required id="form-mail" type="mail" />
</p>
<p>
<label for="form-website">Site web</label>
<input name="website" id="form-website" type="website" />
</p>
<p>
<textarea name="content" required rows="10" cols="30"></textarea>
</p>
<p>
<input type="submit" value="Go go go!" />
</p>
</form>
</div>
<?php foreach ($comments as $comment): ?>
<div>
<p>
<img src="<?php echo $comment['avatar'] ?>" alt="" />
<?php echo $comment['name'] ?>,
posté le <?php echo $comment['date']->format('d/m/Y à H:i') ?>
</p>
<p>
<?php echo $comment['content'] ?>
</p>
</div>
<?php endforeach ?>
</section>
<!--
Afficher la liste des commentaires