ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.UIWrapper.php
Go to the documentation of this file.
1 <?php
2 
20 
25 class UIWrapper
26 {
27  protected \ILIAS\DI\UIServices $ui;
28  protected \ilLanguage $lng;
29 
30  public function __construct(
31  \ILIAS\DI\UIServices $ui,
32  \ilLanguage $lng
33  ) {
34  $this->ui = $ui;
35  $this->lng = $lng;
36  $this->lng->loadLanguageModule("copg");
37  }
38 
39  public function getButton(
40  string $content,
41  string $type,
42  string $action,
43  array $data = null,
44  string $component = "",
45  string $aria_label = ""
46  ): \ILIAS\UI\Component\Button\Standard {
47  $ui = $this->ui;
48  $f = $ui->factory();
49  $b = $f->button()->standard($content, "");
50  if ($data === null) {
51  $data = [];
52  }
53  $b = $b->withOnLoadCode(
54  function ($id) use ($type, $data, $action, $component, $aria_label) {
55  $code = "document.querySelector('#$id').setAttribute('data-copg-ed-type', '$type');
56  document.querySelector('#$id').setAttribute('data-copg-ed-component', '$component');
57  document.querySelector('#$id').setAttribute('data-copg-ed-action', '$action'); ";
58  if ($aria_label !== "") {
59  $code .= "document.querySelector('#$id').setAttribute('aria-label', '$aria_label'); ";
60  }
61  foreach ($data as $key => $val) {
62  $code .= "\n document.querySelector('#$id').setAttribute('data-copg-ed-par-$key', '$val');";
63  }
64  return $code;
65  }
66  );
67  return $b;
68  }
69 
70  public function getRenderedInfoBox(string $text): string
71  {
72  $ui = $this->ui;
73  $f = $ui->factory();
74  $m = $f->messageBox()->info($text);
75  return $ui->renderer()->renderAsync($m);
76  }
77 
78  public function getRenderedFailureBox(): string
79  {
80  $ui = $this->ui;
81  $f = $ui->factory();
82  $m = $f->messageBox()->failure($this->lng->txt("copg_an_error_occured"))
83  ->withLinks([$f->link()->standard($this->lng->txt("copg_details"), "#")]);
84 
85  return $ui->renderer()->renderAsync($m);
86  }
87 
88  public function getRenderedButton(
89  string $content,
90  string $type,
91  string $action,
92  array $data = null,
93  string $component = "",
94  string $aria_label = ""
95  ): string {
96  $ui = $this->ui;
97  $b = $this->getButton($content, $type, $action, $data, $component, $aria_label);
98  return $ui->renderer()->renderAsync($b);
99  }
100 
101  public function getRenderedModalFailureBox(): string
102  {
103  $ui = $this->ui;
104  $f = $ui->factory();
105  $m = $f->messageBox()->failure($this->lng->txt("copg_error_occured_modal"))
106  ->withButtons([$f->button()->standard($this->lng->txt("copg_reload_page"), "#")->withOnLoadCode(function ($id) {
107  return
108  "$(\"#$id\").click(function() { location.reload(); return false;});";
109  })]);
110 
111  return $ui->renderer()->renderAsync($m) . "<p>" . $this->lng->txt("copg_details") . ":</p>";
112  }
113 
114  public function getRenderedButtonGroups(array $groups): string
115  {
116  $ui = $this->ui;
117  $r = $ui->renderer();
118 
119  $tpl = new \ilTemplate("tpl.editor_button_group.html", true, true, "Services/COPage");
120 
121  foreach ($groups as $buttons) {
122  foreach ($buttons as $action => $lng_key) {
123  $tpl->setCurrentBlock("button");
124  $b = $this->getButton($this->lng->txt($lng_key), "multi", $action);
125  $tpl->setVariable("BUTTON", $r->renderAsync($b));
126  $tpl->parseCurrentBlock();
127  }
128  $tpl->setCurrentBlock("section");
129  $tpl->parseCurrentBlock();
130  }
131 
132  return $tpl->get();
133  }
134 
135  public function getRenderedFormFooter(array $buttons): string
136  {
137  $ui = $this->ui;
138  $r = $ui->renderer();
139 
140  $tpl = new \ilTemplate("tpl.form_footer.html", true, true, "Services/COPage");
141 
142  $html = "";
143  foreach ($buttons as $b) {
144  $html .= $ui->renderer()->renderAsync($b);
145  }
146 
147  $tpl->setVariable("BUTTONS", $html);
148 
149  return $tpl->get();
150  }
151 
152  public function getRenderedForm(
153  \ilPropertyFormGUI $form,
154  array $buttons
155  ): string {
156  $form->clearCommandButtons();
157  $cnt = 0;
158  foreach ($buttons as $button) {
159  $cnt++;
160  $form->addCommandButton("", $button[2], "cmd-" . $cnt);
161  }
162  $html = $form->getHTMLAsync();
163  $cnt = 0;
164  foreach ($buttons as $button) {
165  $cnt++;
166  $html = str_replace(
167  "id='cmd-" . $cnt . "'",
168  " data-copg-ed-type='form-button' data-copg-ed-action='" . $button[1] . "' data-copg-ed-component='" . $button[0] . "'",
169  $html
170  );
171  }
172  return $html;
173  }
174 
180  public function sendPage(
181  \ilPageObjectGUI $page_gui,
182  $updated
183  ): Response {
184  $error = null;
185  $page_data = "";
186  $last_change = null;
187  $pc_model = null;
188 
189  if ($updated !== true) {
190  if (is_array($updated)) {
191  $error = "";
192  foreach ($updated as $u) {
193  if (is_array($u)) {
194  $error .= implode("<br />", $u);
195  } else {
196  $error .= "<br />" . $u;
197  }
198  }
199  } elseif (is_string($updated)) {
200  $error = $updated;
201  } else {
202  $error = print_r($updated, true);
203  }
204  } else {
206  $page_gui->setDefaultLinkXml(); // fixes #31225
207  $page_data = $page_gui->showPage();
208  $pc_model = $page_gui->getPageObject()->getPCModel();
209  $last_change = $page_gui->getPageObject()->getLastChange();
210  }
211 
212  $data = new \stdClass();
213  $data->renderedContent = $page_data . $this->getOnloadCode($page_gui);
214  $data->pcModel = $pc_model;
215  $data->error = $error;
216  if ($last_change) {
217  $lu = new \ilDateTime($last_change, IL_CAL_DATETIME);
219  $data->last_update = \ilDatePresentation::formatDate($lu, true);
220  }
221  return new Response($data);
222  }
223 
224  protected function getOnloadCode(\ilPageObjectGUI $page_gui): string
225  {
226  $page = $page_gui->getPageObject();
228  $all_onload_code = [];
229  foreach ($defs as $def) {
230  $pc_class = $def["pc_class"];
232  $pc_obj = new $pc_class($page);
233 
234  // onload code
235  $onload_code = $pc_obj->getOnloadCode("edit");
236  foreach ($onload_code as $code) {
237  $all_onload_code[] = $code;
238  }
239  }
240  $code_str = "";
241  if (count($all_onload_code) > 0) {
242  $code_str = "<script>" . implode("\n", $all_onload_code) . "</script>";
243  }
244  return $code_str;
245  }
246 
247  public function sendFormError(
248  string $form
249  ): Response {
250  $data = new \stdClass();
251  $data->formError = true;
252  $data->form = $form;
253  return new Response($data);
254  }
255 
256  public function getRenderedViewControl(
257  array $actions
258  ): string {
259  $ui = $this->ui;
260  $cnt = 0;
261  $view_modes = [];
262  foreach ($actions as $act) {
263  $cnt++;
264  $view_modes[$act[2]] = "cmd-" . $cnt;
265  }
266  $vc = $ui->factory()->viewControl()->mode($view_modes, "");
267  $html = $ui->renderer()->render($vc);
268  $cnt = 0;
269  foreach ($actions as $act) {
270  $cnt++;
271  $html = str_replace(
272  'data-action="cmd-' . $cnt . '"',
273  " data-copg-ed-type='view-control' data-copg-ed-action='" . $act[1] . "' data-copg-ed-component='" . $act[0] . "'",
274  $html
275  );
276  }
277  $html = str_replace("id=", "data-id=", $html);
278  return $html;
279  }
280 
281 
282  public function getLink(
283  string $content,
284  string $component,
285  string $type,
286  string $action,
287  array $data = null
288  ): \ILIAS\UI\Component\Button\Shy {
289  $ui = $this->ui;
290  $f = $ui->factory();
291  $l = $f->button()->shy($content, "");
292  if ($data === null) {
293  $data = [];
294  }
295  $l = $l->withOnLoadCode(
296  function ($id) use ($component, $type, $data, $action) {
297  $code = "document.querySelector('#$id').setAttribute('data-copg-ed-component', '$component');
298  document.querySelector('#$id').setAttribute('data-copg-ed-type', '$type');
299  document.querySelector('#$id').setAttribute('data-copg-ed-action', '$action')";
300  foreach ($data as $key => $val) {
301  $code .= "\n document.querySelector('#$id').setAttribute('data-copg-ed-par-$key', '$val');";
302  }
303  return $code;
304  }
305  );
306  return $l;
307  }
308 
309  public function getRenderedLink(
310  string $content,
311  string $component,
312  string $type,
313  string $action,
314  array $data = null
315  ): string {
316  $ui = $this->ui;
317  $l = $this->getLink($content, $component, $type, $action, $data);
318  return $ui->renderer()->renderAsync($l);
319  }
320 
321  public function getRenderedIcon(string $type): string
322  {
323  $ui = $this->ui;
324  $f = $ui->factory();
325  $r = $ui->renderer();
326  $i = $f->symbol()->icon()->standard($type, $type, 'medium');
327  return $r->render($i);
328  }
329 }
getRenderedLink(string $content, string $component, string $type, string $action, array $data=null)
Class Factory.
const IL_CAL_DATETIME
$type
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)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
__construct(\ILIAS\DI\UIServices $ui, \ilLanguage $lng)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getButton(string $content, string $type, string $action, array $data=null, string $component="", string $aria_label="")
int $updated
Timestamp for when the object was last updated.
Definition: System.php:158
Class HTTPServicesTest.
getRenderedForm(\ilPropertyFormGUI $form, array $buttons)
string $key
Consumer key/client ID value.
Definition: System.php:193
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
getRenderedButton(string $content, string $type, string $action, array $data=null, string $component="", string $aria_label="")
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
static setUseRelativeDates(bool $a_status)
set use relative dates
sendPage(\ilPageObjectGUI $page_gui, $updated)
Send whole page as response.
$i
Definition: metadata.php:41