ILIAS  release_8 Revision v8.24
class.ilConfirmationGUI.php
Go to the documentation of this file.
1<?php
2
25{
26 protected ilLanguage $lng;
28 private array $hidden_item = [];
30 private array $item = [];
32 private array $buttons = [];
33 private bool $use_images = false;
34 private string $form_name = '';
35 protected string $form_action = '';
36 protected string $headertext = '';
37 protected string $cancel_txt = '';
38 protected string $cancel_cmd = '';
39 protected string $cancel_id = '';
40 protected string $confirm_txt = '';
41 protected string $confirm_cmd = '';
42 protected string $confirm_id = '';
43 private \ilGlobalTemplateInterface $main_tpl;
44
45 public function __construct()
46 {
48 global $DIC;
49 $this->main_tpl = $DIC->ui()->mainTemplate();
50
51 $this->lng = $DIC->language();
52 }
53
54 final public function setFormAction(string $a_form_action): void
55 {
56 $this->form_action = $a_form_action;
57 }
58
59 final public function getFormAction(): string
60 {
61 return $this->form_action;
62 }
63
64 public function setHeaderText(string $a_headertext): void
65 {
66 $this->headertext = $a_headertext;
67 }
68
69 public function getHeaderText(): string
70 {
71 return $this->headertext;
72 }
73
74 public function setFormName(string $a_name): void
75 {
76 $this->form_name = $a_name;
77 }
78
79 final public function addButton(string $a_txt, string $a_cmd): void
80 {
81 $this->buttons[] = [
82 'txt' => $a_txt,
83 'cmd' => $a_cmd
84 ];
85 }
86
87 final public function setCancel(
88 string $a_txt,
89 string $a_cmd,
90 string $a_id = ''
91 ): void {
92 $this->cancel_txt = $a_txt;
93 $this->cancel_cmd = $a_cmd;
94 $this->cancel_id = $a_id;
95 }
96
97 final public function setConfirm(
98 string $a_txt,
99 string $a_cmd,
100 string $a_id = ''
101 ): void {
102 $this->confirm_txt = $a_txt;
103 $this->confirm_cmd = $a_cmd;
104 $this->confirm_id = $a_id;
105 }
106
107 public function addItem(
108 string $a_post_var,
109 string $a_id,
110 string $a_text,
111 string $a_img = '',
112 string $a_alt = ''
113 ): void {
114 $this->item[] = [
115 'var' => $a_post_var,
116 'id' => $a_id,
117 'text' => $a_text,
118 'img' => $a_img,
119 'alt' => $a_alt
120 ];
121
122 if ($a_img !== '') {
123 $this->use_images = true;
124 }
125 }
126
127 public function addHiddenItem(
128 string $a_post_var,
129 string $a_value
130 ): void {
131 $this->hidden_item[] = [
132 'var' => $a_post_var,
133 'value' => $a_value
134 ];
135 }
136
137 final public function getHTML(): string
138 {
139 if ($this->headertext === '') {
140 throw new RuntimeException('Please provide a header text before rendering the confirmation dialogue');
141 }
142
143 if ($this->form_action === '') {
144 throw new RuntimeException('Please provide a form action before rendering the confirmation dialogue');
145 }
146
147 if ($this->confirm_txt === '' || $this->confirm_cmd === '') {
148 throw new RuntimeException('Please provide a confirmation button label and command before rendering the confirmation dialogue');
149 }
150
151 if ($this->cancel_txt === '' || $this->cancel_cmd === '') {
152 throw new RuntimeException('Please provide a cancel button label and command before rendering the confirmation dialogue');
153 }
154
155 $this->main_tpl->setOnScreenMessage('question', $this->getHeaderText());
156
157 // delete/handle items
158 if (count($this->item) > 0) {
159 $ctab = new ilConfirmationTableGUI($this->use_images);
160 $ctab->setData($this->item);
161
162 foreach ($this->buttons as $b) {
163 $ctab->addCommandButton($b["cmd"], $b["txt"]);
164 }
165 $ctab->addCommandButton($this->confirm_cmd, $this->confirm_txt);
166 $ctab->addCommandButton($this->cancel_cmd, $this->cancel_txt);
167 $ctab->setFormAction($this->getFormAction());
168 foreach ($this->hidden_item as $hidden_item) {
169 $ctab->addHiddenInput($hidden_item["var"], $hidden_item["value"]);
170 }
171
172 if ($this->form_name !== '') {
173 $ctab->setFormName($this->form_name);
174 }
175
176 return $ctab->getHTML();
177 }
178
179 // simple version, just ask for confirmation
180 $tb = new ilToolbarGUI();
181 $tb->setPreventDoubleSubmission(true);
182 $tb->setFormAction($this->getFormAction());
183 if ($this->hidden_item) {
184 foreach ($this->hidden_item as $hidden_item) {
185 $hiddenInput = new ilHiddenInputGUI($hidden_item['var']);
186 $hiddenInput->setValue($hidden_item['value']);
187 $tb->addInputItem($hiddenInput);
188 }
189 }
190 $confirm = ilSubmitButton::getInstance();
191 $confirm->setCommand($this->confirm_cmd);
192 $confirm->setCaption($this->confirm_txt, false);
193 $confirm->setId($this->confirm_id);
194
195 $cancel = ilSubmitButton::getInstance();
196 $cancel->setCommand($this->cancel_cmd);
197 $cancel->setCaption($this->cancel_txt, false);
198
199 if ($this->cancel_id !== '') {
200 $cancel->setId($this->cancel_id);
201 }
202
203 $tb->addStickyItem($confirm);
204 $tb->addStickyItem($cancel);
205
206 return $tb->getHTML();
207 }
208}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setFormAction(string $a_form_action)
addItem(string $a_post_var, string $a_id, string $a_text, string $a_img='', string $a_alt='')
ilGlobalTemplateInterface $main_tpl
setCancel(string $a_txt, string $a_cmd, string $a_id='')
setHeaderText(string $a_headertext)
addButton(string $a_txt, string $a_cmd)
setConfirm(string $a_txt, string $a_cmd, string $a_id='')
addHiddenItem(string $a_post_var, string $a_value)
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc