1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Credo\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Paginate Trait |
7
|
|
|
* |
8
|
|
|
* @property \CI_Config $config |
9
|
|
|
* @property \CI_Input $input |
10
|
|
|
* @property \CI_Loader $load |
11
|
|
|
* @property \CI_Pagination $pagination |
12
|
|
|
* @property \CI_URI $uri |
13
|
|
|
* |
14
|
|
|
* @package Credo |
15
|
|
|
* @author Rougin Gutib <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
trait PaginateTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Limits the data based on given configuration and generates pagination links. |
21
|
|
|
* |
22
|
|
|
* @param integer $page |
23
|
|
|
* @param integer $total |
24
|
|
|
* @param array $config |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
3 |
|
public function paginate($page, $total, $config = array()) |
28
|
|
|
{ |
29
|
3 |
|
$this->load->library('pagination'); |
30
|
|
|
|
31
|
3 |
|
$pagination = $this->pagination; |
32
|
|
|
|
33
|
3 |
|
$config = $this->prepare((array) $config); |
34
|
|
|
|
35
|
3 |
|
$config['per_page'] = (integer) $page; |
36
|
|
|
|
37
|
3 |
|
$config['total_rows'] = (integer) $total; |
38
|
|
|
|
39
|
3 |
|
$offset = $this->offset($page, $config); |
40
|
|
|
|
41
|
3 |
|
$pagination->initialize($config); |
42
|
|
|
|
43
|
3 |
|
$links = $pagination->create_links(); |
44
|
|
|
|
45
|
3 |
|
return array($offset, $links); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the offset from the defined configuration. |
50
|
|
|
* |
51
|
|
|
* @param integer $page |
52
|
|
|
* @param array $config |
53
|
|
|
* @return integer |
54
|
|
|
*/ |
55
|
3 |
|
protected function offset($page, array $config) |
56
|
|
|
{ |
57
|
3 |
|
$offset = $this->uri->segment($config['uri_segment']); |
58
|
|
|
|
59
|
3 |
|
if ($config['page_query_string'] === true) { |
60
|
3 |
|
$segment = $config['query_string_segment']; |
61
|
|
|
|
62
|
3 |
|
$offset = $this->input->get($segment); |
63
|
2 |
|
} |
64
|
|
|
|
65
|
3 |
|
$numbers = $config['use_page_numbers'] && $offset !== 0; |
66
|
|
|
|
67
|
3 |
|
return $numbers ? ($page * $offset) - $page : $offset; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the pagination configuration. |
72
|
|
|
* |
73
|
|
|
* @param array $config |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
3 |
|
protected function prepare(array $config) |
77
|
|
|
{ |
78
|
3 |
|
$this->load->helper('url'); |
79
|
|
|
|
80
|
3 |
|
$items = array('base_url' => current_url()); |
81
|
|
|
|
82
|
3 |
|
$items['page_query_string'] = false; |
83
|
3 |
|
$items['query_string_segment'] = 'per_page'; |
84
|
3 |
|
$items['uri_segment'] = 3; |
85
|
3 |
|
$items['use_page_numbers'] = false; |
86
|
|
|
|
87
|
3 |
|
foreach ((array) $items as $key => $value) { |
88
|
3 |
|
if ($this->config->item($key) !== null) { |
89
|
3 |
|
$config[$key] = $this->config->item($key); |
90
|
3 |
|
} elseif (! isset($config[$key])) { |
91
|
3 |
|
$config[$key] = $value; |
92
|
2 |
|
} |
93
|
2 |
|
} |
94
|
|
|
|
95
|
3 |
|
return $config; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|