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