|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package |
|
4
|
|
|
* |
|
5
|
|
|
* @package AnimeDb |
|
6
|
|
|
* @author Peter Gribanov <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
8
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace AnimeDb\Bundle\PaginationBundle\Service; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @package AnimeDb\Bundle\PaginationBundle\Service |
|
17
|
|
|
* @author Peter Gribanov <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Builder |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The number of pages displayed in the navigation |
|
23
|
|
|
* |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $max_navigate = Configuration::DEFAULT_LIST_LENGTH; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param int $max_navigate |
|
30
|
|
|
*/ |
|
31
|
4 |
|
public function __construct($max_navigate) |
|
32
|
|
|
{ |
|
33
|
4 |
|
$this->max_navigate = $max_navigate; |
|
34
|
4 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param int $total_pages |
|
38
|
|
|
* @param int $current_page |
|
39
|
|
|
* |
|
40
|
|
|
* @return Configuration |
|
41
|
|
|
*/ |
|
42
|
|
|
public function paginate($total_pages = 0, $current_page = 1) |
|
43
|
|
|
{ |
|
44
|
|
|
return (new Configuration($total_pages, $current_page)) |
|
45
|
|
|
->setMaxNavigate($this->max_navigate); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param QueryBuilder $query |
|
50
|
|
|
* @param int $per_page |
|
51
|
|
|
* @param int $current_page |
|
52
|
|
|
* |
|
53
|
|
|
* @return Configuration |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function paginateQuery(QueryBuilder $query, $per_page, $current_page = 1) |
|
56
|
|
|
{ |
|
57
|
|
|
$counter = clone $query; |
|
58
|
|
|
$total = $counter |
|
59
|
|
|
->select(sprintf('COUNT(%s)', current($query->getRootAliases()))) |
|
60
|
2 |
|
->getQuery() |
|
61
|
|
|
->getSingleScalarResult(); |
|
62
|
|
|
|
|
63
|
|
|
$query |
|
64
|
2 |
|
->setFirstResult(($current_page - 1) * $per_page) |
|
65
|
|
|
->setMaxResults($per_page); |
|
66
|
|
|
|
|
67
|
|
|
return (new Configuration(ceil($total / $per_page), $current_page)) |
|
68
|
|
|
->setMaxNavigate($this->max_navigate); |
|
69
|
2 |
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|