ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMMTopItemFormGUI.php
Go to the documentation of this file.
1<?php
2
3use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Renderer\Hasher;
6use ILIAS\UI\Component\Input\Factory as InputFactory;
9
15{
16 use Hasher;
17
18 private const F_ICON = 'icon';
22 private $http;
26 private $repository;
30 private $form;
34 private $item_facade;
38 protected $lng;
42 protected $ctrl;
46 protected $ui_fa;
50 protected $ui_re;
57 const F_ACTIVE = 'active';
58 const F_TITLE = 'title';
59 const F_TYPE = 'type';
60 const F_ROLE_BASED_VISIBILITY = "role_based_visibility";
61
62 public function __construct(
67 \ILIAS\DI\HTTPServices $http,
70 ) {
71 $this->repository = $repository;
72 $this->http = $http;
73 $this->ctrl = $ctrl;
74 $this->ui_fa = $ui_fa;
75 $this->ui_re = $ui_re;
76 $this->lng = $lng;
77 $this->item_facade = $item;
78 if (!$this->item_facade->isEmpty()) {
79 $this->ctrl->saveParameterByClass(ilMMTopItemGUI::class, ilMMTopItemGUI::IDENTIFIER);
80 }
81
82 $this->initForm();
83 }
84
85 private function initForm()
86 {
87 $txt = function ($key) {
88 return $this->lng->txt($key);
89 };
90 $f = function () : InputFactory {
91 return $this->ui_fa->input();
92 };
93
94 // TITLE
95 $title = $f()->field()->text($txt('topitem_title_default'), $txt('topitem_title_default_byline'))
96 ->withRequired(true);
97 if (!$this->item_facade->isEmpty()) {
98 $title = $title->withValue($this->item_facade->getDefaultTitle());
99 }
100
101 $items[self::F_TITLE] = $title;
102
103 if ($this->item_facade->supportsCustomIcon()) {
104 // ICON
105 $icon = $f()->field()->file(new ilMMUploadHandlerGUI(), $txt('topitem_icon'))
106 ->withByline($txt('topitem_icon_byline'))
107 ->withAcceptedMimeTypes([ilMimeTypeUtil::IMAGE__SVG_XML])
108 ->withMaxFileSize(ilMMUploadHandlerGUI::MAX_FILE_SIZE);
109
110 if ($this->item_facade->getIconID() !== null) {
111 $icon = $icon->withValue([$this->item_facade->getIconID()]);
112 }
113
114 $items[self::F_ICON] = $icon;
115 }
116
117 // TYPE
118 if (($this->item_facade->isEmpty() || $this->item_facade->isCustom())) {
119 $type_groups = $this->getTypeGroups($f, $this->item_facade->isEmpty());
120 $type = $f()->field()->switchableGroup(
121 $type_groups,
122 $txt('topitem_type'),
123 $txt('topitem_type_byline')
124 )->withRequired(true);
125 if (!$this->item_facade->isEmpty()) {
126 $string = $this->item_facade->getType() === '' ? TopParentItem::class : $this->item_facade->getType();
127 $type = $type->withValue($this->hash($string));
128 } else {
129 $type = $type->withValue($this->hash(TopParentItem::class));
130 }
131 $items[self::F_TYPE] = $type;
132 }
133
134 // ACTIVE
135 $active = $f()->field()->checkbox($txt('topitem_active'), $txt('topitem_active_byline'));
136 $active = $active->withValue($this->item_facade->isActivated());
137 $items[self::F_ACTIVE] = $active;
138
139 // ROLE BASED VISIBILITY
140 if ($this->item_facade->supportsRoleBasedVisibility()) {
141 $access = new ilObjMainMenuAccess();
142 $value_role_based_visibility = NULL;
143 $global_roles = $access->getGlobalRoles();
144 $global_role_ids = $this->item_facade->getGlobalRoleIDs();
145 if($this->item_facade->hasRoleBasedVisibility() && !empty($global_role_ids)) {
146 // remove deleted roles, see https://mantis.ilias.de/view.php?id=34936
147 $value_role_based_visibility[0] = array_intersect(
148 $global_role_ids,
149 array_keys($global_roles)
150 );
151 }
152 $role_based_visibility = $f()->field()->optionalGroup(
153 [
154 $f()->field()->multiSelect(
155 $txt('sub_global_roles'),
156 $global_roles
157 )->withRequired(true)
158 ],
159 $txt('sub_role_based_visibility'),
160 $txt('sub_role_based_visibility_byline')
161 )->withValue($value_role_based_visibility);
162 $items[self::F_ROLE_BASED_VISIBILITY] = $role_based_visibility;
163 }
164
165 // RETURN FORM
166 if ($this->item_facade->isEmpty()) {
167 $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_ADD), "");
168 $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(ilMMTopItemGUI::class,
170 } else {
171 $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_EDIT), "");
172 $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(ilMMTopItemGUI::class,
174 }
175 }
176
177 public function save()
178 {
179 $this->form = $this->form->withRequest($this->http->request());
180 $data = $this->form->getData();
181 if (is_null($data)) {
182 return false;
183 }
184
185 $this->item_facade->setAction((string) $data[0]['action']);
186 $this->item_facade->setDefaultTitle((string) $data[0][self::F_TITLE]);
187 $this->item_facade->setActiveStatus((bool) $data[0][self::F_ACTIVE]);
188 if ($this->item_facade->supportsRoleBasedVisibility()) {
189 $this->item_facade->setRoleBasedVisibility((bool) $data[0][self::F_ROLE_BASED_VISIBILITY]);
190 if ((bool) $data[0][self::F_ROLE_BASED_VISIBILITY] and (bool) !empty($data[0][self::F_ROLE_BASED_VISIBILITY])) {
191 $this->item_facade->setGlobalRoleIDs((array) $data[0][self::F_ROLE_BASED_VISIBILITY][0]);
192 }
193 }
194
195 $this->item_facade->setIsTopItm(true);
196
197 if ($this->item_facade->isEmpty()) {
198 $type = $this->unhash((string) ($data[0][self::F_TYPE][0]));
199 $this->item_facade->setType($type);
200 $this->repository->createItem($this->item_facade);
201 }
202
203 if ($this->item_facade->supportsCustomIcon()) {
204 $icon = (string) $data[0][self::F_ICON][0];
205 $this->item_facade->setIconID($icon);
206 }
207
208 if ($this->item_facade->isCustom()) {
209 $type = $this->item_facade->getType();
210 $type_specific_data = (array) $data[0][self::F_TYPE][1];
211 $type_handler = $this->repository->getTypeHandlerForType($type);
212 $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
213 }
214
215 $this->repository->updateItem($this->item_facade);
216
217 return true;
218 }
219
223 public function getHTML() : string
224 {
225 return $this->ui_re->render([$this->form]);
226 }
227
232 private function getTypeGroups(Closure $f, bool $new): array
233 {
234 $type_groups = [];
235 $type_informations = $this->repository->getPossibleTopItemTypesWithInformation($new);
236 foreach ($type_informations as $classname => $information) {
237 if ($this->item_facade->isEmpty()
238 || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType() && $this->item_facade->isCustom())
239 ) { // https://mantis.ilias.de/view.php?id=24152
240 $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm($this->item_facade->identification());
241 $type_groups[$this->hash($classname)] = $f()->field()->group($inputs,
242 $information->getTypeNameForPresentation());
243 }
244 }
245
246 return $type_groups;
247 }
248}
$section
Definition: Utf8Test.php:83
An exception for terminatinating execution or to throw for unit testing.
This class provides processing control methods.
language handling
Class ilMMItemRepository.
Class ilMMTopItemFormGUI.
const F_ACTIVE
ilMMTopItemFormGUI constructor.
__construct(ilCtrl $ctrl, Factory $ui_fa, Renderer $ui_re, ilLanguage $lng, \ILIAS\DI\HTTPServices $http, ilMMItemFacadeInterface $item, ilMMItemRepository $repository)
getTypeGroups(Closure $f, bool $new)
Class ilMMUploadHandlerGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$txt
Definition: error.php:13
This describes a standard form.
Definition: Standard.php:11
This is how a factory for inputs looks like.
Definition: Factory.php:11
This is how the factory for UI elements looks.
Definition: Factory.php:18
An entity that renders components to a string output.
Definition: Renderer.php:15
Interface ilMMItemFacadeInterface.
Class HTTPServicesTest.
static http()
Fetches the global http state from ILIAS.
Class ChatMainBarProvider \MainMenu\Provider.
$type
repository()
Definition: repository.php:5
$data
Definition: storeScorm.php:23