Commit c98fc782 authored by Richard Mansfield's avatar Richard Mansfield Committed by Nigel McNie
Browse files

Move search result pagination to separate function

parent 77a62dd6
......@@ -64,7 +64,8 @@ function UserSearch() {
this.searchByChildLink = function (element) {
var children = getElementsByTagAndClassName('a', null, element);
if (children.length == 1) {
self.params = parseQueryString(getNodeAttribute(children[0], 'href'));
var href = getNodeAttribute(children[0], 'href');
self.params = parseQueryString(href.substring(href.indexOf('?')+1, href.length));
self.doSearch();
}
}
......
......@@ -130,7 +130,6 @@ function build_admin_user_search_results($search, $offset, $limit, $sortby, $sor
$params = join('&', $params);
$smarty->assign_by_ref('params', $params);
$smarty->assign_by_ref('offset', $offset);
$smarty->assign_by_ref('sortby', $sortby);
$smarty->assign_by_ref('sortdir', $sortdir);
$fieldnames = array('firstname','lastname','username','email','institution');
......@@ -172,18 +171,12 @@ function build_admin_user_search_results($search, $offset, $limit, $sortby, $sor
$results = admin_user_search($queries, $constraints, $offset, $limit, $sortby, $sortdir);
$results['pages'] = ceil($results['count'] / $results['limit']);
$results['page'] = $results['offset'] / $results['limit']; // $results['pages'];
$lastpage = $results['pages'] - 1;
$results['next'] = min($lastpage, $results['page'] + 1);
$results['prev'] = max(0, $results['page'] - 1);
$range = min(1, $lastpage);
$pagenumbers = array_unique(array_merge(range(0, min($range, $results['page'])),
range(max($range, $results['page']-$range),
min($results['page']+$range, $lastpage)),
range(max($range, $lastpage-$range), $lastpage)));
$url = get_config('wwwroot').'admin/users/search.php?'.$params.'&sortby='.$sortby.'&sortdir='.$sortdir;
$pagelinks = table_page_links($results['limit'], $results['offset'], $results['count'], $url);
$smarty->assign_by_ref('pagelinks', $pagelinks);
$smarty->assign_by_ref('results', $results);
$smarty->assign_by_ref('pagenumbers', $pagenumbers);
$smarty->assign_by_ref('institutions', get_records_assoc('institution', '', '', '', 'name,displayname'));
return $smarty->fetch('admin/users/resulttable.tpl');
}
......
......@@ -1822,4 +1822,42 @@ function clean_attributes_2($htmlArray) {
}
/**
* Creates pagination links for a table
* appends an 'offset=n' parameter to the url to get the url for a different page
* @param integer $limit number of items per page
* @param integer $offset offset of first data item on this page
* @param integer $count total number of items
* @param string $url where to get results from
* @return string
*/
function table_page_links($limit, $offset, $count, $url) {
if ($limit <= $count) {
$pages = ceil($count / $limit);
$page = $offset / $limit;
$last = $pages - 1;
$next = min($last, $page + 1);
$prev = max(0, $page - 1);
$pagenumbers = array_values(array(0 => 0,
$prev => $prev,
$page => $page,
$next => $next,
$last => $last));
$smarty = smarty_core();
$smarty->assign_by_ref('pagenumbers', $pagenumbers);
$smarty->assign_by_ref('prev', $prev);
$smarty->assign_by_ref('page', $page);
$smarty->assign_by_ref('pages', $pages);
$smarty->assign_by_ref('next', $next);
$smarty->assign_by_ref('limit', $limit);
$smarty->assign_by_ref('url', $url);
return $smarty->fetch('pager.tpl');
}
return '';
}
?>
......@@ -1572,6 +1572,12 @@ table#initials .initial-letters {
font-weight: bold;
}
.search-results-pages span.dimmed a {
color: #999 !important;
text-decoration: none;
font-weight: bold;
}
table.tablerenderer th.asc {
background: #e5e8da url(../images/down.png) no-repeat right;
}
......
......@@ -2,6 +2,12 @@
<h3>{str tag="results"}</h3>
<table id="searchresults" class="tablerenderer">
<thead>
{if !empty($pagelinks)}
<tr class="search-results-pages">
<td colspan=7>{$pagelinks}</td>
</tr>
{/if}
<tr>
<th></th>
{foreach from=$fieldnames item=f}
......@@ -9,9 +15,10 @@
{/foreach}
<th></th>
</tr>
</thead>
<tbody>
{foreach from=$results.data item=r}
{foreach from=$results.data item=r}
<tr>
<td><img src="{$WWWROOT}thumb.php?type=profileicon&size=40x40&id={$r.id}" alt="{str tag=profileimage}" /></td>
<td>{$r.firstname}</td>
......@@ -21,27 +28,15 @@
<td>{$institutions[$r.institution]->displayname}</td>
<td><a class="suspend-user-link" href="{$WWWROOT}admin/users/suspend.php?id={$r.id}">{str tag=suspenduser section=admin}</a></td>
</tr>
{/foreach}
{/foreach}
</tbody>
{if count($results.data) < $results.count}
<tfoot class="search-results-pages">
<tr>
<td colspan=7>
{if $results.page > $results.prev}
<span class="search-results-page prev"><a href="?{$params}&amp;sortby={$sortby}&amp;sortdir={$sortdir}&amp;offset={$results.limit*$results.prev}">{str tag=prevpage}</a></span>
{/if}
{foreach from=$pagenumbers item=i name=pagenumbers}
{if !$smarty.foreach.pagenumbers.first && $prevpagenum < $i-1}...{/if}
<span class="search-results-page{if $i == $results.page} selected{/if}"><a href="?{$params}&amp;sortby={$sortby}&amp;sortdir={$sortdir}&amp;offset={$i*$results.limit}">{$i+1}</a></span>
{assign var='prevpagenum' value=$i}
{/foreach}
{if $results.page < $results.next}
<span class="search-results-page next"><a href="?{$params}&amp;sortby={$sortby}&amp;sortdir={$sortdir}&amp;offset={$results.limit*$results.next}">{str tag=nextpage}</a></span>
{/if}
</td>
{if !empty($pagelinks)}
<tfoot>
<tr class="search-results-pages">
<td colspan=7>{$pagelinks}</td>
</tr>
</tfoot>
{/if}
{/if}
</table>
{else}
<div>{str tag="noresultsfound"}</div>
......
{if $page != 0}
<span class="search-results-page prev"><a href="{$url}&amp;offset={$limit*$prev}">{str tag=prevpage}</a></span>
{/if}
{foreach from=$pagenumbers item=i name=pagenumbers}
{if !$smarty.foreach.pagenumbers.first && $prevpagenum < $i-1}...{/if}
<span class="search-results-page{if $i == $page} selected{/if}"><a href="{$url}&amp;offset={$i*$limit}">{$i+1}</a></span>
{assign var='prevpagenum' value=$i}
{/foreach}
{if $page < $pages - 1}
<span class="search-results-page next"><a href="{$url}&amp;offset={$limit*$next}">{str tag=nextpage}</a></span>
{/if}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment