lpwd-php-portfolio/lib/project.php

181 lines
4.3 KiB
PHP
Raw Normal View History

2016-01-27 13:14:19 +01:00
<?php
2016-01-27 13:23:07 +01:00
/**
* Retourne une liste de projets en fonction d'une pagination
*
* @param int $page La page courrante
2016-01-28 09:44:27 +01:00
* @param int $limit Nombre de projets par page
2016-01-27 13:23:07 +01:00
* @return array Les projets
*/
function getProjects($page = 1, $limit = 5)
2016-01-27 13:14:19 +01:00
{
if (!is_integer($page)) {
2016-01-28 11:22:33 +01:00
throw new InvalidArgumentException('The argument "page" must be an integer.');
2016-01-27 13:14:19 +01:00
}
2016-01-27 13:23:07 +01:00
2016-01-27 13:14:19 +01:00
if (!is_integer($limit)) {
2016-01-28 11:22:33 +01:00
throw new InvalidArgumentException('The argument "limit" must be an integer.');
2016-01-27 13:14:19 +01:00
}
$from = ($page - 1) * $limit;
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select id, title, illustration, description, date from project limit :from, :limit');
2016-01-27 13:23:07 +01:00
2016-01-27 13:14:19 +01:00
$query->bindParam(':from', $from, PDO::PARAM_INT);
$query->bindParam(':limit', $limit, PDO::PARAM_INT);
$query->execute();
return $query->fetchAll();
}
2016-01-27 13:23:07 +01:00
/**
* Retourne un projet en fonction de son id
*
* @param int $id id du projet
* @return array|false Le projet
*/
function getProject($id)
2016-01-27 13:14:19 +01:00
{
if (!is_integer($id)) {
2016-01-28 11:22:33 +01:00
throw new InvalidArgumentException('The argument "id" must be an integer.');
2016-01-27 13:14:19 +01:00
}
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select id, title, illustration, description, date from project where id=:id');
2016-01-27 13:23:07 +01:00
2016-01-27 13:14:19 +01:00
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query->execute();
return $query->fetch();
}
2016-01-27 13:23:07 +01:00
/**
* @param int $maxPerPage Nombre de projets par page
* @return int Le nombre de pages
*/
function getProjectNumberOfPages($maxPerPage)
2016-01-27 13:14:19 +01:00
{
if (!is_integer($maxPerPage)) {
2016-01-28 11:22:33 +01:00
throw new InvalidArgumentException('The argument "maxPerPage" must be an integer.');
2016-01-27 13:14:19 +01:00
}
2016-01-27 13:23:07 +01:00
2016-01-27 13:14:19 +01:00
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select count(*) as total from project');
$query->execute();
$result = $query->fetch();
2016-01-28 10:38:52 +01:00
return (int) ceil($result['total'] / $maxPerPage);
2016-01-27 13:14:19 +01:00
}
2016-01-27 13:23:07 +01:00
/**
* Returne les commentaires d'un projet
*
* @param int $id L'id du projet
2016-01-28 09:44:27 +01:00
* @return array Les commentaires du projet
2016-01-27 13:23:07 +01:00
*/
function getCommentsByProject($id)
2016-01-27 13:14:19 +01:00
{
// À réaliser
}
2016-01-27 13:31:10 +01:00
2016-01-28 12:06:35 +01:00
/**
* 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
2016-02-04 19:59:27 +01:00
* @param int $projectId L'id du projet associé
2016-01-28 12:06:35 +01:00
*/
2016-02-04 19:59:27 +01:00
function createComment($name, $email, $website, $content, DateTime $date, $projectId)
2016-01-28 12:06:35 +01:00
{
$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,
]);
}
2016-01-28 10:26:50 +01:00
2016-01-28 10:38:52 +01:00
/**
* Génère et retourne une pagination
*
* @param int $page La page courrante
* @param int $numberOfPages Le nombre de pages
* @return array Les pages
*/
2016-02-04 19:59:27 +01:00
function getProjectsPager($page, $numberOfPages)
2016-01-28 10:26:50 +01:00
{
2016-01-28 10:42:31 +01:00
if (!is_integer($page)) {
2016-01-28 11:22:33 +01:00
throw new InvalidArgumentException('The argument "page" must be an integer.');
2016-01-28 10:42:31 +01:00
}
2016-02-04 19:59:27 +01:00
2016-01-28 10:42:31 +01:00
if (!is_integer($numberOfPages)) {
2016-01-28 11:22:33 +01:00
throw new InvalidArgumentException('The argument "numberOfPages" must be an integer.');
2016-01-28 10:42:31 +01:00
}
2016-01-28 10:26:50 +01:00
$pages = [];
if ($numberOfPages > 1) {
$pages[] = array(
'title' => 'Première page',
'page' => 1,
'current' => $page === 1,
);
}
2016-02-04 19:59:27 +01:00
2016-01-28 10:26:50 +01:00
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,
);
}
}
2016-02-04 19:59:27 +01:00
2016-01-28 10:26:50 +01:00
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;
}