routeSQL : Add paginator

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2000 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-03-11 10:37:47 +00:00
parent 3ca3ad6e73
commit a45db83b9e

View File

@@ -96,6 +96,45 @@ class routeSQL
return $dataflash;
}
/** Display a paginator
$nbentries is the total number of elements
num is the number of elements displayed by page
page is the page to display */
private function paginator ($nbentries, $page, $num)
{
// The maximum of links available in the paginator
$maxClickPaginator = 10;
$prePage = false;
$postPage = false;
$content = "<div>";
$displayedNumbers = 0;
for ($i = 1 ; $i < ($nbentries/$num) ; $i++)
{
if ($i < ($page - $maxClickPaginator/2))
{
if ($prePage === false)
$content .= "... ";
$prePage = true;
continue;
}
if ($displayedNumbers >= $maxClickPaginator)
{
if ($postPage === false)
$content .= "... ";
$postPage = true;
continue;
}
$displayedNumbers++;
$content .= "<a href='".$this->url_prefix."?page=$i&amp;num=$num'";
if ($page == $i)
$content .= "style='background-color:#FFFF66;'";
$content .= ">$i</a> ";
}
$content .= "</div>";
return $content;
}
/** Create the routes and the associated actions */
public function routes ()
{
/** Add HTML routes */
@@ -105,18 +144,34 @@ class routeSQL
$route->redirect ("/".$this->url_prefix, "");
});
$route->get ($this->url_prefix, function () use ($route)
$route->get ($this->url_prefix."(\?(page={page})?&?(num={num})?)?",
function ($page, $num) use ($route)
{
// LIST ALL THE OBJECTS OF THE TABLE
// num is the number of elements displayed by page
// page is the page to display
$this->connect();
$csrf = new csrf ();
$token = $csrf->createToken ();
$datas = $this->objectDB->read ();
$titles = $this->objectDB->titles ();
if ($page === null || $page === "") $page = 1;
if ($num === null || $num === "") $num = 10;
if ($num > 1000)
$route->redirect ("/".$this->url_prefix."?page=$page&num=1000", "");
if ($page < 1)
$route->redirect ("/".$this->url_prefix."?page=1&num=$num", "");
// TODO : Push on the last page
if ($page*$num > count ($datas))
{
$route->redirect ("/".$this->url_prefix, "");
}
$content = $this->showflash ();
if ($this->displayActions)
$content .= "<a href='".$route->baseURL().$this->url_prefix."/add'>".
dgettext("domframework","Add new entry")."</a>\n";
$content .= $this->paginator(count ($datas), $page, $num);
$content .= "<table>\n";
$content .= " <thead>\n";
if ($this->displayActions && $this->actionsAtEnd === false)
@@ -135,8 +190,14 @@ class routeSQL
}
else
{
$i = 1;
foreach ($datas as $line)
{
if ($i <= (($page-1)*$num) || $i > (($page-1)*$num + $num))
{
$i++;
continue;
}
$content .= " <tr>";
if ($this->displayActions && $this->actionsAtEnd === false)
{
@@ -171,6 +232,7 @@ class routeSQL
$content .= "</td>";
}
$content .= "</tr>\n";
$i++;
}
}
$content .= " </tbody>\n";