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 =
"",
199 $this->values[$key] = $value;
200 $field = $this->
ui->factory()->input()->field()->checkbox($title, $description);
201 if (!is_null($value)) {
202 $field = $field->withValue($value);
204 $this->addField($key, $field);
212 $this->values[$key] = $value;
213 $field = $this->
ui->factory()->input()->field()->hidden();
214 $field = $field->withValue($value);
215 $this->addField($key, $field);
221 if ($required && ($field = $this->getLastField())) {
223 $field = $field->withRequired(
true,
new NotEmpty(
228 $field = $field->withRequired(
true);
230 $this->replaceLastField($field);
237 if ($disabled && ($field = $this->getLastField())) {
238 $field = $field->withDisabled(
true);
239 $this->disable[$this->last_key] =
true;
240 $this->replaceLastField($field);
248 string $description =
"",
249 ?
string $value =
null
251 $this->values[$key] = $value;
252 $field = $this->
ui->factory()->input()->field()->textarea($title, $description);
253 if (!is_null($value)) {
254 $field = $field->withValue($value);
256 $this->addField($key, $field);
263 string $description =
"",
265 ?
int $min_value =
null,
266 ?
int $max_value =
null
268 $this->values[$key] = $value;
270 if (!is_null($min_value)) {
271 $trans[] = $this->
refinery->int()->isGreaterThanOrEqual($min_value);
273 if (!is_null($max_value)) {
274 $trans[] = $this->
refinery->int()->isLessThanOrEqual($max_value);
276 $field = $this->
ui->factory()->input()->field()->numeric($title, $description);
277 if (count($trans) > 0) {
278 $field = $field->withAdditionalTransformation($this->
refinery->logical()->parallel($trans));
280 if (!is_null($value)) {
281 $field = $field->withValue($value);
283 $this->addField($key, $field);
290 string $description =
"",
293 $this->values[$key] = $value;
294 $field = $this->
ui->factory()->input()->field()->dateTime($title, $description);
296 $format = $this->
user->getDateFormat();
297 $dt_format = (string) $format;
299 $field = $field->withFormat($format);
300 if (!is_null($value)) {
301 $field = $field->withValue(
302 (
new \DateTime($value->get(
IL_CAL_DATE)))->format($dt_format)
305 $this->addField($key, $field);
312 string $description =
"",
315 $this->values[$key] = $value;
316 $field = $this->
ui->factory()->input()->field()->dateTime($title, $description)->withUseTime(
true);
319 $dt_format = $this->data->dateFormat()->withTime12($this->
user->getDateFormat());
321 $dt_format = $this->data->dateFormat()->withTime24($this->
user->getDateFormat());
323 $field = $field->withFormat($dt_format);
325 $field = $field->withValue(
327 ((
string) $dt_format)
331 $this->addField($key, $field);
338 string $description =
"",
341 string $label_from =
"",
342 string $label_to =
""
344 $this->values[$key] = [$from, $to];
345 if ($label_from ===
"") {
346 $label_from = $this->
lng->txt(
"rep_activation_limited_start");
348 if ($label_to ===
"") {
349 $label_to = $this->
lng->txt(
"rep_activation_limited_end");
351 $field = $this->
ui->factory()->input()->field()->duration($title, $description)->withUseTime(
true)->withLabels($label_from, $label_to);
354 $dt_format = $this->data->dateFormat()->withTime12($this->
user->getDateFormat());
356 $dt_format = $this->data->dateFormat()->withTime24($this->
user->getDateFormat());
358 $field = $field->withFormat($dt_format);
359 $val_from = $val_to =
null;
361 $val_from = (new \DateTime(
363 ))->format((
string) $dt_format);
366 $val_to = (new \DateTime(
368 ))->format((
string) $dt_format);
370 $field = $field->withValue([$val_from, $val_to]);
371 $this->addField($key, $field);
377 if (is_null($value)) {
381 return new \ilDateTime($value->format(
"Y-m-d H:i:s"),
IL_CAL_DATETIME);
383 return new \ilDate($value->format(
"Y-m-d"),
IL_CAL_DATE);
390 string $description =
"",
391 ?
string $value =
null
393 $this->values[$key] = $value;
394 $field = $this->
ui->factory()->input()->field()->select($title,
$options, $description);
395 if (!is_null($value)) {
396 $field = $field->withValue($value);
408 string $description =
"",
409 ?
string $value =
null
411 $this->values[$key] = $value;
412 $field = $this->
ui->factory()->input()->field()->radio($title, $description);
420 public function radioOption(
string $value,
string $title,
string $description =
""): self
422 if ($field = $this->getLastField()) {
423 $field = $field->withOption($value, $title, $description);
424 if (($this->values[$this->last_key] ??
null) === $value) {
425 $field = $field->withValue($value);
427 $this->replaceLastField($field);
432 public function switch(
435 string $description =
"",
436 ?
string $value =
null
438 $this->values[$key] = $value;
439 $this->current_switch = [
442 "description" => $description,
452 string $description =
"",
455 $this->values[$key] = $value;
456 $this->current_optional = [
459 "description" => $description,
466 public function group(
string $key,
string $title,
string $description =
"", $disabled =
false): self
468 $this->endCurrentGroup();
469 $this->current_group = [
472 "description" => $description,
473 "disabled" => $disabled,
481 if (!is_null($this->current_group)) {
482 if (!is_null($this->current_switch)) {
484 foreach ($this->current_group[
"fields"] as $key) {
485 $fields[$key] = $this->fields[$key];
487 $this->current_switch[
"groups"][$this->current_group[
"key"]] =
488 $this->
ui->factory()->input()->field()->group(
490 $this->current_group[
"title"]
491 )->withByline($this->current_group[
"description"]);
492 if ($this->current_group[
"disabled"]) {
493 $this->current_switch[
"groups"][$this->current_group[
"key"]] =
494 $this->current_switch[
"groups"][$this->current_group[
"key"]]
495 ->withDisabled(
true);
499 $this->current_group =
null;
502 public function end(): self
504 $this->endCurrentGroup();
505 if (!is_null($this->current_switch)) {
506 $field = $this->
ui->factory()->input()->field()->switchableGroup(
507 $this->current_switch[
"groups"],
508 $this->current_switch[
"title"],
509 $this->current_switch[
"description"]
511 if (!is_null($this->current_switch[
"value"])) {
512 $cvalue = $this->current_switch[
"value"];
513 if (isset($this->current_switch[
"groups"][$cvalue])) {
514 $field = $field->withValue($cvalue);
517 $key = $this->current_switch[
"key"];
518 $this->current_switch =
null;
519 $this->addField($key, $field);
521 if (!is_null($this->current_optional)) {
522 $field = $this->
ui->factory()->input()->field()->optionalGroup(
523 $this->current_optional[
"fields"],
524 $this->current_optional[
"title"],
525 $this->current_optional[
"description"]
527 if ($this->current_optional[
"value"]) {
529 foreach ($this->current_optional[
"fields"] as $key => $input) {
530 $value[$key] = $input->getValue();
532 $field = $field->withValue($value);
534 $field = $field->withValue(
null);
536 $key = $this->current_optional[
"key"];
537 $this->current_optional =
null;
538 $this->addField($key, $field);
546 \Closure $result_handler,
547 string $id_parameter,
548 string $description =
"",
549 ?
int $max_files =
null,
550 array $mime_types = [],
551 array $ctrl_path = [],
552 string $logger_id =
""
561 foreach ([
"application/x-compressed",
"application/x-zip-compressed"] as $zipmime) {
562 if (in_array(
"application/zip", $mime_types) &&
563 !in_array($zipmime, $mime_types)) {
564 $mime_types[] = $zipmime;
568 if (count($mime_types) > 0) {
569 $description .= $this->
lng->txt(
"rep_allowed_types") .
": " .
570 implode(
", ", $mime_types);
573 $field = $this->
ui->factory()->input()->field()->file(
574 $this->upload_handler[$key],
580 if (!is_null($max_files)) {
581 $field = $field->withMaxFiles($max_files);
583 if (count($mime_types) > 0) {
584 $field = $field->withAcceptedMimeTypes($mime_types);
596 if (!isset($this->upload_handler[$key])) {
597 throw new \ilException(
"Unknown file upload field: " . $key);
599 return $this->upload_handler[$key];
606 throw new \ilException(
"Missing Input Key: " . $key);
608 if (isset($this->field[$key])) {
609 throw new \ilException(
"Duplicate Input Key: " . $key);
612 if ($this->current_section !== self::DEFAULT_SECTION) {
613 $field_path[] = $this->current_section;
615 if (!is_null($this->current_group)) {
616 $this->current_group[
"fields"][] = $key;
617 if (!is_null($this->current_switch)) {
618 $field_path[] = $this->current_switch[
"key"];
620 $field_path[] = $key;
622 } elseif (!is_null($this->current_optional)) {
623 $field_path[] = $this->current_optional[
"key"];
624 $this->current_optional[
"fields"][$key] = $field;
625 $field_path[] = $key;
627 $this->sections[$this->current_section][
"fields"][] = $key;
628 $field_path[] = $key;
636 if (!$supress_0_key) {
641 $this->fields[$key] = $field;
642 $this->field_path[$key] = $field_path;
643 $this->last_key = $key;
649 if (!isset($this->fields[$key])) {
650 throw new \ilException(
"Unknown Key: " . $key);
652 return $this->fields[$key];
657 return $this->fields[$this->last_key] ??
null;
662 if ($this->last_key !==
"") {
663 $this->fields[$this->last_key] = $field;
666 if (!is_null($this->current_optional) && isset($this->current_optional[
"fields"][$this->last_key])) {
667 $this->current_optional[
"fields"][$this->last_key] = $field;
675 if (is_null($this->
form)) {
676 $async = ($this->async_mode !== self::ASYNC_NONE);
678 if (!is_null($this->class_path)) {
679 $action = $ctrl->getLinkTargetByClass($this->class_path, $this->cmd,
"", $async);
682 foreach ($this->sections as $sec_key => $section) {
683 if ($sec_key === self::DEFAULT_SECTION) {
684 foreach ($this->sections[$sec_key][
"fields"] as $f_key) {
685 $inputs[$f_key] = $this->getFieldForKey($f_key);
687 } elseif (count($this->sections[$sec_key][
"fields"]) > 0) {
689 foreach ($this->sections[$sec_key][
"fields"] as $f_key) {
690 $sec_inputs[$f_key] = $this->getFieldForKey($f_key);
692 $inputs[$sec_key] = $this->
ui->factory()->input()->field()->section(
695 $section[
"description"]
699 $this->
form = $this->
ui->factory()->input()->container()->form()->standard(
703 if ($this->submit_caption !==
"") {
704 $this->
form = $this->
form->withSubmitLabel($this->submit_caption);
712 return $this->getForm()->getSubmitLabel() ?? $this->
lng->txt(
"save");
717 if (is_null($this->raw_data)) {
718 $request = $this->
http->request();
719 $this->
form = $this->getForm()->withRequest($request);
720 $this->raw_data = $this->
form->getData();
727 return !(is_null($this->raw_data));
733 public function getData(
string $key): mixed
737 if (!isset($this->fields[$key])) {
741 if (isset($this->disable[$key])) {
742 return $this->values[$key];
745 $value = $this->raw_data;
746 foreach ($this->field_path[$key] as $path_key) {
747 if (!isset($value[$path_key])) {
750 $value = $value[$path_key];
753 $field = $this->getFieldForKey($key);
757 $value = $this->getDateTimeData($value, $field->getUseTime());
763 $this->getDateTimeData($value[
"start"], $field->getUseTime()),
764 $this->getDateTimeData($value[
"end"], $field->getUseTime()),
768 if ($field instanceof \
ILIAS\UI\
Component\Input\Field\OptionalGroup) {
769 $value = is_array($value);
777 if ($this->async_mode === self::ASYNC_NONE && !$this->
ctrl->isAsynch()) {
778 $html = $this->
ui->renderer()->render($this->getForm());
780 $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'))