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],
'nbObjectsByPage' => [integer],
'nbObjectsByPageChoices' => array([integer], [integer], ...),
'nbPageLinkByPage' => [integer],
'validPatternRegex' => '[regex]'
),
'predefinedFilters' => array(
@ -243,15 +242,6 @@ contexte dans lequel cette recherche est effectuée.</para>
</listitem>
</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>
<term>validPatternRegex</term>
<listitem>

View file

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

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

View file

@ -53,36 +53,5 @@
{/foreach}
</table>
<span id='LSobject_list_nbresult'>{$LSsearch->label_total|escape:'htmlall'}</span>
{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/{$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}
{assign var=pagination_url value="object/{$LSsearch->LSobject|escape:"url"}"}
{include file='ls:pagination.tpl'}

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}
</table>
{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}
{include file='ls:pagination.tpl'}
<div id='LSdebug_txt_ajax' style='display: none'>{$LSdebug_txt}</div>
<div id='LSerror_txt_ajax' style='display: none'>{$LSerror_txt}</div>

View file

@ -121,37 +121,5 @@
</p>
{/if}
{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/{$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}
{include file='ls:pagination.tpl'}
{/block}