ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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
194
195 public function addSeparator(): void
196 {
197 $this->items[] = array("type" => "separator");
198 $this->has_separator = true;
199 }
200
201 public function addText(string $a_text): void
202 {
203 $this->items[] = array("type" => "text", "text" => $a_text);
204 }
205
206 public function addSpacer(?string $a_width = null): void
207 {
208 $this->items[] = array("type" => "spacer", "width" => $a_width);
209 }
210
211 public function addComponent(\ILIAS\UI\Component\Component $a_comp): void
212 {
213 $this->items[] = array("type" => "component", "component" => $a_comp);
214 }
215
216 public function addLink(
217 string $a_caption,
218 string $a_url,
219 bool $a_disabled = false
220 ): void {
221 $this->items[] = array("type" => "link", "txt" => $a_caption, "cmd" => $a_url, "disabled" => $a_disabled);
222 }
223
224 public function setOpenFormTag(
225 bool $a_val
226 ): void {
227 $this->open_form_tag = $a_val;
228 }
229
230 public function getOpenFormTag(): bool
231 {
232 return $this->open_form_tag;
233 }
234
235 public function setCloseFormTag(bool $a_val): void
236 {
237 $this->close_form_tag = $a_val;
238 }
239
240 public function getCloseFormTag(): bool
241 {
242 return $this->close_form_tag;
243 }
244
245 public function setFormName(string $a_val): void
246 {
247 $this->form_name = $a_val;
248 }
249
250 public function getFormName(): string
251 {
252 return $this->form_name;
253 }
254
255
259 public function getGroupedItems(): array
260 {
261 $groups = array();
262 $group = array();
263 foreach ($this->items as $item) {
264 if ($item['type'] === 'separator') {
265 $groups[] = $group;
266 $group = array();
267 } else {
268 $group[] = $item;
269 }
270 }
271 if (count($group)) {
272 $groups[] = $group;
273 }
274
275 return $groups;
276 }
277
278 public function getHTML(): string
279 {
281
282 $this->applyAutoStickyToSingleElement();
283
284 if (count($this->items) || count($this->sticky_items)) {
285 $tpl = new ilTemplate("tpl.toolbar.html", true, true, "components/ILIAS/UIComponent/Toolbar");
286 $tpl->setVariable('TOOLBAR_ID', $this->getId());
287 $tpl->setVariable('MORE_LABEL', $this->lng->txt('toolbar_more_actions'));
288
289 if (count($this->sticky_items)) {
290 $tpl_sticky = new ilTemplate("tpl.toolbar_sticky_items.html", true, true, "components/ILIAS/UIComponent/Toolbar");
292 foreach ($this->sticky_items as $sticky_item) {
293 if ($sticky_item['label']) {
294 $tpl_sticky->setCurrentBlock('input_label');
295 $tpl_sticky->setVariable('INPUT_ID', $sticky_item['item']->getFieldId());
296 $tpl_sticky->setVariable('TXT_INPUT', $sticky_item['item']->getTitle());
297 $tpl_sticky->parseCurrentBlock();
298 }
299
300 if ($sticky_item['item'] instanceof ilToolbarItem) {
301 $tpl_sticky->setCurrentBlock('sticky_item');
302 $tpl_sticky->setVariable('STICKY_ITEM_HTML', $sticky_item['item']->getToolbarHTML());
303 $tpl_sticky->parseCurrentBlock();
304 } elseif ($sticky_item['item'] instanceof \ILIAS\UI\Component\Component) {
305 $tpl_sticky->setCurrentBlock("sticky_item");
306 $tpl_sticky->setVariable("STICKY_ITEM_HTML", $this->ui->renderer()->render($sticky_item['item']));
307 $tpl_sticky->parseCurrentBlock();
308 }
309 }
310 $tpl->setCurrentBlock('sticky_items');
311 $tpl->setVariable('STICKY_ITEMS', $tpl_sticky->get());
312 $tpl->parseCurrentBlock();
313 }
314
315 $markup_items = '';
316 foreach ($this->getGroupedItems() as $i => $group) {
317 $tpl_items = new ilTemplate("tpl.toolbar_items.html", true, true, "components/ILIAS/UIComponent/Toolbar");
318 if ($i > 0) {
319 static $tpl_separator;
320 if ($tpl_separator === null) {
321 $tpl_separator = new ilTemplate('tpl.toolbar_separator.html', true, true, 'components/ILIAS/UIComponent/Toolbar');
322 }
323 $tpl_separator->touchBlock('separator');
324 $markup_items .= $tpl_separator->get();
325 }
326 foreach ($group as $item) {
327 $tpl_items->setCurrentBlock("item");
328 switch ($item["type"]) {
329 case "button":
330 $tpl_items->setVariable("BTN_TXT", $item["txt"]);
331 $tpl_items->setVariable("BTN_LINK", $item["cmd"]);
332 if ($item["target"] != "") {
333 $tpl_items->setVariable("BTN_TARGET", 'target="' . $item["target"] . '"');
334 }
335 if ($item["id"] != "") {
336 $tpl_items->setVariable("BID", 'id="' . $item["id"] . '"');
337 }
338 if (($item['add_attrs'])) {
339 $tpl_items->setVariable('BTN_ADD_ARG', $item['add_attrs']);
340 }
341 $tpl_items->setVariable('BTN_CLASS', $item['class']);
342 break;
343
344 case "fbutton":
345 $tpl_items->setVariable("SUB_TXT", $item["txt"]);
346 $tpl_items->setVariable("SUB_CMD", $item["cmd"]);
347 if ($item["primary"]) {
348 $tpl_items->setVariable("SUB_CLASS", " emphsubmit");
349 } elseif ($item["class"]) {
350 $tpl_items->setVariable("SUB_CLASS", " " . $item["class"]);
351 }
352 break;
353
354 case "button_obj":
355 $tpl_items->setVariable("BUTTON_OBJ", $item["instance"]->render());
356 break;
357
358 case "input":
359 if ($item["label"]) {
360 $tpl_items->setVariable("TXT_INPUT", $item["input"]->getTitle());
361 $tpl_items->setVariable("INPUT_ID", $item["input"]->getFieldId());
362 }
363 $tpl_items->setVariable("INPUT_HTML", $item["input"]->getToolbarHTML());
364 break;
365
366 // bs-patch start
367 case "dropdown":
368 $tpl_items->setVariable("TXT_DROPDOWN", $item["txt"]);
369 $tpl_items->setVariable("DROP_DOWN", $item["dd_html"]);
370 break;
371 // bs-patch end
372 case "text":
373 $tpl_items->setVariable("VAL_TEXT", $item["text"]);
374 break;
375
376 case "component":
377 $tpl_items->setVariable("COMPONENT", $this->ui->renderer()->render($item["component"]));
378 break;
379
380 case "link":
381 if ($item["disabled"] == false) {
382 $tpl_items->setVariable("LINK_TXT", $item["txt"]);
383 $tpl_items->setVariable("LINK_URL", $item["cmd"]);
384 } else {
385 $tpl_items->setVariable("LINK_DISABLED_TXT", $item["txt"]);
386 }
387 break;
388 }
389 $tpl_items->parseCurrentBlock();
390 }
391 $tpl_itemgroups = new ilTemplate("tpl.toolbar_itemgroup.html", true, true, 'components/ILIAS/UIComponent/Toolbar');
392 $tpl_itemgroups->setCurrentBlock("itemgroup");
393 $tpl_itemgroups->setVariable("ITEMS", $tpl_items->get());
394 $tpl_itemgroups->parseCurrentBlock();
395 $markup_items .= $tpl_itemgroups->get();
396 }
397
398 $tpl->setVariable('ITEMS', $markup_items);
399 $tpl->setVariable("TXT_FUNCTIONS", $lng->txt("functions"));
400 if ($this->lead_img["img"] != "") {
401 $tpl->setCurrentBlock("lead_image");
402 $tpl->setVariable("IMG_SRC", $this->lead_img["img"]);
403 $tpl->setVariable("IMG_ALT", $this->lead_img["alt"]);
404 $tpl->parseCurrentBlock();
405 }
406
407 // form?
408 if ($this->getFormAction() !== "") {
409 // #18947
410 $GLOBALS["tpl"]->addJavaScript("assets/js/Form.js");
411
412 if ($this->getOpenFormTag()) {
413 $tpl->setCurrentBlock("form_open");
414 $tpl->setVariable("FORMACTION", $this->getFormAction());
415 if ($this->getPreventDoubleSubmission()) {
416 $tpl->setVariable("FORM_CLASS", "preventDoubleSubmission");
417 }
418 if ($this->multipart) {
419 $tpl->setVariable("ENC_TYPE", 'enctype="multipart/form-data"');
420 }
421 if ($this->form_target !== "") {
422 $tpl->setVariable("TARGET", ' target="' . $this->form_target . '" ');
423 }
424 if ($this->form_name !== "") {
425 $tpl->setVariable("FORMNAME", 'name="' . $this->getFormName() . '"');
426 }
427
428 $tpl->parseCurrentBlock();
429 }
430 if ($this->getCloseFormTag()) {
431 $tpl->touchBlock("form_close");
432 }
433 }
434
435 // id
436 if ($this->getId() !== "") {
437 $tpl->setVariable("ID", ' id="' . $this->getId() . '" ');
438 }
439
440 // hidden style
441 if ($this->getHidden()) {
442 $tpl->setVariable("HIDDEN_CLASS", 'ilNoDisplay');
443 }
444
445 return $tpl->get();
446 }
447 return "";
448 }
449
450 public function getItems(): array
451 {
452 return $this->items;
453 }
454
455 public function setItems(array $items): void
456 {
457 $this->items = $items;
458 }
459
465 protected function applyAutoStickyToSingleElement(): void
466 {
467 if (count($this->items) === 1 && count($this->sticky_items) === 0) {
468 $supported_types = ['button', 'fbutton', 'button_obj'];
469 $item = $this->items[0];
470 if (!in_array($item['type'], $supported_types)) {
471 return;
472 }
473 $button = null;
474 switch ($item['type']) {
475 case 'button_obj':
476 $button = $item['instance'];
477 break;
478 case 'fbutton':
479 $button = ilSubmitButton::getInstance();
480 $button->setPrimary($item['primary']);
481 $button->setCaption($item['txt'], false);
482 $button->setCommand($item['cmd']);
483 break;
484 case 'button':
485 $button = ilLinkButton::getInstance();
486 $button->setCaption($item['txt'], false);
487 $button->setUrl($item['cmd']);
488 $button->setTarget($item['target']);
489 $button->setId($item['id']);
490 break;
491 }
492 $this->addStickyItem($button);
493 $this->items = [];
494 }
495 }
496}
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)
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