diff --git a/routeSQL.php b/routeSQL.php index 8416951..663d9a6 100644 --- a/routeSQL.php +++ b/routeSQL.php @@ -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 = "
"; + $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 .= "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 .= "". dgettext("domframework","Add new entry")."\n"; + $content .= $this->paginator(count ($datas), $page, $num); $content .= "\n"; $content .= " \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 .= " "; if ($this->displayActions && $this->actionsAtEnd === false) { @@ -171,6 +232,7 @@ class routeSQL $content .= ""; } $content .= "\n"; + $i++; } } $content .= " \n";