ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMMSubitemFormGUI.php
Go to the documentation of this file.
1<?php
2
3use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Renderer\Hasher;
5use ILIAS\UI\Component\Input\Factory as InputFactory;
8
15{
16 use Hasher;
17 const F_TITLE = "title";
18 const F_TYPE = "type";
19 const F_PARENT = "parent";
20 const F_ACTIVE = "active";
24 private $repository;
28 private $form;
32 protected $lng;
36 protected $ctrl;
40 protected $ui_fa;
44 protected $ui_re;
48 private $item_facade;
49
50
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
77 private function initForm()
78 {
79 // TITLE
80 $txt = function ($id) : string {
81 return $this->lng->txt($id);
82 };
83 $f = function () : InputFactory {
84 return $this->ui_fa->input();
85 };
86
87 $title = $f()->field()->text($txt('sub_title_default'), $txt('sub_title_default_byline'));
88 if (!$this->item_facade->isEmpty()) {
89 $title = $title->withValue($this->item_facade->getDefaultTitle());
90 }
91 $items[self::F_TITLE] = $title;
92
93 // TYPE
94 $type = $f()->field()->radio($txt('sub_type'), $txt('sub_type_byline'))->withRequired(true);
95 $type_informations = $this->repository->getPossibleSubItemTypesWithInformation();
96
97 foreach ($type_informations as $classname => $information) {
98 if ($this->item_facade->isEmpty()
99 || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType() && $this->item_facade->isCustom())
100 ) { // https://mantis.ilias.de/view.php?id=24152
101 $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm($this->item_facade->identification());
102 $type = $type->withOption($this->hash($classname), $information->getTypeNameForPresentation(), $information->getTypeBylineForPresentation(), $inputs);
103 }
104 }
105
106 if (!$this->item_facade->isEmpty() && $this->item_facade->isCustom()) {
107 $type = $type->withValue($this->hash($this->item_facade->getType()));
108 } elseif ($this->item_facade->isCustom()) {
109 $type = $type->withValue($this->hash(reset(array_keys($type_informations))));
110 }
111
112 if ($this->item_facade->isEmpty() || $this->item_facade->isCustom()) {
113 $items[self::F_TYPE] = $type;
114 }
115
116 // PARENT
117 $parent = $f()->field()->select($txt('sub_parent'), $this->repository->getPossibleParentsForFormAndTable())
118 ->withRequired(true);
119 if (!$this->item_facade->isEmpty() && !$this->item_facade->isInLostItem()) {
120 $parent = $parent->withValue($this->item_facade->getParentIdentificationString());
121 } else {
122 $parent = $parent->withValue(reset(array_keys($this->repository->getPossibleParentsForFormAndTable())));
123 }
124 $items[self::F_PARENT] = $parent;
125
126 // ACTIVE
127 $active = $f()->field()->checkbox($txt('sub_active'), $txt('sub_active_byline'));
128 if (!$this->item_facade->isEmpty()) {
129 $active = $active->withValue($this->item_facade->isAvailable());
130 }
131 $items[self::F_ACTIVE] = $active;
132
133 // RETURN FORM
134 if ($this->item_facade->isEmpty()) {
135 $section = $f()->field()->section($items, $txt(ilMMSubItemGUI::CMD_ADD), "");
136 $this->form = $f()->container()->form()
137 ->standard($this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_CREATE), [$section]);
138 } else {
139 $section = $f()->field()->section($items, $txt(ilMMSubItemGUI::CMD_EDIT), "");
140 $this->form = $f()->container()->form()
141 ->standard($this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_UPDATE), [$section]);
142 }
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]['value']));
167 $this->item_facade->setType($type);
168 $r->createItem($this->item_facade);
169 }
170 if ($this->item_facade->isCustom()) {
171 $type = $this->item_facade->getType();
172 $type_specific_data = (array) $data[0][self::F_TYPE]['group_values'];
173 $type_handler = $this->repository->getTypeHandlerForType($type);
174 $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
175 }
176
177 $r->updateItem($this->item_facade);
178
179 return true;
180 }
181
182
183 public function getHTML()
184 {
185 return $this->ui_re->render([$this->form]);
186 }
187}
$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.
$txt
Definition: error.php:11
$r
Definition: example_031.php:79
if(!array_key_exists('StateId', $_REQUEST)) $id
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:16
An entity that renders components to a string output.
Definition: Renderer.php:15
Interface ilMMItemFacadeInterface.
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
$type
repository()
Definition: repository.php:5
global $DIC
Definition: saml.php:7
$data
Definition: bench.php:6