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
180 $this->values[$key] = $value;
181 $field = $this->
ui->factory()->input()->field()->text($title, $description);
182 if (!is_null($value)) {
183 $field = $field->withValue($value);
185 $this->addField($key, $field);
192 string $description =
"",
195 $this->values[$key] = $value;
196 $field = $this->
ui->factory()->input()->field()->checkbox($title, $description);
197 if (!is_null($value)) {
198 $field = $field->withValue($value);
200 $this->addField($key, $field);
208 $this->values[$key] = $value;
209 $field = $this->
ui->factory()->input()->field()->hidden();
210 $field = $field->withValue($value);
211 $this->addField($key, $field);
217 if ($required && ($field = $this->getLastField())) {
219 $field = $field->withRequired(
true,
new NotEmpty(
224 $field = $field->withRequired(
true);
226 $this->replaceLastField($field);
233 if ($disabled && ($field = $this->getLastField())) {
234 $field = $field->withDisabled(
true);
235 $this->disable[$this->last_key] =
true;
236 $this->replaceLastField($field);
244 string $description =
"",
245 ?
string $value =
null
247 $this->values[$key] = $value;
248 $field = $this->
ui->factory()->input()->field()->textarea($title, $description);
249 if (!is_null($value)) {
250 $field = $field->withValue($value);
252 $this->addField($key, $field);
259 string $description =
"",
261 ?
int $min_value =
null,
262 ?
int $max_value =
null
264 $this->values[$key] = $value;
266 if (!is_null($min_value)) {
267 $trans[] = $this->
refinery->int()->isGreaterThanOrEqual($min_value);
269 if (!is_null($max_value)) {
270 $trans[] = $this->
refinery->int()->isLessThanOrEqual($max_value);
272 $field = $this->
ui->factory()->input()->field()->numeric($title, $description);
273 if (count($trans) > 0) {
274 $field = $field->withAdditionalTransformation($this->
refinery->logical()->parallel($trans));
276 if (!is_null($value)) {
277 $field = $field->withValue($value);
279 $this->addField($key, $field);
286 string $description =
"",
289 $this->values[$key] = $value;
290 $field = $this->
ui->factory()->input()->field()->dateTime($title, $description);
292 $format = $this->
user->getDateFormat();
293 $dt_format = (string) $format;
295 $field = $field->withFormat($format);
296 if (!is_null($value)) {
297 $field = $field->withValue(
298 (
new \DateTime($value->get(
IL_CAL_DATE)))->format($dt_format)
301 $this->addField($key, $field);
308 string $description =
"",
311 $this->values[$key] = $value;
312 $field = $this->
ui->factory()->input()->field()->dateTime($title, $description)->withUseTime(
true);
315 $dt_format = $this->data->dateFormat()->withTime12($this->
user->getDateFormat());
317 $dt_format = $this->data->dateFormat()->withTime24($this->
user->getDateFormat());
319 $field = $field->withFormat($dt_format);
321 $field = $field->withValue(
323 ((
string) $dt_format)
327 $this->addField($key, $field);
334 string $description =
"",
337 string $label_from =
"",
338 string $label_to =
""
340 $this->values[$key] = [$from, $to];
341 if ($label_from ===
"") {
342 $label_from = $this->
lng->txt(
"rep_activation_limited_start");
344 if ($label_to ===
"") {
345 $label_to = $this->
lng->txt(
"rep_activation_limited_end");
347 $field = $this->
ui->factory()->input()->field()->duration($title, $description)->withUseTime(
true)->withLabels($label_from, $label_to);
350 $dt_format = $this->data->dateFormat()->withTime12($this->
user->getDateFormat());
352 $dt_format = $this->data->dateFormat()->withTime24($this->
user->getDateFormat());
354 $field = $field->withFormat($dt_format);
355 $val_from = $val_to =
null;
357 $val_from = (new \DateTime(
359 ))->format((
string) $dt_format);
362 $val_to = (new \DateTime(
364 ))->format((
string) $dt_format);
366 $field = $field->withValue([$val_from, $val_to]);
367 $this->addField($key, $field);
373 if (is_null($value)) {
377 return new \ilDateTime($value->format(
"Y-m-d H:i:s"),
IL_CAL_DATETIME);
379 return new \ilDate($value->format(
"Y-m-d"),
IL_CAL_DATE);
386 string $description =
"",
387 ?
string $value =
null
389 $this->values[$key] = $value;
390 $field = $this->
ui->factory()->input()->field()->select($title,
$options, $description);
391 if (!is_null($value)) {
392 $field = $field->withValue($value);
404 string $description =
"",
405 ?
string $value =
null
407 $this->values[$key] = $value;
408 $field = $this->
ui->factory()->input()->field()->radio($title, $description);
409 if (!is_null($value)) {
410 $field = $field->withOption($value,
"");
411 $field = $field->withValue($value);
420 public function radioOption(
string $value,
string $title,
string $description =
""): self
422 if ($field = $this->getLastField()) {
423 $field = $field->withOption($value, $title, $description);
424 $this->replaceLastField($field);
429 public function switch(
432 string $description =
"",
433 ?
string $value =
null
435 $this->values[$key] = $value;
436 $this->current_switch = [
439 "description" => $description,
449 string $description =
"",
452 $this->values[$key] = $value;
453 $this->current_optional = [
456 "description" => $description,
463 public function group(
string $key,
string $title,
string $description =
"", $disabled =
false): self
465 $this->endCurrentGroup();
466 $this->current_group = [
469 "description" => $description,
470 "disabled" => $disabled,
478 if (!is_null($this->current_group)) {
479 if (!is_null($this->current_switch)) {
481 foreach ($this->current_group[
"fields"] as $key) {
482 $fields[$key] = $this->fields[$key];
484 $this->current_switch[
"groups"][$this->current_group[
"key"]] =
485 $this->
ui->factory()->input()->field()->group(
487 $this->current_group[
"title"]
488 )->withByline($this->current_group[
"description"]);
489 if ($this->current_group[
"disabled"]) {
490 $this->current_switch[
"groups"][$this->current_group[
"key"]] =
491 $this->current_switch[
"groups"][$this->current_group[
"key"]]
492 ->withDisabled(
true);
496 $this->current_group =
null;
499 public function end(): self
501 $this->endCurrentGroup();
502 if (!is_null($this->current_switch)) {
503 $field = $this->
ui->factory()->input()->field()->switchableGroup(
504 $this->current_switch[
"groups"],
505 $this->current_switch[
"title"],
506 $this->current_switch[
"description"]
508 if (!is_null($this->current_switch[
"value"])) {
509 $cvalue = $this->current_switch[
"value"];
510 if (isset($this->current_switch[
"groups"][$cvalue])) {
511 $field = $field->withValue($cvalue);
514 $key = $this->current_switch[
"key"];
515 $this->current_switch =
null;
516 $this->addField($key, $field);
518 if (!is_null($this->current_optional)) {
519 $field = $this->
ui->factory()->input()->field()->optionalGroup(
520 $this->current_optional[
"fields"],
521 $this->current_optional[
"title"],
522 $this->current_optional[
"description"]
524 if ($this->current_optional[
"value"]) {
526 foreach ($this->current_optional[
"fields"] as $key => $input) {
527 $value[$key] = $input->getValue();
529 $field = $field->withValue($value);
531 $field = $field->withValue(
null);
533 $key = $this->current_optional[
"key"];
534 $this->current_optional =
null;
535 $this->addField($key, $field);
543 \Closure $result_handler,
544 string $id_parameter,
545 string $description =
"",
546 ?
int $max_files =
null,
547 array $mime_types = [],
548 array $ctrl_path = [],
549 string $logger_id =
""
558 foreach ([
"application/x-compressed",
"application/x-zip-compressed"] as $zipmime) {
559 if (in_array(
"application/zip", $mime_types) &&
560 !in_array($zipmime, $mime_types)) {
561 $mime_types[] = $zipmime;
565 if (count($mime_types) > 0) {
566 $description .= $this->
lng->txt(
"rep_allowed_types") .
": " .
567 implode(
", ", $mime_types);
570 $field = $this->
ui->factory()->input()->field()->file(
571 $this->upload_handler[$key],
577 if (!is_null($max_files)) {
578 $field = $field->withMaxFiles($max_files);
580 if (count($mime_types) > 0) {
581 $field = $field->withAcceptedMimeTypes($mime_types);
593 if (!isset($this->upload_handler[$key])) {
594 throw new \ilException(
"Unknown file upload field: " . $key);
596 return $this->upload_handler[$key];
603 throw new \ilException(
"Missing Input Key: " . $key);
605 if (isset($this->field[$key])) {
606 throw new \ilException(
"Duplicate Input Key: " . $key);
609 if ($this->current_section !== self::DEFAULT_SECTION) {
610 $field_path[] = $this->current_section;
612 if (!is_null($this->current_group)) {
613 $this->current_group[
"fields"][] = $key;
614 if (!is_null($this->current_switch)) {
615 $field_path[] = $this->current_switch[
"key"];
617 $field_path[] = $key;
619 } elseif (!is_null($this->current_optional)) {
620 $field_path[] = $this->current_optional[
"key"];
621 $this->current_optional[
"fields"][$key] = $field;
622 $field_path[] = $key;
624 $this->sections[$this->current_section][
"fields"][] = $key;
625 $field_path[] = $key;
633 if (!$supress_0_key) {
638 $this->fields[$key] = $field;
639 $this->field_path[$key] = $field_path;
640 $this->last_key = $key;
646 if (!isset($this->fields[$key])) {
647 throw new \ilException(
"Unknown Key: " . $key);
649 return $this->fields[$key];
654 return $this->fields[$this->last_key] ??
null;
659 if ($this->last_key !==
"") {
660 $this->fields[$this->last_key] = $field;
668 if (is_null($this->
form)) {
669 $async = ($this->async_mode !== self::ASYNC_NONE);
671 if (!is_null($this->class_path)) {
672 $action = $ctrl->getLinkTargetByClass($this->class_path, $this->cmd,
"", $async);
675 foreach ($this->sections as $sec_key => $section) {
676 if ($sec_key === self::DEFAULT_SECTION) {
677 foreach ($this->sections[$sec_key][
"fields"] as $f_key) {
678 $inputs[$f_key] = $this->getFieldForKey($f_key);
680 } elseif (count($this->sections[$sec_key][
"fields"]) > 0) {
682 foreach ($this->sections[$sec_key][
"fields"] as $f_key) {
683 $sec_inputs[$f_key] = $this->getFieldForKey($f_key);
685 $inputs[$sec_key] = $this->
ui->factory()->input()->field()->section(
688 $section[
"description"]
692 $this->
form = $this->
ui->factory()->input()->container()->form()->standard(
696 if ($this->submit_caption !==
"") {
697 $this->
form = $this->
form->withSubmitLabel($this->submit_caption);
705 return $this->getForm()->getSubmitLabel() ?? $this->
lng->txt(
"save");
710 if (is_null($this->raw_data)) {
711 $request = $this->
http->request();
712 $this->
form = $this->getForm()->withRequest($request);
713 $this->raw_data = $this->
form->getData();
720 return !(is_null($this->raw_data));
726 public function getData(
string $key): mixed
730 if (!isset($this->fields[$key])) {
734 if (isset($this->disable[$key])) {
735 return $this->values[$key];
738 $value = $this->raw_data;
739 foreach ($this->field_path[$key] as $path_key) {
740 if (!isset($value[$path_key])) {
743 $value = $value[$path_key];
746 $field = $this->getFieldForKey($key);
750 $value = $this->getDateTimeData($value, $field->getUseTime());
756 $this->getDateTimeData($value[
"start"], $field->getUseTime()),
757 $this->getDateTimeData($value[
"end"], $field->getUseTime()),
761 if ($field instanceof \
ILIAS\UI\
Component\Input\Field\OptionalGroup) {
762 $value = is_array($value);
770 if ($this->async_mode === self::ASYNC_NONE && !$this->
ctrl->isAsynch()) {
771 $html = $this->
ui->renderer()->render($this->getForm());
773 $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'))