ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
IconFormUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\File\Icon;
22 
25 
30 {
31  public const MODE_CREATE = 'create';
32  public const MODE_EDIT = 'edit';
33  public const FORM_ICON_CREATION = 'form_icon_creation';
34  public const FORM_ICON_UPDATING = 'form_icon_updating';
35  public const INPUT_ICON = 'input_icon';
36  public const INPUT_DESC_ICON = 'input_desc_icon';
37  public const INPUT_ACTIVE = 'input_active';
38  public const INPUT_DESC_ACTIVE = 'input_desc_active';
39  public const INPUT_SUFFIXES = 'input_suffixes';
40  public const INPUT_DESC_SUFFIXES = 'input_desc_suffixes';
41 
42  private \ilCtrl $ctrl;
43  private \ilLanguage $lng;
44  private \ilTabsGUI $tabs;
45  private \ILIAS\UI\Factory $ui_factory;
47  private \ILIAS\UI\Component\Input\Container\Form\Standard $icon_form;
48  private \ILIAS\Refinery\Factory $refinery;
49  private Icon $icon;
50  private string $mode;
52 
53  public function __construct(
54  Icon $icon,
55  string $mode,
56  IconRepositoryInterface $icon_repo
57  ) {
58  $this->icon = $icon;
59  $this->mode = $mode;
60  $this->icon_repo = $icon_repo;
61  global $DIC;
62  $this->ctrl = $DIC->ctrl();
63  $this->lng = $DIC->language();
64  $this->tabs = $DIC->tabs();
65  $this->ui_factory = $DIC->ui()->factory();
66  $this->wrapper = $DIC->http()->wrapper();
67  $this->refinery = $DIC->refinery();
68 
69  $this->initIconForm();
70  }
71 
72  protected function initIconForm(): void
73  {
74  $this->tabs->clearSubTabs();
75  $this->tabs->clearTargets();
76  $back_target = $this->ctrl->getLinkTargetByClass(
77  ilObjFileIconsOverviewGUI::class,
79  );
80  $this->tabs->setBackTarget($this->lng->txt('back'), $back_target);
81 
82  if ($this->mode == self::MODE_EDIT) {
83  $form_title = $this->lng->txt(self::FORM_ICON_UPDATING);
84  $form_action = $this->ctrl->getFormActionByClass(
85  ilObjFileIconsOverviewGUI::class,
87  );
88  } else {
89  $form_title = $this->lng->txt(self::FORM_ICON_CREATION);
90  $form_action = $this->ctrl->getFormActionByClass(
91  ilObjFileIconsOverviewGUI::class,
93  );
94  }
95 
96  $rid = $this->icon->getRid();
97  $this->ctrl->setParameterByClass(ilIconUploadHandlerGUI::class, 'rid', $rid);
98  $icon_input = $this->ui_factory->input()->field()->file(
100  $this->lng->txt(self::INPUT_ICON),
101  $this->lng->txt(self::INPUT_DESC_ICON)
102  )->withAcceptedMimeTypes(
103  [MimeType::IMAGE__SVG_XML]
104  )->withRequired(
105  true
106  );
107  if ($rid !== "") {
108  $icon_input = $icon_input->withValue([$rid]);
109  }
110 
111  $active_input = $this->ui_factory->input()->field()->checkbox(
112  $this->lng->txt(self::INPUT_ACTIVE),
113  $this->lng->txt(self::INPUT_DESC_ACTIVE)
114  )->withValue(
115  $this->icon->isActive()
116  );
117 
118  $suffix_input = $this->ui_factory->input()->field()->text(
119  $this->lng->txt(self::INPUT_SUFFIXES),
120  $this->lng->txt(self::INPUT_DESC_SUFFIXES)
121  )->withRequired(
122  true
123  )->withValue(
124  $this->icon_repo->turnSuffixesArrayIntoString($this->icon->getSuffixes())
125  )->withAdditionalTransformation(
126  $this->refinery->custom()->transformation(function ($suffixes_input): array {
127  return $this->icon_repo->turnSuffixesStringIntoArray($suffixes_input);
128  })
129  )->withAdditionalTransformation(
130  $this->refinery->custom()->constraint(function ($suffixes_input): bool {
131  return $this->icon_repo->hasSuffixInputOnlyAllowedCharacters($suffixes_input);
132  }, $this->lng->txt('msg_error_suffixes_with_forbidden_characters'))
133  )->withAdditionalTransformation(
134  $this->refinery->custom()->constraint(function ($suffixes_input): bool {
135  return $this->icon_repo->hasSuffixInputNoDuplicatesToItsOwnEntries($suffixes_input);
136  }, $this->lng->txt('msg_error_duplicate_suffix_entries'))
137  )->withAdditionalTransformation(
138  $this->refinery->custom()->constraint(function ($suffixes_input): bool {
139  //retrieve the value of the active_input as it is needed for the causesNoActiveSuffixesConflict validation
140  $section = $this->icon_form->getInputs()[0];
141  $inputs = $section->getInputs();
142  $input_active = $inputs[self::INPUT_ACTIVE];
143  $field_is_active = $input_active->getName();
144  $to_bool = $this->refinery->custom()->transformation(function ($checkbox_input_value): bool {
145  return $this->transformCheckboxInputValueToBool($checkbox_input_value);
146  });
147  $post_is_active_value = $this->wrapper->post()->retrieve($field_is_active, $to_bool);
148 
149  return $this->icon_repo->causesNoActiveSuffixesConflict(
150  $suffixes_input,
151  $post_is_active_value,
152  $this->icon
153  );
154  }, $this->lng->txt('msg_error_active_suffixes_conflict'))
155  );
156 
157  $section = $this->ui_factory->input()->field()->section([
158  self::INPUT_ICON => $icon_input,
159  self::INPUT_ACTIVE => $active_input,
160  self::INPUT_SUFFIXES => $suffix_input
161  ], $form_title);
162  $this->icon_form = $this->ui_factory->input()->container()->form()->standard($form_action, [$section]);
163  }
164 
165  public function getIconForm(): \ILIAS\UI\Component\Input\Container\Form\Standard
166  {
167  return $this->icon_form;
168  }
169 
170  public function transformCheckboxInputValueToBool($checkbox_input_value): bool
171  {
172  if ($checkbox_input_value !== null) {
173  $checkbox_input_value_str = $this->refinery->kindlyTo()->string()->transform($checkbox_input_value);
174  if ($checkbox_input_value_str == "checked") {
175  return true;
176  }
177  }
178  return false;
179  }
180 }
ILIAS UI Component Input Container Form Standard $icon_form
Definition: IconFormUI.php:47
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ILIAS UI Factory $ui_factory
Definition: IconFormUI.php:45
global $DIC
Definition: shib_login.php:25
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
ILIAS Refinery Factory $refinery
Definition: IconFormUI.php:48
__construct(Icon $icon, string $mode, IconRepositoryInterface $icon_repo)
Definition: IconFormUI.php:53
WrapperFactory $wrapper
Definition: IconFormUI.php:46
IconRepositoryInterface $icon_repo
Definition: IconFormUI.php:51
transformCheckboxInputValueToBool($checkbox_input_value)
Definition: IconFormUI.php:170