ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 if ($this->item_facade->getIconID() !== null) {
109 $icon = $icon->withValue([$this->item_facade->getIconID()]);
110 }
111
112 $items[self::F_ICON] = $icon;
113 }
114
115 // TYPE
116 if (($this->item_facade->isEmpty() || $this->item_facade->isCustom())) {
117 $type_groups = $this->getTypeGroups($f);
118 $type = $f()->field()->switchableGroup($type_groups, $txt('topitem_type'),
119 $txt('topitem_type_byline'))->withRequired(true);
120 if (!$this->item_facade->isEmpty()) {
121 $string = $this->item_facade->getType() === '' ? TopParentItem::class : $this->item_facade->getType();
122 $type = $type->withValue($this->hash($string));
123 } else {
124 $type = $type->withValue($this->hash(TopParentItem::class));
125 }
126 $items[self::F_TYPE] = $type;
127 }
128
129 // ACTIVE
130 $active = $f()->field()->checkbox($txt('topitem_active'), $txt('topitem_active_byline'));
131 $active = $active->withValue($this->item_facade->isActivated());
132 $items[self::F_ACTIVE] = $active;
133
134 // RETURN FORM
135 if ($this->item_facade->isEmpty()) {
136 $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_ADD), "");
137 $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(ilMMTopItemGUI::class,
139 } else {
140 $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_EDIT), "");
141 $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(ilMMTopItemGUI::class,
143 }
144 }
145
146 public function save()
147 {
148 $this->form = $this->form->withRequest($this->http->request());
149 $data = $this->form->getData();
150 if (is_null($data)) {
151 return false;
152 }
153
154 $this->item_facade->setAction((string) $data[0]['action']);
155 $this->item_facade->setDefaultTitle((string) $data[0][self::F_TITLE]);
156 $this->item_facade->setActiveStatus((bool) $data[0][self::F_ACTIVE]);
157 $this->item_facade->setIsTopItm(true);
158
159 if ($this->item_facade->isEmpty()) {
160 $type = $this->unhash((string) ($data[0][self::F_TYPE][0]));
161 $this->item_facade->setType($type);
162 $this->repository->createItem($this->item_facade);
163 }
164
165 if ($this->item_facade->supportsCustomIcon()) {
166 $icon = (string) $data[0][self::F_ICON][0];
167 $this->item_facade->setIconID($icon);
168 }
169
170 if ($this->item_facade->isCustom()) {
171 $type = $this->item_facade->getType();
172 $type_specific_data = (array) $data[0][self::F_TYPE][1];
173 $type_handler = $this->repository->getTypeHandlerForType($type);
174 $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
175 }
176
177 $this->repository->updateItem($this->item_facade);
178
179 return true;
180 }
181
185 public function getHTML() : string
186 {
187 return $this->ui_re->render([$this->form]);
188 }
189
194 private function getTypeGroups(Closure $f) : array
195 {
196 $type_groups = [];
197 $type_informations = $this->repository->getPossibleTopItemTypesWithInformation();
198 foreach ($type_informations as $classname => $information) {
199 if ($this->item_facade->isEmpty()
200 || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType() && $this->item_facade->isCustom())
201 ) { // https://mantis.ilias.de/view.php?id=24152
202 $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm($this->item_facade->identification());
203 $type_groups[$this->hash($classname)] = $f()->field()->group($inputs,
204 $information->getTypeNameForPresentation());
205 }
206 }
207
208 return $type_groups;
209 }
210}
$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)
Class ilMMUploadHandlerGUI.
$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