ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
class.ilAccordionGUI.php
Go to the documentation of this file.
1<?php
2
24{
25 public const VERTICAL = "vertical";
26 public const HORIZONTAL = "horizontal";
27 public const FORCE_ALL_OPEN = "ForceAllOpen";
28 public const FIRST_OPEN = "FirstOpen";
29 public const ALL_CLOSED = "AllClosed";
30 protected string $orientation;
31 protected ilObjUser $user;
32 protected array $items = array();
33 protected array $force_open = array();
34 protected static int $accordion_cnt = 0;
35 protected bool $use_session_storage = false;
36 protected bool $allow_multi_opened = false;
37 protected string $show_all_element = "";
38 protected string $hide_all_element = "";
39 protected ?int $contentwidth = null;
40 protected ?int $contentheight = null;
41 protected string $headerclass = "";
42 protected string $contentclass = "";
43 protected string $icontainerclass = "";
44 protected string $containerclass = "";
45 protected string $id = "";
46 protected bool $head_class_set = false;
48 protected string $active_headerclass = "";
49 protected string $behaviour = self::FIRST_OPEN;
50
51 public function __construct()
52 {
53 global $DIC;
54
55 $this->main_tpl = $DIC->ui()->mainTemplate();
56
57 $this->user = $DIC->user();
59 }
60
61 public function setId(string $a_val): void
62 {
63 $this->id = $a_val;
64 }
65
66 public function getId(): string
67 {
68 return $this->id;
69 }
70
71 public function setOrientation(string $a_orientation): void
72 {
73 if (in_array(
74 $a_orientation,
76 )) {
77 $this->orientation = $a_orientation;
78 }
79 }
80
81 public function getOrientation(): string
82 {
83 return $this->orientation;
84 }
85
86 public function setContainerClass(string $a_containerclass): void
87 {
88 $this->containerclass = $a_containerclass;
89 }
90
91 public function getContainerClass(): string
92 {
94 }
95
96 public function setInnerContainerClass(string $a_containerclass): void
97 {
98 $this->icontainerclass = $a_containerclass;
99 }
100
101 public function getInnerContainerClass(): string
102 {
104 }
105
106 public function setHeaderClass(string $a_headerclass): void
107 {
108 $this->headerclass = $a_headerclass;
109 }
110
111 public function getHeaderClass(): string
112 {
113 return $this->headerclass;
114 }
115
116 public function setActiveHeaderClass(string $a_h_class): void
117 {
118 $this->active_headerclass = $a_h_class;
119 }
120
121 public function getActiveHeaderClass(): string
122 {
124 }
125
126 public function setContentClass(string $a_contentclass): void
127 {
128 $this->contentclass = $a_contentclass;
129 }
130
131 public function getContentClass(): string
132 {
133 return $this->contentclass;
134 }
135
136 public function setContentWidth(?int $a_contentwidth): void
137 {
138 $this->contentwidth = $a_contentwidth;
139 }
140
141 public function getContentWidth(): ?int
142 {
143 return $this->contentwidth;
144 }
145
146 public function setContentHeight(?int $a_contentheight): void
147 {
148 $this->contentheight = $a_contentheight;
149 }
150
151 public function getContentHeight(): ?int
152 {
154 }
155
159 public function setBehaviour(string $a_val): void
160 {
161 $this->behaviour = $a_val;
162 }
163
164 public function getBehaviour(): string
165 {
166 return $this->behaviour;
167 }
168
169 public function setUseSessionStorage(bool $a_val): void
170 {
171 $this->use_session_storage = $a_val;
172 }
173
174 public function getUseSessionStorage(): bool
175 {
177 }
178
179 public function setAllowMultiOpened(bool $a_val): void
180 {
181 $this->allow_multi_opened = $a_val;
182 }
183
184 public function getAllowMultiOpened(): bool
185 {
187 }
188
192 public function setShowAllElement(string $a_val): void
193 {
194 $this->show_all_element = $a_val;
195 }
196
197 public function getShowAllElement(): string
198 {
200 }
201
205 public function setHideAllElement(string $a_val): void
206 {
207 $this->hide_all_element = $a_val;
208 }
209
210 public function getHideAllElement(): string
211 {
213 }
214
218 public static function addJavaScript(?ilGlobalTemplate $main_tpl = null): void
219 {
220 global $DIC;
221
222 if ($main_tpl != null) {
223 $tpl = $main_tpl;
224 } else {
225 $tpl = $DIC["tpl"];
226 }
227
228 foreach (self::getLocalJavascriptFiles() as $f) {
229 $tpl->addJavaScript($f, true, 3);
230 }
231 }
232
236 public static function addCss(): void
237 {
238 global $DIC;
239
240 $tpl = $DIC["tpl"];
241
242 foreach (self::getLocalCssFiles() as $f) {
243 $tpl->addCss($f);
244 }
245 }
246
247 public static function getLocalJavascriptFiles(): array
248 {
249 $debug = false;
250 if ($debug) {
251 return array(
252 "../components/ILIAS/Accordion/resources/accordion.js"
253 );
254 }
255 return array(
256 "assets/js/accordion.js"
257 );
258 }
259
260 public static function getLocalCssFiles(): array
261 {
262 return array(
263 "./components/ILIAS/Accordion/css/accordion.css"
264 );
265 }
266
267 public function addItem(
268 string $a_header,
269 string $a_content,
270 bool $a_force_open = false
271 ): void {
272 $this->items[] = array("header" => $a_header,
273 "content" => $a_content, "force_open" => $a_force_open);
274
275 if ($a_force_open) {
276 $this->force_open[] = sizeof($this->items);
277 }
278 }
279
280 public function getItems(): array
281 {
282 return $this->items;
283 }
284
285 public function getHTML(bool $async = false): string
286 {
287 $ilUser = $this->user;
288
289 self::$accordion_cnt++;
290
291 $or_short = ($this->getOrientation() == ilAccordionGUI::HORIZONTAL)
292 ? "H"
293 : "V";
294
295 $width = (int) $this->getContentWidth();
296 $height = (int) $this->getContentHeight();
297 if ($this->getOrientation() == ilAccordionGUI::HORIZONTAL) {
298 if ($width == 0) {
299 $width = 200;
300 }
301 if ($height == 0) {
302 $height = 100;
303 }
304 }
305
306 $this->addJavascript();
307 $this->addCss();
308
309 $tpl = new ilTemplate("tpl.accordion.html", true, true, "components/ILIAS/Accordion");
310 foreach ($this->getItems() as $item) {
311 $tpl->setCurrentBlock("item");
312 $tpl->setVariable("HEADER", $item["header"]);
313 $tpl->setVariable("CONTENT", $item["content"]);
314 $tpl->setVariable("HEADER_CLASS", $this->getHeaderClass()
315 ? $this->getHeaderClass() : "il_" . $or_short . "AccordionHead");
316 $tpl->setVariable("CONTENT_CLASS", $this->getContentClass()
317 ? $this->getContentClass() : "il_" . $or_short . "AccordionContent");
318
319 if ($this->getBehaviour() != self::FORCE_ALL_OPEN) {
320 $tpl->setVariable("HIDE_CONTENT_CLASS", "ilAccHideContent");
321 }
322
323 $tpl->setVariable("OR_SHORT", $or_short);
324
325 $tpl->setVariable("INNER_CONTAINER_CLASS", $this->getInnerContainerClass()
326 ? $this->getInnerContainerClass() : "il_" . $or_short . "AccordionInnerContainer");
327
328
329 if ($height > 0) {
330 $tpl->setVariable("HEIGHT", "height:" . $height . "px;");
331 }
332 if ($height > 0 && $this->getOrientation() == ilAccordionGUI::HORIZONTAL) {
333 $tpl->setVariable("HHEIGHT", "height:" . $height . "px;");
334 }
335 $tpl->parseCurrentBlock();
336 }
337
338 $tpl->setVariable("CONTAINER_CLASS", $this->getContainerClass()
339 ? $this->getContainerClass() : "il_" . $or_short . "AccordionContainer");
340
341 $options["orientation"] = $this->getOrientation();
342 $options["int_id"] = $this->getId();
343
344 if ($this->getUseSessionStorage() && $this->getId() != "") {
346
347 $ctab = $stor->getProperty(
348 $this->getId(),
349 $ilUser->getId(),
350 "opened"
351 );
352 $ctab_arr = explode(";", $ctab);
353
354 foreach ($this->force_open as $fo) {
355 if (!in_array($fo, $ctab_arr)) {
356 $ctab_arr[] = $fo;
357 }
358 }
359 $ctab = implode(";", $ctab_arr);
360
361 if ($ctab == "0") {
362 $ctab = "";
363 }
364
365 $options["initial_opened"] = $ctab;
366 $options["save_url"] = "./ilias.php?baseClass=ilaccordionpropertiesstoragegui&cmd=setOpenedTab" .
367 "&accordion_id=" . $this->getId() . "&user_id=" . $ilUser->getId();
368 }
369
370 $options["behaviour"] = $this->getBehaviour();
371 if ($this->getOrientation() == ilAccordionGUI::HORIZONTAL) {
372 $options["toggle_class"] = 'il_HAccordionToggleDef';
373 $options["toggle_act_class"] = 'il_HAccordionToggleActiveDef';
374 $options["content_class"] = 'il_HAccordionContentDef';
375 } else {
376 $options["toggle_class"] = 'il_VAccordionToggleDef';
377 $options["toggle_act_class"] = 'il_VAccordionToggleActiveDef';
378 $options["content_class"] = 'il_VAccordionContentDef';
379 }
380
381
382 if ($width > 0) {
383 $options["width"] = $width;
384 } else {
385 $options["width"] = null;
386 }
387 if ($width > 0 && $this->getOrientation() == ilAccordionGUI::VERTICAL) {
388 $tpl->setVariable("CWIDTH", 'style="width:' . $width . 'px;"');
389 }
390
391 if ($this->head_class_set) {
392 $options["active_head_class"] = $this->getActiveHeaderClass();
393 } else {
394 if ($this->getOrientation() == ilAccordionGUI::VERTICAL) {
395 $options["active_head_class"] = "il_HAccordionHeadActive";
396 } else {
397 $options["active_head_class"] = "il_VAccordionHeadActive";
398 }
399 }
400
401 $options["height"] = null;
402 $options["id"] = 'accordion_' . $this->getId() . '_' . self::$accordion_cnt;
403 $options["multi"] = (bool) $this->getAllowMultiOpened();
404 $options["show_all_element"] = $this->getShowAllElement();
405 $options["hide_all_element"] = $this->getHideAllElement();
406
407 $tpl->setVariable("ACC_ID", $options["id"]);
408
409 $html = $tpl->get();
410 $code = $this->getOnloadCode($options);
411 if (!$async) {
412 $this->main_tpl->addOnLoadCode($code);
413 } else {
414 $html .= "<script>$code</script>";
415 }
416 return $html;
417 }
418
419 protected function getOnloadCode(array $options): string
420 {
421 return 'il.Accordion.add(' . json_encode($options, JSON_THROW_ON_ERROR) . ');';
422 }
423}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setId(string $a_val)
setContentClass(string $a_contentclass)
setContentWidth(?int $a_contentwidth)
setShowAllElement(string $a_val)
setContentHeight(?int $a_contentheight)
setContainerClass(string $a_containerclass)
setActiveHeaderClass(string $a_h_class)
getOnloadCode(array $options)
setUseSessionStorage(bool $a_val)
setOrientation(string $a_orientation)
addItem(string $a_header, string $a_content, bool $a_force_open=false)
getHTML(bool $async=false)
setInnerContainerClass(string $a_containerclass)
setAllowMultiOpened(bool $a_val)
setHideAllElement(string $a_val)
setBehaviour(string $a_val)
Set behaviour "ForceAllOpen" | "FirstOpen" | "AllClosed".
static addCss()
Add required css.
setHeaderClass(string $a_headerclass)
static getLocalJavascriptFiles()
static addJavaScript(?ilGlobalTemplate $main_tpl=null)
Add javascript files that are necessary to run accordion.
ilGlobalTemplateInterface $main_tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
special template class to simplify handling of ITX/PEAR
User class.
special template class to simplify handling of ITX/PEAR
global $DIC
Definition: shib_login.php:26