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