ILIAS  release_7 Revision v7.30-3-g800a261c036
Pagination.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2017 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3
5
7use ILIAS\UI\Component\ViewControl\Pagination as PaginationInterface;
12
14
15class Pagination implements PaginationInterface
16{
19 use Triggerer;
20
24 protected $total_entries = 0;
25
29 protected $page_size;
30
34 protected $current_page = 0;
35
40
44 protected $target_url;
45
49 protected $parameter_name = "pagination_offset";
50
55
59 protected $dd_threshold;
60
64 protected $dropdown_label;
65
70
72 {
73 $this->signal_generator = $signal_generator;
74 $this->initSignals();
75 $this->dropdown_label = self::DEFAULT_DROPDOWN_LABEL;
76 }
77
81 public function withResetSignals()
82 {
83 $clone = clone $this;
84 $clone->initSignals();
85 return $clone;
86 }
87
93 protected function initSignals()
94 {
95 $this->internal_signal = $this->signal_generator->create();
96 }
97
101 public function getInternalSignal() : Signal
102 {
104 }
105
109 public function withTargetURL(string $url, string $parameter_name) : PaginationInterface
110 {
111 $this->checkStringArg("url", $url);
112 $this->checkStringArg("parameter_name", $parameter_name);
113 $clone = clone $this;
114 $clone->target_url = $url;
115 $clone->parameter_name = $parameter_name;
116 return $clone;
117 }
118
122 public function getTargetURL()
123 {
124 return $this->target_url;
125 }
126
130 public function getParameterName() : string
131 {
133 }
134
138 public function withTotalEntries(int $total) : PaginationInterface
139 {
140 $this->checkIntArg("total", $total);
141 $clone = clone $this;
142 $clone->total_entries = $total;
143 return $clone;
144 }
145
149 public function withPageSize(int $size) : PaginationInterface
150 {
151 $this->checkIntArg("size", $size);
152 //raise, if size < 1
153 $clone = clone $this;
154 $clone->page_size = $size;
155 return $clone;
156 }
157
161 public function getPageSize() : int
162 {
163 return $this->page_size;
164 }
165
169 public function withCurrentPage(int $page) : PaginationInterface
170 {
171 $this->checkIntArg("page", $page);
172 $clone = clone $this;
173 $clone->current_page = $page;
174 return $clone;
175 }
176
180 public function getCurrentPage() : int
181 {
182 return $this->current_page;
183 }
184
185 protected function getOffset() : int
186 {
187 $offset = $this->page_size * $this->current_page;
188 return $offset;
189 }
190
194 public function withOnSelect(Signal $signal) : PaginationInterface
195 {
196 return $this->withTriggeredSignal($signal, 'select');
197 }
198
202 public function getNumberOfPages() : int
203 {
204 $pages = ceil($this->total_entries / $this->page_size);
205 return (int) $pages;
206 }
207
211 public function withMaxPaginationButtons(int $amount) : PaginationInterface
212 {
213 $this->checkIntArg("amount", $amount);
214 $clone = clone $this;
215 $clone->max_pages_shown = $amount;
216 return $clone;
217 }
218
222 public function getMaxPaginationButtons()
223 {
225 }
226
227 protected function getPageLength() : int
228 {
229 if ($this->getOffset() + $this->page_size > $this->total_entries) {
230 return $this->total_entries - $this->getOffset();
231 }
232 return $this->page_size;
233 }
234
238 public function withDropdownAt(int $amount) : PaginationInterface
239 {
240 $this->checkIntArg("amount", $amount);
241 $clone = clone $this;
242 $clone->dd_threshold = $amount;
243 return $clone;
244 }
245
249 public function getDropdownAt()
250 {
251 return $this->dd_threshold;
252 }
253
257 public function withDropdownLabel(string $template) : PaginationInterface
258 {
259 $clone = clone $this;
260 $clone->dropdown_label = $template;
261 return $clone;
262 }
263
267 public function getDropdownLabel() : string
268 {
270 }
271
275 public function getDefaultDropdownLabel() : string
276 {
278 }
279
280 public function getRange() : Range
281 {
282 $f = new \ILIAS\Data\Factory();
283 return $f->range($this->getOffset(), $this->getPageLength());
284 }
285}
$size
Definition: RandomTest.php:84
$total
Definition: Utf8Test.php:87
An exception for terminatinating execution or to throw for unit testing.
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:11
withDropdownAt(int $amount)
Layout; when number of page-entries reaches $amount, the options will be rendered as dropdown.
Definition: Pagination.php:238
getRange()
Get the current number of entries on this page.
Definition: Pagination.php:280
getNumberOfPages()
Calculate the total number of pages.
Definition: Pagination.php:202
getParameterName()
Get the parameter this instance uses.
Definition: Pagination.php:130
getMaxPaginationButtons()
Get the maximum amount of page-entries (not records per page!) to be shown.int|null
Definition: Pagination.php:222
getInternalSignal()
Get the internal signal that is triggered on click of a button.
Definition: Pagination.php:101
__construct(SignalGeneratorInterface $signal_generator)
Definition: Pagination.php:71
withTargetURL(string $url, string $parameter_name)
Get a Pagination with this target-url.Shy-Buttons in this control will link to this url and add $para...
Definition: Pagination.php:109
getTargetURL()
Get the url this instance should trigger.string|null
Definition: Pagination.php:122
getCurrentPage()
Get the currently selected page.
Definition: Pagination.php:180
getDefaultDropdownLabel()
Get the default label (for comparison, mainly) - the default label will be translated,...
Definition: Pagination.php:275
withCurrentPage(int $page)
Set the selected page.
Definition: Pagination.php:169
withTotalEntries(int $total)
Initialize with the total amount of entries of the controlled data-list.
Definition: Pagination.php:138
initSignals()
Set the internal signals for this component.
Definition: Pagination.php:93
withMaxPaginationButtons(int $amount)
Layout; define, how many page-options are shown (max).
Definition: Pagination.php:211
getDropdownAt()
Below this value, the options are directly rendered as shy-buttons, on and above this value a dropdow...
Definition: Pagination.php:249
withDropdownLabel(string $template)
Layout; set the label for dropdown.If need (or wish) arises, you can give a template-string with vari...
Definition: Pagination.php:257
getDropdownLabel()
Get the template for the label of the dropdown.
Definition: Pagination.php:267
withPageSize(int $size)
Set the amount of entries per page.
Definition: Pagination.php:149
getPageSize()
Get the number of entries per page.
Definition: Pagination.php:161
This describes a Pagination Control.
Definition: Pagination.php:15
withTriggeredSignal(Component\Signal $signal, $event)
Add a triggered signal, replacing any other signals registered on the same event.
Definition: Triggerer.php:48
checkStringArg($which, $value)
Throw an InvalidArgumentException if $value is no string.
checkIntArg($which, $value)
Throw an InvalidArgumentException if $value is no int.
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
trait ComponentHelper
Provides common functionality for component implementations.
$url