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