ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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  $f = $ui->factory();
86  $m = $f->messageBox()->info($text);
87  if (count($buttons)) {
88  $m = $m->withButtons($buttons);
89  }
90  return $ui->renderer()->renderAsync($m);
91  }
92 
93  public function getRenderedSuccessBox(string $text): string
94  {
95  $ui = $this->ui;
96  $f = $ui->factory();
97  $m = $f->messageBox()->success($text);
98  return $ui->renderer()->renderAsync($m);
99  }
100 
101  public function getRenderedFailureBox(): string
102  {
103  $ui = $this->ui;
104  $f = $ui->factory();
105  $m = $f->messageBox()->failure($this->lng->txt("copg_an_error_occured"))
106  ->withLinks([$f->link()->standard($this->lng->txt("copg_details"), "#")]);
107 
108  return $ui->renderer()->renderAsync($m);
109  }
110 
111  public function getRenderedButton(
112  string $content,
113  string $type,
114  string $action,
115  array $data = null,
116  string $component = "",
117  bool $primary = false,
118  string $aria_label = ""
119  ): string {
120  $ui = $this->ui;
121  $b = $this->getButton($content, $type, $action, $data, $component, $primary, $aria_label);
122  return $ui->renderer()->renderAsync($b);
123  }
124 
125  public function getRenderedModalFailureBox(): string
126  {
127  $ui = $this->ui;
128  $f = $ui->factory();
129  $m = $f->messageBox()->failure($this->lng->txt("copg_error_occured_modal"))
130  ->withButtons([$f->button()->standard($this->lng->txt("copg_reload_page"), "#")->withOnLoadCode(function ($id) {
131  return
132  "$(\"#$id\").click(function() { location.reload(); return false;});";
133  })]);
134 
135  return $ui->renderer()->renderAsync($m) . "<p>" . $this->lng->txt("copg_details") . ":</p>";
136  }
137 
138  public function getRenderedButtonGroups(array $groups): string
139  {
140  $ui = $this->ui;
141  $r = $ui->renderer();
142 
143  $tpl = new \ilTemplate("tpl.editor_button_group.html", true, true, "Services/COPage");
144 
145  foreach ($groups as $buttons) {
146  foreach ($buttons as $action => $lng_key) {
147  $tpl->setCurrentBlock("button");
148  $b = $this->getButton($this->lng->txt($lng_key), "multi", $action);
149  $tpl->setVariable("BUTTON", $r->renderAsync($b));
150  $tpl->parseCurrentBlock();
151  }
152  $tpl->setCurrentBlock("section");
153  $tpl->parseCurrentBlock();
154  }
155 
156  return $tpl->get();
157  }
158 
159  public function getRenderedFormFooter(array $buttons): string
160  {
161  $ui = $this->ui;
162  $r = $ui->renderer();
163 
164  $tpl = new \ilTemplate("tpl.form_footer.html", true, true, "Services/COPage");
165 
166  $html = "";
167  foreach ($buttons as $b) {
168  $html .= $ui->renderer()->renderAsync($b);
169  }
170 
171  $tpl->setVariable("BUTTONS", $html);
172 
173  return $tpl->get();
174  }
175 
176  public function getRenderedForm(
177  \ilPropertyFormGUI $form,
178  array $buttons
179  ): string {
180  $form->clearCommandButtons();
181  $cnt = 0;
182  foreach ($buttons as $button) {
183  $cnt++;
184  $form->addCommandButton("", $button[2], "cmd-" . $cnt);
185  }
186  $html = $form->getHTMLAsync();
187  $cnt = 0;
188  foreach ($buttons as $button) {
189  $cnt++;
190  $html = str_replace(
191  "id='cmd-" . $cnt . "'",
192  " data-copg-ed-type='form-button' data-copg-ed-action='" . $button[1] . "' data-copg-ed-component='" . $button[0] . "'",
193  $html
194  );
195  }
196  return $html;
197  }
198 
199  public function getRenderedAdapterForm(
200  FormAdapterGUI $form,
201  array $buttons,
202  string $id = ""
203  ): string {
204  $button_html = "";
205  foreach ($buttons as $button) {
206  $button_html .= $this->getRenderedButton(
207  $button[2],
208  "form-button",
209  $button[1],
210  null,
211  $button[0]
212  );
213  }
214  $html = $form->render();
215  $tag = "button";
216  $html = preg_replace("#\\<" . $tag . "(.*)/" . $tag . ">#iUs", "", $html, 1);
217  $footer_pos = stripos($html, "il-standard-form-footer");
218 
219  $html =
220  substr($html, 0, $footer_pos) .
221  preg_replace("#\\<" . $tag . "(.*)/" . $tag . ">#iUs", $button_html, substr($html, $footer_pos), 1);
222 
223  if ($id !== "") {
224  $html = str_replace("<form ", "<form id='$id' ", $html);
225  }
226  return $html;
227  }
228 
234  public function sendPage(
235  \ilPageObjectGUI $page_gui,
236  $updated
237  ): Response {
238  $error = null;
239  $page_data = "";
240  $last_change = null;
241  $pc_model = null;
242 
243  if ($updated !== true) {
244  if (is_array($updated)) {
245  $error = "";
246  foreach ($updated as $u) {
247  if (is_array($u)) {
248  $error .= implode("<br />", $u);
249  } else {
250  $error .= "<br />" . $u;
251  }
252  }
253  } elseif (is_string($updated)) {
254  $error = $updated;
255  } else {
256  $error = print_r($updated, true);
257  }
258  } else {
260  $page_gui->setDefaultLinkXml(); // fixes #31225
261  $page_gui->setTemplateOutput(false);
262  $page_data = $page_gui->showPage();
263  $pc_model = $page_gui->getPageObject()->getPCModel();
264  $last_change = $page_gui->getPageObject()->getLastChange();
265  }
266 
267  $data = new \stdClass();
268  $data->renderedContent = $page_data . $this->getOnloadCode($page_gui);
269  $data->pcModel = $pc_model;
270  $data->error = $error;
271  if ($last_change) {
272  $lu = new \ilDateTime($last_change, IL_CAL_DATETIME);
274  $data->last_update = \ilDatePresentation::formatDate($lu, true);
275  }
276  return new Response($data);
277  }
278 
279  protected function getOnloadCode(\ilPageObjectGUI $page_gui): string
280  {
281  $page = $page_gui->getPageObject();
282  $defs = $this->pc_def->definition()->getPCDefinitions();
283  $all_onload_code = [];
284  foreach ($defs as $def) {
285  $pc_class = $def["pc_class"];
287  $pc_obj = new $pc_class($page);
288 
289  // onload code
290  $onload_code = $pc_obj->getOnloadCode("edit");
291  foreach ($onload_code as $code) {
292  $all_onload_code[] = $code;
293  }
294  }
295  $code_str = "";
296  if (count($all_onload_code) > 0) {
297  $code_str = "<script>" . implode("\n", $all_onload_code) . "</script>";
298  }
299  return $code_str;
300  }
301 
302  public function sendFormError(
303  string $form
304  ): Response {
305  $data = new \stdClass();
306  $data->formError = true;
307  $data->form = $form;
308  return new Response($data);
309  }
310 
311  public function getRenderedViewControl(
312  array $actions
313  ): string {
314  $ui = $this->ui;
315  $cnt = 0;
316  $view_modes = [];
317  foreach ($actions as $act) {
318  $cnt++;
319  $view_modes[$act[2]] = "cmd-" . $cnt;
320  }
321  $vc = $ui->factory()->viewControl()->mode($view_modes, "");
322  $html = $ui->renderer()->render($vc);
323  $cnt = 0;
324  foreach ($actions as $act) {
325  $cnt++;
326  $html = str_replace(
327  'data-action="cmd-' . $cnt . '"',
328  " data-copg-ed-type='view-control' data-copg-ed-action='" . $act[1] . "' data-copg-ed-component='" . $act[0] . "'",
329  $html
330  );
331  }
332  $html = str_replace("id=", "data-id=", $html);
333  return $html;
334  }
335 
336 
337  public function getLink(
338  string $content,
339  string $component,
340  string $type,
341  string $action,
342  array $data = null
343  ): \ILIAS\UI\Component\Button\Shy {
344  $ui = $this->ui;
345  $f = $ui->factory();
346  $l = $f->button()->shy($content, "");
347  if ($data === null) {
348  $data = [];
349  }
350  $l = $l->withOnLoadCode(
351  function ($id) use ($component, $type, $data, $action) {
352  $code = "document.querySelector('#$id').setAttribute('data-copg-ed-component', '$component');
353  document.querySelector('#$id').setAttribute('data-copg-ed-type', '$type');
354  document.querySelector('#$id').setAttribute('data-copg-ed-action', '$action')";
355  foreach ($data as $key => $val) {
356  $code .= "\n document.querySelector('#$id').setAttribute('data-copg-ed-par-$key', '$val');";
357  }
358  return $code;
359  }
360  );
361  return $l;
362  }
363 
364  public function getRenderedLink(
365  string $content,
366  string $component,
367  string $type,
368  string $action,
369  array $data = null
370  ): string {
371  $ui = $this->ui;
372  $l = $this->getLink($content, $component, $type, $action, $data);
373  return $ui->renderer()->renderAsync($l);
374  }
375 
376  public function getRenderedIcon(string $type): string
377  {
378  $ui = $this->ui;
379  $f = $ui->factory();
380  $r = $ui->renderer();
381  $i = $f->symbol()->icon()->standard($type, $type, 'medium');
382  return $r->render($i);
383  }
384 
386  string $title = "",
387  bool $leading_image = false
388  ): string {
389  $ui = $this->ui;
390  $f = $ui->factory();
391  $r = $ui->renderer();
392  $dd = $f->dropdown()->standard([
393  $f->link()->standard("#link-label#", "#")
394  ]);
395 
396  $item = $f->item()->standard("#item-title#")->withActions($dd);
397  if ($leading_image) {
398  $item = $item->withLeadImage(
399  $f->image()->responsive("#img-src#", "#img-alt#")
400  );
401  }
402  $p = $f->panel()->listing()->standard(
403  $title,
404  [$f->item()->group(
405  "",
406  [$item]
407  )]
408  );
409 
410  return $r->render($p);
411  }
412 }
getRenderedLink(string $content, string $component, string $type, string $action, array $data=null)
const IL_CAL_DATETIME
getRenderedListingPanelTemplate(string $title="", bool $leading_image=false)
setOutputMode(string $a_mode=self::PRESENTATION)
getLink(string $content, string $component, string $type, string $action, array $data=null)
Class ChatMainBarProvider .
Class ilPageObjectGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
ILIAS COPage PC DomainService $pc_def
__construct(\ILIAS\DI\UIServices $ui, \ilLanguage $lng)
setTemplateOutput(bool $a_output=true)
getRenderedButton(string $content, string $type, string $action, array $data=null, string $component="", bool $primary=false, string $aria_label="")
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
int $updated
Timestamp for when the object was last updated.
Definition: System.php:158
getRenderedAdapterForm(FormAdapterGUI $form, array $buttons, string $id="")
Class HTTPServicesTest.
getRenderedForm(\ilPropertyFormGUI $form, array $buttons)
getRenderedInfoBox(string $text, array $buttons=[])
string $key
Consumer key/client ID value.
Definition: System.php:193
getButton(string $content, string $type, string $action, array $data=null, string $component="", bool $primary=false, string $aria_label="")
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
showPage()
display content of page
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
ilErrorHandling $error
Definition: class.ilias.php:55
static setUseRelativeDates(bool $a_status)
set use relative dates
sendPage(\ilPageObjectGUI $page_gui, $updated)
Send whole page as response.
$r