ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.UIWrapper.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2020 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
6 
11 class UIWrapper
12 {
16  protected $ui;
17 
21  protected $lng;
22 
26  public function __construct(\ILIAS\DI\UIServices $ui, \ilLanguage $lng)
27  {
28  $this->ui = $ui;
29  $this->lng = $lng;
30  $this->lng->loadLanguageModule("copg");
31  }
32 
41  public function getButton(
42  string $content,
43  string $type,
44  string $action,
45  array $data = null,
46  string $component = ""
47  ) : \ILIAS\UI\Component\Button\Standard {
48  $ui = $this->ui;
49  $f = $ui->factory();
50  $b = $f->button()->standard($content, "");
51  if ($data === null) {
52  $data = [];
53  }
54  $b = $b->withOnLoadCode(
55  function ($id) use ($type, $data, $action, $component) {
56  $code = "document.querySelector('#$id').setAttribute('data-copg-ed-type', '$type');
57  document.querySelector('#$id').setAttribute('data-copg-ed-component', '$component');
58  document.querySelector('#$id').setAttribute('data-copg-ed-action', '$action')";
59  foreach ($data as $key => $val) {
60  $code .= "\n document.querySelector('#$id').setAttribute('data-copg-ed-par-$key', '$val');";
61  }
62  return $code;
63  }
64  );
65  return $b;
66  }
67 
68  public function getRenderedInfoBox($text)
69  {
70  $ui = $this->ui;
71  $f = $ui->factory();
72  $m = $f->messageBox()->info($text);
73  return $ui->renderer()->renderAsync($m);
74  }
75 
76  public function getRenderedFailureBox()
77  {
78  $ui = $this->ui;
79  $f = $ui->factory();
80  $m = $f->messageBox()->failure($this->lng->txt("copg_an_error_occured"))
81  ->withLinks([$f->link()->standard($this->lng->txt("copg_details"), "#")]);
82 
83  return $ui->renderer()->renderAsync($m);
84  }
85 
86  public function getRenderedModalFailureBox() : string
87  {
88  $ui = $this->ui;
89  $f = $ui->factory();
90  $m = $f->messageBox()->failure($this->lng->txt("copg_error_occured_modal"))
91  ->withButtons([$f->button()->standard($this->lng->txt("copg_reload_page"), "#")->withOnLoadCode(function ($id) {
92  return
93  "$(\"#$id\").click(function() { location.reload(); return false;});";
94  })]);
95 
96  return $ui->renderer()->renderAsync($m)."<p>".$this->lng->txt("copg_details").":</p>";
97  }
98 
107  public function getRenderedButton(string $content, string $type, string $action, array $data = null,
108  string $component = "") : string
109  {
110  $ui = $this->ui;
111  $b = $this->getButton($content, $type, $action, $data, $component);
112  return $ui->renderer()->renderAsync($b);
113  }
114 
119  public function getRenderedButtonGroups($groups)
120  {
121  $ui = $this->ui;
122  $r = $ui->renderer();
123 
124  $tpl = new \ilTemplate("tpl.editor_button_group.html", true, true, "Services/COPage");
125 
126  foreach ($groups as $buttons) {
127  foreach ($buttons as $action => $lng_key) {
128  $tpl->setCurrentBlock("button");
129  $b = $this->getButton($this->lng->txt($lng_key), "multi", $action);
130  $tpl->setVariable("BUTTON", $r->renderAsync($b));
131  $tpl->parseCurrentBlock();
132  }
133  $tpl->setCurrentBlock("section");
134  $tpl->parseCurrentBlock();
135  }
136 
137  return $tpl->get();
138  }
139 
144  public function getRenderedFormFooter($buttons)
145  {
146  $ui = $this->ui;
147  $r = $ui->renderer();
148 
149  $tpl = new \ilTemplate("tpl.form_footer.html", true, true, "Services/COPage");
150 
151  $html = "";
152  foreach ($buttons as $b) {
153  $html .= $ui->renderer()->renderAsync($b);
154  }
155 
156  $tpl->setVariable("BUTTONS", $html);
157 
158  return $tpl->get();
159  }
160 
166  public function getRenderedForm(\ilPropertyFormGUI $form, $buttons)
167  {
168  $form->clearCommandButtons();
169  $cnt = 0;
170  foreach ($buttons as $button) {
171  $cnt++;
172  $form->addCommandButton("", $button[2], "cmd-" . $cnt);
173  }
174  $html = $form->getHTML();
175  $cnt = 0;
176  foreach ($buttons as $button) {
177  $cnt++;
178  $html = str_replace(
179  "id='cmd-" . $cnt . "'",
180  " data-copg-ed-type='form-button' data-copg-ed-action='" . $button[1] . "' data-copg-ed-component='" . $button[0] . "'",
181  $html
182  );
183  }
184  return $html;
185  }
186 
191  public function sendPage($page_gui, $updated) : Response
192  {
193  $error = null;
194  $rendered_content = null;
195  $last_change = null;
196 
197  if ($updated !== true) {
198  if (is_array($updated)) {
199  $error = implode("<br />", $updated);
200  } elseif (is_string($updated)) {
201  $error = $updated;
202  } else {
203  $error = print_r($updated, true);
204  }
205  } else {
206  $page_gui->setOutputMode(\ilPageObjectGUI::EDIT);
207  $page_gui->setDefaultLinkXml(); // fixes #31225
208  $page_data = $page_gui->showPage();
209  $pc_model = $page_gui->getPageObject()->getPCModel();
210  $last_change = $page_gui->getPageObject()->getLastChange();
211  }
212 
213  $data = new \stdClass();
214  $data->renderedContent = $page_data;
215  $data->pcModel = $pc_model;
216  $data->error = $error;
217  if ($last_change) {
218  $lu = new \ilDateTime($last_change, IL_CAL_DATETIME);
220  $data->last_update = \ilDatePresentation::formatDate($lu, true);
221  }
222  return new Response($data);
223  }
224 
225  public function getRenderedViewControl($actions) : string
226  {
227  $ui = $this->ui;
228  $cnt = 0;
229  $view_modes = [];
230  foreach ($actions as $act) {
231  $cnt++;
232  $view_modes[$act[2]] = "cmd-" . $cnt;
233  }
234  $vc = $ui->factory()->viewControl()->mode($view_modes, "");
235  $html = $ui->renderer()->render($vc);
236  $cnt = 0;
237  foreach ($actions as $act) {
238  $cnt++;
239  $html = str_replace(
240  'data-action="cmd-' . $cnt . '"',
241  " data-copg-ed-type='view-control' data-copg-ed-action='" . $act[1] . "' data-copg-ed-component='" . $act[0] . "'",
242  $html
243  );
244  }
245  $html = str_replace("id=", "data-id=", $html);
246  return $html;
247  }
248 
249 
258  public function getLink(
259  string $content,
260  string $component,
261  string $type,
262  string $action,
263  array $data = null
264  ) : \ILIAS\UI\Component\Button\Shy {
265  $ui = $this->ui;
266  $f = $ui->factory();
267  $l = $f->button()->shy($content, "");
268  if ($data === null) {
269  $data = [];
270  }
271  $l = $l->withOnLoadCode(
272  function ($id) use ($component, $type, $data, $action) {
273  $code = "document.querySelector('#$id').setAttribute('data-copg-ed-component', '$component');
274  document.querySelector('#$id').setAttribute('data-copg-ed-type', '$type');
275  document.querySelector('#$id').setAttribute('data-copg-ed-action', '$action')";
276  foreach ($data as $key => $val) {
277  $code .= "\n document.querySelector('#$id').setAttribute('data-copg-ed-par-$key', '$val');";
278  }
279  return $code;
280  }
281  );
282  return $l;
283  }
284 
293  public function getRenderedLink(string $content, string $component, string $type, string $action, array $data = null) : string
294  {
295  $ui = $this->ui;
296  $l = $this->getLink($content, $component, $type, $action, $data);
297  return $ui->renderer()->renderAsync($l);
298  }
299 
305  public function getRenderedIcon($type)
306  {
307  $ui = $this->ui;
308  $f = $ui->factory();
309  $r = $ui->renderer();
310  $i = $f->symbol()->icon()->standard($type, $type, 'medium');
311  return $r->render($i);
312  }
313 }
getRenderedLink(string $content, string $component, string $type, string $action, array $data=null)
Get rendered button.
Class Factory.
$data
Definition: storeScorm.php:23
const IL_CAL_DATETIME
getRenderedFormFooter($buttons)
Get rendered form footer.
This class represents a property form user interface.
$type
getLink(string $content, string $component, string $type, string $action, array $data=null)
Get multi button.
sendPage($page_gui, $updated)
Send whole page as response.
Class ChatMainBarProvider .
static setUseRelativeDates($a_status)
set use relative dates
getRenderedForm(\ilPropertyFormGUI $form, $buttons)
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
__construct(\ILIAS\DI\UIServices $ui, \ilLanguage $lng)
Constructor.
clearCommandButtons()
Remove all command buttons.
getButton(string $content, string $type, string $action, array $data=null, string $component="")
Get multi button.
Class HTTPServicesTest.
addCommandButton($a_cmd, $a_text, $a_id="")
Add Command button.
ui()
Definition: ui.php:5
getRenderedButtonGroups($groups)
Get multi actions.
getRenderedButton(string $content, string $type, string $action, array $data=null, string $component="")
Get rendered button.
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
getRenderedIcon($type)
Get rendered icon.
$i
Definition: metadata.php:24