lpwd-php-portfolio/lib/project.php

143 lines
3.1 KiB
PHP

<?php
/**
* Retourne une liste de projets en fonction d'une pagination
*
* @param int $page La page courrante
* @param int $limit Nombre de projets par page
* @return array Les projets
*/
function getProjects($page = 1, $limit = 5)
{
if (!is_integer($page)) {
throw new InvalidArgumentException('The argument "page" must be an interger.');
}
if (!is_integer($limit)) {
throw new InvalidArgumentException('The argument "limit" must be an interger.');
}
$from = ($page - 1) * $limit;
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select id, title, illustration, description, date from project 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 interger.');
}
$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 interger.');
}
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select count(*) as total from project');
$query->execute();
$result = $query->fetch();
return ceil($result['total'] / $maxPerPage);
}
/**
* Returne les commentaires d'un projet
*
* @param int $id L'id du projet
* @return array Les commentaires du projet
*/
function getCommentsByProject($id)
{
// À réaliser
}
// Prévoir une fonction pour enregistrer un commentaire
function getProjectsPager($page, $numberOfPages)
{
$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;
}