diff --git a/README.md b/README.md index c6afced..f9400e9 100644 --- a/README.md +++ b/README.md @@ -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 $ git commit -m "Explications des modifications" $ git push -u origin nom-prenom ``` + +[sessions]: http://php.net/manual/fr/function.session-start.php diff --git a/lib/database.php b/lib/database.php index 1d014bb..d666144 100644 --- a/lib/database.php +++ b/lib/database.php @@ -3,7 +3,7 @@ /** * @return PDO Une instance PDO */ -function getDatabaseConnection() +function getDatabaseConnection() { static $pdo; diff --git a/lib/project.php b/lib/project.php index 566d20c..770c4a3 100644 --- a/lib/project.php +++ b/lib/project.php @@ -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; +} \ No newline at end of file diff --git a/project.php b/project.php index 2415db3..bb7ac60 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); @@ -39,71 +65,80 @@ $comments = getCommentsByProject($id); -

- -

+

+ +

-
-
-
- -
+
+
+
+ +
-

- -

+

+ +

- + -

- -

-
-
+

+ +

+
+
+

Commentaires

- - -

Posté par :

-

Email :

-

Site web :

- Commentaire : -

-

- - - - - -
- - - - - - - - - - -
- - - + +
+

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

+ +

+ +

+
+ +
- + \ No newline at end of file