Conflicts:
	lib/project.php
	project.php
This commit is contained in:
Lex 2016-03-10 13:53:30 +01:00
commit ce06182532
4 changed files with 181 additions and 93 deletions

View file

@ -43,29 +43,53 @@ Un commentaire est composé de :
La date est générée automatiquement.
Tous les champs sont obligatoires à l'exception du site web.
Il faudra afficher l'avatar de l'auteur en utilisant le service [gravatar](https://fr.gravatar.com/site/implement/images/php/). *Ne pas réutiliser ce code source mais ne vous génez par pour vous en inspirer.*
Il faudra afficher l'avatar de l'auteur en utilisant le service [gravatar](https://fr.gravatar.com/site/implement/images/php/). *Ne pas réutiliser ce code source mais ne vous génez pas pour vous en inspirer.*
Flux
----
Réalisez un flux RSS présentant les 10 derniers projets mis en ligne.
Vous réalisez également un flux Atom.
Vous devrez concevoir et réaliser des fonctions php.
Réécriture d'URL
----------------
Documentez-vous et réalisez une réécriture des URL (`url rewriting`) afin de les rendre plus sexy.
Je souhaites accéder aux projets via : http://localhost/projet/`{id}`/`{slug}`
Le slug est une chaine de caractère contenant le nom du projet sans les caractères spéciaux. Vous devez donc créer une fonction pour le générer.
Exemple : le slug de `Je suis une licorne, c'est classe quand même, non ?` est `je-suis-une-licorne-c-est-classe-quand-meme-non`. Si j'avais un projet avec ce titre et si son *id* était 42, alors l'adresse du projet serait : http://localhost/projet/42/je-suis-une-licorne-c-est-classe-quand-meme-non
Administration
--------------
Pour le moment, la rédaction des articles est réalisée via phpmyadmin. Par ailleurs, il n'y a aucun modération des commentaires.
Vous devez donc développer un backoffice (BO) pour administrer les contenus du portfolio.
L'accès au BO est reservé aux administrateurs. Prévoyez ainsi une authentification avec la méthode que vous préférez parmis :
* Base de données + formulaire __(recommandé)__ - @see [sessions] [sessions]
* Identifiants en durs dans le code + formulaire (il manque pas grand chose pour utiliser une bdd ;)) - @see [sessions] [sessions]
* Basic ou Digest HTTP authentication (pour les flemmards !)
Les commentaires doivent pouvoir être mis hors-ligne voire supprimés.
Les articles doivent pouvoir être ajoutés, modifiés et supprimés depuis l'interface.
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 ...
@ -73,3 +97,5 @@ $ git commit add <les fichiers modifiés>
$ git commit -m "Explications des modifications"
$ git push -u origin nom-prenom
```
[sessions]: http://php.net/manual/fr/function.session-start.php

View file

@ -3,7 +3,7 @@
/**
* @return PDO Une instance PDO
*/
function getDatabaseConnection()
function getDatabaseConnection()
{
static $pdo;

View file

@ -7,7 +7,7 @@
* @param int $limit Nombre de projets par page
* @return array Les projets
*/
function getProjects($page = 1, $limit = 5)
function getProjects($page = 1, $limit = 5, $orderBy = 'date', $orderBySens = 'ASC')
{
if (!is_integer($page)) {
throw new InvalidArgumentException('The argument "page" must be an integer.');
@ -21,7 +21,17 @@ function getProjects($page = 1, $limit = 5)
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select id, title, illustration, description, date from project limit :from, :limit');
$query = $pdo->prepare(
'
select
id, title, illustration, description, date
from
project
order by
'.$orderBy.' '.$orderBySens.'
limit
:from, :limit'
);
$query->bindParam(':from', $from, PDO::PARAM_INT);
$query->bindParam(':limit', $limit, PDO::PARAM_INT);
@ -37,8 +47,6 @@ function getProjects($page = 1, $limit = 5)
* @param int $id id du projet
* @return array|false Le projet
*/
function getProject($id)
{
if (!is_integer($id)) {
@ -80,21 +88,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)
{
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select id, name, website, email, date, content from comment where project_id=:id');
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query = $pdo->prepare('select * from comment where project_id=:project_id');
$query->bindParam(':project_id', $projectId, PDO::PARAM_INT);
$query->execute();
return $query->fetchAll();
$comments = $query->fetchAll();
foreach ($comments as $index => $comment) {
$comment['avatar'] = getGravatar($comment['email']);
$comment['date'] = new DateTime($comment['date']);
$comments[$index] = $comment;
}
return $comments;
}
/**
@ -105,14 +121,15 @@ function getCommentsByProject($id)
* @param string $website Site web de l'auteur
* @param string $content Contenu du commentaire
* @param DateTime $date Date du commentaire
* @param int $projectId L'id du projet associé
* @param int $projectId L'id du projet associé
*/
function createComment($name, $email, $website, $content, DateTime $date, $projectId)
function createComment($name, $email, $website, $content, DateTime $date, $projectId)
{
$pdo = getDatabaseConnection();
$query = $pdo->prepare(
'insert into comment(name, email, website, content, date, project_id) value(:name, :email, :website, :content, :date, :project_id)'
'insert into comment(name, email, website, content, date, project_id)
value(:name, :email, :website, :content, :date, :project_id)'
);
$query->execute([
@ -132,12 +149,12 @@ function createComment($name, $email, $website, $content, DateTime $date, $proje
* @param int $numberOfPages Le nombre de pages
* @return array Les pages
*/
function getProjectsPager($page, $numberOfPages)
function getProjectsPager($page, $numberOfPages)
{
if (!is_integer($page)) {
throw new InvalidArgumentException('The argument "page" must be an integer.');
}
if (!is_integer($numberOfPages)) {
throw new InvalidArgumentException('The argument "numberOfPages" must be an integer.');
}
@ -151,7 +168,7 @@ function getProjectsPager($page, $numberOfPages)
'current' => $page === 1,
);
}
if ($page > 1) {
$pages[] = array(
'title' => 'Précédent',
@ -169,7 +186,7 @@ function getProjectsPager($page, $numberOfPages)
);
}
}
if ($page < $numberOfPages) {
$pages[] = array(
'title' => 'Suivant',
@ -189,6 +206,16 @@ 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);
@ -39,71 +65,80 @@ $comments = getCommentsByProject($id);
</title>
</head>
<body>
<h1>
<?php echo $project['title'] ?>
</h1>
<h1>
<?php echo $project['title'] ?>
</h1>
<section>
<article>
<figure>
<img src="images/<?php echo $project['illustration'] ?>" alt="" width="130px" />
</figure>
<section>
<article>
<figure>
<img src="images/<?php echo $project['illustration'] ?>" alt="" width="130px" />
</figure>
<p class="description">
<?php echo $project['description'] ?>
</p>
<p class="description">
<?php echo $project['description'] ?>
</p>
<?php $date = new DateTime($project['date']); ?>
<?php $date = new DateTime($project['date']); ?>
<p class="date">
<time datetime="<?php echo $date->format(DateTime::W3C) ?>">
<?php echo $date->format('d/m/Y') ?>
</time>
</p>
</article>
</section>
<p class="date">
<time datetime="<?php echo $date->format(DateTime::W3C) ?>">
<?php echo $date->format('d/m/Y') ?>
</time>
</p>
</article>
</section>
<section>
<h2>Commentaires</h2>
<?php if (count($comments)): ?>
<?php foreach ($comments as $comment): ?>
<p>Posté par : <?php echo $comment['name'] ?></p>
<p>Email : <?php echo $comment['email'] ?></p>
<p>Site web : <?php echo $comment['website'] ?></p>
<span>Commentaire :</span>
<p class="comment-content"><?php echo $comment['content'] ?></p>
<p class="date"><?php echo $comment['date'] ?></p>
<?php endforeach ?>
<?php endif ?>
<form method="post" action="lib/project.php">
<label for="name">Nom :</label>
<input type="text" name="name" id="name">
<label for="email">Email :</label>
<input type="text" name="email" id="email">
<label for="website">Votre site web</label>
<input type="text" name="website" id="website">
<label for="content">Votre message</label>
<input type="text" name="content" id="content">
<input type="hidden" name="project_id" value="<?php echo $id ?>">
<input type="submit" value="Envoyer">
</form>
<!--
Afficher la liste des commentaires
<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>
Un commentaire est composé de :
- son auteur
- le site web de l'auteur
- l'email de l'auteur
- sa date de publication
- son contenu
<p>
<input type="submit" value="Go go go!" />
</p>
</form>
</div>
Il faudra afficher l'avatar de l'auteur en utilisant gravatar : https://fr.gravatar.com/site/implement/images/php/
Attention : ne pas utiliser cette fonction, il faut écrire la votre !
-->
<?php foreach ($comments as $comment): ?>
<div>
<p>
<img src="<?php echo $comment['avatar'] ?>" alt="" />
<?php if ($comment['website']): ?>
<a href="<?php echo htmlspecialchars($comment['website']) ?>">
<?php endif ?>
<?php echo htmlspecialchars($comment['name']) ?>
<?php if ($comment['website']): ?>
</a>
<?php endif ?>
- Posté le <?php echo $comment['date']->format('d/m/Y à H:i') ?>
</p>
<p>
<?php echo nl2br(htmlspecialchars($comment['content'])) ?>
</p>
</div>
<?php endforeach ?>
</section>
</body>
</html>
</html>