19declare(strict_types=1);
36 protected \ilLanguage
$lng;
40 protected \ILIAS\Data\Factory
$data;
50 protected \ILIAS\HTTP\Services
$http;
51 protected \ilCtrlInterface
$ctrl;
52 protected \ILIAS\DI\UIServices
$ui;
55 protected array
$sections = [self::DEFAULT_SECTION => [
"title" =>
"",
"description" =>
"",
"fields" => []]];
60 protected ?Form\Standard
$form =
null;
80 $this->
ui = $DIC->ui();
81 $this->
ctrl = $DIC->ctrl();
82 $this->
http = $DIC->http();
83 $this->
lng = $DIC->language();
85 $this->main_tpl =
$DIC->ui()->mainTemplate();
86 $this->
user = $DIC->user();
87 $this->data = new \ILIAS\Data\Factory();
91 $this->
lng->loadLanguageModule(
"rep");
96 return "il.repository.ui.init();\n" .
97 "il.repository.core.init('" . ILIAS_HTTP_PATH .
"')";
104 if (!isset(
$DIC[
"ui.factory"])) {
108 $f =
$DIC->ui()->factory();
109 $r =
$DIC->ui()->renderer();
110 if (!self::$initialised) {
123 $d[] =
$f->input()->field()->text(
"");
125 self::$initialised =
true;
148 return ($this->async_mode !== self::ASYNC_NONE);
159 string $description =
""
161 if ($this->title ==
"") {
165 $this->sections[$key] = [
167 "description" => $description,
170 $this->current_section = $key;
177 string $description =
"",
178 ?
string $value =
null,
181 $this->values[$key] = $value;
182 $field = $this->
ui->factory()->input()->field()->text($title, $description);
183 if ($max_length > 0) {
184 $field = $field->withMaxLength($max_length);
186 if (!is_null($value)) {
187 $field = $field->withValue($value);
189 $this->addField($key, $field);
196 string $description =
"",
198 ?
bool $disabled =
null
200 $this->values[$key] = $value;
201 $field = $this->
ui->factory()->input()->field()->checkbox($title, $description);
202 if (!is_null($value)) {
203 $field = $field->withValue($value);
205 if (!is_null($disabled)) {
206 $field = $field->withDisabled($disabled);
208 $this->addField($key, $field);
216 $this->values[$key] = $value;
217 $field = $this->
ui->factory()->input()->field()->hidden();
218 $field = $field->withValue($value);
219 $this->addField($key, $field);
225 if ($required && ($field = $this->getLastField())) {
227 $field = $field->withRequired(
true,
new NotEmpty(
232 $field = $field->withRequired(
true);
234 $this->replaceLastField($field);
241 if ($disabled && ($field = $this->getLastField())) {
242 $field = $field->withDisabled(
true);
243 $this->disable[$this->last_key] =
true;
244 $this->replaceLastField($field);
252 string $description =
"",
253 ?
string $value =
null
255 $this->values[$key] = $value;
256 $field = $this->
ui->factory()->input()->field()->textarea($title, $description);
257 if (!is_null($value)) {
258 $field = $field->withValue($value);
260 $this->addField($key, $field);
267 string $description =
"",
269 ?
int $min_value =
null,
270 ?
int $max_value =
null
272 $this->values[$key] = $value;
274 if (!is_null($min_value)) {
275 $trans[] = $this->
refinery->int()->isGreaterThanOrEqual($min_value);
277 if (!is_null($max_value)) {
278 $trans[] = $this->
refinery->int()->isLessThanOrEqual($max_value);
280 $field = $this->
ui->factory()->input()->field()->numeric($title, $description);
281 if (count($trans) > 0) {
282 $field = $field->withAdditionalTransformation($this->
refinery->logical()->parallel($trans));
284 if (!is_null($value)) {
285 $field = $field->withValue($value);
287 $this->addField($key, $field);
294 string $description =
"",
297 $this->values[$key] = $value;
298 $field = $this->
ui->factory()->input()->field()->dateTime($title, $description);
300 $format = $this->
user->getDateFormat();
301 $dt_format = (string) $format;
303 $field = $field->withFormat($format);
304 if (!is_null($value)) {
305 $field = $field->withValue(
306 (
new \DateTime($value->get(
IL_CAL_DATE)))->format($dt_format)
309 $this->addField($key, $field);
316 string $description =
"",
319 $this->values[$key] = $value;
320 $field = $this->
ui->factory()->input()->field()->dateTime($title, $description)->withUseTime(
true);
323 $dt_format = $this->data->dateFormat()->withTime12($this->
user->getDateFormat());
325 $dt_format = $this->data->dateFormat()->withTime24($this->
user->getDateFormat());
327 $field = $field->withFormat($dt_format);
329 $field = $field->withValue(
331 ((
string) $dt_format)
335 $this->addField($key, $field);
342 string $description =
"",
345 string $label_from =
"",
346 string $label_to =
""
348 $this->values[$key] = [$from, $to];
349 if ($label_from ===
"") {
350 $label_from = $this->
lng->txt(
"rep_activation_limited_start");
352 if ($label_to ===
"") {
353 $label_to = $this->
lng->txt(
"rep_activation_limited_end");
355 $field = $this->
ui->factory()->input()->field()->duration($title, $description)->withUseTime(
true)->withLabels($label_from, $label_to);
358 $dt_format = $this->data->dateFormat()->withTime12($this->
user->getDateFormat());
360 $dt_format = $this->data->dateFormat()->withTime24($this->
user->getDateFormat());
362 $field = $field->withFormat($dt_format);
363 $val_from = $val_to =
null;
365 $val_from = (new \DateTime(
367 ))->format((
string) $dt_format);
370 $val_to = (new \DateTime(
372 ))->format((
string) $dt_format);
374 $field = $field->withValue([$val_from, $val_to]);
375 $this->addField($key, $field);
381 if (is_null($value)) {
385 return new \ilDateTime($value->format(
"Y-m-d H:i:s"),
IL_CAL_DATETIME);
387 return new \ilDate($value->format(
"Y-m-d"),
IL_CAL_DATE);
394 string $description =
"",
395 ?
string $value =
null
397 $this->values[$key] = $value;
398 $field = $this->
ui->factory()->input()->field()->select($title,
$options, $description);
399 if (!is_null($value)) {
400 $field = $field->withValue($value);
412 string $description =
"",
413 ?
string $value =
null
415 $this->values[$key] = $value;
416 $field = $this->
ui->factory()->input()->field()->radio($title, $description);
424 public function radioOption(
string $value,
string $title,
string $description =
""): self
426 if ($field = $this->getLastField()) {
427 $field = $field->withOption($value, $title, $description);
428 if (($this->values[$this->last_key] ??
null) === $value) {
429 $field = $field->withValue($value);
431 $this->replaceLastField($field);
436 public function switch(
439 string $description =
"",
440 ?
string $value =
null
442 $this->values[$key] = $value;
443 $this->current_switch = [
446 "description" => $description,
456 string $description =
"",
459 $this->values[$key] = $value;
460 $this->current_optional = [
463 "description" => $description,
470 public function group(
string $key,
string $title,
string $description =
"", $disabled =
false): self
472 $this->endCurrentGroup();
473 $this->current_group = [
476 "description" => $description,
477 "disabled" => $disabled,
485 if (!is_null($this->current_group)) {
486 if (!is_null($this->current_switch)) {
488 foreach ($this->current_group[
"fields"] as $key) {
489 $fields[$key] = $this->fields[$key];
491 $this->current_switch[
"groups"][$this->current_group[
"key"]] =
492 $this->
ui->factory()->input()->field()->group(
494 $this->current_group[
"title"]
495 )->withByline($this->current_group[
"description"]);
496 if ($this->current_group[
"disabled"]) {
497 $this->current_switch[
"groups"][$this->current_group[
"key"]] =
498 $this->current_switch[
"groups"][$this->current_group[
"key"]]
499 ->withDisabled(
true);
503 $this->current_group =
null;
506 public function end(): self
508 $this->endCurrentGroup();
509 if (!is_null($this->current_switch)) {
510 $field = $this->
ui->factory()->input()->field()->switchableGroup(
511 $this->current_switch[
"groups"],
512 $this->current_switch[
"title"],
513 $this->current_switch[
"description"]
515 if (!is_null($this->current_switch[
"value"])) {
516 $cvalue = $this->current_switch[
"value"];
517 if (isset($this->current_switch[
"groups"][$cvalue])) {
518 $field = $field->withValue($cvalue);
521 $key = $this->current_switch[
"key"];
522 $this->current_switch =
null;
523 $this->addField($key, $field);
525 if (!is_null($this->current_optional)) {
526 $field = $this->
ui->factory()->input()->field()->optionalGroup(
527 $this->current_optional[
"fields"],
528 $this->current_optional[
"title"],
529 $this->current_optional[
"description"]
531 if ($this->current_optional[
"value"]) {
533 foreach ($this->current_optional[
"fields"] as $key => $input) {
534 $value[$key] = $input->getValue();
536 $field = $field->withValue($value);
538 $field = $field->withValue(
null);
540 $key = $this->current_optional[
"key"];
541 $this->current_optional =
null;
542 $this->addField($key, $field);
550 \Closure $result_handler,
551 string $id_parameter,
552 string $description =
"",
553 ?
int $max_files =
null,
554 array $mime_types = [],
555 array $ctrl_path = [],
556 string $logger_id =
""
565 foreach ([
"application/x-compressed",
"application/x-zip-compressed"] as $zipmime) {
566 if (in_array(
"application/zip", $mime_types) &&
567 !in_array($zipmime, $mime_types)) {
568 $mime_types[] = $zipmime;
572 if (count($mime_types) > 0) {
573 $description .= $this->
lng->txt(
"rep_allowed_types") .
": " .
574 implode(
", ", $mime_types);
577 $field = $this->
ui->factory()->input()->field()->file(
578 $this->upload_handler[$key],
584 if (!is_null($max_files)) {
585 $field = $field->withMaxFiles($max_files);
587 if (count($mime_types) > 0) {
588 $field = $field->withAcceptedMimeTypes($mime_types);
600 if (!isset($this->upload_handler[$key])) {
601 throw new \ilException(
"Unknown file upload field: " . $key);
603 return $this->upload_handler[$key];
610 throw new \ilException(
"Missing Input Key: " . $key);
612 if (isset($this->field[$key])) {
613 throw new \ilException(
"Duplicate Input Key: " . $key);
616 if ($this->current_section !== self::DEFAULT_SECTION) {
617 $field_path[] = $this->current_section;
619 if (!is_null($this->current_group)) {
620 $this->current_group[
"fields"][] = $key;
621 if (!is_null($this->current_switch)) {
622 $field_path[] = $this->current_switch[
"key"];
624 $field_path[] = $key;
626 } elseif (!is_null($this->current_optional)) {
627 $field_path[] = $this->current_optional[
"key"];
628 $this->current_optional[
"fields"][$key] = $field;
629 $field_path[] = $key;
631 $this->sections[$this->current_section][
"fields"][] = $key;
632 $field_path[] = $key;
640 if (!$supress_0_key) {
645 $this->fields[$key] = $field;
646 $this->field_path[$key] = $field_path;
647 $this->last_key = $key;
653 if (!isset($this->fields[$key])) {
654 throw new \ilException(
"Unknown Key: " . $key);
656 return $this->fields[$key];
661 return $this->fields[$this->last_key] ??
null;
666 if ($this->last_key !==
"") {
667 $this->fields[$this->last_key] = $field;
670 if (!is_null($this->current_optional) && isset($this->current_optional[
"fields"][$this->last_key])) {
671 $this->current_optional[
"fields"][$this->last_key] = $field;
679 if (is_null($this->
form)) {
680 $async = ($this->async_mode !== self::ASYNC_NONE);
682 if (!is_null($this->class_path)) {
683 $action = $ctrl->getLinkTargetByClass($this->class_path, $this->cmd,
"", $async);
686 foreach ($this->sections as $sec_key => $section) {
687 if ($sec_key === self::DEFAULT_SECTION) {
688 foreach ($this->sections[$sec_key][
"fields"] as $f_key) {
689 $inputs[$f_key] = $this->getFieldForKey($f_key);
691 } elseif (count($this->sections[$sec_key][
"fields"]) > 0) {
693 foreach ($this->sections[$sec_key][
"fields"] as $f_key) {
694 $sec_inputs[$f_key] = $this->getFieldForKey($f_key);
696 $inputs[$sec_key] = $this->
ui->factory()->input()->field()->section(
699 $section[
"description"]
703 $this->
form = $this->
ui->factory()->input()->container()->form()->standard(
707 if ($this->submit_caption !==
"") {
708 $this->
form = $this->
form->withSubmitLabel($this->submit_caption);
716 return $this->getForm()->getSubmitLabel() ?? $this->
lng->txt(
"save");
721 if (is_null($this->raw_data)) {
722 $request = $this->
http->request();
723 $this->
form = $this->getForm()->withRequest($request);
724 $this->raw_data = $this->
form->getData();
731 return !(is_null($this->raw_data));
737 public function getData(
string $key): mixed
741 if (!isset($this->fields[$key])) {
745 if (isset($this->disable[$key])) {
746 return $this->values[$key];
749 $value = $this->raw_data;
750 foreach ($this->field_path[$key] as $path_key) {
751 if (!isset($value[$path_key])) {
754 $value = $value[$path_key];
757 $field = $this->getFieldForKey($key);
761 $value = $this->getDateTimeData($value, $field->getUseTime());
767 $this->getDateTimeData($value[
"start"], $field->getUseTime()),
768 $this->getDateTimeData($value[
"end"], $field->getUseTime()),
772 if ($field instanceof \
ILIAS\UI\
Component\Input\Field\OptionalGroup) {
773 $value = is_array($value);
781 if ($this->async_mode === self::ASYNC_NONE && !$this->
ctrl->isAsynch()) {
782 $html = $this->
ui->renderer()->render($this->getForm());
784 $html = $this->
ui->renderer()->renderAsync($this->getForm()) .
"<script>" . $this->
getOnLoadCode() .
"</script>";
@classDescription Date and time handling
addJavaScript(string $a_js_file, bool $a_add_version_parameter=true, int $a_batch=2)
Add a javascript file that should be included in the header.
addOnLoadCode(string $a_code, int $a_batch=2)
Add on load code.
static http()
Fetches the global http state from ILIAS.
form(?array $class_path, string $cmd, string $submit_caption="")
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
if(!file_exists('../ilias.ini.php'))