ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilButtonBase.php
Go to the documentation of this file.
1 <?php
2 
25 abstract class ilButtonBase implements ilToolbarItem
26 {
27  protected ilLanguage $lng;
28  protected int $type = 0; // [int]
29  protected ?string $id = ""; // [string]
30  protected string $caption = ""; // [string]
31  protected bool $caption_is_lng_id = false; // [bool]
32  protected bool $primary = false; // [bool]
33  protected bool $omit_prevent_double_submission = false; // [bool]
34  protected string $onclick = ""; // [string]
35  protected int $acc_key = 0;
36  protected bool $disabled = false; // [bool]
37  protected array $css = array(); // [array]
38  protected bool $apply_default_css = true;
39 
40  public const TYPE_SUBMIT = 1;
41  public const TYPE_LINK = 2;
42  public const TYPE_SPLIT = 3;
43  public const TYPE_BUTTON = 4;
44 
45  protected function __construct(int $a_type)
46  {
47  global $DIC;
48 
49  $this->lng = $DIC->language();
50  $this->setType($a_type);
51  }
52 
53  public function __clone()
54  {
55  $this->setId(null);
56  }
57 
58  abstract public static function getInstance(): self;
59 
60  //
61  // properties
62  //
63 
64  protected function setType(int $a_value): void
65  {
66  $this->type = $a_value;
67  }
68 
69  public function getType(): int
70  {
71  return $this->type;
72  }
73 
74  public function setId(?string $a_value): void
75  {
76  $this->id = $a_value;
77  }
78 
79  public function getId(): ?string
80  {
81  return $this->id;
82  }
83 
84  public function setCaption(string $a_value, bool $a_is_lng_id = true): void
85  {
86  $this->caption = $a_value;
87  $this->caption_is_lng_id = $a_is_lng_id;
88  }
89 
90  public function getCaption(bool $a_translate = true): string
91  {
92  $lng = $this->lng;
93 
94  $caption = $this->caption;
95 
96  if ($this->caption_is_lng_id &&
97  $a_translate) {
98  $caption = $lng->txt($caption);
99  }
100 
101  return $caption;
102  }
103 
104  public function setPrimary(bool $a_value): void
105  {
106  $this->primary = $a_value;
107  }
108 
109  public function isPrimary(): bool
110  {
111  return $this->primary;
112  }
113 
117  public function setOmitPreventDoubleSubmission(bool $a_value): void
118  {
119  $this->omit_prevent_double_submission = $a_value;
120  }
121 
122  public function getOmitPreventDoubleSubmission(): bool
123  {
125  }
126 
127  public function setOnClick(string $a_value): void
128  {
129  $this->onclick = trim($a_value);
130  }
131 
132  public function getOnClick(): string
133  {
134  return $this->onclick;
135  }
136 
137  public function setDisabled(bool $a_value): void
138  {
139  $this->disabled = $a_value;
140  }
141 
142  public function isDisabled(): bool
143  {
144  return $this->disabled;
145  }
146 
147  public function addCSSClass(string $a_value): void
148  {
149  $this->css[] = $a_value;
150  }
151 
152  public function getCSSClasses(): array
153  {
154  return $this->css;
155  }
156 
157 
158  //
159  // render
160  //
161 
162  protected function gatherCssClasses(): string
163  {
164  $css = array_unique($this->getCSSClasses());
165 
166  if ($this->isPrimary()) {
167  $css[] = "btn-primary";
168  }
169  if ($this->getOmitPreventDoubleSubmission()) {
170  $css[] = "omitPreventDoubleSubmission";
171  }
172 
173  return implode(" ", $css);
174  }
175 
176  protected function renderAttributesHelper(array $a_attr): string
177  {
178  $res = array();
179 
180  foreach ($a_attr as $id => $value) {
181  if (trim($value)) {
182  $res[] = strtolower(trim($id)) . '="' . $value . '"';
183  }
184  }
185 
186  if (count($res)) {
187  return " " . implode(" ", $res);
188  }
189  return "";
190  }
191 
195  protected function renderAttributes(array $a_additional_attr = null): string
196  {
197  $attr = array();
198  $attr["id"] = $this->getId();
199  $attr["class"] = $this->gatherCssClasses();
200  $attr["onclick"] = $this->getOnClick();
201 
202  if ($this->isDisabled()) {
203  $attr["disabled"] = "disabled";
204  }
205 
206  if (count($a_additional_attr)) {
207  $attr = array_merge($attr, $a_additional_attr);
208  }
209 
210  return $this->renderAttributesHelper($attr);
211  }
212 
213  protected function prepareRender(): void
214  {
215  if ($this->applyDefaultCss()) {
216  $this->addCSSClass("btn");
217  $this->addCSSClass("btn-default");
218  }
219  }
220 
221  public function applyDefaultCss(?bool $apply_default_css = null): ?bool
222  {
223  if (null === $apply_default_css) {
225  }
226 
227  $this->apply_default_css = $apply_default_css;
228  return false;
229  }
230 
231  abstract public function render(): string;
232 
233  public function getToolbarHTML(): string
234  {
235  return $this->render();
236  }
237 }
$res
Definition: ltiservices.php:69
renderAttributesHelper(array $a_attr)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
bool $omit_prevent_double_submission
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setOnClick(string $a_value)
getToolbarHTML()
Get input item HTML to be inserted into ilToolbarGUI.
global $DIC
Definition: feed.php:28
static getInstance()
setId(?string $a_value)
__construct(int $a_type)
setDisabled(bool $a_value)
addCSSClass(string $a_value)
setCaption(string $a_value, bool $a_is_lng_id=true)
renderAttributes(array $a_additional_attr=null)
Render current HTML attributes.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getCaption(bool $a_translate=true)
disabled()
Example showing how to plug a disabled checkbox into a form.
Definition: disabled.php:10
setType(int $a_value)
setOmitPreventDoubleSubmission(bool $a_value)
Toggle double submission prevention status.
applyDefaultCss(?bool $apply_default_css=null)
setPrimary(bool $a_value)