Merge branch 'master' of https://gitlab.deblan.org/deblan/lpwd-php-portfolio into pitton
Conflicts: lib/project.php project.php
This commit is contained in:
commit
ce06182532
34
README.md
34
README.md
|
@ -43,29 +43,53 @@ Un commentaire est composé de :
|
||||||
|
|
||||||
La date est générée automatiquement.
|
La date est générée automatiquement.
|
||||||
Tous les champs sont obligatoires à l'exception du site web.
|
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
|
Flux
|
||||||
----
|
----
|
||||||
|
|
||||||
Réalisez un flux RSS présentant les 10 derniers projets mis en ligne.
|
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.
|
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
|
Petit plus
|
||||||
----------
|
----------
|
||||||
|
|
||||||
Les étudiants qui me proposeront une correction via des `pull-request`
|
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
|
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
|
au dépôt et vous me transmettrez votre travail dans une branche portant
|
||||||
votre nom : `nom-prenom`.
|
votre nom : `nom-prenom`.
|
||||||
|
|
||||||
#### Installation du projet
|
#### 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 branch nom-prenom # à modifier
|
||||||
$ git checkout nom-prenom
|
$ git checkout nom-prenom
|
||||||
$ ... modifications ...
|
$ ... modifications ...
|
||||||
|
@ -73,3 +97,5 @@ $ git commit add <les fichiers modifiés>
|
||||||
$ git commit -m "Explications des modifications"
|
$ git commit -m "Explications des modifications"
|
||||||
$ git push -u origin nom-prenom
|
$ git push -u origin nom-prenom
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[sessions]: http://php.net/manual/fr/function.session-start.php
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
/**
|
/**
|
||||||
* @return PDO Une instance PDO
|
* @return PDO Une instance PDO
|
||||||
*/
|
*/
|
||||||
function getDatabaseConnection()
|
function getDatabaseConnection()
|
||||||
{
|
{
|
||||||
static $pdo;
|
static $pdo;
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
* @param int $limit Nombre de projets par page
|
* @param int $limit Nombre de projets par page
|
||||||
* @return array Les projets
|
* @return array Les projets
|
||||||
*/
|
*/
|
||||||
function getProjects($page = 1, $limit = 5)
|
function getProjects($page = 1, $limit = 5, $orderBy = 'date', $orderBySens = 'ASC')
|
||||||
{
|
{
|
||||||
if (!is_integer($page)) {
|
if (!is_integer($page)) {
|
||||||
throw new InvalidArgumentException('The argument "page" must be an integer.');
|
throw new InvalidArgumentException('The argument "page" must be an integer.');
|
||||||
|
@ -21,7 +21,17 @@ function getProjects($page = 1, $limit = 5)
|
||||||
|
|
||||||
$pdo = getDatabaseConnection();
|
$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(':from', $from, PDO::PARAM_INT);
|
||||||
$query->bindParam(':limit', $limit, 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
|
* @param int $id id du projet
|
||||||
* @return array|false Le projet
|
* @return array|false Le projet
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function getProject($id)
|
function getProject($id)
|
||||||
{
|
{
|
||||||
if (!is_integer($id)) {
|
if (!is_integer($id)) {
|
||||||
|
@ -80,21 +88,29 @@ function getProjectNumberOfPages($maxPerPage)
|
||||||
/**
|
/**
|
||||||
* Returne les commentaires d'un projet
|
* 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
|
* @return array Les commentaires du projet
|
||||||
*/
|
*/
|
||||||
|
function getCommentsByProject($projectId)
|
||||||
function getCommentsByProject($id)
|
|
||||||
{
|
{
|
||||||
$pdo = getDatabaseConnection();
|
$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();
|
$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 $website Site web de l'auteur
|
||||||
* @param string $content Contenu du commentaire
|
* @param string $content Contenu du commentaire
|
||||||
* @param DateTime $date Date 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();
|
$pdo = getDatabaseConnection();
|
||||||
|
|
||||||
$query = $pdo->prepare(
|
$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([
|
$query->execute([
|
||||||
|
@ -132,12 +149,12 @@ function createComment($name, $email, $website, $content, DateTime $date, $proje
|
||||||
* @param int $numberOfPages Le nombre de pages
|
* @param int $numberOfPages Le nombre de pages
|
||||||
* @return array Les pages
|
* @return array Les pages
|
||||||
*/
|
*/
|
||||||
function getProjectsPager($page, $numberOfPages)
|
function getProjectsPager($page, $numberOfPages)
|
||||||
{
|
{
|
||||||
if (!is_integer($page)) {
|
if (!is_integer($page)) {
|
||||||
throw new InvalidArgumentException('The argument "page" must be an integer.');
|
throw new InvalidArgumentException('The argument "page" must be an integer.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_integer($numberOfPages)) {
|
if (!is_integer($numberOfPages)) {
|
||||||
throw new InvalidArgumentException('The argument "numberOfPages" must be an integer.');
|
throw new InvalidArgumentException('The argument "numberOfPages" must be an integer.');
|
||||||
}
|
}
|
||||||
|
@ -151,7 +168,7 @@ function getProjectsPager($page, $numberOfPages)
|
||||||
'current' => $page === 1,
|
'current' => $page === 1,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($page > 1) {
|
if ($page > 1) {
|
||||||
$pages[] = array(
|
$pages[] = array(
|
||||||
'title' => 'Précédent',
|
'title' => 'Précédent',
|
||||||
|
@ -169,7 +186,7 @@ function getProjectsPager($page, $numberOfPages)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($page < $numberOfPages) {
|
if ($page < $numberOfPages) {
|
||||||
$pages[] = array(
|
$pages[] = array(
|
||||||
'title' => 'Suivant',
|
'title' => 'Suivant',
|
||||||
|
@ -189,6 +206,16 @@ function getProjectsPager($page, $numberOfPages)
|
||||||
return $pages;
|
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;
|
||||||
|
}
|
167
project.php
167
project.php
|
@ -17,14 +17,40 @@ if (false === $project) {
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
|
|
||||||
// createComment(
|
if (isset($_POST['name'], $_POST['email'], $_POST['website'], $_POST['content'])) {
|
||||||
// 'Simon',
|
$name = trim($_POST['name']);
|
||||||
// 'simon@deblan.fr',
|
$email = trim($_POST['email']);
|
||||||
// 'https://www.deblan.io/',
|
$website = trim($_POST['website']);
|
||||||
// 'Mon second super commentaire !',
|
$content = trim($_POST['content']);
|
||||||
// new DateTime('now'),
|
|
||||||
// $id
|
$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);
|
$comments = getCommentsByProject($id);
|
||||||
|
|
||||||
|
@ -39,71 +65,80 @@ $comments = getCommentsByProject($id);
|
||||||
</title>
|
</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>
|
<h1>
|
||||||
<?php echo $project['title'] ?>
|
<?php echo $project['title'] ?>
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
<article>
|
<article>
|
||||||
<figure>
|
<figure>
|
||||||
<img src="images/<?php echo $project['illustration'] ?>" alt="" width="130px" />
|
<img src="images/<?php echo $project['illustration'] ?>" alt="" width="130px" />
|
||||||
</figure>
|
</figure>
|
||||||
|
|
||||||
<p class="description">
|
<p class="description">
|
||||||
<?php echo $project['description'] ?>
|
<?php echo $project['description'] ?>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<?php $date = new DateTime($project['date']); ?>
|
<?php $date = new DateTime($project['date']); ?>
|
||||||
|
|
||||||
<p class="date">
|
<p class="date">
|
||||||
<time datetime="<?php echo $date->format(DateTime::W3C) ?>">
|
<time datetime="<?php echo $date->format(DateTime::W3C) ?>">
|
||||||
<?php echo $date->format('d/m/Y') ?>
|
<?php echo $date->format('d/m/Y') ?>
|
||||||
</time>
|
</time>
|
||||||
</p>
|
</p>
|
||||||
</article>
|
</article>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
<section>
|
||||||
<h2>Commentaires</h2>
|
<h2>Commentaires</h2>
|
||||||
|
|
||||||
<?php if (count($comments)): ?>
|
<div>
|
||||||
<?php foreach ($comments as $comment): ?>
|
<form action="" method="POST">
|
||||||
<p>Posté par : <?php echo $comment['name'] ?></p>
|
<p>
|
||||||
<p>Email : <?php echo $comment['email'] ?></p>
|
<label for="form-name">Nom</label>
|
||||||
<p>Site web : <?php echo $comment['website'] ?></p>
|
<input name="name" required id="form-name" type="text" />
|
||||||
<span>Commentaire :</span>
|
</p>
|
||||||
<p class="comment-content"><?php echo $comment['content'] ?></p>
|
<p>
|
||||||
<p class="date"><?php echo $comment['date'] ?></p>
|
<label for="form-mail">Email</label>
|
||||||
<?php endforeach ?>
|
<input name="email" required id="form-mail" type="mail" />
|
||||||
<?php endif ?>
|
</p>
|
||||||
|
<p>
|
||||||
|
<label for="form-website">Site web</label>
|
||||||
|
<input name="website" id="form-website" type="website" />
|
||||||
<form method="post" action="lib/project.php">
|
</p>
|
||||||
<label for="name">Nom :</label>
|
<p>
|
||||||
<input type="text" name="name" id="name">
|
<textarea name="content" required rows="10" cols="30"></textarea>
|
||||||
<label for="email">Email :</label>
|
</p>
|
||||||
<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
|
|
||||||
|
|
||||||
Un commentaire est composé de :
|
<p>
|
||||||
- son auteur
|
<input type="submit" value="Go go go!" />
|
||||||
- le site web de l'auteur
|
</p>
|
||||||
- l'email de l'auteur
|
</form>
|
||||||
- sa date de publication
|
</div>
|
||||||
- son contenu
|
|
||||||
|
|
||||||
Il faudra afficher l'avatar de l'auteur en utilisant gravatar : https://fr.gravatar.com/site/implement/images/php/
|
<?php foreach ($comments as $comment): ?>
|
||||||
Attention : ne pas utiliser cette fonction, il faut écrire la votre !
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in a new issue