ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.UIWrapper.php
Go to the documentation of this file.
1 <?php
2 
20 
23 
28 class UIWrapper
29 {
30  protected \ILIAS\COPage\PC\DomainService $pc_def;
31  protected \ILIAS\DI\UIServices $ui;
32  protected \ilLanguage $lng;
33 
34  public function __construct(
35  \ILIAS\DI\UIServices $ui,
36  \ilLanguage $lng
37  ) {
38  global $DIC;
39 
40  $this->pc_def = $DIC->copage()->internal()->domain()->pc();
41  $this->ui = $ui;
42  $this->lng = $lng;
43  $this->lng->loadLanguageModule("copg");
44  }
45 
46  public function getButton(
47  string $content,
48  string $type,
49  string $action,
50  ?array $data = null,
51  string $component = "",
52  bool $primary = false,
53  string $aria_label = ""
55  $ui = $this->ui;
56  $f = $ui->factory();
57  if ($primary) {
58  $b = $f->button()->primary($content, "");
59  } else {
60  $b = $f->button()->standard($content, "");
61  }
62  if ($data === null) {
63  $data = [];
64  }
65  $b = $b->withOnLoadCode(
66  function ($id) use ($type, $data, $action, $component, $aria_label) {
67  $code = "document.querySelector('#$id').setAttribute('data-copg-ed-type', '$type');
68  document.querySelector('#$id').setAttribute('data-copg-ed-component', '$component');
69  document.querySelector('#$id').setAttribute('data-copg-ed-action', '$action'); ";
70  if ($aria_label !== "") {
71  $code .= "document.querySelector('#$id').setAttribute('aria-label', '$aria_label'); ";
72  }
73  foreach ($data as $key => $val) {
74  $code .= "\n document.querySelector('#$id').setAttribute('data-copg-ed-par-$key', '$val');";
75  }
76  return $code;
77  }
78  );
79  return $b;
80  }
81 
82  public function getRenderedInfoBox(string $text, array $buttons = []): string
83  {
84  $ui = $this->ui;
85 
86  // workaround to clear async code from dropdowns
87  $ui->renderer()->renderAsync($ui->factory()->legacy()->content(""));
88 
89  $f = $ui->factory();
90  $m = $f->messageBox()->info($text);
91  if (count($buttons)) {
92  $m = $m->withButtons($buttons);
93  }
94  return $ui->renderer()->renderAsync($m);
95  }
96 
97  public function getRenderedSuccessBox(string $text): string
98  {
99  $ui = $this->ui;
100 
101  // workaround to clear async code from dropdowns
102  $ui->renderer()->renderAsync($ui->factory()->legacy()->content(""));
103 
104  $f = $ui->factory();
105  $m = $f->messageBox()->success($text);
106  return $ui->renderer()->renderAsync($m);
107  }
108 
109  public function getRenderedFailureBox(): string
110  {
111  $ui = $this->ui;
112 
113  // workaround to clear async code from dropdowns
114  $ui->renderer()->renderAsync($ui->factory()->legacy()->content(""));
115 
116  $f = $ui->factory();
117  $m = $f->messageBox()->failure($this->lng->txt("copg_an_error_occured"))
118  ->withLinks([$f->link()->standard($this->lng->txt("copg_details"), "#")]);
119 
120  return $ui->renderer()->renderAsync($m);
121  }
122 
123  public function getRenderedButton(
124  string $content,
125  string $type,
126  string $action,
127  ?array $data = null,
128  string $component = "",
129  bool $primary = false,
130  string $aria_label = ""
131  ): string {
132  $ui = $this->ui;
133 
134  // workaround to clear async code from dropdowns
135  $ui->renderer()->renderAsync($ui->factory()->legacy()->content(""));
136  /*
137  $something = htmlentities($ui->renderer()->renderAsync($ui->factory()->legacy("")));
138  if ($something !== "&lt;script data-replace-marker=&quot;script&quot;&gt;&lt;/script&gt;") {
139  echo $something;
140  exit;
141  }*/
142 
143 
144  $b = $this->getButton($content, $type, $action, $data, $component, $primary, $aria_label);
145  return $ui->renderer()->renderAsync($b);
146  }
147 
148  public function getRenderedModalFailureBox(): string
149  {
150  $ui = $this->ui;
151 
152  // workaround to clear async code from dropdowns
153  $ui->renderer()->renderAsync($ui->factory()->legacy()->content(""));
154 
155  $f = $ui->factory();
156  $m = $f->messageBox()->failure($this->lng->txt("copg_error_occured_modal"))
157  ->withButtons([$f->button()->standard($this->lng->txt("copg_reload_page"), "#")->withOnLoadCode(function ($id) {
158  return
159  "$(\"#$id\").click(function() { location.reload(); return false;});";
160  })]);
161 
162  return $ui->renderer()->renderAsync($m) . "<p>" . $this->lng->txt("copg_details") . ":</p>";
163  }
164 
165  public function getRenderedButtonGroups(array $groups): string
166  {
167  $ui = $this->ui;
168  $r = $ui->renderer();
169 
170  // workaround to clear async code from dropdowns
171  $ui->renderer()->renderAsync($ui->factory()->legacy()->content(""));
172 
173 
174  $tpl = new \ilTemplate("tpl.editor_button_group.html", true, true, "components/ILIAS/COPage");
175 
176  foreach ($groups as $buttons) {
177  foreach ($buttons as $action => $lng_key) {
178  $tpl->setCurrentBlock("button");
179  $b = $this->getButton($this->lng->txt($lng_key), "multi", $action);
180  $tpl->setVariable("BUTTON", $r->renderAsync($b));
181  $tpl->parseCurrentBlock();
182  }
183  $tpl->setCurrentBlock("section");
184  $tpl->parseCurrentBlock();
185  }
186 
187  return $tpl->get();
188  }
189 
190  public function getRenderedFormFooter(array $buttons): string
191  {
192  $ui = $this->ui;
193  $r = $ui->renderer();
194 
195  // workaround to clear async code from dropdowns
196  $ui->renderer()->renderAsync($ui->factory()->legacy()->content(""));
197 
198  $tpl = new \ilTemplate("tpl.form_footer.html", true, true, "components/ILIAS/COPage");
199 
200  $html = "";
201  foreach ($buttons as $b) {
202  $html .= $ui->renderer()->renderAsync($b);
203  }
204 
205  $tpl->setVariable("BUTTONS", $html);
206 
207  return $tpl->get();
208  }
209 
210  public function getRenderedForm(
211  \ilPropertyFormGUI $form,
212  array $buttons
213  ): string {
214  $form->clearCommandButtons();
215  $cnt = 0;
216  foreach ($buttons as $button) {
217  $cnt++;
218  $form->addCommandButton("", $button[2], "cmd-" . $cnt);
219  }
220  $html = $form->getHTMLAsync();
221  $cnt = 0;
222  foreach ($buttons as $button) {
223  $cnt++;
224  $html = str_replace(
225  "id='cmd-" . $cnt . "'",
226  " data-copg-ed-type='form-button' data-copg-ed-action='" . $button[1] . "' data-copg-ed-component='" . $button[0] . "'",
227  $html
228  );
229  }
230  return $html;
231  }
232 
233  public function getRenderedAdapterForm(
234  FormAdapterGUI $form,
235  array $buttons,
236  string $id = "",
237  bool $in_modal = false
238  ): string {
239  $button_html = "";
240  foreach ($buttons as $button) {
241  $button_html .= $this->getRenderedButton(
242  $button[2],
243  "form-button",
244  $button[1],
245  null,
246  $button[0]
247  );
248  }
249  $html = $form->render();
250  $tag = "button";
251  $del_count = $in_modal ? 2 : 1;
252  $html = preg_replace("#\\<" . $tag . "([^>]*)btn-default(.*)/" . $tag . ">#iUs", "", $html, $del_count);
253  $footer_pos = stripos($html, "il-standard-form-footer");
254  if (!is_int($footer_pos)) {
255  $footer_pos = stripos($html, "c-form__footer");
256  }
257 
258  $html =
259  substr($html, 0, $footer_pos) .
260  preg_replace("#\\<" . $tag . "([^>]*)btn-default(.*)/" . $tag . ">#iUs", $button_html, substr($html, $footer_pos), 1);
261 
262  if ($id !== "") {
263  $html = str_replace("<form ", "<form id='$id' ", $html);
264  }
265  return $html;
266  }
267 
273  public function sendPage(
274  \ilPageObjectGUI $page_gui,
275  $updated
276  ): Response {
277  $error = null;
278  $page_data = "";
279  $last_change = null;
280  $pc_model = null;
281 
282  if ($updated !== true) {
283  if (is_array($updated)) {
284  $error = "";
285  foreach ($updated as $u) {
286  if (is_array($u)) {
287  $error .= implode("<br />", $u);
288  } else {
289  $error .= "<br />" . $u;
290  }
291  }
292  } elseif (is_string($updated)) {
293  $error = $updated;
294  } else {
295  $error = print_r($updated, true);
296  }
297  } else {
299  $page_gui->setDefaultLinkXml(); // fixes #31225
300  $page_gui->setTemplateOutput(false);
301  $page_data = $page_gui->showPage();
302  $pc_model = $page_gui->getPageObject()->getPCModel();
303  $last_change = $page_gui->getPageObject()->getLastChange();
304  }
305 
306  $data = new \stdClass();
307  $data->renderedContent = $page_data . $this->getOnloadCode($page_gui);
308  $data->pcModel = $pc_model;
309  $data->error = $error;
310  if ($last_change) {
311  $lu = new \ilDateTime($last_change, IL_CAL_DATETIME);
313  $data->last_update = \ilDatePresentation::formatDate($lu, true);
314  }
315  return new Response($data);
316  }
317 
318  protected function getOnloadCode(\ilPageObjectGUI $page_gui): string
319  {
320  $page = $page_gui->getPageObject();
321  $defs = $this->pc_def->definition()->getPCDefinitions();
322  $all_onload_code = [];
323  foreach ($defs as $def) {
324  $pc_class = $def["pc_class"];
326  $pc_obj = new $pc_class($page);
327 
328  // onload code
329  $onload_code = $pc_obj->getOnloadCode("edit");
330  foreach ($onload_code as $code) {
331  $all_onload_code[] = $code;
332  }
333  }
334  $code_str = "";
335  if (count($all_onload_code) > 0) {
336  $code_str = "<script>" . implode("\n", $all_onload_code) . "</script>";
337  }
338  return $code_str;
339  }
340 
341  public function sendFormError(
342  string $form
343  ): Response {
344  $data = new \stdClass();
345  $data->formError = true;
346  $data->form = $form;
347  return new Response($data);
348  }
349 
350  public function getRenderedViewControl(
351  array $actions
352  ): string {
353  $ui = $this->ui;
354  $cnt = 0;
355  $view_modes = [];
356  foreach ($actions as $act) {
357  $cnt++;
358  $view_modes[$act[2]] = "cmd-" . $cnt;
359  }
360  $vc = $ui->factory()->viewControl()->mode($view_modes, "");
361  $html = $ui->renderer()->renderAsync($vc);
362  $cnt = 0;
363  foreach ($actions as $act) {
364  $cnt++;
365  $html = str_replace(
366  'data-action="cmd-' . $cnt . '"',
367  " data-copg-ed-type='view-control' data-copg-ed-action='" . $act[1] . "' data-copg-ed-component='" . $act[0] . "'",
368  $html
369  );
370  }
371  $html = str_replace("id=", "data-id=", $html);
372  return $html;
373  }
374 
375 
376  public function getLink(
377  string $content,
378  string $component,
379  string $type,
380  string $action,
381  ?array $data = null
382  ): \ILIAS\UI\Component\Button\Shy {
383  $ui = $this->ui;
384  $f = $ui->factory();
385  $l = $f->button()->shy($content, "");
386  if ($data === null) {
387  $data = [];
388  }
389  $l = $l->withOnLoadCode(
390  function ($id) use ($component, $type, $data, $action) {
391  $code = "document.querySelector('#$id').setAttribute('data-copg-ed-component', '$component');
392  document.querySelector('#$id').setAttribute('data-copg-ed-type', '$type');
393  document.querySelector('#$id').setAttribute('data-copg-ed-action', '$action')";
394  foreach ($data as $key => $val) {
395  $code .= "\n document.querySelector('#$id').setAttribute('data-copg-ed-par-$key', '$val');";
396  }
397  return $code;
398  }
399  );
400  return $l;
401  }
402 
403  public function getRenderedLink(
404  string $content,
405  string $component,
406  string $type,
407  string $action,
408  ?array $data = null
409  ): string {
410  $ui = $this->ui;
411  // workaround to clear async code from dropdowns
412  $ui->renderer()->renderAsync($ui->factory()->legacy()->content(""));
413  $l = $this->getLink($content, $component, $type, $action, $data);
414  return $ui->renderer()->renderAsync($l);
415  }
416 
417  public function getRenderedIcon(string $type): string
418  {
419  $ui = $this->ui;
420  $f = $ui->factory();
421  $r = $ui->renderer();
422  $i = $f->symbol()->icon()->standard($type, $type, 'medium');
423  return $r->render($i);
424  }
425 
427  string $title = "",
428  bool $leading_image = false
429  ): string {
430  $ui = $this->ui;
431  $f = $ui->factory();
432  $r = $ui->renderer();
433  $dd = $f->dropdown()->standard([
434  $f->link()->standard("#link-label#", "#")
435  ]);
436 
437  $item = $f->item()->standard("#item-title#")->withActions($dd);
438  if ($leading_image) {
439  $item = $item->withLeadImage(
440  $f->image()->responsive("#img-src#", "#img-alt#")
441  );
442  }
443  $p = $f->panel()->listing()->standard(
444  $title,
445  [$f->item()->group(
446  "",
447  [$item]
448  )]
449  );
450 
451  return $r->renderAsync($p);
452  }
453 }
static array static setUseRelativeDates(bool $a_status)
set use relative dates
const IL_CAL_DATETIME
getRenderedListingPanelTemplate(string $title="", bool $leading_image=false)
setOutputMode(string $a_mode=self::PRESENTATION)
Interface Observer Contains several chained tasks and infos about them.
getRenderedAdapterForm(FormAdapterGUI $form, array $buttons, string $id="", bool $in_modal=false)
Class ilPageObjectGUI.
getLink(string $content, string $component, string $type, string $action, ?array $data=null)
ILIAS COPage PC DomainService $pc_def
getButton(string $content, string $type, string $action, ?array $data=null, string $component="", bool $primary=false, string $aria_label="")
getRenderedButton(string $content, string $type, string $action, ?array $data=null, string $component="", bool $primary=false, string $aria_label="")
__construct(\ILIAS\DI\UIServices $ui, \ilLanguage $lng)
setTemplateOutput(bool $a_output=true)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class HTTPServicesTest.
getRenderedForm(\ilPropertyFormGUI $form, array $buttons)
getRenderedInfoBox(string $text, array $buttons=[])
global $DIC
Definition: shib_login.php:26
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
getRenderedLink(string $content, string $component, string $type, string $action, ?array $data=null)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
showPage()
display content of page
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
ilErrorHandling $error
Definition: class.ilias.php:69
sendPage(\ilPageObjectGUI $page_gui, $updated)
Send whole page as response.
$r