ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMMTopItemFormGUI.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;
20 private $http;
24 private $repository;
28 private $form;
32 private $item_facade;
36 protected $lng;
40 protected $ctrl;
44 protected $ui_fa;
48 protected $ui_re;
56 const F_ACTIVE = 'active';
57 const F_TITLE = 'title';
58 const F_TYPE = 'type';
59
60
62 {
63 $this->repository = $repository;
64 $this->http = $http;
65 $this->ctrl = $ctrl;
66 $this->ui_fa = $ui_fa;
67 $this->ui_re = $ui_re;
68 $this->lng = $lng;
69 $this->item_facade = $item;
70 if (!$this->item_facade->isEmpty()) {
71 $this->ctrl->saveParameterByClass(ilMMTopItemGUI::class, ilMMTopItemGUI::IDENTIFIER);
72 }
73
74 $this->initForm();
75 }
76
77
78 private function initForm()
79 {
80 $txt = function ($key) {
81 return $this->lng->txt($key);
82 };
83 $f = function () : InputFactory {
84 return $this->ui_fa->input();
85 };
86
87 // TITLE
88 $title = $f()->field()->text($txt('topitem_title_default'), $txt('topitem_title_default_byline'))
89 ->withRequired(true);
90 if (!$this->item_facade->isEmpty()) {
91 $title = $title->withValue($this->item_facade->getDefaultTitle());
92 }
93
94 $items[self::F_TITLE] = $title;
95
96 // TYPE
97 $type = $f()->field()->radio($txt('topitem_type'), $txt('topitem_type_byline'))->withRequired(true);
98 $type_informations = $this->repository->getPossibleTopItemTypesWithInformation();
99
100 $type_i = 0;
101 foreach ($type_informations as $classname => $information) {
102 if ($this->item_facade->isEmpty()
103 || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType() && $this->item_facade->isCustom())
104 ) { // https://mantis.ilias.de/view.php?id=24152
105 $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm($this->item_facade->identification());
106 $type = $type->withOption($this->hash($classname), $information->getTypeNameForPresentation(), $information->getTypeBylineForPresentation(), $inputs);
107 $type_i++;
108 }
109 }
110
111 if (!$this->item_facade->isEmpty() && $this->item_facade->isCustom()) {
112 $type = $type->withValue($this->hash($this->item_facade->getType()));
113 } elseif ($this->item_facade->isCustom()) {
114 $type = $type->withValue($this->hash(reset(array_keys($type_informations))));
115 }
116
117 if (($this->item_facade->isEmpty() || $this->item_facade->isCustom()) && $type_i > 0) {
118 $items[self::F_TYPE] = $type;
119 }
120
121 // ACTIVE
122 $active = $f()->field()->checkbox($txt('topitem_active'), $txt('topitem_active_byline'));
123 if (!$this->item_facade->isEmpty()) {
124 $active = $active->withValue($this->item_facade->isAvailable());
125 }
126 $items[self::F_ACTIVE] = $active;
127
128 // RETURN FORM
129 if ($this->item_facade->isEmpty()) {
130 $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_ADD), "");
131 $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(ilMMTopItemGUI::class, ilMMTopItemGUI::CMD_CREATE), [$section]);
132 } else {
133 $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_EDIT), "");
134 $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(ilMMTopItemGUI::class, ilMMTopItemGUI::CMD_UPDATE), [$section]);
135 }
136 }
137
138
139 public function save()
140 {
141 $this->form = $this->form->withRequest($this->http->request());
142 $data = $this->form->getData();
143 if (is_null($data)) {
144 return false;
145 }
146
147 $this->item_facade->setAction((string) $data[0]['action']);
148 $this->item_facade->setDefaultTitle((string) $data[0][self::F_TITLE]);
149 $this->item_facade->setActiveStatus((bool) $data[0][self::F_ACTIVE]);
150 $this->item_facade->setIsTopItm(true);
151
152 if ($this->item_facade->isEmpty()) {
153 $type = $this->unhash((string) ($data[0][self::F_TYPE]['value']));
154 $this->item_facade->setType($type);
155 $this->repository->createItem($this->item_facade);
156 }
157
158 if ($this->item_facade->isCustom()) {
159 $type = $this->item_facade->getType();
160 $type_specific_data = (array) $data[0][self::F_TYPE]['group_values'];
161 $type_handler = $this->repository->getTypeHandlerForType($type);
162 $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
163 }
164
165 $this->repository->updateItem($this->item_facade);
166
167 return true;
168 }
169
170
174 public function getHTML() : string
175 {
176 return $this->ui_re->render([$this->form]);
177 }
178}
$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)
$key
Definition: croninfo.php:18
$txt
Definition: error.php:11
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.
Class HTTPServicesTest.
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
static http()
Fetches the global http state from ILIAS.
Class BaseForm.
$type
repository()
Definition: repository.php:5
$data
Definition: bench.php:6