ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
ModalFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\UI\Factory as UIFactory;
24use ILIAS\UI\Component\Modal\RoundTrip as RoundtripModal;
37
39{
40 public const int MAX_LENGTH = 128;
41
43 protected UIFactory $factory;
47 protected ConstraintDictionaryInterface $constraint_dictionary;
49
50 public function __construct(
52 UIFactory $factory,
56 ConstraintDictionaryInterface $constraint_dictionary,
58 ) {
59 $this->link_provider = $link_provider;
60 $this->factory = $factory;
61 $this->presenter = $presenter;
62 $this->properties_fetcher = $properties_fetcher;
63 $this->form_factory = $form_factory;
64 $this->constraint_dictionary = $constraint_dictionary;
65 $this->path_factory = $path_factory;
66 }
67
68 public function delete(
69 PathInterface $base_path,
70 ElementInterface $to_be_deleted,
71 bool $props_from_data = false
72 ): ?FlexibleModal {
73 if (!$this->isDeletable($to_be_deleted)) {
74 return null;
75 }
76
77 $action = $this->link_provider->delete(
78 $base_path,
79 $to_be_deleted
80 );
81
82 $items = [];
83 $index = 0;
84 if ($props_from_data) {
85 $content = $this->properties_fetcher->getPropertiesByData($to_be_deleted);
86 } else {
87 $content = $this->properties_fetcher->getPropertiesByPreview($to_be_deleted);
88 }
89 foreach ($content as $key => $value) {
90 $items[] = $this->factory->modal()->interruptiveItem()->keyValue(
91 'md_delete_' . $index,
92 $this->presenter->utilities()->shortenString($key, self::MAX_LENGTH),
93 $this->presenter->utilities()->shortenString($value, self::MAX_LENGTH),
94 );
95 $index++;
96 }
97
98 $modal = $this->factory->modal()->interruptive(
99 $this->getModalTitle(
100 Command::DELETE_FULL,
101 $to_be_deleted
102 ),
103 $this->presenter->utilities()->txt('meta_delete_confirm'),
104 (string) $action
105 )->withAffectedItems($items);
106
107 return new FlexibleModal($modal);
108 }
109
110 public function update(
111 PathInterface $base_path,
112 ElementInterface $to_be_updated,
113 ?RequestForFormInterface $request = null
114 ): FlexibleModal {
115 $form = $this->form_factory->getUpdateForm(
116 $base_path,
117 $to_be_updated,
118 false
119 );
120 $modal = $this->getRoundtripModal(
121 $to_be_updated,
122 $form,
123 Command::UPDATE_FULL,
124 $request
125 );
126
127 return new FlexibleModal($modal);
128 }
129
130 public function create(
131 PathInterface $base_path,
132 ElementInterface $to_be_created,
133 ?RequestForFormInterface $request = null
134 ): FlexibleModal {
135 $form = $this->form_factory->getCreateForm(
136 $base_path,
137 $to_be_created,
138 false
139 );
140 // if the modal is empty, directly return the form action
141 if (empty($form->getInputs())) {
142 return new FlexibleModal($form->getPostURL());
143 }
144
145 $modal = $this->getRoundtripModal(
146 $to_be_created,
147 $form,
148 Command::CREATE_FULL,
149 $request
150 );
151
152 return new FlexibleModal($modal);
153 }
154
155 protected function getRoundtripModal(
156 ElementInterface $element,
157 StandardForm $form,
158 Command $action_cmd,
160 ): RoundtripModal {
161 $modal = $this->factory->modal()->roundtrip(
162 $this->getModalTitle($action_cmd, $element),
163 null,
164 $form->getInputs(),
165 $form->getPostURL()
166 );
167 return $this->handleError($modal, $element, $request);
168 }
169
170 protected function handleError(
171 RoundtripModal $modal,
172 ElementInterface $element,
174 ): RoundtripModal {
175 if (is_null($request)) {
176 return $modal;
177 }
178 $action_path = $this->path_factory->toElement($element, true);
179 if (strtolower($action_path->toString()) !== strtolower($request->path()?->toString() ?? '')) {
180 $request = null;
181 }
182 // For error handling, make the modal open on load and pass request
183 if ($request) {
184 $modal = $request->applyRequestToModal($modal);
185
186 /*
187 * Show error message in a box, since KS groups don't pass along
188 * errors on their own.
189 */
190 if (
191 ($group = $modal->getInputs()[0]) instanceof Group &&
192 $error = $group->getError()
193 ) {
194 $modal = $this->factory->modal()->roundtrip(
195 $modal->getTitle(),
196 [$this->factory->messageBox()->failure($error)],
197 $modal->getInputs(),
198 $modal->getPostURL()
199 );
200 $modal = $request->applyRequestToModal($modal);
201 }
202
203 $modal = $modal->withOnLoad($modal->getShowSignal());
204 }
205 return $modal;
206 }
207
208 protected function getModalTitle(
209 Command $action_cmd,
210 ElementInterface $element
211 ): string {
212 switch ($action_cmd) {
213 case Command::UPDATE_FULL:
214 $title_key = 'meta_edit_element';
215 break;
216
217 case Command::CREATE_FULL:
218 $title_key = 'meta_add_element';
219 break;
220
221 case Command::DELETE_FULL:
222 $title_key = 'meta_delete_element';
223 break;
224
225 default:
226 throw new \ilMDEditorException(
227 'Invalid action: ' . $action_cmd->name
228 );
229 }
230 return $this->presenter->utilities()->txtFill(
231 $title_key,
232 $this->presenter->elements()->nameWithParents(
233 $element,
234 null,
235 false,
236 true
237 )
238 );
239 }
240
241 protected function isDeletable(ElementInterface $element): bool
242 {
243 foreach ($this->constraint_dictionary->tagsForElement($element) as $tag) {
244 if ($tag->restriction() === Restriction::NOT_DELETABLE) {
245 return false;
246 }
247 }
248 return true;
249 }
250}
factory()
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
getRoundtripModal(ElementInterface $element, StandardForm $form, Command $action_cmd, ?RequestForFormInterface $request)
getModalTitle(Command $action_cmd, ElementInterface $element)
update(PathInterface $base_path, ElementInterface $to_be_updated, ?RequestForFormInterface $request=null)
__construct(LinkProvider $link_provider, UIFactory $factory, PresenterInterface $presenter, PropertiesFetcher $properties_fetcher, FormFactory $form_factory, ConstraintDictionaryInterface $constraint_dictionary, FactoryInterface $path_factory)
create(PathInterface $base_path, ElementInterface $to_be_created, ?RequestForFormInterface $request=null)
handleError(RoundtripModal $modal, ElementInterface $element, ?RequestForFormInterface $request)
This describes a standard form.
Definition: Standard.php:30
Describes the monoid operation of grouping form inputs.
Definition: Group.php:32
modal(string $title="", string $cancel_label="")
if(!file_exists('../ilias.ini.php'))