Universal pagination; closed #3

This commit is contained in:
Marcin Kurczewski 2013-10-16 13:07:01 +02:00
parent 9e73d0de91
commit e0d01298fc
9 changed files with 234 additions and 97 deletions

View file

@ -10,6 +10,7 @@ mediaPath=./public_html/media/
title=szurubooru
[browsing]
usersPerPage=8
postsPerPage=20
thumbWidth=140
thumbHeight=140

@ -1 +1 @@
Subproject commit 898973195c1c6db2905cf2a52c80b0a8ba142058
Subproject commit 5a936768ee7baeff89c192b7d35f6e8c692ee08f

View file

@ -0,0 +1,31 @@
img {
float: left;
margin-right: 0.5em;
}
h1 {
margin-top: 0;
margin-bottom: 0.25em;
}
.user {
line-height: 1.5em;
margin-bottom: 1em;
width: 25em;
display: inline-block;
}
nav.sort-styles ul {
list-style-type: none;
margin: 0 0 1em 0;
text-align: center;
padding: 0;
}
nav.sort-styles li {
display: inline-block;
font-size: 105%;
margin: 0 1em;
padding-bottom: 0.2em;
}
nav.sort-styles li.active {
border-bottom: 3px solid firebrick;
}

View file

@ -143,9 +143,11 @@ class PostController
$searchDbQuery->offset('?')->put(($page - 1) * $postsPerPage);
$posts = $searchDbQuery->get();
$this->context->transport->page = $page;
$this->context->transport->postCount = $postCount;
$this->context->transport->pageCount = $pageCount;
$this->context->transport->paginator = new StdClass;
$this->context->transport->paginator->page = $page;
$this->context->transport->paginator->pageCount = $pageCount;
$this->context->transport->paginator->entityCount = $postCount;
$this->context->transport->paginator->entities = $posts;
$this->context->transport->posts = $posts;
}

View file

@ -13,11 +13,78 @@ class UserController
/**
* @route /users
* @route /users/{page}
* @route /users/{sortStyle}
* @route /users/{sortStyle}/{page}
* @validate sortStyle alpha|alpha,asc|alpha,desc|date,asc|date,desc|pending
* @validate page [0-9]+
*/
public function listAction()
public function listAction($sortStyle, $page)
{
$this->context->subTitle = 'users';
throw new SimpleException('Not implemented');
$this->context->stylesheets []= 'user-list.css';
$this->context->stylesheets []= 'paginator.css';
if ($this->config->browsing->endlessScrolling)
$this->context->scripts []= 'paginator-endless.js';
$page = intval($page);
$usersPerPage = intval($this->config->browsing->usersPerPage);
$this->context->subTitle = 'browsing users';
PrivilegesHelper::confirmWithException($this->context->user, Privilege::ListUsers);
if ($sortStyle == '' or $sortStyle == 'alpha')
$sortStyle = 'alpha,asc';
if ($sortStyle == 'date')
$sortStyle = 'date,asc';
$buildDbQuery = function($dbQuery, $sortStyle)
{
$dbQuery->from('user');
switch ($sortStyle)
{
case 'alpha,asc':
$dbQuery->orderBy('name')->asc();
break;
case 'alpha,desc':
$dbQuery->orderBy('name')->desc();
break;
case 'date,asc':
$dbQuery->orderBy('join_date')->asc();
break;
case 'date,desc':
$dbQuery->orderBy('join_date')->desc();
break;
case 'pending':
$dbQuery->whereNot('staff_confirmed')->and($this->config->registration->staffActivation);
break;
default:
throw new SimpleException('Unknown sort style');
}
};
$countDbQuery = R::$f->begin();
$countDbQuery->select('COUNT(1)')->as('count');
$buildDbQuery($countDbQuery, $sortStyle);
$userCount = intval($countDbQuery->get('row')['count']);
$pageCount = ceil($userCount / $usersPerPage);
$page = max(1, min($pageCount, $page));
$searchDbQuery = R::$f->begin();
$searchDbQuery->select('user.*');
$buildDbQuery($searchDbQuery, $sortStyle);
$searchDbQuery->limit('?')->put($usersPerPage);
$searchDbQuery->offset('?')->put(($page - 1) * $usersPerPage);
$users = $searchDbQuery->get();
$users = R::convertToBeans('user', $users);
$this->context->sortStyle = $sortStyle;
$this->context->transport->paginator = new StdClass;
$this->context->transport->paginator->page = $page;
$this->context->transport->paginator->pageCount = $pageCount;
$this->context->transport->paginator->entityCount = $userCount;
$this->context->transport->paginator->entities = $users;
$this->context->transport->paginator->params = func_get_args();
$this->context->transport->users = $users;
}
@ -275,9 +342,11 @@ class UserController
$posts = $searchDbQuery->get();
$this->context->transport->user = $user;
$this->context->transport->tab = $tab;
$this->context->transport->page = $page;
$this->context->transport->postCount = $postCount;
$this->context->transport->pageCount = $pageCount;
$this->context->transport->paginator = new StdClass;
$this->context->transport->paginator->page = $page;
$this->context->transport->paginator->pageCount = $pageCount;
$this->context->transport->paginator->entityCount = $postCount;
$this->context->transport->paginator->entities = $posts;
$this->context->transport->posts = $posts;
}

63
src/Views/paginator.phtml Normal file
View file

@ -0,0 +1,63 @@
<?php
$pagesVisible = [];
$pagesVisible []= 1;
$pagesVisible []= $this->context->transport->paginator->pageCount;
$delta = 3;
$pagesVisible = array_merge($pagesVisible, range($this->context->transport->paginator->page - $delta, $this->context->transport->paginator->page + $delta));
$pagesVisible = array_filter($pagesVisible, function($x) { return $x >= 1 and $x <= $this->context->transport->paginator->pageCount; });
$pagesVisible = array_unique($pagesVisible);
sort($pagesVisible, SORT_NUMERIC);
if (!function_exists('pageUrl'))
{
function pageUrl($page)
{
$context = \Chibi\Registry::getContext();
$controller = $context->route->simpleControllerName;
$action = $context->route->simpleActionName;
$page = max(1, $page);
$page = min($context->transport->paginator->pageCount, $page);
$params = $context->route->arguments;
$params['page'] = $page;
return \Chibi\UrlHelper::route($controller, $action, $params);
}
}
?>
<?php if (!empty($pagesVisible)): ?>
<nav class="paginator-wrapper">
<ul class="paginator">
<?php if ($this->context->transport->paginator->page > 1): ?>
<li class="prev">
<?php else: ?>
<li class="prev disabled">
<?php endif ?>
<a href="<?php echo pageUrl($this->context->transport->paginator->page - 1) ?>">
&laquo;
</a>
</li>
<?php foreach ($pagesVisible as $page): ?>
<?php if ($page == $this->context->transport->paginator->page): ?>
<li class="active">
<?php else: ?>
<li>
<?php endif ?>
<a href="<?php echo pageUrl($page) ?>">
<?php echo $page ?>
</a>
</li>
<?php endforeach ?>
<?php if ($this->context->transport->paginator->page < $this->context->transport->paginator->pageCount): ?>
<li class="next">
<?php else: ?>
<li class="next disabled">
<?php endif ?>
<a href="<?php echo pageUrl($this->context->transport->paginator->page + 1) ?>">
&raquo;
</a>
</li>
</ul>
</nav>
<?php endif ?>

View file

@ -8,74 +8,8 @@
</a>
<?php endforeach ?>
</div>
<?php endif ?>
<?php
if (!function_exists('pageUrl'))
{
function pageUrl($page)
{
$context = \Chibi\Registry::getContext();
$controller = $context->route->simpleControllerName;
$action = $context->route->simpleActionName;
$page = max(1, $page);
$page = min($context->transport->pageCount, $page);
$params = [];
$params['page'] = $page;
if ($context->transport->searchQuery != '')
{
$params['query'] = $context->transport->searchQuery;
}
return \Chibi\UrlHelper::route($controller, $action, $params);
}
}
$this->renderFile('paginator');
?>
<?php
$pagesVisible = [];
$pagesVisible []= 1;
$pagesVisible []= $this->context->transport->pageCount;
$delta = 3;
$pagesVisible = array_merge($pagesVisible, range($this->context->transport->page - $delta, $this->context->transport->page + $delta));
$pagesVisible = array_filter($pagesVisible, function($x) { return $x >= 1 and $x <= $this->context->transport->pageCount; });
$pagesVisible = array_unique($pagesVisible);
sort($pagesVisible, SORT_NUMERIC);
?>
<?php if (!empty($pagesVisible)): ?>
<nav class="paginator-wrapper">
<ul class="paginator">
<?php if ($this->context->transport->page > 1): ?>
<li class="prev">
<?php else: ?>
<li class="prev disabled">
<?php endif ?>
<a href="<?php echo pageUrl($this->context->transport->page - 1) ?>">
&laquo;
</a>
</li>
<?php foreach ($pagesVisible as $page): ?>
<?php if ($page == $this->context->transport->page): ?>
<li class="active">
<?php else: ?>
<li>
<?php endif ?>
<a href="<?php echo pageUrl($page) ?>">
<?php echo $page ?>
</a>
</li>
<?php endforeach ?>
<?php if ($this->context->transport->page < $this->context->transport->pageCount): ?>
<li class="next">
<?php else: ?>
<li class="next disabled">
<?php endif ?>
<a href="<?php echo pageUrl($this->context->transport->page + 1) ?>">
&raquo;
</a>
</li>
</ul>
</nav>
<?php endif ?>

53
src/Views/user-list.phtml Normal file
View file

@ -0,0 +1,53 @@
<nav class="sort-styles">
<ul>
<?php
$sortStyles =
[
'alpha,asc' => 'Sort A&rarr;Z',
'alpha,desc' => 'Sort Z&rarr;A',
'date,asc' => 'Sort old&rarr;new',
'date,desc' => 'Sort new&rarr;old',
];
if ($this->config->registration->staffActivation)
$sortStyles['pending'] = 'Pending staff review';
?>
<?php foreach ($sortStyles as $key => $text): ?>
<?php if ($this->context->sortStyle == $key): ?>
<li class="active">
<?php else: ?>
<li>
<?php endif ?>
<a href="<?php echo \Chibi\UrlHelper::route('user', 'list', ['sortStyle' => $key]) ?>"><?php echo $text ?></a>
</li>
<?php endforeach ?>
</ul>
</nav>
<?php if (empty($this->context->transport->users)): ?>
<p class="alert alert-warning">No users to show.</p>
<?php else: ?>
<div class="users paginator-content">
<?php foreach ($this->context->transport->users as $user): ?>
<div class="user">
<a href="<?php echo \Chibi\UrlHelper::route('user', 'view', ['name' => $user->name]) ?>">
<img src="<?php echo $user->getAvatarUrl(100) ?>" alt="<?php echo $user->name ?>"/>
</a>
<div class="details">
<h1>
<a href="<?php echo \Chibi\UrlHelper::route('user', 'view', ['name' => $user->name]) ?>">
<?php echo $user->name ?>
</a>
</h1>
<div class="date-registered">Date registered: <?php echo date('Y-m-d H:i', $user->join_date) ?></div>
<div class="fav-count">Favorite count: <?php echo $user->countOwn('favoritee') ?></div>
<div class="post-count">Post count: <?php echo $user->alias('uploader')->countOwn('post') ?></div>
</div>
</div>
<?php endforeach ?>
</div>
<?php $this->renderFile('paginator') ?>
<?php endif ?>

View file

@ -142,23 +142,7 @@
</div>
<?php if (isset($this->context->transport->posts)): ?>
<?php
/* heh, what a stupid hack */
function pageUrl($page)
{
$context = \Chibi\Registry::getContext();
$controller = $context->route->simpleControllerName;
$action = $context->route->simpleActionName;
$page = max(1, $page);
$page = min($context->transport->pageCount, $page);
$params = [];
$params['page'] = $page;
$params['tab'] = $context->transport->tab;
$params['name'] = $context->transport->user->name;
return \Chibi\UrlHelper::route($controller, $action, $params);
}
$this->renderFile('post-list')
?>
<?php $this->renderFile('post-list') ?>
<?php endif ?>
<?php if ($this->context->transport->tab == 'edit'): ?>