ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilToolbarGUI.php
Go to the documentation of this file.
1<?php
2
28{
29 protected ilLanguage $lng;
30 protected static int $instances = 0;
31 protected string $id = '';
32 protected string $form_action = '';
33 protected bool $hidden = false;
34 public array $items = array();
35 protected array $lead_img = array(
36 'img' => '',
37 'alt' => '',
38 );
39 protected bool $open_form_tag = true;
40 protected bool $close_form_tag = true;
41 protected string $form_target = "";
42 protected string $form_name = "";
43 protected bool $prevent_double_submission = false;
44 protected array $sticky_items = array();
45 protected bool $has_separator = false;
46 protected \ILIAS\DI\UIServices $ui;
47 protected bool $multipart = false;
48
49 public function __construct()
50 {
52 global $DIC;
53
54 $this->lng = $DIC->language();
55 $this->ui = $DIC->ui();
56
57 self::$instances++;
58 }
59
63 public function setFormAction(
64 string $a_val,
65 bool $a_multipart = false,
66 string $a_target = ""
67 ): void {
68 $this->form_action = $a_val;
69 $this->multipart = $a_multipart;
70 $this->form_target = $a_target;
71 }
72
73 public function getFormAction(): string
74 {
75 return $this->form_action;
76 }
77
78 public function setLeadingImage(
79 string $a_img,
80 string $a_alt
81 ): void {
82 $this->lead_img = array("img" => $a_img, "alt" => $a_alt);
83 }
84
85 public function setHidden(bool $a_val): void
86 {
87 $this->hidden = $a_val;
88 }
89
90 public function getHidden(): bool
91 {
92 return $this->hidden;
93 }
94
95 public function setId(string $a_val): void
96 {
97 $this->id = $a_val;
98 }
99
100 public function getId(): string
101 {
102 return $this->id ?: self::$instances;
103 }
104
105 public function setPreventDoubleSubmission(bool $a_val): void
106 {
107 $this->prevent_double_submission = $a_val;
108 }
109
110 public function getPreventDoubleSubmission(): bool
111 {
112 return $this->prevent_double_submission;
113 }
114
118 public function addButton(
119 string $a_txt,
120 string $a_cmd,
121 string $a_target = "",
122 ?int $a_acc_key = null,
123 string $a_additional_attrs = '',
124 string $a_id = "",
125 string $a_class = 'submit'
126 ): void {
127 $this->items[] = array("type" => "button", "txt" => $a_txt, "cmd" => $a_cmd,
128 "target" => $a_target, "acc_key" => $a_acc_key, 'add_attrs' => $a_additional_attrs,
129 "id" => $a_id, "class" => $a_class);
130 }
131
135 public function addFormButton(
136 string $a_txt,
137 string $a_cmd,
138 ?int $a_acc_key = null,
139 bool $a_primary = false,
140 ?string $a_class = null
141 ): void {
142 if ($a_primary) {
143 $button = ilSubmitButton::getInstance();
144 $button->setPrimary(true);
145 $button->setCaption($a_txt, false);
146 $button->setCommand($a_cmd);
147 $this->addStickyItem($button);
148 } else {
149 $this->items[] = array("type" => "fbutton", "txt" => $a_txt, "cmd" => $a_cmd,
150 "acc_key" => $a_acc_key, "primary" => $a_primary, "class" => $a_class);
151 }
152 }
153
154 public function addInputItem(
155 ilToolbarItem $a_item,
156 bool $a_output_label = false
157 ): void {
158 $this->items[] = array("type" => "input", "input" => $a_item, "label" => $a_output_label);
159 }
160
161
167 public function addStickyItem(
168 $a_item,
169 bool $a_output_label = false
170 ): void {
171 $this->sticky_items[] = array("item" => $a_item, "label" => $a_output_label);
172 }
173
178 public function addButtonInstance(ilButtonBase $a_button): void
179 {
180 if ($a_button->isPrimary()) {
181 $this->addStickyItem($a_button);
182 } else {
183 $this->items[] = array("type" => "button_obj", "instance" => $a_button);
184 }
185 }
186
187 public function addDropDown(
188 string $a_txt,
189 string $a_dd_html
190 ): void {
191 $this->items[] = array("type" => "dropdown", "txt" => $a_txt, "dd_html" => $a_dd_html);
192 }
193
195 {
196 $this->items[] = array("type" => "adv_sel_list", "list" => $adv);
197 }
198
199 public function addSeparator(): void
200 {
201 $this->items[] = array("type" => "separator");
202 $this->has_separator = true;
203 }
204
205 public function addText(string $a_text): void
206 {
207 $this->items[] = array("type" => "text", "text" => $a_text);
208 }
209
210 public function addSpacer(?string $a_width = null): void
211 {
212 $this->items[] = array("type" => "spacer", "width" => $a_width);
213 }
214
215 public function addComponent(\ILIAS\UI\Component\Component $a_comp): void
216 {
217 $this->items[] = array("type" => "component", "component" => $a_comp);
218 }
219
220 public function addLink(
221 string $a_caption,
222 string $a_url,
223 bool $a_disabled = false
224 ): void {
225 $this->items[] = array("type" => "link", "txt" => $a_caption, "cmd" => $a_url, "disabled" => $a_disabled);
226 }
227
228 public function setOpenFormTag(
229 bool $a_val
230 ): void {
231 $this->open_form_tag = $a_val;
232 }
233
234 public function getOpenFormTag(): bool
235 {
236 return $this->open_form_tag;
237 }
238
239 public function setCloseFormTag(bool $a_val): void
240 {
241 $this->close_form_tag = $a_val;
242 }
243
244 public function getCloseFormTag(): bool
245 {
246 return $this->close_form_tag;
247 }
248
249 public function setFormName(string $a_val): void
250 {
251 $this->form_name = $a_val;
252 }
253
254 public function getFormName(): string
255 {
256 return $this->form_name;
257 }
258
259
263 public function getGroupedItems(): array
264 {
265 $groups = array();
266 $group = array();
267 foreach ($this->items as $item) {
268 if ($item['type'] === 'separator') {
269 $groups[] = $group;
270 $group = array();
271 } else {
272 $group[] = $item;
273 }
274 }
275 if (count($group)) {
276 $groups[] = $group;
277 }
278
279 return $groups;
280 }
281
282 public function getHTML(): string
283 {
285
286 $this->applyAutoStickyToSingleElement();
287
288 if (count($this->items) || count($this->sticky_items)) {
289 $tpl = new ilTemplate("tpl.toolbar.html", true, true, "components/ILIAS/UIComponent/Toolbar");
290 $tpl->setVariable('TOOLBAR_ID', $this->getId());
291 $tpl->setVariable('MORE_LABEL', $this->lng->txt('toolbar_more_actions'));
292
293 if (count($this->sticky_items)) {
294 $tpl_sticky = new ilTemplate("tpl.toolbar_sticky_items.html", true, true, "components/ILIAS/UIComponent/Toolbar");
296 foreach ($this->sticky_items as $sticky_item) {
297 if ($sticky_item['label']) {
298 $tpl_sticky->setCurrentBlock('input_label');
299 $tpl_sticky->setVariable('INPUT_ID', $sticky_item['item']->getFieldId());
300 $tpl_sticky->setVariable('TXT_INPUT', $sticky_item['item']->getTitle());
301 $tpl_sticky->parseCurrentBlock();
302 }
303
304 if ($sticky_item['item'] instanceof ilToolbarItem) {
305 $tpl_sticky->setCurrentBlock('sticky_item');
306 $tpl_sticky->setVariable('STICKY_ITEM_HTML', $sticky_item['item']->getToolbarHTML());
307 $tpl_sticky->parseCurrentBlock();
308 } elseif ($sticky_item['item'] instanceof \ILIAS\UI\Component\Component) {
309 $tpl_sticky->setCurrentBlock("sticky_item");
310 $tpl_sticky->setVariable("STICKY_ITEM_HTML", $this->ui->renderer()->render($sticky_item['item']));
311 $tpl_sticky->parseCurrentBlock();
312 }
313 }
314 $tpl->setCurrentBlock('sticky_items');
315 $tpl->setVariable('STICKY_ITEMS', $tpl_sticky->get());
316 $tpl->parseCurrentBlock();
317 }
318
319 $markup_items = '';
320 foreach ($this->getGroupedItems() as $i => $group) {
321 $tpl_items = new ilTemplate("tpl.toolbar_items.html", true, true, "components/ILIAS/UIComponent/Toolbar");
322 if ($i > 0) {
323 static $tpl_separator;
324 if ($tpl_separator === null) {
325 $tpl_separator = new ilTemplate('tpl.toolbar_separator.html', true, true, 'components/ILIAS/UIComponent/Toolbar');
326 }
327 $tpl_separator->touchBlock('separator');
328 $markup_items .= $tpl_separator->get();
329 }
330 foreach ($group as $item) {
331 $tpl_items->setCurrentBlock("item");
332 switch ($item["type"]) {
333 case "button":
334 $tpl_items->setVariable("BTN_TXT", $item["txt"]);
335 $tpl_items->setVariable("BTN_LINK", $item["cmd"]);
336 if ($item["target"] != "") {
337 $tpl_items->setVariable("BTN_TARGET", 'target="' . $item["target"] . '"');
338 }
339 if ($item["id"] != "") {
340 $tpl_items->setVariable("BID", 'id="' . $item["id"] . '"');
341 }
342 if (($item['add_attrs'])) {
343 $tpl_items->setVariable('BTN_ADD_ARG', $item['add_attrs']);
344 }
345 $tpl_items->setVariable('BTN_CLASS', $item['class']);
346 break;
347
348 case "fbutton":
349 $tpl_items->setVariable("SUB_TXT", $item["txt"]);
350 $tpl_items->setVariable("SUB_CMD", $item["cmd"]);
351 if ($item["primary"]) {
352 $tpl_items->setVariable("SUB_CLASS", " emphsubmit");
353 } elseif ($item["class"]) {
354 $tpl_items->setVariable("SUB_CLASS", " " . $item["class"]);
355 }
356 break;
357
358 case "button_obj":
359 $tpl_items->setVariable("BUTTON_OBJ", $item["instance"]->render());
360 break;
361
362 case "input":
363 if ($item["label"]) {
364 $tpl_items->setVariable("TXT_INPUT", $item["input"]->getTitle());
365 $tpl_items->setVariable("INPUT_ID", $item["input"]->getFieldId());
366 }
367 $tpl_items->setVariable("INPUT_HTML", $item["input"]->getToolbarHTML());
368 break;
369
370 // bs-patch start
371 case "dropdown":
372 $tpl_items->setVariable("TXT_DROPDOWN", $item["txt"]);
373 $tpl_items->setVariable("DROP_DOWN", $item["dd_html"]);
374 break;
375 // bs-patch end
376 case "text":
377 $tpl_items->setVariable("VAL_TEXT", $item["text"]);
378 break;
379
380 case "component":
381 $tpl_items->setVariable("COMPONENT", $this->ui->renderer()->render($item["component"]));
382 break;
383
384 case "adv_sel_list":
385 $tpl_items->setVariable("COMPONENT", $item["list"]->getHTML());
386 $tpl_items->parseCurrentBlock();
387 break;
388
389 case "link":
390 if ($item["disabled"] == false) {
391 $tpl_items->setVariable("LINK_TXT", $item["txt"]);
392 $tpl_items->setVariable("LINK_URL", $item["cmd"]);
393 } else {
394 $tpl_items->setVariable("LINK_DISABLED_TXT", $item["txt"]);
395 }
396 break;
397 }
398 $tpl_items->parseCurrentBlock();
399 }
400 $tpl_itemgroups = new ilTemplate("tpl.toolbar_itemgroup.html", true, true, 'components/ILIAS/UIComponent/Toolbar');
401 $tpl_itemgroups->setCurrentBlock("itemgroup");
402 $tpl_itemgroups->setVariable("ITEMS", $tpl_items->get());
403 $tpl_itemgroups->parseCurrentBlock();
404 $markup_items .= $tpl_itemgroups->get();
405 }
406
407 $tpl->setVariable('ITEMS', $markup_items);
408 $tpl->setVariable("TXT_FUNCTIONS", $lng->txt("functions"));
409 if ($this->lead_img["img"] != "") {
410 $tpl->setCurrentBlock("lead_image");
411 $tpl->setVariable("IMG_SRC", $this->lead_img["img"]);
412 $tpl->setVariable("IMG_ALT", $this->lead_img["alt"]);
413 $tpl->parseCurrentBlock();
414 }
415
416 // form?
417 if ($this->getFormAction() !== "") {
418 // #18947
419 $GLOBALS["tpl"]->addJavaScript("assets/js/Form.js");
420
421 if ($this->getOpenFormTag()) {
422 $tpl->setCurrentBlock("form_open");
423 $tpl->setVariable("FORMACTION", $this->getFormAction());
424 if ($this->getPreventDoubleSubmission()) {
425 $tpl->setVariable("FORM_CLASS", "preventDoubleSubmission");
426 }
427 if ($this->multipart) {
428 $tpl->setVariable("ENC_TYPE", 'enctype="multipart/form-data"');
429 }
430 if ($this->form_target !== "") {
431 $tpl->setVariable("TARGET", ' target="' . $this->form_target . '" ');
432 }
433 if ($this->form_name !== "") {
434 $tpl->setVariable("FORMNAME", 'name="' . $this->getFormName() . '"');
435 }
436
437 $tpl->parseCurrentBlock();
438 }
439 if ($this->getCloseFormTag()) {
440 $tpl->touchBlock("form_close");
441 }
442 }
443
444 // id
445 if ($this->getId() !== "") {
446 $tpl->setVariable("ID", ' id="' . $this->getId() . '" ');
447 }
448
449 // hidden style
450 if ($this->getHidden()) {
451 $tpl->setVariable("HIDDEN_CLASS", 'ilNoDisplay');
452 }
453
454 return $tpl->get();
455 }
456 return "";
457 }
458
459 public function getItems(): array
460 {
461 return $this->items;
462 }
463
464 public function setItems(array $items): void
465 {
466 $this->items = $items;
467 }
468
474 protected function applyAutoStickyToSingleElement(): void
475 {
476 if (count($this->items) === 1 && count($this->sticky_items) === 0) {
477 $supported_types = ['button', 'fbutton', 'button_obj'];
478 $item = $this->items[0];
479 if (!in_array($item['type'], $supported_types)) {
480 return;
481 }
482 $button = null;
483 switch ($item['type']) {
484 case 'button_obj':
485 $button = $item['instance'];
486 break;
487 case 'fbutton':
488 $button = ilSubmitButton::getInstance();
489 $button->setPrimary($item['primary']);
490 $button->setCaption($item['txt'], false);
491 $button->setCommand($item['cmd']);
492 break;
493 case 'button':
494 $button = ilLinkButton::getInstance();
495 $button->setCaption($item['txt'], false);
496 $button->setUrl($item['cmd']);
497 $button->setTarget($item['target']);
498 $button->setId($item['id']);
499 break;
500 }
501 $this->addStickyItem($button);
502 $this->items = [];
503 }
504 }
505}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
special template class to simplify handling of ITX/PEAR
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setFormAction(string $a_val, bool $a_multipart=false, string $a_target="")
Set form action (if form action is set, toolbar is wrapped into form tags)
ILIAS DI UIServices $ui
setOpenFormTag(bool $a_val)
bool $prevent_double_submission
setPreventDoubleSubmission(bool $a_val)
addButtonInstance(ilButtonBase $a_button)
Add button instance.
setCloseFormTag(bool $a_val)
setId(string $a_val)
addAdvancedSelectionList(ilAdvancedSelectionListGUI $adv)
setHidden(bool $a_val)
setItems(array $items)
addButton(string $a_txt, string $a_cmd, string $a_target="", ?int $a_acc_key=null, string $a_additional_attrs='', string $a_id="", string $a_class='submit')
addInputItem(ilToolbarItem $a_item, bool $a_output_label=false)
addComponent(\ILIAS\UI\Component\Component $a_comp)
addStickyItem( $a_item, bool $a_output_label=false)
Add a sticky item.
applyAutoStickyToSingleElement()
If the toolbar consists of only one button, make it sticky Note: Atm this is only possible for button...
addSpacer(?string $a_width=null)
setFormName(string $a_val)
static int $instances
addDropDown(string $a_txt, string $a_dd_html)
setLeadingImage(string $a_img, string $a_alt)
addFormButton(string $a_txt, string $a_cmd, ?int $a_acc_key=null, bool $a_primary=false, ?string $a_class=null)
addText(string $a_text)
getGroupedItems()
Get all groups (items separated by a separator)
addLink(string $a_caption, string $a_url, bool $a_disabled=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54