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