Added Paginator helper. Issue #123.

This commit is contained in:
Dmitry Khomutov 2017-10-24 21:42:18 +07:00
parent 65e38d6428
commit 3ab04a6503
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
4 changed files with 91 additions and 64 deletions

View file

@ -54,6 +54,7 @@
"sensiolabs/ansi-to-html": "1.1.*",
"pda/pheanstalk": "3.1.*",
"guzzlehttp/guzzle": "6.2.*",
"jasongrimes/paginator": "~1.0",
"phpunit/phpunit": "5.7.*",
"codeception/codeception": "2.3.*",

47
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "211a9ef8c70d709f19b3ac593d3370fe",
"content-hash": "a1c5840a2a1901f312df1fe761e236d0",
"packages": [
{
"name": "behat/gherkin",
@ -685,6 +685,51 @@
"homepage": "https://github.com/JakubOnderka/PHP-Parallel-Lint",
"time": "2015-12-15T10:42:16+00:00"
},
{
"name": "jasongrimes/paginator",
"version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/jasongrimes/php-paginator.git",
"reference": "8b18245b8a9dbdf69918ebd1e1f4a229a06030cd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/jasongrimes/php-paginator/zipball/8b18245b8a9dbdf69918ebd1e1f4a229a06030cd",
"reference": "8b18245b8a9dbdf69918ebd1e1f4a229a06030cd",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.2"
},
"type": "library",
"autoload": {
"psr-0": {
"JasonGrimes": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jason Grimes",
"email": "jason@grimesit.com"
}
],
"description": "A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.",
"homepage": "http://github.com/jasongrimes/php-paginator",
"keywords": [
"pager",
"pagination",
"paginator"
],
"time": "2015-10-15T09:42:44+00:00"
},
{
"name": "mnsami/composer-custom-directory-installer",
"version": "1.1.1",

View file

@ -6,6 +6,7 @@ use b8;
use b8\Exception\HttpException\NotFoundException;
use b8\Form;
use b8\Store;
use JasonGrimes\Paginator;
use PHPCensor;
use PHPCensor\BuildFactory;
use PHPCensor\Helper\Github;
@ -95,7 +96,7 @@ class ProjectController extends PHPCensor\Controller
$perPage = $_SESSION['php-censor-user']->getFinalPerPage();
$builds = $this->getLatestBuildsHtml($projectId, $environment, $branch, (($page - 1) * $perPage), $perPage);
$pages = $builds[1] == 0 ? 1 : ceil($builds[1] / $perPage);
$pages = $builds[1] == 0 ? 1 : (integer)ceil($builds[1] / $perPage);
if ($page > $pages) {
$response = new RedirectResponse();
@ -104,6 +105,19 @@ class ProjectController extends PHPCensor\Controller
return $response;
}
$urlPattern = APP_URL . 'project/view/' . $project->getId();
$params = [];
if (!empty($branch)) {
$params['branch'] = $branch;
}
if (!empty($environment)) {
$params['environment'] = $environment;
}
$urlPattern = $urlPattern . '?' . str_replace('%28%3Anum%29', '(:num)', http_build_query(array_merge($params, ['page' => '(:num)'])));
$paginator = new Paginator($builds[1], $perPage, $page, $urlPattern);
$this->view->builds = $builds[0];
$this->view->total = $builds[1];
$this->view->project = $project;
@ -112,8 +126,8 @@ class ProjectController extends PHPCensor\Controller
$this->view->environment = urldecode($environment);
$this->view->environments = $project->getEnvironmentsNames();
$this->view->page = $page;
$this->view->pages = $pages;
$this->view->perPage = $perPage;
$this->view->paginator = $paginator;
$this->layout->title = $project->getTitle();
$this->layout->subtitle = '';

View file

@ -1,4 +1,12 @@
<?php use PHPCensor\Helper\Lang; ?>
<?php
/**
* @var \JasonGrimes\Paginator $paginator
*/
use PHPCensor\Helper\Lang;
?>
<script>
var PROJECT_ID = <?= $project->getId(); ?>;
var PROJECT_ENVIRONMENT = '<?= $environment; ?>';
@ -116,68 +124,27 @@
</tbody>
</table>
</div>
<?php
<div>
<ul class="pagination">
<?php if ($paginator->getPrevUrl()): ?>
<li><a href="<?php echo $paginator->getPrevUrl(); ?>"><?= Lang::get('prev_link'); ?></a></li>
<?php endif; ?>
print '<div><ul class="pagination">';
<?php foreach ($paginator->getPages() as $pageArray): ?>
<?php if ($pageArray['url']): ?>
<li <?php echo $pageArray['isCurrent'] ? 'class="active"' : ''; ?>>
<a href="<?php echo $pageArray['url']; ?>"><?php echo $pageArray['num']; ?></a>
</li>
<?php else: ?>
<li class="disabled"><span><?php echo $pageArray['num']; ?></span></li>
<?php endif; ?>
<?php endforeach; ?>
$project_url = APP_URL . 'project/view/' . $project->getId();
$params = [];
if (!empty($branch)) {
$params['branch'] = $branch;
}
if (!empty($environment)) {
$params['environment'] = $environment;
}
if ($page > 1) {
print '<li><a href="' . $project_url . '?' . http_build_query(array_merge($params, ['page' => ($page - 1)])) . '">' . Lang::get('prev_link') . '</a></li>';
}
if ($pages > 1) {
$start = $page - 3;
if ($start <= 0) {
$start = 1;
}
$end = $page + 3;
if ($end > $pages) {
$end = $pages;
}
if ($start > 1) {
print '<li><a href="' . $project_url . '?' . http_build_query($params) . '">1...</a></li>';
}
for($i = $start; $i <= $end; $i++)
{
if ($pages > $end && $i == $pages) {
continue;
}
if ($i == $page) {
print '<li class="bg-blue"><span>' . $i . '</span></li>';
} else {
print '<li><a href="' . $project_url . '?' . http_build_query(array_merge($params, ['page' => $i])) . '">' . $i . '</a></li>';
}
}
if ($pages > $end) {
print '<li><a href="' . $project_url . '?' . http_build_query(array_merge($params, ['page' => $pages])) . '">...' . $pages . '</a></li>';
}
}
if ($page < ($pages - 1)) {
print '<li><a href="' . $project_url . '?' . http_build_query(array_merge($params, ['page' => ($page == $pages) ? $pages : ($page + 1)])) . '">' . Lang::get('next_link') . '</a></li>';
}
print '</ul></div>';
?>
<?php if ($paginator->getNextUrl()): ?>
<li><a href="<?php echo $paginator->getNextUrl(); ?>"><?= Lang::get('prev_link'); ?></a></li>
<?php endif; ?>
</ul>
</div>
</div>
<div class="col-lg-3 col-md-4 col-sm-4">