diff --git a/VERSIONS_HOTLINE/18161_pager.txt b/VERSIONS_HOTLINE/18161_pager.txt new file mode 100644 index 0000000000000000000000000000000000000000..b40b13302b819cac2e05b7c006f31da52b3229ce --- /dev/null +++ b/VERSIONS_HOTLINE/18161_pager.txt @@ -0,0 +1 @@ +- ticket #18161: sur le paginateur du résultat de recherche, les liens précédent et suivant ont respectivement la classe "previous" et "next" pour faciliter la personnalisation du rendu diff --git a/library/ZendAfi/View/Helper/Pager.php b/library/ZendAfi/View/Helper/Pager.php index 834511f5fed1b826a24eaa197378786fb5f70404..955072c9b1d47e7b712298789df9e8abd246a1ef 100644 --- a/library/ZendAfi/View/Helper/Pager.php +++ b/library/ZendAfi/View/Helper/Pager.php @@ -16,78 +16,92 @@ * * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE * along with AFI-OPAC 2.0; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -////////////////////////////////////////////////////////////////////////////////////////// -// OPAC3 : Pager -////////////////////////////////////////////////////////////////////////////////////////// class ZendAfi_View_Helper_Pager extends ZendAfi_View_Helper_BaseHelper { - function Pager($nombre,$nb_par_page,$page,$url) { - if (!$nombre or !$nb_par_page) + function Pager($nombre, $nb_par_page, $page,$url) { + if (!$nombre or !$nb_par_page) return ''; - if (is_array($url)) - $url = $this->view->url($url); - - // Nombre de pages - if(!$page) $page=1; - $nb_pages=intval($nombre / $nb_par_page); - if($nombre % $nb_par_page) $nb_pages++; - if($nb_pages == 1) return; - - // Url - if (substr($url,0,4) !== "java") { - if (strPos($url,"?") === false) - $url .= '/page/'; - else { - $url.="&"; - - $pos=strPos($url,"page="); - - if ($pos >0) - $url=substr($url,0,$pos); - - $url.="page="; - } - } - - // Bornes - if($nb_pages < 11){$deb=1; $fin=$nb_pages;} - else - { - $deb=$page-5; - if($deb < 1) $deb=1; - $fin=$deb+9; - if($fin > $nb_pages) $fin=$nb_pages; - } - - // Html - if($page > 1) - $html=$this->getLigne($url,($page-1),"«"); - else - $html = ''; + $page = max(1,(int)$page); + $nb_pages = intval($nombre / $nb_par_page); + if($nombre % $nb_par_page) + $nb_pages++; + + if($nb_pages == 1) + return ''; + + $url = $this->_formatUrl($url); + + return + '<div class="pager">' + . $this->_renderPreviousLink($url, $page) + . $this->_renderPageLinks($url, $page, $nb_pages) + . $this->_renderNextLink($url, $page, $nb_pages) + . '</div>'; + } + + protected function _renderPageLinks($url, $page, $nb_pages) { + $html = ''; + $deb = max(1, $page-5); + $fin = min($nb_pages, $deb + 9); for($i=$deb; $i <= $fin; $i++) - { - if($i == $page) $href="#"; else $href=$url; - $html.=$this->getLigne($href,$i,$i); - } - if($page < $nb_pages) $html.=$this->getLigne($url,($page+1),"»"); - return '<div class="pager">'.$html.'</div>'; + $html .= $this->_renderLink(($i == $page ? '#' : $url), + $i, + $i); + return $html; + } + + + protected function _renderPreviousLink($url, $page) { + if ($page > 1) + return $this->_renderLink($url, ($page-1), "«", 'previous'); + return ''; + } + + + protected function _renderNextLink($url, $page, $nb_pages) { + if ($page < $nb_pages) + return $this->_renderLink($url, ($page+1), "»", 'next'); + return ''; } - - private function getLigne($href, $page, $libelle) { - $class = ''; - if ($href == "#") { - $class = 'class="current"'; - } - - if (substr($href, 0, 4) == "java") + + + protected function _formatUrl($url) { + if (is_array($url)) + return $this->view->url($url) . '/page/'; + + if (substr($url,0,4) == "java") //(only ?)usage found in ZendAfi_View_Helper_Notice_Avis:80 + return $url; + + if (strPos($url,"?") === false) + return $url . '/page/'; + + $url .= "&"; + $pos = strPos($url, "page="); + if ($pos >0) + $url = substr($url,0,$pos); + + return $url . "page="; + } + + + protected function _renderLink($href, $page, $libelle, $class = '') { + if ($href == "#") + $class = 'current'; + + if (substr($href, 0, 4) == "java") //(only ?)usage found in ZendAfi_View_Helper_Notice_Avis:80 $href = str_replace("@PAGE@", $page, $href); - else + else $href .= $page; - return '<span '.$class.'><a href="'.$href.'">'.$libelle.'</a></span>'; + return + '<span ' . ($class ? 'class="' . $class .'"' : '') . '>' + . '<a href="' . $href . '">' . $libelle . '</a>' + . '</span>'; } -} \ No newline at end of file +} + +?> \ No newline at end of file diff --git a/tests/library/ZendAfi/View/Helper/PagerTest.php b/tests/library/ZendAfi/View/Helper/PagerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..86f99a84d46beefc0a17d4890103b688d1513a3d --- /dev/null +++ b/tests/library/ZendAfi/View/Helper/PagerTest.php @@ -0,0 +1,164 @@ +<?php +/** + * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved. + * + * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * AFI-OPAC 2.0 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with AFI-OPAC 2.0; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +class View_Helper_PagerWith50ElementsTest extends ViewHelperTestCase { + protected + $_html, + $_helper; + + public function setUp() { + parent::setUp(); + $this->_helper = new ZendAfi_View_Helper_Pager(); + $this->_helper->setView(new ZendAfi_Controller_Action_Helper_View()); + $this->_html = $this->_helper->pager(50, 8, 3, ['controller' => 'recherche', 'action' => 'simple']); + } + + + /** @test */ + public function linkToPageOneShouldBePresent() { + $this->assertXPathContentContains($this->_html, + '//div[@class="pager"]/span/a[contains(@href, "/recherche/simple/page/1")]', + '1'); + } + + + /** @test */ + public function linkToPageSevenShouldBePresent() { + $this->assertXPathContentContains($this->_html, + '//a[contains(@href, "/recherche/simple/page/7")]', + '7'); + } + + + /** @test */ + public function linkToPageHeightShouldNotBePresent() { + $this->assertNotXPath($this->_html, + '//a[contains(@href, "/recherche/simple/page/8")]'); + } + + + /** @test */ + public function linkToPageThreeShouldHaveClassCurrent() { + $this->assertXPathContentContains($this->_html, + '//span[@class="current"]/a[contains(@href, "#3")]', + '3'); + } + + + /** @test */ + public function previousLinkShouldPointPageTwo() { + $this->assertXPathContentContains($this->_html, + '//span[@class="previous"]/a[contains(@href, "/recherche/simple/page/2")]', + '«'); + } + + + /** @test */ + public function nextLinkShouldPointPageFour() { + $this->assertXPathContentContains($this->_html, + '//span[@class="next"]/a[contains(@href, "/recherche/simple/page/4")]', + '»'); + } + + /** @test */ + public function onFirstPagePreviousLinkShouldNotBePresent() { + $this->assertNotXPathContentContains($this->_helper->pager(50, 8, 1, []), + '//a', + '«'); + } + + + /** @test */ + public function onLastPageNextLinkShouldNotBePresent() { + $this->assertNotXPathContentContains($this->_helper->pager(50, 8, 7, []), + '//a', + '»'); + } + + + /** @test */ + public function withTextualLinkPageParamShouldBeAddedWithQuestionMark() { + $this->assertXPathContentContains($this->_helper->pager(50, 8, 3, '/recherche/simple?foo=bar'), + '//a[contains(@href, "/recherche/simple?foo=bar&page=7")]', + '7'); + } + + /** @test */ + public function withTextualLinkAlreadyContainingPageParamItShouldNotDuplicate() { + $this->assertXPathContentContains($this->_helper->pager(50, 8, 3, '/recherche/simple?foo=bar&page=3'), + '//a[contains(@href, "/recherche/simple?foo=bar&page=7")]', + '7'); + } + + /** @test */ + public function withJavascriptLinkPageParamShouldBeReplaced() { + $this->assertNotXPathContentContains($this->_helper->pager(50, 8, 7, 'javascript: infos_bloc(avis, @PAGE@)'), + '//a[contains(@href, "javascript: infos_bloc(avis, 7)")]', + '7'); + } +} + + + +class View_Helper_PagerWith500ElementsTest extends ViewHelperTestCase { + protected + $_html, + $_helper; + + public function setUp() { + parent::setUp(); + $this->_helper = new ZendAfi_View_Helper_Pager(); + $this->_helper->setView(new ZendAfi_Controller_Action_Helper_View()); + $this->_html = $this->_helper->pager(500, 8, 30, ['controller' => 'recherche', 'action' => 'simple']); + } + + + /** @test */ + public function linkToPageTwentyFourShouldNotBePresent() { + $this->assertNotXPath($this->_html, + '//a[contains(@href, "/recherche/simple/page/24")]'); + } + + /** @test */ + public function linkToPageTwentyFiveShouldBePresent() { + $this->assertXPathContentContains($this->_html, + '//a[contains(@href, "/recherche/simple/page/25")]', + '25'); + } + + + /** @test */ + public function linkToPageThirtyFourShouldBePresent() { + $this->assertXPathContentContains($this->_html, + '//a[contains(@href, "/recherche/simple/page/34")]', + '34'); + } + + + /** @test */ + public function linkToPageThirtyFiveShouldNotBePresent() { + $this->assertNotXPath($this->_html, + '//a[contains(@href, "/recherche/simple/page/35")]'); + } + +} + +?> \ No newline at end of file