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); $query->execute(); return $query->fetchAll(); } /** * Retourne un projet en fonction de son id * * @param int $id id du projet * @return array|false Le projet */ function getProject($id) { if (!is_integer($id)) { throw new InvalidArgumentException('The argument "id" must be an integer.'); } $pdo = getDatabaseConnection(); $query = $pdo->prepare('select id, title, illustration, description, date from project where id=:id'); $query->bindParam(':id', $id, PDO::PARAM_INT); $query->execute(); return $query->fetch(); } /** * @param int $maxPerPage Nombre de projets par page * @return int Le nombre de pages */ function getProjectNumberOfPages($maxPerPage) { if (!is_integer($maxPerPage)) { throw new InvalidArgumentException('The argument "maxPerPage" must be an integer.'); } $pdo = getDatabaseConnection(); $query = $pdo->prepare('select count(*) as total from project'); $query->execute(); $result = $query->fetch(); return (int) ceil($result['total'] / $maxPerPage); } /** * Returne les commentaires d'un projet * * @param int $projectId L'id du projet * @return array Les commentaires du projet */ function getCommentsByProject($projectId) { $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; } /** * Enregistre un nouveau commentaire * * @param string $name Nom de l'auteur * @param string $email Email de l'auteur * @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é */ 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)' ); $query->execute([ ':name' => $name, ':email' => $email, ':website' => $website, ':content' => $content, ':date' => $date->format('Y-m-d H:i:s'), ':project_id' => $projectId, ]); } /** * Génère et retourne une pagination * * @param int $page La page courrante * @param int $numberOfPages Le nombre de pages * @return array Les pages */ 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.'); } $pages = []; if ($numberOfPages > 1) { $pages[] = array( 'title' => 'Première page', 'page' => 1, 'current' => $page === 1, ); } if ($page > 1) { $pages[] = array( 'title' => 'Précédent', 'page' => $page - 1, 'current' => false, ); } if ($numberOfPages > 1) { for ($i = 1; $i <= $numberOfPages; $i++) { $pages[] = array( 'title' => $i, 'page' => $i, 'current' => $page === $i, ); } } if ($page < $numberOfPages) { $pages[] = array( 'title' => 'Suivant', 'page' => $page + 1, 'current' => false, ); } if ($numberOfPages > 1) { $pages[] = array( 'title' => 'Dernière page', 'page' => $numberOfPages, 'current' => $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; }