ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMMSubitemFormGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Renderer\Hasher;
24use ILIAS\UI\Component\Input\Factory as InputFactory;
28use ILIAS\GlobalScreen\GUI\I18n\Translator;
29use Psr\Http\Message\RequestInterface;
30
36{
37 use Hasher;
38
39 public const F_TITLE = "title";
40 public const F_TYPE = "type";
41 public const F_PARENT = "parent";
42 public const F_ACTIVE = "active";
43 public const F_ICON = "icon";
44 public const F_ROLE_BASED_VISIBILITY = "role_based_visibility";
45
46 private Standard $form;
47
57 public function __construct(
58 protected ilCtrl $ctrl,
59 protected ILIAS\UI\Factory $ui_fa,
60 protected Renderer $ui_re,
61 protected Translator $lng,
62 private RequestInterface $request,
63 private ilMMItemFacadeInterface $item_facade,
64 private ilMMItemRepository $repository,
65 private ?ilMMItemFacadeInterface $parent_item = null
66 ) {
67 $this->initForm();
68 }
69
70 private function initForm(): void
71 {
72 // TITLE
73 $txt = (fn($id): string => $this->lng->txt($id));
74 $f = (fn(): InputFactory => $this->ui_fa->input());
75
76 $title = $f()->field()->text($txt('sub_title_default'), $txt('sub_title_default_byline'));
77 if (!$this->item_facade->isEmpty()) {
78 $title = $title->withValue($this->item_facade->getDefaultTitle());
79 }
80 $items[self::F_TITLE] = $title;
81
82 // TYPE
83 if (($this->item_facade->isEmpty() || $this->item_facade->isCustom())) {
84 $type_groups = $this->getTypeGroups($f);
85 $type = $f()->field()->switchableGroup(
86 $type_groups,
87 $txt('sub_type'),
88 $txt('sub_type_byline')
89 )->withRequired(true);
90 if (!$this->item_facade->isEmpty()) {
91 $string = $this->item_facade->getType() === '' ? Link::class : $this->item_facade->getType();
92 $type = $type->withValue($this->hash($string));
93 } else {
94 $type = $type->withValue($this->hash(Link::class));
95 }
96 $items[self::F_TYPE] = $type;
97 }
98
99 // ICON
100 if ($this->item_facade->supportsCustomIcon()) {
101 // ICON
102 $icon = $f()->field()->file(new ilMMUploadHandlerGUI(), $txt('sub_icon'))
103 ->withByline($txt('sub_icon_byline'))
104 ->withAcceptedMimeTypes([MimeType::IMAGE__SVG_XML]);
105 if ($this->item_facade->getIconID() !== null) {
106 $icon = $icon->withValue([$this->item_facade->getIconID()]);
107 }
108
109 $items[self::F_ICON] = $icon;
110 }
111
112 // PARENT
113 $possible_parents = array_keys($this->repository->getPossibleParentsForFormAndTable());
114 if (!$this->item_facade->isEmpty()
115 && !$this->item_facade->isInLostItem()
116 && in_array($this->item_facade->getParentIdentificationString(), $possible_parents, true)) {
117
118 $parent = $f()->field()
119 ->select(
120 $txt('sub_parent'),
121 $this->repository->getPossibleParentsForFormAndTable()
122 )
123 ->withRequired(true)
124 ->withValue($this->item_facade->getParentIdentificationString());
125 } else {
126 $parent = $f()->field()
127 ->hidden()
128 ->withRequired(true)
129 ->withValue(
130 $this->parent_item?->identification()->serialize() ?? reset($possible_parents)
131 );
132 }
133 $items[self::F_PARENT] = $parent;
134
135 // ACTIVE
136 $active = $f()->field()->checkbox($txt('sub_active'), $txt('sub_active_byline'));
137 $active = $active->withValue($this->item_facade->isActivated());
138 $items[self::F_ACTIVE] = $active;
139
140 // ROLE BASED VISIBILITY
141 if ($this->item_facade->supportsRoleBasedVisibility()) {
142 $access = new ilObjMainMenuAccess();
143 $value_role_based_visibility = null;
144 if ($this->item_facade->hasRoleBasedVisibility() && !empty($this->item_facade->getGlobalRoleIDs())) {
145 // remove deleted roles, see https://mantis.ilias.de/view.php?id=34936
146 $value_role_based_visibility[0] = array_intersect(
147 $this->item_facade->getGlobalRoleIDs(),
148 array_keys($access->getGlobalRoles())
149 );
150 }
151 $role_based_visibility = $f()->field()->optionalGroup(
152 [
153 $f()->field()->multiSelect(
154 $txt('sub_global_roles'),
155 $access->getGlobalRoles()
156 )->withRequired(false)
157 ],
158 $txt('sub_role_based_visibility'),
159 $txt('sub_role_based_visibility_byline')
160 )->withValue($value_role_based_visibility);
161 $items[self::F_ROLE_BASED_VISIBILITY] = $role_based_visibility;
162 }
163
164 // RETURN FORM
165 if ($this->item_facade->isEmpty()) {
166 $this->form = $f()->container()->form()->standard(
167 $this->ctrl->getLinkTargetByClass(
168 ilMMSubItemGUI::class,
170 ),
171 $items
172 );
173 } else {
174 $this->form = $f()->container()->form()->standard(
175 $this->ctrl->getLinkTargetByClass(
176 ilMMSubItemGUI::class,
178 ),
179 $items
180 );
181 }
182 }
183
184 public function save(): bool
185 {
186 $r = new ilMMItemRepository();
187 $this->form = $this->form->withRequest($this->request);
188 $data = $this->form->getData();
189
190 if (is_null($data)) {
191 return false;
192 }
193
194 $role_based_visibility = $data[self::F_ROLE_BASED_VISIBILITY] ?? false;
195 $this->item_facade->setDefaultTitle((string) $data[self::F_TITLE]);
196 $this->item_facade->setActiveStatus((bool) $data[self::F_ACTIVE]);
197 $this->item_facade->setRoleBasedVisibility((bool) $role_based_visibility);
198
199 if ($role_based_visibility) {
200 $this->item_facade->setGlobalRoleIDs((array) $role_based_visibility[0]);
201 }
202 if ((string) $data[self::F_PARENT] !== '' && (string) $data[self::F_PARENT] !== '0') {
203 $this->item_facade->setParent((string) $data[self::F_PARENT]);
204 }
205 $this->item_facade->setIsTopItm(false);
206
207 if ($this->item_facade->isEmpty()) {
208 $type = $this->unhash((string) ($data[self::F_TYPE][0]));
209 $this->item_facade->setType($type);
210 $r->createItem($this->item_facade);
211 }
212
213 if ($this->item_facade->supportsCustomIcon()) {
214 $icon = (string) ($data[self::F_ICON][0] ?? '');
215 $this->item_facade->setIconID($icon);
216 }
217
218 if ($this->item_facade->isCustom()) {
219 $type = $this->item_facade->getType();
220 $type_specific_data = (array) $data[self::F_TYPE][1];
221 $type_handler = $this->repository->getTypeHandlerForType($type);
222 $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
223 }
224
225 $r->updateItem($this->item_facade);
226
227 return true;
228 }
229
233 public function getHTML(): string
234 {
235 return $this->ui_re->render([$this->form]);
236 }
237
238 public function get(): Standard
239 {
240 return $this->form;
241 }
242
247 private function getTypeGroups(Closure $f): array
248 {
249 $type_groups = [];
250 $type_informations = $this->repository->getPossibleSubItemTypesWithInformation();
251 foreach ($type_informations as $classname => $information) {
252 if ($this->item_facade->isEmpty()
253 || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType(
254 ) && $this->item_facade->isCustom())
255 ) { // https://mantis.ilias.de/view.php?id=24152
256 $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm(
257 $this->item_facade->identification()
258 );
259 $type_groups[$this->hash($classname)] = $f()->field()->group(
260 $inputs,
261 $information->getTypeNameForPresentation()
262 );
263 }
264 }
265
266 return $type_groups;
267 }
268}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Mime type determination.
Definition: MimeType.php:30
Class ilCtrl provides processing control methods.
Class ilMMSubitemFormGUI.
__construct(protected ilCtrl $ctrl, protected ILIAS\UI\Factory $ui_fa, protected Renderer $ui_re, protected Translator $lng, private RequestInterface $request, private ilMMItemFacadeInterface $item_facade, private ilMMItemRepository $repository, private ?ilMMItemFacadeInterface $parent_item=null)
ilMMSubitemFormGUI constructor.
Class ilMMUploadHandlerGUI.
Class ilObjMainMenuAccess.
$txt
Definition: error.php:31
This describes a standard form.
Definition: Standard.php:29
This is how a factory for inputs looks like.
Definition: Factory.php:27
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
Interface ilMMItemFacadeInterface.
form( $class_path, string $cmd, string $submit_caption="")
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31