ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilDateTimeInputGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
28  protected ilObjUser $user;
29  protected ?ilDateTime $date = null;
30  protected string $time = "00:00:00";
31  protected bool $showtime = false;
32  protected int $minute_step_size = 5;
33  protected ?int $startyear = null;
34  protected string $invalid_input = '';
35  protected bool $side_by_side = true;
36  protected bool $valid = false;
37 
38  public function __construct(
39  string $a_title = "",
40  string $a_postvar = ""
41  ) {
42  global $DIC;
43 
44  $this->lng = $DIC->language();
45  $this->user = $DIC->user();
46  parent::__construct($a_title, $a_postvar);
47  $this->setType("datetime");
48  }
49 
59  public function setDate(ilDateTime $a_date = null): void
60  {
61  $this->date = $a_date;
62  }
63 
64  public function getDate(): ?ilDateTime
65  {
66  return $this->date;
67  }
68 
69  public function setShowTime(bool $a_showtime): void
70  {
71  $this->showtime = $a_showtime;
72  }
73 
74  public function getShowTime(): bool
75  {
76  return $this->showtime;
77  }
78 
79  public function setStartYear(int $a_year): void
80  {
81  $this->startyear = $a_year;
82  }
83 
84  public function getStartYear(): ?int
85  {
86  return $this->startyear;
87  }
88 
94  public function setMinuteStepSize(int $a_step_size): void
95  {
96  $this->minute_step_size = $a_step_size;
97  }
98 
102  public function getMinuteStepSize(): int
103  {
104  return 1;
105  }
106 
107  public function setValueByArray(array $a_values): void
108  {
109  $incoming = $a_values[$this->getPostVar()] ?? "";
110  $this->setDate(ilCalendarUtil::parseIncomingDate($incoming, (bool) $this->getDatePickerTimeFormat()));
111 
112  foreach ($this->getSubItems() as $item) {
113  $item->setValueByArray($a_values);
114  }
115  }
116 
117  protected function getDatePickerTimeFormat(): int
118  {
119  return (int) $this->getShowTime();
120  }
121 
122  public function hasInvalidInput(): bool
123  {
124  return (bool) $this->invalid_input;
125  }
126 
127  public function checkInput(): bool
128  {
129  $lng = $this->lng;
130 
131  if ($this->getDisabled()) {
132  return true;
133  }
134 
135  $post = $this->str($this->getPostVar());
136 
137  // always done to make sure there are no obsolete values left
138  $this->setDate(null);
139 
140  $valid = false;
141  if (trim($post)) {
143  if ($parsed) {
144  $this->setDate($parsed);
145  $valid = true;
146  }
147  } elseif (!$this->getRequired()) {
148  $valid = true;
149  }
150 
151  if ($valid &&
152  $this->getDate() &&
153  $this->getStartYear() &&
154  $this->getDate()->get(IL_CAL_FKT_DATE, "Y") < $this->getStartYear()) {
155  $valid = false;
156  }
157 
158  $this->valid = $valid;
159 
160  if (!$valid) {
161  $this->invalid_input = $post;
162  $this->setAlert($lng->txt("form_msg_wrong_date"));
163  }
164 
165  if ($valid) {
166  $valid = $this->checkSubItemsInput();
167  }
168 
169  return $valid;
170  }
171 
172  public function getInput(): ?string
173  {
174  if ($this->valid && $this->getDate() !== null) {
175  // getInput() should return a generic format
176  $post_format = $this->getShowTime()
178  : IL_CAL_DATE;
179  return $this->getDate()->get($post_format);
180  }
181  return null;
182  }
183 
184  public function setSideBySide(bool $a_val): void
185  {
186  // not relevant for native html date(time) pickers
187  }
188 
189  protected function getDatetimeFormatForInput(): string
190  {
191  $format = 'Y-m-d';
192  if ($this->getShowTime()) {
193  $format .= '\TH:i';
194  }
195  return $format;
196  }
197 
198  public function render(): string
199  {
200  $ilUser = $this->user;
201  $lng = $this->lng;
202 
203  $tpl = new ilTemplate("tpl.prop_datetime.html", true, true, "components/ILIAS/Form");
204 
205  // config picker
206  if (!$this->getDisabled()) {
207  $picker_id = 'p' . md5($this->getPostVar()); // :TODO: unique?
208  $tpl->setVariable('DATEPICKER_ID', $picker_id);
209  } else {
210  $tpl->setVariable('DATEPICKER_DISABLED', 'disabled="disabled" ');
211  }
212 
213  $type = 'date';
214  if ($this->getShowTime()) {
215  $type = 'datetime-local';
216  }
217  $tpl->setVariable('DATEPICKER_TYPE', $type);
218 
219  // current value
220  $out_format = $this->getDatetimeFormatForInput();
221  $date_value = htmlspecialchars($this->invalid_input);
222  if (!$date_value &&
223  $this->getDate()) {
224  $date_value = $this->getDate()->get(IL_CAL_FKT_DATE, $out_format, $ilUser->getTimeZone());
225  }
226  $tpl->setVariable('DATEPICKER_START_VALUE', $date_value);
227 
228  /*
229  * For date input, step is in days, for datetime-local
230  * it is in seconds.
231  */
232  $step_size = 60;
233  if (!$this->getShowTime()) {
234  $step_size = 1;
235  }
236  $tpl->setVariable('DATEPICKER_STEP', $step_size);
237 
238  if ($this->getStartYear()) {
239  $min = DateTimeImmutable::createFromFormat(
240  'Y',
241  (string) $this->getStartYear()
242  )->format($this->getDatetimeFormatForInput());
243  $tpl->setVariable('DATEPICKER_MIN', $min);
244  }
245 
246  $tpl->setVariable('DATEPICKER_VALUE', $date_value);
247  $tpl->setVariable('DATE_ID', $this->getPostVar());
248 
249  if ($this->getRequired()) {
250  $tpl->setVariable("REQUIRED", "required=\"required\"");
251  }
252 
253  return $tpl->get();
254  }
255 
256  public function getOnloadCode(): array
257  {
258  // no js necessary anymore
259  return [];
260  }
261 
262  public function insert(ilTemplate $a_tpl): void
263  {
264  $html = $this->render();
265 
266  $a_tpl->setCurrentBlock("prop_generic");
267  $a_tpl->setVariable("PROP_GENERIC", $html);
268  $a_tpl->parseCurrentBlock();
269  }
270 
271  public function getTableFilterHTML(): string
272  {
273  $html = $this->render();
274  return $html;
275  }
276 
277  public function serializeData(): string
278  {
279  if ($this->getDate()) {
280  return serialize($this->getDate()->get(IL_CAL_UNIX));
281  }
282  return "";
283  }
284 
285  public function unserializeData(string $a_data): void
286  {
287  $tmp = unserialize($a_data);
288  if ($tmp) {
289  // we used to serialize the complete instance
290  if (is_object($tmp)) {
291  $date = $tmp;
292  } else {
293  $date = $this->getShowTime()
294  ? new ilDateTime($tmp, IL_CAL_UNIX)
295  : new ilDate($tmp, IL_CAL_UNIX);
296  }
297  $this->setDate($date);
298  } else {
299  $this->setDate();
300  }
301  }
302 
303  public function getPostValueForComparison(): string
304  {
305  return $this->serializeData();
306  }
307 
308  public function getToolbarHTML(): string
309  {
310  $html = $this->render();
311  return $html;
312  }
313 
314  public function hideSubForm(): bool
315  {
316  return (!$this->getDate() || $this->getDate()->isNull());
317  }
318 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static parseIncomingDate($value, bool $add_time=false)
Try to parse incoming value to date object.
const IL_CAL_DATETIME
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...
getMinuteStepSize()
Fixed to one minute increments, see https://mantis.ilias.de/view.php?id=42740.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setShowTime(bool $a_showtime)
const IL_CAL_UNIX
This class represents a date/time property in a property form.
setValueByArray(array $a_values)
getTableFilterHTML()
Get input item HTML to be inserted into table filters.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
global $DIC
Definition: shib_login.php:25
const IL_CAL_FKT_DATE
setDate(ilDateTime $a_date=null)
set date E.g $dt_form->setDate(new ilDateTime(time(),IL_CAL_UTC)); or $dt_form->setDate(new ilDateTim...
setMinuteStepSize(int $a_step_size)
Set minute step size E.g 5 => The selection will only show 00,05,10...
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
const IL_CAL_DATE
getToolbarHTML()
Get input item HTML to be inserted into ilToolbarGUI.
__construct(Container $dic, ilPlugin $plugin)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(string $a_title="", string $a_postvar="")
$post
Definition: ltitoken.php:46