Salut à tous,
Tout d'abord, je sais que ce Post serais plus à ça place sur DevBBS mais vu qu'il y a 4 Post par an, j'ai préféré le mettre ici.
Un truc récurrent quand on développe, c'est la pagination.
Jusqu’à maintenant je la recodais à chaque fois mais la j'ai eu un peu plus de temps que d'habitude donc j'ai fait une classe dédiée.
Plutôt que de me la garder, la voici. Si vous avez des idées d'améliorations a y apporter ne vous gêner pas !
Il y a le code Twig en bas.
[code:1:31767da61d]<?php
/**
* Class de Pagination
*
* @author 2ShEp
* @version 0.1
*/
class Pagination {
private $nbEleByPage = null;
private $nbEle = null;
private $nbPage = null;
private $currentPage = null;
private $output = array();
public function __construct() {
}
/**
* Initialise les attributs de la classe
*
* @param Integer $nbEle
* @param Integer $nbEleByPage
* @param Integer $currentPage
* @throws Exception
*/
public function init($nbEle, $nbEleByPage = 20, $currentPage = 1) {
$this->nbEle = (int) $nbEle;
$this->nbEleByPage = (int) $nbEleByPage;
$this->nbPage = ceil($this->nbEle / $this->nbEleByPage);
$this->currentPage = (is_numeric($currentPage)) ? $currentPage : 1;
if ($this->currentPage > $this->nbPage)
throw new Exception("Page inexistante");
return true;
}
/**
* Génère le tableau d'informations
*
* @param String $type ENUM('reduce','all')
* @param Integer $nb_liens
* @return Boolean
*/
public function generate($type = 'reduce', $nb_liens = 7){
switch($type){
case "all":
break;
case "reduce":
// first & previous
if ($this->currentPage > 1) {
// first
$this->output[] = array('lien_actif' => 1, 'num_page' => 1, 'lib' => '<<');
// previous
$this->output[] = array('lien_actif' => 1, 'num_page' => $this->currentPage-1, 'lib' => '<');
}
// liste
$median = ceil(($nb_liens-1)/2);
$i = $this->currentPage - $median;
if ($this->currentPage + $median >= $this->nbPage) {
$i = $this->nbPage - $nb_liens + 1;
}
if ($i <= 0) {
$i = 1;
}
while ($i < $this->currentPage) {
$this->output[] = array('lien_actif' => 1, 'num_page' => $i, 'lib' => $i);
$i++;
}
$i = $this->currentPage;
while (($i <= ($this->currentPage + $median) || $i <= $nb_liens) && $i <= $this->nbPage) {
$this->output[] = array('lien_actif' => (($i != $this->currentPage) ? 1 : 0), 'num_page' => $i, 'lib' => $i);
$i++;
}
// next & last
if ($this->currentPage < $this->nbPage) {
// next
$this->output[] = array('lien_actif' => 1, 'num_page' => $this->currentPage+1, 'lib' => '>');
// last
$this->output[] = array('lien_actif' => 1, 'num_page' => $this->nbPage, 'lib' => '>>');
}
break;
}
return true;
}
/**
*
* @return Array
*/
public function getOutput(){
return $this->output;
}
/**
* Retourne un code HTML basique
*
* @return String : Code HTML
*/
public function getHtmlOutput(){
$html_output = "";
if($this->nbPage > 1){
$html_output .= "<div class=\"pagination\"><span>Pages : </span>";
foreach($this->output as $page){
$html_output .= "<span>";
if($page["lien_actif"] == 1){
if(strstr($_SERVER["REQUEST_URI"],"no_page"))
$url = preg_replace("#&no_page=[0-9]*#","&no_page=".$page["num_page"],$_SERVER["REQUEST_URI"]);
else
$url = $_SERVER["REQUEST_URI"]."&no_page=".$page["num_page"];
$html_output .= "<a href=\"".$url."\">".$page["lib"]."</a> ";
}else{
$html_output .= "<strong>".$page["num_page"]."</strong> ";
}
$html_output .= "</span>";
}
$html_output .= "</div>";
}
return $html_output;
}
public function getNbPage(){
return $this->nbPage;
}
}
?>[/code:1:31767da61d]
Voici le code Twig qui va avec, si ca en intéresse certains.
[code:1:31767da61d]{% block pagination %}
{% if pagination|length > 1 %}
<div class="pagination">
Pages :
{% for p in pagination %}
<span>
{% if p.lien_actif == 1 %}
{% set parameters = parameters|merge({'page': p.num_page}) %}
<a href="{{ path(app.request.attributes.get('_route'), parameters) }}">{{ p.lib }}</a>
{% else %}
<strong>{{ p.num_page }}</strong>
{% endif %}
</span>
{% endfor %}
</div>
{% endif %}
{% endblock %}[/code:1:31767da61d]
Merci d'avoir lu jusqu’à la :)
++
2ShEp