ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilMMSubitemFormGUI.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 const F_TITLE = "title";
19 const F_TYPE = "type";
20 const F_PARENT = "parent";
21 const F_ACTIVE = "active";
22 const F_ICON = "icon";
26 private $repository;
30 private $form;
34 protected $lng;
38 protected $ctrl;
42 protected $ui_fa;
46 protected $ui_re;
50 private $item_facade;
51
62 {
63 $this->ctrl = $ctrl;
64 $this->ui_fa = $ui_fa;
65 $this->ui_re = $ui_re;
66 $this->lng = $lng;
67 $this->item_facade = $item;
68 $this->repository = $repository;
69 if (!$this->item_facade->isEmpty()) {
70 $this->ctrl->saveParameterByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::IDENTIFIER);
71 }
72
73 $this->initForm();
74 }
75
76 private function initForm() : void
77 {
78 // TITLE
79 $txt = function ($id) : string {
80 return $this->lng->txt($id);
81 };
82 $f = function () : InputFactory {
83 return $this->ui_fa->input();
84 };
85
86 $title = $f()->field()->text($txt('sub_title_default'), $txt('sub_title_default_byline'));
87 if (!$this->item_facade->isEmpty()) {
88 $title = $title->withValue($this->item_facade->getDefaultTitle());
89 }
90 $items[self::F_TITLE] = $title;
91
92 // TYPE
93 if (($this->item_facade->isEmpty() || $this->item_facade->isCustom())) {
94 $type_groups = $this->getTypeGroups($f);
95 $type = $f()->field()->switchableGroup($type_groups, $txt('sub_type'), $txt('sub_type_byline'))->withRequired(true);
96 if (!$this->item_facade->isEmpty()) {
97 $string = $this->item_facade->getType() === '' ? Link::class : $this->item_facade->getType();
98 $type = $type->withValue($this->hash($string));
99 } else {
100 $type = $type->withValue($this->hash(Link::class));
101 }
102 $items[self::F_TYPE] = $type;
103 }
104
105 // ICON
106 if ($this->item_facade->supportsCustomIcon()) {
107 // ICON
108 $icon = $f()->field()->file(new ilMMUploadHandlerGUI(), $txt('sub_icon'))
109 ->withByline($txt('sub_icon_byline'))
110 ->withAcceptedMimeTypes([ilMimeTypeUtil::IMAGE__SVG_XML]);
111 if ($this->item_facade->getIconID() !== null) {
112 $icon = $icon->withValue([$this->item_facade->getIconID()]);
113 }
114
115 $items[self::F_ICON] = $icon;
116 }
117
118 // PARENT
119 $parent = $f()->field()->select($txt('sub_parent'), $this->repository->getPossibleParentsForFormAndTable())
120 ->withRequired(true);
121 if (!$this->item_facade->isEmpty() && !$this->item_facade->isInLostItem()) {
122 $parent = $parent->withValue($this->item_facade->getParentIdentificationString());
123 } else {
124 $array = array_keys($this->repository->getPossibleParentsForFormAndTable());
125 $parent = $parent->withValue(reset($array));
126 }
127 $items[self::F_PARENT] = $parent;
128
129 // ACTIVE
130 $active = $f()->field()->checkbox($txt('sub_active'), $txt('sub_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(ilMMSubItemGUI::CMD_ADD), "");
137 $this->form = $f()->container()->form()
138 ->standard($this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_CREATE), [$section]);
139 } else {
140 $section = $f()->field()->section($items, $txt(ilMMSubItemGUI::CMD_EDIT), "");
141 $this->form = $f()->container()->form()
142 ->standard($this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_UPDATE), [$section]);
143 }
144 }
145
146 public function save() : bool
147 {
148 global $DIC;
149 $r = new ilMMItemRepository();
150 $this->form = $this->form->withRequest($DIC->http()->request());
151 $data = $this->form->getData();
152
153 if (is_null($data)) {
154 return false;
155 }
156
157 $this->item_facade->setAction((string) $data[0]['action']);
158 $this->item_facade->setDefaultTitle((string) $data[0][self::F_TITLE]);
159 $this->item_facade->setActiveStatus((bool) $data[0][self::F_ACTIVE]);
160 if ((string) $data[0][self::F_PARENT]) {
161 $this->item_facade->setParent((string) $data[0][self::F_PARENT]);
162 }
163 $this->item_facade->setIsTopItm(false);
164
165 if ($this->item_facade->isEmpty()) {
166 $type = $this->unhash((string) ($data[0][self::F_TYPE][0]));
167 $this->item_facade->setType($type);
168 $r->createItem($this->item_facade);
169 }
170
171 if ($this->item_facade->supportsCustomIcon()) {
172 $icon = (string) $data[0][self::F_ICON][0];
173 $this->item_facade->setIconID($icon);
174 }
175
176 if ($this->item_facade->isCustom()) {
177 $type = $this->item_facade->getType();
178 $type_specific_data = (array) $data[0][self::F_TYPE][1];
179 $type_handler = $this->repository->getTypeHandlerForType($type);
180 $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
181 }
182
183 $r->updateItem($this->item_facade);
184
185 return true;
186 }
187
188 public function getHTML()
189 {
190 return $this->ui_re->render([$this->form]);
191 }
192
197 private function getTypeGroups(Closure $f) : array
198 {
199 $type_groups = [];
200 $type_informations = $this->repository->getPossibleSubItemTypesWithInformation();
201 foreach ($type_informations as $classname => $information) {
202 if ($this->item_facade->isEmpty()
203 || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType() && $this->item_facade->isCustom())
204 ) { // https://mantis.ilias.de/view.php?id=24152
205 $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm($this->item_facade->identification());
206 $type_groups[$this->hash($classname)] = $f()->field()->group($inputs, $information->getTypeNameForPresentation());
207 }
208 }
209
210 return $type_groups;
211 }
212}
$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 ilMMSubitemFormGUI.
__construct(ilCtrl $ctrl, Factory $ui_fa, Renderer $ui_re, ilLanguage $lng, ilMMItemFacadeInterface $item, ilMMItemRepository $repository)
ilMMSubitemFormGUI constructor.
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.
$type
repository()
Definition: repository.php:5
$data
Definition: storeScorm.php:23
$DIC
Definition: xapitoken.php:46