Fix/improve/factorize search pagination

First page of a search is now one instead of zero. The pagination.tpl 
template file contain factorized code to display pages list HTML code.
This commit is contained in:
Benjamin Renard 2021-02-22 17:31:57 +01:00
parent 8c317e127c
commit 5b7b22dde1
7 changed files with 65 additions and 128 deletions

View file

@ -35,7 +35,6 @@ configuration des &LSobjects;, dans la variable <varname>LSsearch</varname>
'displayFormat' => [LSformat], 'displayFormat' => [LSformat],
'nbObjectsByPage' => [integer], 'nbObjectsByPage' => [integer],
'nbObjectsByPageChoices' => array([integer], [integer], ...), 'nbObjectsByPageChoices' => array([integer], [integer], ...),
'nbPageLinkByPage' => [integer],
'validPatternRegex' => '[regex]' 'validPatternRegex' => '[regex]'
), ),
'predefinedFilters' => array( 'predefinedFilters' => array(
@ -243,15 +242,6 @@ contexte dans lequel cette recherche est effectuée.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>nbPageLinkByPage</term>
<listitem>
<simpara>Entier déterminant le nombre maximum de liens vers d'autres pages
affichés sous le résultat de la recherche.</simpara>
<simpara>Par défaut : <literal>10</literal></simpara>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term>validPatternRegex</term> <term>validPatternRegex</term>
<listitem> <listitem>

View file

@ -61,7 +61,6 @@ class LSsearch extends LSlog_staticLoggerClass {
'displayFormat' => NULL, 'displayFormat' => NULL,
'nbObjectsByPage' => NB_LSOBJECT_LIST, 'nbObjectsByPage' => NB_LSOBJECT_LIST,
'nbObjectsByPageChoices' => NULL, 'nbObjectsByPageChoices' => NULL,
'nbPageLinkByPage' => 10,
'customInfos' => array(), 'customInfos' => array(),
'withoutCache' => false, 'withoutCache' => false,
'extraDisplayedColumns' => false, 'extraDisplayedColumns' => false,
@ -335,7 +334,7 @@ class LSsearch extends LSlog_staticLoggerClass {
// nbObjectsByPage // nbObjectsByPage
if (isset($params['nbObjectsByPage'])) { if (isset($params['nbObjectsByPage'])) {
if (((int)$params['nbObjectsByPage'])>1 ) { if (((int)$params['nbObjectsByPage'])>=1 ) {
$this -> params['nbObjectsByPage'] = (int)$params['nbObjectsByPage']; $this -> params['nbObjectsByPage'] = (int)$params['nbObjectsByPage'];
} }
else { else {
@ -1102,31 +1101,33 @@ class LSsearch extends LSlog_staticLoggerClass {
* *
* @retval array The information of the page * @retval array The information of the page
**/ **/
public function getPage($page=0) { public function getPage($page=1) {
if (!LSsession::loadLSclass('LSsearchEntry')) { if (!LSsession::loadLSclass('LSsearchEntry')) {
LSerror::addErrorCode('LSsession_05',$this -> LSobject); LSerror::addErrorCode('LSsession_05',$this -> LSobject);
return; return;
} }
$page = (int)$page; $page = (int)$page;
if ($page < 1)
$page = 1;
$retval=array( $retval = array(
'nb' => $page, 'nb' => $page,
'nbPages' => 1, 'nbPages' => 1,
'list' => array(), 'list' => array(),
'total' => $this -> total 'total' => $this -> total
); );
if ($retval['total']>0) { if ($retval['total'] > 0) {
if (!$this->params['nbObjectsByPage']) { if (!$this->params['nbObjectsByPage']) {
$this->params['nbObjectsByPage']=NB_LSOBJECT_LIST; $this->params['nbObjectsByPage'] = NB_LSOBJECT_LIST;
} }
$retval['nbPages']=ceil($retval['total']/$this->params['nbObjectsByPage']); $retval['nbPages'] = ceil($retval['total'] / $this->params['nbObjectsByPage']);
$sortTable=$this -> getSortTable(); $sortTable=$this -> getSortTable();
$list = array_slice( $list = array_slice(
$sortTable, $sortTable,
($page * $this->params['nbObjectsByPage']), (($page - 1) * $this->params['nbObjectsByPage']),
$this->params['nbObjectsByPage'] $this->params['nbObjectsByPage']
); );
@ -1625,7 +1626,7 @@ class LSsearch extends LSlog_staticLoggerClass {
} }
else { else {
// Retrieve page // Retrieve page
$page = $search -> getPage(($page_nb-1)); $page = $search -> getPage($page_nb);
/* /*
* $page = array( * $page = array(
* 'nb' => $page, * 'nb' => $page,
@ -1647,7 +1648,7 @@ class LSsearch extends LSlog_staticLoggerClass {
'total' => $search -> total, 'total' => $search -> total,
); );
if (!$all) { if (!$all) {
$export['page'] = $page['nb'] + 1; $export['page'] = $page['nb'];
$export['nbPages'] = $page['nbPages']; $export['nbPages'] = $page['nbPages'];
} }
foreach(($all?$entries:$page['list']) as $obj) { foreach(($all?$entries:$page['list']) as $obj) {
@ -1707,7 +1708,7 @@ class LSsearch extends LSlog_staticLoggerClass {
if ($all) if ($all)
echo "Total: ".$search -> total."\n"; echo "Total: ".$search -> total."\n";
else else
echo "Page ".($page['nb']+1)." on ".$page['nbPages']." / Total: ".$search -> total."\n"; echo "Page ".($page['nb'])." on ".$page['nbPages']." / Total: ".$search -> total."\n";
return true; return true;
} }

View file

@ -125,7 +125,7 @@ function handle_old_index_ajax_php($request) {
LSurl :: add_handler('#^index_ajax\.php#', 'handle_old_index_ajax_php', false); LSurl :: add_handler('#^index_ajax\.php#', 'handle_old_index_ajax_php', false);
/* /*
* Handle global seearch request * Handle global search request
* *
* @param[in] $request LSurlRequest The request * @param[in] $request LSurlRequest The request
* *
@ -181,7 +181,7 @@ function handle_global_search($request) {
$LSsearch -> run(); $LSsearch -> run();
if ($LSsearch -> total > 0) { if ($LSsearch -> total > 0) {
$page = $LSsearch -> getPage(0); $page = $LSsearch -> getPage(1);
LStemplate :: assign('page', $page); LStemplate :: assign('page', $page);
LStemplate :: assign('LSsearch', $LSsearch); LStemplate :: assign('LSsearch', $LSsearch);
$pages[] = LSsession :: fetchTemplate('global_search_one_page.tpl'); $pages[] = LSsession :: fetchTemplate('global_search_one_page.tpl');
@ -496,8 +496,8 @@ function handle_LSobject_search($request) {
$LSsearch -> redirectWhenOnlyOneResult(); $LSsearch -> redirectWhenOnlyOneResult();
// Handle page parameter and retreive corresponding page from search // Handle page parameter and retreive corresponding page from search
$page = (isset($_REQUEST['page'])?(int)$_REQUEST['page']:0); $page_nb = (isset($_REQUEST['page'])?(int)$_REQUEST['page']:1);
$page = $LSsearch -> getPage($page); $page = $LSsearch -> getPage($page_nb);
// Set template variables // Set template variables
LStemplate :: assign('page', $page); LStemplate :: assign('page', $page);
@ -691,7 +691,7 @@ function handle_LSobject_select($request) {
$LSsearch -> setParamsFromRequest(); $LSsearch -> setParamsFromRequest();
$LSsearch -> setParam('nbObjectsByPage', NB_LSOBJECT_LIST_SELECT); $LSsearch -> setParam('nbObjectsByPage', NB_LSOBJECT_LIST_SELECT);
$page = (isset($_REQUEST['page'])?(int)$_REQUEST['page']:0); $page_nb = (isset($_REQUEST['page'])?(int)$_REQUEST['page']:1);
// Run search // Run search
$LSsearch -> run(); $LSsearch -> run();
@ -730,7 +730,7 @@ function handle_LSobject_select($request) {
) )
) )
); );
LStemplate :: assign('page', $LSsearch -> getPage($page)); LStemplate :: assign('page', $LSsearch -> getPage($page_nb));
LStemplate :: assign('LSsearch', $LSsearch); LStemplate :: assign('LSsearch', $LSsearch);
LStemplate :: assign('LSselect_id', $request->LSselect_id); LStemplate :: assign('LSselect_id', $request->LSselect_id);
LStemplate :: assign('selectable_object_types', LSselect :: getSelectableObjectTypes($request->LSselect_id)); LStemplate :: assign('selectable_object_types', LSselect :: getSelectableObjectTypes($request->LSselect_id));
@ -1579,7 +1579,7 @@ function handle_api_LSobject_search($request) {
} }
else { else {
// Retrieve page // Retrieve page
$page_nb = (isset($_REQUEST['page'])?(int)$_REQUEST['page']:0); $page_nb = (isset($_REQUEST['page'])?(int)$_REQUEST['page']:1);
$page = $search -> getPage($page_nb); $page = $search -> getPage($page_nb);
/* /*
@ -1603,7 +1603,7 @@ function handle_api_LSobject_search($request) {
'total' => $search -> total, 'total' => $search -> total,
); );
if (!$all) { if (!$all) {
$data['page'] = $page['nb'] + 1; $data['page'] = $page['nb'];
$data['nbPages'] = $page['nbPages']; $data['nbPages'] = $page['nbPages'];
} }
foreach(($all?$entries:$page['list']) as $obj) { foreach(($all?$entries:$page['list']) as $obj) {

View file

@ -53,36 +53,5 @@
{/foreach} {/foreach}
</table> </table>
<span id='LSobject_list_nbresult'>{$LSsearch->label_total|escape:'htmlall'}</span> <span id='LSobject_list_nbresult'>{$LSsearch->label_total|escape:'htmlall'}</span>
{if $page.nbPages > 1} {assign var=pagination_url value="object/{$LSsearch->LSobject|escape:"url"}"}
<p class='LSobject-list-page'> {include file='ls:pagination.tpl'}
{if $page.nbPages > 10}
{if $page.nb > 5}
{if $page.nb > $page.nbPages-6}
{assign var=start value=$page.nbPages-12}
{else}
{assign var=start value=$page.nb-6}
{/if}
{else}
{assign var=start value=0}
{/if}
<a href='object/{$LSsearch->LSobject|escape:'url'}?page=0' class='LSobject-list-page'>&lt;</a>
{foreach from=0|range:10 item=i}
{if $page.nb==$start+$i}
<strong class='LSobject-list-page'>{$page.nb+1}</strong>
{else}
<a href='object/{$LSsearch->LSobject|escape:'url'}?page={$i+$start}' class='LSobject-list-page'>{$i+$start+1}</a>
{/if}
{/foreach}
<a href='object/{$LSsearch->LSobject|escape:'url'}?page={$page.nbPages-1}' class='LSobject-list-page'>&gt;</a>
{else}
{section name=listpage loop=$page.nbPages step=1}
{if $page.nb == $smarty.section.listpage.index}
<strong class='LSobject-list-page'>{$page.nb+1}</strong>
{else}
<a href='object/{$LSsearch->LSobject|escape:'url'}?page={$smarty.section.listpage.index}' class='LSobject-list-page'>{$smarty.section.listpage.index+1}</a>
{/if}
{/section}
{/if}
</p>
{/if}

View file

@ -0,0 +1,41 @@
{if !isset($pagination_url)}
{assign var=pagination_url value=$request->current_url}
{/if}
{if $page.nbPages > 1}
<p class='LSobject-list-page'>
{if $page.nbPages > 9}
{if $page.nb > 5}
{if $page.nb+4 > $page.nbPages}
{assign var=start value=$page.nbPages-8}
{else}
{assign var=start value=$page.nb-4}
{/if}
{else}
{assign var=start value=1}
{/if}
{if $start != 1}
<a href='{$pagination_url}?page=1' class='LSobject-list-page'>&lt;</a>
{/if}
{foreach from=0|range:8 item=i}
{if $page.nb==$start+$i}
<strong class='LSobject-list-page'>{$page.nb}</strong>
{else}
<a href='{$pagination_url}?page={$start+$i}' class='LSobject-list-page'>{$start+$i}</a>
{/if}
{/foreach}
{if $start + 9 <= $page.nbPages}
<a href='{$pagination_url}?page={$page.nbPages}' class='LSobject-list-page'>&gt;</a>
{/if}
{else}
{section name=listpage loop=$page.nbPages step=1}
{if $page.nb == $smarty.section.listpage.index+1}
<strong class='LSobject-list-page'>{$page.nb}</strong>
{else}
<a href='{$pagination_url}?page={$smarty.section.listpage.index+1}' class='LSobject-list-page'>{$smarty.section.listpage.index+1}</a>
{/if}
{/section}
{/if}
</p>
{/if}

View file

@ -43,39 +43,7 @@
{/foreach} {/foreach}
</table> </table>
{include file='ls:pagination.tpl'}
{if $page.nbPages > 1}
<p class='LSobject-list-page'>
{if $page.nbPages > 10}
{if $page.nb > 5}
{if $page.nb > $page.nbPages-6}
{assign var=start value=$page.nbPages-12}
{else}
{assign var=start value=$page.nb-6}
{/if}
{else}
{assign var=start value=0}
{/if}
<a href='object/select/{$LSselect_id|escape:"url"}?page=0' class='LSobject-list-page'>&lt;</a>
{foreach from=0|range:10 item=i}
{if $page.nb==$start+$i}
<strong class='LSobject-list-page'>{$page.nb+1}</strong>
{else}
<a href='object/select/{$LSselect_id|escape:"url"}?page={$i+$start}' class='LSobject-list-page'>{$i+$start+1}</a>
{/if}
{/foreach}
<a href='object/select/{$LSselect_id|escape:"url"}?page={$page.nbPages-1}' class='LSobject-list-page'>&gt;</a>
{else}
{section name=listpage loop=$page.nbPages step=1}
{if $page.nb == $smarty.section.listpage.index}
<strong class='LSobject-list-page'>{$page.nb+1}</strong>
{else}
<a href='object/select/{$LSselect_id|escape:"url"}?page={$smarty.section.listpage.index}' class='LSobject-list-page'>{$smarty.section.listpage.index+1}</a>
{/if}
{/section}
{/if}
</p>
{/if}
<div id='LSdebug_txt_ajax' style='display: none'>{$LSdebug_txt}</div> <div id='LSdebug_txt_ajax' style='display: none'>{$LSdebug_txt}</div>
<div id='LSerror_txt_ajax' style='display: none'>{$LSerror_txt}</div> <div id='LSerror_txt_ajax' style='display: none'>{$LSerror_txt}</div>

View file

@ -121,37 +121,5 @@
</p> </p>
{/if} {/if}
{if $page.nbPages > 1} {include file='ls:pagination.tpl'}
<p class='LSobject-list-page'>
{if $page.nbPages > 10}
{if $page.nb > 5}
{if $page.nb > $page.nbPages-6}
{assign var=start value=$page.nbPages-12}
{else}
{assign var=start value=$page.nb-6}
{/if}
{else}
{assign var=start value=0}
{/if}
<a href='object/{$LSsearch->LSobject|escape:"url"}?page=0' class='LSobject-list-page'>&lt;</a>
{foreach from=0|range:10 item=i}
{if $page.nb==$start+$i}
<strong class='LSobject-list-page'>{$page.nb+1}</strong>
{else}
<a href='object/{$LSsearch->LSobject|escape:"url"}?page={$i+$start}' class='LSobject-list-page'>{$i+$start+1}</a>
{/if}
{/foreach}
<a href='object/{$LSsearch->LSobject|escape:"url"}?page={$page.nbPages-1}' class='LSobject-list-page'>&gt;</a>
{else}
{section name=listpage loop=$page.nbPages step=1}
{if $page.nb == $smarty.section.listpage.index}
<strong class='LSobject-list-page'>{$page.nb+1}</strong>
{else}
<a href='object/{$LSsearch->LSobject|escape:"url"}?page={$smarty.section.listpage.index}' class='LSobject-list-page'>{$smarty.section.listpage.index+1}</a>
{/if}
{/section}
{/if}
</p>
{/if}
{/block} {/block}