ILIAS  release_8 Revision v8.24
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;
28
34{
35 use Hasher;
36
37 public const F_TITLE = "title";
38 public const F_TYPE = "type";
39 public const F_PARENT = "parent";
40 public const F_ACTIVE = "active";
41 public const F_ICON = "icon";
42 public const F_ROLE_BASED_VISIBILITY = "role_based_visibility";
43
45
46 private Standard $form;
47
48 protected ilLanguage $lng;
49
50 protected ilCtrl $ctrl;
51
53
55
57
67 public function __construct(
74 ) {
75 $this->ctrl = $ctrl;
76 $this->ui_fa = $ui_fa;
77 $this->ui_re = $ui_re;
78 $this->lng = $lng;
79 $this->item_facade = $item;
80 $this->repository = $repository;
81 if (!$this->item_facade->isEmpty()) {
82 $this->ctrl->saveParameterByClass(ilMMSubItemGUI::class, ilMMAbstractItemGUI::IDENTIFIER);
83 }
84
85 $this->initForm();
86 }
87
88 private function initForm(): void
89 {
90 // TITLE
91 $txt = function ($id): string {
92 return $this->lng->txt($id);
93 };
94 $f = function (): InputFactory {
95 return $this->ui_fa->input();
96 };
97
98 $title = $f()->field()->text($txt('sub_title_default'), $txt('sub_title_default_byline'));
99 if (!$this->item_facade->isEmpty()) {
100 $title = $title->withValue($this->item_facade->getDefaultTitle());
101 }
102 $items[self::F_TITLE] = $title;
103
104 // TYPE
105 if (($this->item_facade->isEmpty() || $this->item_facade->isCustom())) {
106 $type_groups = $this->getTypeGroups($f);
107 $type = $f()->field()->switchableGroup($type_groups, $txt('sub_type'), $txt('sub_type_byline'))->withRequired(true);
108 if (!$this->item_facade->isEmpty()) {
109 $string = $this->item_facade->getType() === '' ? Link::class : $this->item_facade->getType();
110 $type = $type->withValue($this->hash($string));
111 } else {
112 $type = $type->withValue($this->hash(Link::class));
113 }
114 $items[self::F_TYPE] = $type;
115 }
116
117 // ICON
118 if ($this->item_facade->supportsCustomIcon()) {
119 // ICON
120 $icon = $f()->field()->file(new ilMMUploadHandlerGUI(), $txt('sub_icon'))
121 ->withByline($txt('sub_icon_byline'))
122 ->withAcceptedMimeTypes([MimeType::IMAGE__SVG_XML]);
123 if ($this->item_facade->getIconID() !== null) {
124 $icon = $icon->withValue([$this->item_facade->getIconID()]);
125 }
126
127 $items[self::F_ICON] = $icon;
128 }
129
130 // PARENT
131 $parent = $f()->field()->select($txt('sub_parent'), $this->repository->getPossibleParentsForFormAndTable())
132 ->withRequired(true);
133
134 $possible_parents = array_keys($this->repository->getPossibleParentsForFormAndTable());
135
136 if (!$this->item_facade->isEmpty() && !$this->item_facade->isInLostItem() && in_array($this->item_facade->getParentIdentificationString(), $possible_parents)) {
137 $parent = $parent->withValue($this->item_facade->getParentIdentificationString());
138 } else {
139 $parent = $parent->withValue(reset($possible_parents));
140 }
141 $items[self::F_PARENT] = $parent;
142
143 // ACTIVE
144 $active = $f()->field()->checkbox($txt('sub_active'), $txt('sub_active_byline'));
145 $active = $active->withValue($this->item_facade->isActivated());
146 $items[self::F_ACTIVE] = $active;
147
148 // ROLE BASED VISIBILITY
149 if ($this->item_facade->supportsRoleBasedVisibility()) {
150 $access = new ilObjMainMenuAccess();
151 $value_role_based_visibility = null;
152 if ($this->item_facade->hasRoleBasedVisibility() && !empty($this->item_facade->getGlobalRoleIDs())) {
153 // remove deleted roles, see https://mantis.ilias.de/view.php?id=34936
154 $value_role_based_visibility[0] = array_intersect(
155 $this->item_facade->getGlobalRoleIDs(),
156 array_keys($access->getGlobalRoles())
157 );
158 }
159 $role_based_visibility = $f()->field()->optionalGroup(
160 [
161 $f()->field()->multiSelect(
162 $txt('sub_global_roles'),
163 $access->getGlobalRoles()
164 )->withRequired(false)
165 ],
166 $txt('sub_role_based_visibility'),
167 $txt('sub_role_based_visibility_byline')
168 )->withValue($value_role_based_visibility);
169 $items[self::F_ROLE_BASED_VISIBILITY] = $role_based_visibility;
170 }
171
172 // RETURN FORM
173 if ($this->item_facade->isEmpty()) {
174 $section = $f()->field()->section($items, $txt(ilMMSubItemGUI::CMD_ADD), "");
175 $this->form = $f()->container()->form()
176 ->standard($this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_CREATE), [$section]);
177 } else {
178 $section = $f()->field()->section($items, $txt(ilMMSubItemGUI::CMD_EDIT), "");
179 $this->form = $f()->container()->form()
180 ->standard($this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_UPDATE), [$section]);
181 }
182 }
183
184 public function save(): bool
185 {
186 global $DIC;
187 $r = new ilMMItemRepository();
188 $this->form = $this->form->withRequest($DIC->http()->request());
189 $data = $this->form->getData();
190
191 if (is_null($data)) {
192 return false;
193 }
194
195 $role_based_visibility = $data[0][self::F_ROLE_BASED_VISIBILITY] ?? false;
196 $this->item_facade->setDefaultTitle((string) $data[0][self::F_TITLE]);
197 $this->item_facade->setActiveStatus((bool) $data[0][self::F_ACTIVE]);
198 $this->item_facade->setRoleBasedVisibility((bool) $role_based_visibility);
199
200 if ($role_based_visibility) {
201 $this->item_facade->setGlobalRoleIDs((array) $role_based_visibility[0]);
202 }
203 if ((string) $data[0][self::F_PARENT]) {
204 $this->item_facade->setParent((string) $data[0][self::F_PARENT]);
205 }
206 $this->item_facade->setIsTopItm(false);
207
208 if ($this->item_facade->isEmpty()) {
209 $type = $this->unhash((string) ($data[0][self::F_TYPE][0]));
210 $this->item_facade->setType($type);
211 $r->createItem($this->item_facade);
212 }
213
214 if ($this->item_facade->supportsCustomIcon()) {
215 $icon = (string) ($data[0][self::F_ICON][0] ?? '');
216 $this->item_facade->setIconID($icon);
217 }
218
219 if ($this->item_facade->isCustom()) {
220 $type = $this->item_facade->getType();
221 $type_specific_data = (array) $data[0][self::F_TYPE][1];
222 $type_handler = $this->repository->getTypeHandlerForType($type);
223 $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
224 }
225
226 $r->updateItem($this->item_facade);
227
228 return true;
229 }
230
231 public function getHTML(): string
232 {
233 return $this->ui_re->render([$this->form]);
234 }
235
240 private function getTypeGroups(Closure $f): array
241 {
242 $type_groups = [];
243 $type_informations = $this->repository->getPossibleSubItemTypesWithInformation();
244 foreach ($type_informations as $classname => $information) {
245 if ($this->item_facade->isEmpty()
246 || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType() && $this->item_facade->isCustom())
247 ) { // https://mantis.ilias.de/view.php?id=24152
248 $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm($this->item_facade->identification());
249 $type_groups[$this->hash($classname)] = $f()->field()->group($inputs, $information->getTypeNameForPresentation());
250 }
251 }
252
253 return $type_groups;
254 }
255}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Mime type determination.
Definition: MimeType.php:28
Class ilCtrl provides processing control methods.
language handling
Class ilMMItemRepository.
Class ilMMSubitemFormGUI.
ilMMItemRepository $repository
ilMMItemFacadeInterface $item_facade
__construct(ilCtrl $ctrl, Factory $ui_fa, Renderer $ui_re, ilLanguage $lng, ilMMItemFacadeInterface $item, ilMMItemRepository $repository)
ilMMSubitemFormGUI constructor.
Class ilMMUploadHandlerGUI.
Class ilObjMainMenuAccess.
$txt
Definition: error.php:13
global $DIC
Definition: feed.php:28
This describes a standard form.
Definition: Standard.php:27
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)
$type