ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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;
28use ILIAS\GlobalScreen\GUI\I18n\Translator;
29use Psr\Http\Message\RequestInterface;
30
36{
37 use Hasher;
38
42 private const F_ICON = 'icon';
43
44 private Standard $form;
45
53 public const F_ACTIVE = 'active';
54 public const F_TITLE = 'title';
55 public const F_TYPE = 'type';
56 public const F_ROLE_BASED_VISIBILITY = "role_based_visibility";
57
58 public function __construct(
59 protected ilCtrl $ctrl,
60 protected ILIAS\UI\Factory $ui_fa,
61 protected Renderer $ui_re,
62 protected Translator $lng,
63 private RequestInterface $request,
64 private ilMMItemFacadeInterface $item_facade,
65 private ilMMItemRepository $repository
66 ) {
67 $this->access = new ilObjMainMenuAccess();
68 $this->initForm();
69 }
70
71 private function initForm(): void
72 {
73 $txt = (fn($key): string => $this->lng->txt($key));
74 $f = (fn(): InputFactory => $this->ui_fa->input());
75
76 // TITLE
77 $title = $f()->field()->text($txt('topitem_title_default'), $txt('topitem_title_default_byline'))
78 ->withRequired(true);
79 if (!$this->item_facade->isEmpty()) {
80 $title = $title->withValue($this->item_facade->getDefaultTitle());
81 }
82
83 $items[self::F_TITLE] = $title;
84
85 if ($this->item_facade->supportsCustomIcon()) {
86 // ICON
87 $icon = $f()->field()->file(new ilMMUploadHandlerGUI(), $txt('topitem_icon'))
88 ->withByline($txt('topitem_icon_byline'))
89 ->withAcceptedMimeTypes([MimeType::IMAGE__SVG_XML]);
90 if ($this->item_facade->getIconID() !== null) {
91 $icon = $icon->withValue([$this->item_facade->getIconID()]);
92 }
93
94 $items[self::F_ICON] = $icon;
95 }
96
97 // TYPE
98 if (($this->item_facade->isEmpty() || $this->item_facade->isCustom())) {
99 $type_groups = $this->getTypeGroups($f, $this->item_facade->isEmpty());
100 $type = $f()->field()->switchableGroup(
101 $type_groups,
102 $txt('topitem_type'),
103 $txt('topitem_type_byline')
104 )->withRequired(true);
105 if (!$this->item_facade->isEmpty()) {
106 $string = $this->item_facade->getType() === '' ? TopParentItem::class : $this->item_facade->getType();
107 $type = $type->withValue($this->hash($string));
108 } else {
109 $type = $type->withValue($this->hash(TopParentItem::class));
110 }
111 $items[self::F_TYPE] = $type;
112 }
113
114 // ACTIVE
115 $active = $f()->field()->checkbox($txt('topitem_active'), $txt('topitem_active_byline'));
116 $active = $active->withValue($this->item_facade->isActivated());
117 $items[self::F_ACTIVE] = $active;
118
119 // ROLE BASED VISIBILITY
120 if ($this->item_facade->supportsRoleBasedVisibility()) {
121 $value_role_based_visibility = null;
122 $global_roles = $this->access->getGlobalRoles();
123 $global_role_ids = $this->item_facade->getGlobalRoleIDs();
124 if ($this->item_facade->hasRoleBasedVisibility() && !empty($global_role_ids)) {
125 // remove deleted roles, see https://mantis.ilias.de/view.php?id=34936
126 $value_role_based_visibility[0] = array_intersect(
127 $global_role_ids,
128 array_keys($global_roles)
129 );
130 }
131 $role_based_visibility = $f()->field()->optionalGroup(
132 [
133 $f()->field()->multiSelect(
134 $txt('sub_global_roles'),
135 $global_roles
136 )->withRequired(false)
137 ],
138 $txt('sub_role_based_visibility'),
139 $txt('sub_role_based_visibility_byline')
140 )->withValue($value_role_based_visibility);
141 $items[self::F_ROLE_BASED_VISIBILITY] = $role_based_visibility;
142 }
143
144 // RETURN FORM
145 if ($this->item_facade->isEmpty()) {
146 $this->form = $f()->container()->form()->standard(
147 $this->ctrl->getLinkTargetByClass(
148 ilMMTopItemGUI::class,
150 ),
151 $items
152 );
153 } else {
154 $this->form = $f()->container()->form()->standard(
155 $this->ctrl->getLinkTargetByClass(
156 ilMMTopItemGUI::class,
158 ),
159 $items
160 );
161 }
162 }
163
164 public function save(): bool
165 {
166 $this->form = $this->form->withRequest($this->request);
167 $data = $this->form->getData();
168 if (is_null($data)) {
169 return false;
170 }
171
172 $this->item_facade->setAction((string) ($data['action'] ?? ''));
173 $this->item_facade->setDefaultTitle((string) $data[self::F_TITLE]);
174 $this->item_facade->setActiveStatus((bool) $data[self::F_ACTIVE]);
175 if ($this->item_facade->supportsRoleBasedVisibility()) {
176 $this->item_facade->setRoleBasedVisibility((bool) $data[self::F_ROLE_BASED_VISIBILITY]);
177 if ($data[self::F_ROLE_BASED_VISIBILITY] && !empty($data[self::F_ROLE_BASED_VISIBILITY])) {
178 $this->item_facade->setGlobalRoleIDs((array) $data[self::F_ROLE_BASED_VISIBILITY][0]);
179 }
180 }
181
182 $this->item_facade->setIsTopItm(true);
183
184 if ($this->item_facade->isEmpty()) {
185 $type = $this->unhash((string) ($data[self::F_TYPE][0]));
186 $this->item_facade->setType($type);
187 $this->repository->createItem($this->item_facade);
188 }
189
190 if ($this->item_facade->supportsCustomIcon()) {
191 $icon = (string) ($data[self::F_ICON][0] ?? '');
192 $this->item_facade->setIconID($icon);
193 }
194
195 if ($this->item_facade->isCustom()) {
196 $type = $this->item_facade->getType();
197 $type_specific_data = (array) $data[self::F_TYPE][1];
198 $type_handler = $this->repository->getTypeHandlerForType($type);
199 $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
200 }
201
202 $this->repository->updateItem($this->item_facade);
203
204 return true;
205 }
206
210 public function getHTML(): string
211 {
212 return $this->ui_re->render([$this->form]);
213 }
214
215 public function get(): Standard
216 {
217 return $this->form;
218 }
219
220 private function getTypeGroups(Closure $f, bool $new): array
221 {
222 $type_groups = [];
223 $type_informations = $this->repository->getPossibleTopItemTypesWithInformation($new);
224 foreach ($type_informations as $classname => $information) {
225 if ($this->item_facade->isEmpty()
226 || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType(
227 ) && $this->item_facade->isCustom())
228 ) { // https://mantis.ilias.de/view.php?id=24152
229 $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm(
230 $this->item_facade->identification()
231 );
232 $type_groups[$this->hash($classname)] = $f()->field()->group(
233 $inputs,
234 $information->getTypeNameForPresentation()
235 );
236 }
237 }
238
239 return $type_groups;
240 }
241}
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 ilMMTopItemFormGUI.
const F_ACTIVE
ilMMTopItemFormGUI constructor.
ilObjMainMenuAccess $access
__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)
getTypeGroups(Closure $f, bool $new)
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