1: | <?php |
2: | |
3: | namespace Zippy\Html\DataList; |
4: | |
5: | use Zippy\Html\HtmlComponent; |
6: | use Zippy\Interfaces\Requestable; |
7: | use Zippy\WebApplication; |
8: | use Zippy\HtpRequest; |
9: | use Zippy\Interfaces\EventReceiver; |
10: | use Zippy\Event; |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | class Paginator extends HtmlComponent implements Requestable |
17: | { |
18: | private $datalist; |
19: | private $maxbuttons = 10; |
20: | private $firstButton = 1; |
21: | private $ajax; |
22: | protected $event = null; |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | public function __construct($id, \Zippy\Html\DataList\AbstractList $datalist, $pagesize = 0, $ajax = false) { |
32: | parent::__construct($id); |
33: | $this->datalist = $datalist; |
34: | $this->ajax = $ajax; |
35: | if ($pagesize > 0) { |
36: | $this->datalist->setPageSize($pagesize); |
37: | } |
38: | } |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | final protected function RenderImpl() { |
48: | $content = $this->getContent($this->datalist->getPageCount(), $this->datalist->getCurrentPage()); |
49: | $this->getTag()->html($content); |
50: | } |
51: | |
52: | |
53: | |
54: | |
55: | public function getContent($pages, $currentpage) { |
56: | if ($pages <= 1) { |
57: | return ""; |
58: | } |
59: | if ($currentpage > $pages) { |
60: | $currentpage = 1; |
61: | } |
62: | |
63: | |
64: | $content = "<ul class=\"pagination\">"; |
65: | |
66: | |
67: | $iLeft = (int)$this->maxbuttons / 2; |
68: | $iRight = $iLeft; |
69: | |
70: | |
71: | if ($pages <= $iRight + $iRight + 1) { |
72: | for ($i = 1; $i <= $pages; $i++) { |
73: | |
74: | if ($currentpage == $i) { |
75: | $content .= "<li class=\"page-item active\"><a class=\"page-link\" href=\"javascript:void(0);\" > {$i} </a></li>"; |
76: | } else { |
77: | $content .= "<li class=\"page-item\" ><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"" . $this->getUrl($i) . "\"> {$i} </a></li>"; |
78: | } |
79: | |
80: | } |
81: | } else { |
82: | if ($currentpage > $iLeft && $currentpage < ($pages - $iRight)) { |
83: | $content .= "<Li class=\"page-item\" ><a class=\"page-link\" href='void(0);' aria-label=\"Previous\" onclick=\"" . $this->getUrl(1) . "\"><span aria-hidden=\"true\" >«</span></a></li>"; |
84: | |
85: | for ($i = $currentpage - $iLeft; $i <= $currentpage + $iRight; $i++) { |
86: | |
87: | if ($currentpage == $i) { |
88: | $content .= "<li class=\"page-item active\"><a class=\"page-link\" href=\"javascript:void(0);\" > {$i} </a></li>"; |
89: | } else { |
90: | $content .= "<li class=\"page-item\" ><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"" . $this->getUrl($i) . "\"> {$i} </a></li>"; |
91: | } |
92: | |
93: | } |
94: | $content .= "<li class=\"page-item\" ><a class=\"page-link\" href='void(0);' aria-label=\"Next\" onclick=\"" . $this->getUrl($pages) . "\"> <span aria-hidden=\"true\">»</span></a></li>"; |
95: | |
96: | } elseif ($currentpage <= $iLeft) { |
97: | |
98: | $iSlice = 1 + $iLeft - $currentpage; |
99: | for ($i = 1; $i <= $currentpage + ($iRight + $iSlice); $i++) { |
100: | if ($currentpage == $i) { |
101: | $content .= "<li class=\"page-item active\"><a class=\"page-link\" href=\"javascript:void(0);\" > {$i} </a></li>"; |
102: | } else { |
103: | $content .= "<li class=\"page-item\" ><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"" . $this->getUrl($i) . "\"> {$i} </a></li>"; |
104: | } |
105: | |
106: | } |
107: | $content .= "<li class=\"page-item\" ><a class=\"page-link\" href='void(0);' aria-label=\"Next\" onclick=\"" . $this->getUrl($pages) . "\"> <span aria-hidden=\"true\">»</span></a></li>"; |
108: | |
109: | } else { |
110: | $content .= "<Li class=\"page-item\" ><a class=\"page-link\" href='void(0);' aria-label=\"Previous\" onclick=\"" . $this->getUrl(1) . "\"><span aria-hidden=\"true\" >«</span></a></li>"; |
111: | |
112: | $iSlice = $iRight - ($pages - $currentpage); |
113: | |
114: | for ($i = $currentpage - ($iLeft + $iSlice); $i <= $pages; $i++) { |
115: | if ($currentpage == $i) { |
116: | $content .= "<li class=\"page-item active\"><a class=\"page-link\" href=\"javascript:void(0);\" > {$i} </a></li>"; |
117: | } else { |
118: | $content .= "<li class=\"page-item\" ><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"" . $this->getUrl($i) . "\"> {$i} </a></li>"; |
119: | } |
120: | } |
121: | |
122: | } |
123: | } |
124: | |
125: | $content = $content . "</ul>"; |
126: | $countall = $this->datalist->getAllRowsCount(); |
127: | $show = $currentpage * $this->datalist->getPageSize(); |
128: | if ($pages == $currentpage) { |
129: | $show = $countall; |
130: | } |
131: | if ($countall <= $this->datalist->getPageSize()) { |
132: | $show = $countall; |
133: | } |
134: | |
135: | |
136: | $content = "<table ><tr><td valign='middle'>{$show} строк из {$countall} </td><td align='right'> {$content}</td></tr></table>"; |
137: | |
138: | return $content; |
139: | } |
140: | |
141: | |
142: | |
143: | |
144: | final public function RequestHandle() { |
145: | $p = WebApplication::$app->getRequest()->request_params[$this->id]; |
146: | |
147: | if ($this->event != null) { |
148: | $this->event->onEvent($this, $p[0]); |
149: | } |
150: | |
151: | $this->datalist->setCurrentPage($p[0]); |
152: | $this->datalist->Reload(false); |
153: | } |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | public function onPage(EventReceiver $receiver, $handler) { |
162: | $this->event = new Event($receiver, $handler); |
163: | |
164: | } |
165: | |
166: | private function getUrl($pageno) { |
167: | $url = $this->owner->getURLNode(); |
168: | |
169: | if ($this->ajax == true) { |
170: | $url .= "::" . $this->id . ":" . $pageno . "&ajax=true"; |
171: | $onclick = "getUpdate('{$url}');event.returnValue=false; return false;"; |
172: | } else { |
173: | $url .= "::" . $this->id . ":" . $pageno; |
174: | $onclick = "window.location='{$url}';event.returnValue=false; return false;"; |
175: | } |
176: | |
177: | return $onclick; |
178: | } |
179: | |
180: | |
181: | |
182: | |
183: | final public function Reset() { |
184: | $this->datalist->setCurrentPage(1); |
185: | } |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | |
192: | final public function setMaxButtons($maxbuttons) { |
193: | if ($maxbuttons > 1) { |
194: | $this->maxbuttons = $maxbuttons - 1; |
195: | } |
196: | } |
197: | |
198: | |
199: | } |
200: | |