From 9c4b5e9f352f0efd4031a84c5936e28b88593bff Mon Sep 17 00:00:00 2001 From: Klaus Silveira Date: Wed, 23 May 2012 20:58:38 -0300 Subject: [PATCH] Abstracting the pager and improving the pagination system --- controllers/commitController.php | 21 ++++++--------------- lib/Application/Utils.php | 17 +++++++++++++++++ lib/Git/Repository.php | 10 ++++++++-- views/commits.twig | 8 ++++---- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/controllers/commitController.php b/controllers/commitController.php index faa55fe..6eeb60e 100644 --- a/controllers/commitController.php +++ b/controllers/commitController.php @@ -2,13 +2,8 @@ $app->get('{repo}/commits/{branch}', function($repo, $branch) use($app) { $repository = $app['git']->getRepository($app['git.repos'] . $repo); - $pageNumber = $app['request']->get('page'); - $pageNumber = (empty($pageNumber)) ? 0 : $pageNumber; - $totalCommits = $repository->getTotalCommits(); - $lastPage = intval($totalCommits / 15); - // If total commits are integral multiple of 15, the lastPage will be commits/15 - 1. - $lastPage = ($lastPage * 15 == $totalCommits) ? $lastPage - 1 : $lastPage; - $commits = $repository->getCommits(null, $pageNumber); + $pager = $app['utils']->getPager($app['request']->get('page'), $repository->getTotalCommits()); + $commits = $repository->getCommits(null, $pager['current']); foreach ($commits as $commit) { $date = $commit->getDate(); @@ -19,8 +14,7 @@ $app->get('{repo}/commits/{branch}', function($repo, $branch) use($app) { return $app['twig']->render('commits.twig', array( 'baseurl' => $app['baseurl'], 'page' => 'commits', - 'pagenumber' => $pageNumber, - 'lastpage' => $lastPage, + 'pager' => $pager, 'repo' => $repo, 'branch' => $branch, 'branches' => $repository->getBranches(), @@ -33,10 +27,8 @@ $app->get('{repo}/commits/{branch}', function($repo, $branch) use($app) { $app->get('{repo}/commits/{branch}/{file}/', function($repo, $branch, $file) use($app) { $repository = $app['git']->getRepository($app['git.repos'] . $repo); - $pageNumber = $app['request']->get('page'); - $pageNumber = (empty($pageNumber)) ? 0 : $pageNumber; - $lastPage = round($repository->getTotalCommits() / 15, 0, PHP_ROUND_HALF_UP); - $commits = $repository->getCommits($file, $pagenumber); + $pager = $app['utils']->getPager($app['request']->get('page'), $repository->getTotalCommits($file)); + $commits = $repository->getCommits($file, $pager['current']); foreach ($commits as $commit) { $date = $commit->getDate(); @@ -47,8 +39,7 @@ $app->get('{repo}/commits/{branch}/{file}/', function($repo, $branch, $file) use return $app['twig']->render('commits.twig', array( 'baseurl' => $app['baseurl'], 'page' => 'commits', - 'pagenumber' => $pageNumber, - 'lastpage' => $lastPage, + 'pager' => $pager, 'repo' => $repo, 'branch' => $branch, 'branches' => $repository->getBranches(), diff --git a/lib/Application/Utils.php b/lib/Application/Utils.php index 401c6cf..25e9c4b 100644 --- a/lib/Application/Utils.php +++ b/lib/Application/Utils.php @@ -203,4 +203,21 @@ class Utils return 'text'; } } + + public function getPager($pageNumber, $totalCommits) + { + $pageNumber = (empty($pageNumber)) ? 0 : $pageNumber; + $lastPage = intval($totalCommits / 15); + // If total commits are integral multiple of 15, the lastPage will be commits/15 - 1. + $lastPage = ($lastPage * 15 == $totalCommits) ? $lastPage - 1 : $lastPage; + $nextPage = $pageNumber + 1; + $previousPage = $pageNumber - 1; + + return array('current' => $pageNumber, + 'next' => $nextPage, + 'previous' => $previousPage, + 'last' => $lastPage, + 'total' => $totalCommits, + ); + } } \ No newline at end of file diff --git a/lib/Git/Repository.php b/lib/Git/Repository.php index ee18331..27a8238 100644 --- a/lib/Git/Repository.php +++ b/lib/Git/Repository.php @@ -222,9 +222,15 @@ class Repository * @access public * @return integer Total number of commits */ - public function getTotalCommits() + public function getTotalCommits($file = null) { - $commits = $this->getClient()->run($this, "rev-list --all --count"); + $command = "rev-list --all --count"; + + if ($file) { + $command .= " $file"; + } + + $commits = $this->getClient()->run($this, $command); return $commits; } diff --git a/views/commits.twig b/views/commits.twig index c92ec12..b792f3e 100644 --- a/views/commits.twig +++ b/views/commits.twig @@ -40,14 +40,14 @@ {% endfor %}