ILIAS  release_8 Revision v8.24
class.ilDateTimeInputGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 protected ilObjUser $user;
29 protected ?ilDateTime $date = null;
30 protected string $time = "00:00:00";
31 protected bool $showtime = false;
32 protected bool $showseconds = 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
100 public function getMinuteStepSize(): int
101 {
103 }
104
105 public function setShowSeconds(bool $a_showseconds): void
106 {
107 $this->showseconds = $a_showseconds;
108 }
109
110 public function getShowSeconds(): bool
111 {
112 return $this->showseconds;
113 }
114
115 public function setValueByArray(array $a_values): void
116 {
117 $incoming = $a_values[$this->getPostVar()] ?? "";
118 $this->setDate(ilCalendarUtil::parseIncomingDate($incoming, (bool) $this->getDatePickerTimeFormat()));
119
120 foreach ($this->getSubItems() as $item) {
121 $item->setValueByArray($a_values);
122 }
123 }
124
125 protected function getDatePickerTimeFormat(): int
126 {
127 return (int) $this->getShowTime() + (int) $this->getShowSeconds();
128 }
129
130 public function hasInvalidInput(): bool
131 {
132 return (bool) $this->invalid_input;
133 }
134
135 public function checkInput(): bool
136 {
138
139 if ($this->getDisabled()) {
140 return true;
141 }
142
143 $post = $this->str($this->getPostVar());
144
145 // always done to make sure there are no obsolete values left
146 $this->setDate(null);
147
148 $valid = false;
149 if (trim($post)) {
151 if ($parsed) {
152 $this->setDate($parsed);
153 $valid = true;
154 }
155 } elseif (!$this->getRequired()) {
156 $valid = true;
157 }
158
159 if ($valid &&
160 $this->getDate() &&
161 $this->getStartYear() &&
162 $this->getDate()->get(IL_CAL_FKT_DATE, "Y") < $this->getStartYear()) {
163 $valid = false;
164 }
165
166 $this->valid = $valid;
167
168 if (!$valid) {
169 $this->invalid_input = $post;
170 $this->setAlert($lng->txt("form_msg_wrong_date"));
171 }
172
173 if ($valid) {
174 $valid = $this->checkSubItemsInput();
175 }
176
177 return $valid;
178 }
179
180 public function getInput(): ?string
181 {
182 if ($this->valid && $this->getDate() !== null) {
183 // getInput() should return a generic format
184 $post_format = $this->getShowTime()
186 : IL_CAL_DATE;
187 return $this->getDate()->get($post_format);
188 }
189 return null;
190 }
191
192 public function setSideBySide(bool $a_val): void
193 {
194 $this->side_by_side = $a_val;
195 }
196
197 public function getSideBySide(): bool
198 {
199 return $this->side_by_side;
200 }
201
202 protected function parseDatePickerConfig(): array
203 {
204 $config = null;
205 if ($this->getMinuteStepSize()) {
206 $config['stepping'] = $this->getMinuteStepSize();
207 }
208 if ($this->getStartYear()) {
209 $config['minDate'] = $this->getStartYear() . '-01-01';
210 }
211 $config['sideBySide'] = $this->getSideBySide();
212 return $config;
213 }
214
215 public function render(): string
216 {
219
220 $tpl = new ilTemplate("tpl.prop_datetime.html", true, true, "Services/Form");
221
222 // config picker
223 if (!$this->getDisabled()) {
224 $picker_id = md5($this->getPostVar()); // :TODO: unique?
225 $tpl->setVariable('DATEPICKER_ID', $picker_id);
226
228 $picker_id,
230 $this->parseDatePickerConfig(),
231 null,
232 null,
233 null,
234 "subform_" . $this->getPostVar()
235 );
236 } else {
237 $tpl->setVariable('DATEPICKER_DISABLED', 'disabled="disabled" ');
238 }
239
240 // :TODO: i18n?
242 $tpl->setVariable('PLACEHOLDER', $pl_format);
243
244 // accessibility description
245 $tpl->setVariable(
246 'DESCRIPTION',
247 ilLegacyFormElementsUtil::prepareFormOutput($lng->txt("form_date_aria_desc") . " " . $pl_format)
248 );
249
250 // current value
251 $date_value = htmlspecialchars($this->invalid_input);
252 if (!$date_value &&
253 $this->getDate()) {
255 $date_value = $this->getDate()->get(IL_CAL_FKT_DATE, $out_format, $ilUser->getTimeZone());
256 }
257
258 $tpl->setVariable('DATEPICKER_VALUE', $date_value);
259 $tpl->setVariable('DATE_ID', $this->getPostVar());
260
261 if ($this->getRequired()) {
262 $tpl->setVariable("REQUIRED", "required=\"required\"");
263 }
264
265 return $tpl->get();
266 }
267
268 public function getOnloadCode(): array
269 {
270 $code = [];
271 if (!$this->getDisabled()) {
272 $picker_id = md5($this->getPostVar());
273
275 $picker_id,
277 $this->parseDatePickerConfig(),
278 null,
279 null,
280 null,
281 "subform_" . $this->getPostVar()
282 );
283 }
284 return $code;
285 }
286
287 public function insert(ilTemplate $a_tpl): void
288 {
289 $html = $this->render();
290
291 $a_tpl->setCurrentBlock("prop_generic");
292 $a_tpl->setVariable("PROP_GENERIC", $html);
293 $a_tpl->parseCurrentBlock();
294 }
295
296 public function getTableFilterHTML(): string
297 {
298 $html = $this->render();
299 return $html;
300 }
301
302 public function serializeData(): string
303 {
304 if ($this->getDate()) {
305 return serialize($this->getDate()->get(IL_CAL_UNIX));
306 }
307 return "";
308 }
309
310 public function unserializeData(string $a_data): void
311 {
312 $tmp = unserialize($a_data);
313 if ($tmp) {
314 // we used to serialize the complete instance
315 if (is_object($tmp)) {
316 $date = $tmp;
317 } else {
318 $date = $this->getShowTime()
319 ? new ilDateTime($tmp, IL_CAL_UNIX)
320 : new ilDate($tmp, IL_CAL_UNIX);
321 }
322 $this->setDate($date);
323 } else {
324 $this->setDate();
325 }
326 }
327
328 public function getPostValueForComparison(): string
329 {
330 return trim($this->str($this->getPostVar()));
331 }
332
333 public function getToolbarHTML(): string
334 {
335 $html = $this->render();
336 return $html;
337 }
338
339 public function hideSubForm(): bool
340 {
341 return (!$this->getDate() || $this->getDate()->isNull());
342 }
343}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
const IL_CAL_DATE
const IL_CAL_UNIX
const IL_CAL_DATETIME
const IL_CAL_FKT_DATE
static getUserDateFormat(int $a_add_time=0, bool $a_for_parsing=false)
Parse current user setting into date/time format.
static parseIncomingDate($a_value, bool $a_add_time=false)
Try to parse incoming value to date object.
static addDateTimePicker(string $a_id, ?int $a_add_time=null, ?array $a_custom_config=null, ?string $a_id2=null, ?array $a_custom_config2=null, ?string $a_toggle_id=null, ?string $a_subform_id=null)
Add date time picker to element.
static getCodeForPicker(string $a_id, ?int $a_add_time=null, ?array $a_custom_config=null, ?string $a_id2=null, ?array $a_custom_config2=null, ?string $a_toggle_id=null, ?string $a_subform_id=null)
Add date time picker to element.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getTableFilterHTML()
Get input item HTML to be inserted into table filters.
setShowTime(bool $a_showtime)
getToolbarHTML()
Get input item HTML to be inserted into ilToolbarGUI.
setShowSeconds(bool $a_showseconds)
setValueByArray(array $a_values)
checkInput()
Check input, strip slashes etc.
__construct(string $a_title="", string $a_postvar="")
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... minutes.
@classDescription Date and time handling
Class for single dates.
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 prepareFormOutput($a_str, bool $a_strip=false)
User class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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
$ilUser
Definition: imgupload.php:34
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$post
Definition: ltitoken.php:49
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc