ILIAS  release_8 Revision v8.24
class.ilMMTopItemFormGUI.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;
29
35{
36 use Hasher;
37
38 private const F_ICON = 'icon';
39
40 private Services $http;
41
43
44 private Standard $form;
45
47
49
50 protected ilLanguage $lng;
51
52 protected ilCtrl $ctrl;
53
55
63 public const F_ACTIVE = 'active';
64 public const F_TITLE = 'title';
65 public const F_TYPE = 'type';
66 public const F_ROLE_BASED_VISIBILITY = "role_based_visibility";
67
68 public function __construct(
76 ) {
77 $this->repository = $repository;
78 $this->http = $http;
79 $this->ctrl = $ctrl;
80 $this->ui_fa = $ui_fa;
81 $this->ui_re = $ui_re;
82 $this->lng = $lng;
83 $this->item_facade = $item;
84 $this->access = new ilObjMainMenuAccess();
85 if (!$this->item_facade->isEmpty()) {
86 $this->ctrl->saveParameterByClass(ilMMTopItemGUI::class, ilMMAbstractItemGUI::IDENTIFIER);
87 }
88
89 $this->initForm();
90 }
91
92 private function initForm(): void
93 {
94 $txt = function ($key) {
95 return $this->lng->txt($key);
96 };
97 $f = function (): InputFactory {
98 return $this->ui_fa->input();
99 };
100
101 // TITLE
102 $title = $f()->field()->text($txt('topitem_title_default'), $txt('topitem_title_default_byline'))
103 ->withRequired(true);
104 if (!$this->item_facade->isEmpty()) {
105 $title = $title->withValue($this->item_facade->getDefaultTitle());
106 }
107
108 $items[self::F_TITLE] = $title;
109
110 if ($this->item_facade->supportsCustomIcon()) {
111 // ICON
112 $icon = $f()->field()->file(new ilMMUploadHandlerGUI(), $txt('topitem_icon'))
113 ->withByline($txt('topitem_icon_byline'))
114 ->withAcceptedMimeTypes([MimeType::IMAGE__SVG_XML]);
115 if ($this->item_facade->getIconID() !== null) {
116 $icon = $icon->withValue([$this->item_facade->getIconID()]);
117 }
118
119 $items[self::F_ICON] = $icon;
120 }
121
122 // TYPE
123 if (($this->item_facade->isEmpty() || $this->item_facade->isCustom())) {
124 $type_groups = $this->getTypeGroups($f, $this->item_facade->isEmpty());
125 $type = $f()->field()->switchableGroup(
126 $type_groups,
127 $txt('topitem_type'),
128 $txt('topitem_type_byline')
129 )->withRequired(true);
130 if (!$this->item_facade->isEmpty()) {
131 $string = $this->item_facade->getType() === '' ? TopParentItem::class : $this->item_facade->getType();
132 $type = $type->withValue($this->hash($string));
133 } else {
134 $type = $type->withValue($this->hash(TopParentItem::class));
135 }
136 $items[self::F_TYPE] = $type;
137 }
138
139 // ACTIVE
140 $active = $f()->field()->checkbox($txt('topitem_active'), $txt('topitem_active_byline'));
141 $active = $active->withValue($this->item_facade->isActivated());
142 $items[self::F_ACTIVE] = $active;
143
144 // ROLE BASED VISIBILITY
145 if ($this->item_facade->supportsRoleBasedVisibility()) {
146 $value_role_based_visibility = null;
147 $global_roles = $this->access->getGlobalRoles();
148 $global_role_ids = $this->item_facade->getGlobalRoleIDs();
149 if ($this->item_facade->hasRoleBasedVisibility() && !empty($global_role_ids)) {
150 // remove deleted roles, see https://mantis.ilias.de/view.php?id=34936
151 $value_role_based_visibility[0] = array_intersect(
152 $global_role_ids,
153 array_keys($global_roles)
154 );
155 }
156 $role_based_visibility = $f()->field()->optionalGroup(
157 [
158 $f()->field()->multiSelect(
159 $txt('sub_global_roles'),
160 $global_roles
161 )->withRequired(false)
162 ],
163 $txt('sub_role_based_visibility'),
164 $txt('sub_role_based_visibility_byline')
165 )->withValue($value_role_based_visibility);
166 $items[self::F_ROLE_BASED_VISIBILITY] = $role_based_visibility;
167 }
168
169 // RETURN FORM
170 if ($this->item_facade->isEmpty()) {
171 $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_ADD), "");
172 $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(
173 ilMMTopItemGUI::class,
175 ), [$section]);
176 } else {
177 $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_EDIT), "");
178 $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(
179 ilMMTopItemGUI::class,
181 ), [$section]);
182 }
183 }
184
185 public function save(): bool
186 {
187 $this->form = $this->form->withRequest($this->http->request());
188 $data = $this->form->getData();
189 if (is_null($data)) {
190 return false;
191 }
192
193 $this->item_facade->setAction((string) ($data[0]['action'] ?? ''));
194 $this->item_facade->setDefaultTitle((string) $data[0][self::F_TITLE]);
195 $this->item_facade->setActiveStatus((bool) $data[0][self::F_ACTIVE]);
196 if ($this->item_facade->supportsRoleBasedVisibility()) {
197 $this->item_facade->setRoleBasedVisibility((bool) $data[0][self::F_ROLE_BASED_VISIBILITY]);
198 if ($data[0][self::F_ROLE_BASED_VISIBILITY] and !empty($data[0][self::F_ROLE_BASED_VISIBILITY])) {
199 $this->item_facade->setGlobalRoleIDs((array) $data[0][self::F_ROLE_BASED_VISIBILITY][0]);
200 }
201 }
202
203 $this->item_facade->setIsTopItm(true);
204
205 if ($this->item_facade->isEmpty()) {
206 $type = $this->unhash((string) ($data[0][self::F_TYPE][0]));
207 $this->item_facade->setType($type);
208 $this->repository->createItem($this->item_facade);
209 }
210
211 if ($this->item_facade->supportsCustomIcon()) {
212 $icon = (string) ($data[0][self::F_ICON][0] ?? '');
213 $this->item_facade->setIconID($icon);
214 }
215
216 if ($this->item_facade->isCustom()) {
217 $type = $this->item_facade->getType();
218 $type_specific_data = (array) $data[0][self::F_TYPE][1];
219 $type_handler = $this->repository->getTypeHandlerForType($type);
220 $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
221 }
222
223 $this->repository->updateItem($this->item_facade);
224
225 return true;
226 }
227
231 public function getHTML(): string
232 {
233 return $this->ui_re->render([$this->form]);
234 }
235
240 private function getTypeGroups(Closure $f, bool $new): array
241 {
242 $type_groups = [];
243 $type_informations = $this->repository->getPossibleTopItemTypesWithInformation($new);
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(
250 $inputs,
251 $information->getTypeNameForPresentation()
252 );
253 }
254 }
255
256 return $type_groups;
257 }
258}
Mime type determination.
Definition: MimeType.php:28
Class Services.
Definition: Services.php:38
Class ilCtrl provides processing control methods.
language handling
Class ilMMItemRepository.
Class ilMMTopItemFormGUI.
const F_ACTIVE
ilMMTopItemFormGUI constructor.
ilObjMainMenuAccess $access
__construct(ilCtrl $ctrl, Factory $ui_fa, Renderer $ui_re, ilLanguage $lng, Services $http, ilMMItemFacadeInterface $item, ilMMItemRepository $repository)
ilMMItemFacadeInterface $item_facade
ilMMItemRepository $repository
getTypeGroups(Closure $f, bool $new)
Class ilMMUploadHandlerGUI.
Class ilObjMainMenuAccess.
$txt
Definition: error.php:13
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.
static http()
Fetches the global http state from ILIAS.
string $key
Consumer key/client ID value.
Definition: System.php:193
form( $class_path, string $cmd)
$type