Commit 88c11fe3 authored by Taddeus Kroes's avatar Taddeus Kroes

Added pagination to match list

parent b3986358
......@@ -49,7 +49,7 @@ $view = new Slim\Latte\LatteView(array(
$args = func_get_args();
assert(count($args) > 0);
$fmt = gettext(array_shift($args));
return strpos($fmt, '%s') !== false ? vsprintf($fmt, $args) : $fmt;
return strpos($fmt, '%') !== false ? vsprintf($fmt, $args) : $fmt;
});
// install {form} macro's from Bootstrap form renderer
......
......@@ -4,5 +4,6 @@
},
"max_session_lifetime": "2 weeks",
"log.enable": true,
"debug": false
"debug": false,
"max_list_items": 20
}
......@@ -225,3 +225,15 @@ msgid ""
msgstr ""
"Responsieve website (voor mobiel en desktop) voor het opslaan van scores "
"behaald tijdens het boogschieten, bijv. training en wedstrijden."
msgid "No search results."
msgstr ""
msgid "No results"
msgstr "Geen resultaten"
msgid "1 result"
msgstr "1 resultaat"
msgid "%d results"
msgstr "%d resultaten"
......@@ -129,13 +129,14 @@ function create_tag($match, $tagname) {
} catch(PDOException $e) {}
}
function user_matches($user_id, $admin_view) {
global $app, $db;
function user_matches($user_id, $admin_view, $page) {
global $config, $app, $db;
$matches = $db->table('match')
->where(array('user_id' => $user_id))
->order('created_at DESC');
// Filtering
$filter_form = filter_form($matches, $admin_view ? $user_id : null);
if ($filter_form->isSubmitted()) {
......@@ -153,12 +154,22 @@ function user_matches($user_id, $admin_view) {
$matches->where('name LIKE ?', "%$values->name%");
}
// Pagination
$pagesize = $config['max_list_items'];
$nresults = $matches->count();
$npages = intval(ceil($nresults / floatval($pagesize)));
$matches->limit($pagesize, ($page - 1) * $pagesize);
$dbuser = $admin_view ? find_user($user_id) : null;
$app->render('match/list', compact('matches', 'filter_form', 'dbuser'));
$base_uri = $admin_view ? "user/$user_id/matches" : 'matches';
$app->render('match/list', compact(
'matches', 'filter_form', 'dbuser', 'page', 'npages', 'nresults',
'base_uri'
));
}
$app->get('/matches', function () use ($user) {
return user_matches($user->getId(), false);
$app->get('/matches(/:page)', function ($page=1) use ($user) {
return user_matches($user->getId(), false, max(1, intval($page)));
});
$app->get('/match/:id', render_match_action('view'));
$app->get('/match/:id/scores', render_match_action('scores'));
......
......@@ -45,8 +45,8 @@ $app->get('/user/:id', function ($id) use ($app, $db) {
$app->render('user/view', array('dbuser' => find_user($id)));
});
$app->get('/user/:id/matches', function ($id) {
return user_matches($id, true);
$app->get('/user/:id/matches(/:page)', function ($id, $page=1) {
return user_matches($id, true, max(1, intval($page)));
});
$app->get('/user/:id/edit', function ($id) use ($app) {
......
......@@ -36,7 +36,7 @@
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li n:foreach="$menu as $m" n:if="$m"
n:class="'/'.$m[0] == $app->request()->getPathInfo() ? active">
n:class="strpos($app->request()->getPathInfo(), '/'.$m[0]) === 0 ? active">
<a href="{$m[0]}">{$m[1]}</a>
</li>
</ul>
......
......@@ -2,7 +2,15 @@
{block content}
<h2 class="page-header">
{if $dbuser}
{if $filter_form->isSubmitted()}
{if $nresults == 0}
{_'No results'}
{elseif $nresults == 1}
{_'1 result'}
{else}
{_'%d results', $nresults}
{/if}
{elseif $dbuser}
{_'%s\'s matches', $dbuser->username}
{else}
{_'Matches'}
......@@ -66,4 +74,16 @@
</tr>
</tbody>
</table>
{var $query_string = ($q = $_SERVER['QUERY_STRING']) ? '?' . $q}
<nav n:if="$npages > 1">
<ul class="pagination">
<li n:foreach="pagination_range($npages, $page) as $p"
class="{$p == $page ? 'active'} {$p == 0 ? 'disabled'}">
<a n:if="$p" href="{$base_uri}/{$p}{$query_string}">{$p}</a>
<a n:if="!$p" href="javascript:void(0)">&hellip;</a>
</li>
</ul>
</nav>
{/block}
......@@ -142,3 +142,26 @@ class UserHelper {
return $score / $matches->count();
}
}
// Create pagination buttons (0 means a "..." entry)
function pagination_range($numpages, $curpage, $numaround=2) {
$pages = array();
if ($curpage > $numaround + 1) {
$pages[] = 1;
if ($curpage > $numaround + 2)
$pages[] = 0;
}
for ($i = max(1, $curpage - $numaround); $i <= min($curpage + $numaround, $numpages); $i++)
$pages[] = $i;
if ($curpage <= $numpages - $numaround - 1) {
if ($curpage <= $numpages - $numaround - 2)
$pages[] = 0;
$pages[] = $numpages;
}
return $pages;
}
Markdown is supported
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