ILIAS  release_8 Revision v8.24
class.ilDurationInputGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 protected int $months = 0;
29 protected int $days = 0;
30 protected int $hours = 0;
31 protected int $minutes = 0;
32 protected int $seconds = 0;
33 protected bool $showmonths = false;
34 protected bool $showdays = false;
35 protected bool $showhours = true;
36 protected bool $showminutes = true;
37 protected bool $showseconds = false;
38
39 public function __construct(
40 string $a_title = "",
41 string $a_postvar = ""
42 ) {
43 global $DIC;
44
45 $this->lng = $DIC->language();
46 parent::__construct($a_title, $a_postvar);
47 $this->setType("duration");
48 }
49
50 public function setDays(int $a_days): void
51 {
52 $this->days = $a_days;
53 }
54
55 public function getDays(): int
56 {
57 return $this->days;
58 }
59
60 public function setHours(int $a_hours): void
61 {
62 $this->hours = $a_hours;
63 }
64
65 public function getHours(): int
66 {
67 return $this->hours;
68 }
69
70 public function setMinutes(int $a_minutes): void
71 {
72 $this->minutes = $a_minutes;
73 }
74
75 public function getMinutes(): int
76 {
77 return $this->minutes;
78 }
79
80 public function setSeconds(int $a_seconds): void
81 {
82 $this->seconds = $a_seconds;
83 }
84
85 public function setMonths(int $a_months): void
86 {
87 $this->months = $a_months;
88 }
89
90 public function getMonths(): int
91 {
92 return $this->months;
93 }
94
95 public function getSeconds(): int
96 {
97 return $this->seconds;
98 }
99
100 public function setShowMonths(bool $a_show_months): void
101 {
102 $this->showmonths = $a_show_months;
103 }
104
105 public function getShowMonths(): bool
106 {
107 return $this->showmonths;
108 }
109
110 public function setShowDays(bool $a_showdays): void
111 {
112 $this->showdays = $a_showdays;
113 }
114
115 public function getShowDays(): bool
116 {
117 return $this->showdays;
118 }
119
120 public function setShowHours(bool $a_showhours): void
121 {
122 $this->showhours = $a_showhours;
123 }
124
125 public function getShowHours(): bool
126 {
127 return $this->showhours;
128 }
129
130 public function setShowMinutes(bool $a_showminutes): void
131 {
132 $this->showminutes = $a_showminutes;
133 }
134
135 public function getShowMinutes(): bool
136 {
137 return $this->showminutes;
138 }
139
140 public function setShowSeconds(bool $a_showseconds): void
141 {
142 $this->showseconds = $a_showseconds;
143 }
144
145 public function getShowSeconds(): bool
146 {
147 return $this->showseconds;
148 }
149
150 public function setValueByArray(array $a_values): void
151 {
152 $values = ($a_values[$this->getPostVar()] ?? []);
153 $value_or_zero = fn ($part) => array_key_exists($part, $values ?? []) ? (int) $values[$part] : 0;
154 $this->setMonths($value_or_zero("MM"));
155 $this->setDays($value_or_zero("dd"));
156 $this->setHours($value_or_zero("hh"));
157 $this->setMinutes($value_or_zero("mm"));
158 $this->setSeconds($value_or_zero("ss"));
159 }
160
161 public function checkInput(): bool
162 {
163 return true;
164 }
165
166 public function getInput(): array
167 {
168 return $this->strArray($this->getPostVar());
169 }
170
171 public function insert(ilTemplate $a_tpl): void
172 {
173 $html = $this->render();
174
175 $a_tpl->setCurrentBlock("prop_generic");
176 $a_tpl->setVariable("PROP_GENERIC", $html);
177 $a_tpl->parseCurrentBlock();
178 }
179
180 public function render(): string
181 {
183
184 $tpl = new ilTemplate("tpl.prop_duration.html", true, true, "Services/Form");
185
186 if ($this->getShowMonths()) {
187 $tpl->setCurrentBlock("dur_months");
188 $tpl->setVariable("TXT_MONTHS", $lng->txt("form_months"));
189 $val = array();
190 for ($i = 0; $i <= 36; $i++) {
191 $val[$i] = $i;
192 }
193 $tpl->setVariable(
194 "SELECT_MONTHS",
196 $this->getMonths(),
197 $this->getPostVar() . "[MM]",
198 $val,
199 false,
200 true,
201 0,
202 '',
203 [],
204 $this->getDisabled()
205 )
206 );
207 $tpl->parseCurrentBlock();
208 }
209 if ($this->getShowDays()) {
210 $tpl->setCurrentBlock("dur_days");
211 $tpl->setVariable("TXT_DAYS", $lng->txt("form_days"));
212 $val = array();
213 for ($i = 0; $i <= 366; $i++) {
214 $val[$i] = $i;
215 }
216 $tpl->setVariable(
217 "SELECT_DAYS",
219 $this->getDays(),
220 $this->getPostVar() . "[dd]",
221 $val,
222 false,
223 true,
224 0,
225 '',
226 [],
227 $this->getDisabled()
228 )
229 );
230 $tpl->parseCurrentBlock();
231 }
232 if ($this->getShowHours()) {
233 $tpl->setCurrentBlock("dur_hours");
234 $tpl->setVariable("TXT_HOURS", $lng->txt("form_hours"));
235 $val = array();
236 for ($i = 0; $i <= 23; $i++) {
237 $val[$i] = $i;
238 }
239 $tpl->setVariable(
240 "SELECT_HOURS",
242 $this->getHours(),
243 $this->getPostVar() . "[hh]",
244 $val,
245 false,
246 true,
247 0,
248 '',
249 [],
250 $this->getDisabled()
251 )
252 );
253 $tpl->parseCurrentBlock();
254 }
255 if ($this->getShowMinutes()) {
256 $tpl->setCurrentBlock("dur_minutes");
257 $tpl->setVariable("TXT_MINUTES", $lng->txt("form_minutes"));
258 $val = array();
259 for ($i = 0; $i <= 59; $i++) {
260 $val[$i] = $i;
261 }
262 $tpl->setVariable(
263 "SELECT_MINUTES",
265 $this->getMinutes(),
266 $this->getPostVar() . "[mm]",
267 $val,
268 false,
269 true,
270 0,
271 '',
272 [],
273 $this->getDisabled()
274 )
275 );
276 $tpl->parseCurrentBlock();
277 }
278 if ($this->getShowSeconds()) {
279 $tpl->setCurrentBlock("dur_seconds");
280 $tpl->setVariable("TXT_SECONDS", $lng->txt("form_seconds"));
281 $val = array();
282 for ($i = 0; $i <= 59; $i++) {
283 $val[$i] = $i;
284 }
285 $tpl->setVariable(
286 "SELECT_SECONDS",
288 $this->getSeconds(),
289 $this->getPostVar() . "[ss]",
290 $val,
291 false,
292 true,
293 0,
294 '',
295 [],
296 $this->getDisabled()
297 )
298 );
299 $tpl->parseCurrentBlock();
300 }
301
302 return $tpl->get();
303 }
304
305 public function getTableFilterHTML(): string
306 {
307 $html = $this->render();
308 return $html;
309 }
310
311 public function serializeData(): string
312 {
313 $data = array("months" => $this->getMonths(),
314 "days" => $this->getDays(),
315 "hours" => $this->getHours(),
316 "minutes" => $this->getMinutes(),
317 "seconds" => $this->getSeconds());
318
319 return serialize($data);
320 }
321
322 public function unserializeData(string $a_data): void
323 {
324 $data = unserialize($a_data);
325
326 $this->setMonths($data["months"]);
327 $this->setDays($data["days"]);
328 $this->setHours($data["hours"]);
329 $this->setMinutes($data["minutes"]);
330 $this->setSeconds($data["seconds"]);
331 }
332
333 public function getValueInSeconds(): int
334 {
335 $value = 0;
336 if ($this->getShowMonths()) {
337 $value += $this->getMonths() * 30 * 24 * 60 * 60;
338 }
339 if ($this->getShowDays()) {
340 $value += $this->getDays() * 24 * 60 * 60;
341 }
342 if ($this->getShowHours()) {
343 $value += $this->getHours() * 60 * 60;
344 }
345 if ($this->getShowMinutes()) {
346 $value += $this->getMinutes() * 60;
347 }
348 if ($this->getShowSeconds()) {
349 $value += $this->getSeconds();
350 }
351 return $value;
352 }
353
355 {
356 $values = $this->getInput();
357 $value_or_zero = fn ($part) => array_key_exists($part, $values ?? []) ? (int) $values[$part] : 0;
358 $value = 0;
359 if ($this->getShowMonths()) {
360 $value += $value_or_zero("MM") * 30 * 24 * 60 * 60;
361 }
362 if ($this->getShowDays()) {
363 $value += $value_or_zero("dd") * 24 * 60 * 60;
364 }
365 if ($this->getShowHours()) {
366 $value += $value_or_zero("hh") * 60 * 60;
367 }
368 if ($this->getShowMinutes()) {
369 $value += $value_or_zero("mm") * 60;
370 }
371 if ($this->getShowSeconds()) {
372 $value += $value_or_zero("ss");
373 }
374 return $value;
375 }
376
380 public function getValueAsArray(): array
381 {
382 return [
383 'MM' => $this->getMonths(),
384 'dd' => $this->getDays(),
385 'hh' => $this->getHours(),
386 'mm' => $this->getMinutes(),
387 'ss' => $this->getSeconds()
388 ];
389 }
390}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
checkInput()
Check input, strip slashes etc.
setValueByArray(array $a_values)
__construct(string $a_title="", string $a_postvar="")
setShowMonths(bool $a_show_months)
setShowMinutes(bool $a_showminutes)
setShowSeconds(bool $a_showseconds)
setShowHours(bool $a_showhours)
This class represents a property in a property form.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static formSelect( $selected, string $varname, array $options, bool $multiple=false, bool $direct_text=false, int $size=0, string $style_class="", array $attribs=[], bool $disabled=false)
Builds a select form field with options and shows the selected option first.
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
global $DIC
Definition: feed.php:28
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc