ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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;
47 public static string $owl_path = "./node_modules/owl.carousel/dist";
48 public static string $owl_js_path = "/owl.carousel.js";
49 public static string $owl_css_path = "/assets/owl.carousel.css";
51 protected string $active_headerclass = "";
52 protected string $behaviour = self::FIRST_OPEN;
53
54 public function __construct()
55 {
56 global $DIC;
57
58 $this->main_tpl = $DIC->ui()->mainTemplate();
59
60 $this->user = $DIC->user();
62 }
63
64 public function setId(string $a_val): void
65 {
66 $this->id = $a_val;
67 }
68
69 public function getId(): string
70 {
71 return $this->id;
72 }
73
74 public function setOrientation(string $a_orientation): void
75 {
76 if (in_array(
77 $a_orientation,
79 )) {
80 $this->orientation = $a_orientation;
81 }
82 }
83
84 public function getOrientation(): string
85 {
86 return $this->orientation;
87 }
88
89 public function setContainerClass(string $a_containerclass): void
90 {
91 $this->containerclass = $a_containerclass;
92 }
93
94 public function getContainerClass(): string
95 {
97 }
98
99 public function setInnerContainerClass(string $a_containerclass): void
100 {
101 $this->icontainerclass = $a_containerclass;
102 }
103
104 public function getInnerContainerClass(): string
105 {
107 }
108
109 public function setHeaderClass(string $a_headerclass): void
110 {
111 $this->headerclass = $a_headerclass;
112 }
113
114 public function getHeaderClass(): string
115 {
116 return $this->headerclass;
117 }
118
119 public function setActiveHeaderClass(string $a_h_class): void
120 {
121 $this->active_headerclass = $a_h_class;
122 }
123
124 public function getActiveHeaderClass(): string
125 {
127 }
128
129 public function setContentClass(string $a_contentclass): void
130 {
131 $this->contentclass = $a_contentclass;
132 }
133
134 public function getContentClass(): string
135 {
136 return $this->contentclass;
137 }
138
139 public function setContentWidth(?int $a_contentwidth): void
140 {
141 $this->contentwidth = $a_contentwidth;
142 }
143
144 public function getContentWidth(): ?int
145 {
146 return $this->contentwidth;
147 }
148
149 public function setContentHeight(?int $a_contentheight): void
150 {
151 $this->contentheight = $a_contentheight;
152 }
153
154 public function getContentHeight(): ?int
155 {
157 }
158
162 public function setBehaviour(string $a_val): void
163 {
164 $this->behaviour = $a_val;
165 }
166
167 public function getBehaviour(): string
168 {
169 return $this->behaviour;
170 }
171
172 public function setUseSessionStorage(bool $a_val): void
173 {
174 $this->use_session_storage = $a_val;
175 }
176
177 public function getUseSessionStorage(): bool
178 {
180 }
181
182 public function setAllowMultiOpened(bool $a_val): void
183 {
184 $this->allow_multi_opened = $a_val;
185 }
186
187 public function getAllowMultiOpened(): bool
188 {
190 }
191
195 public function setShowAllElement(string $a_val): void
196 {
197 $this->show_all_element = $a_val;
198 }
199
200 public function getShowAllElement(): string
201 {
203 }
204
208 public function setHideAllElement(string $a_val): void
209 {
210 $this->hide_all_element = $a_val;
211 }
212
213 public function getHideAllElement(): string
214 {
216 }
217
221 public static function addJavaScript(?ilGlobalTemplate $main_tpl = null): void
222 {
223 global $DIC;
224
225 if ($main_tpl != null) {
226 $tpl = $main_tpl;
227 } else {
228 $tpl = $DIC["tpl"];
229 }
230
232
234
235 foreach (self::getLocalJavascriptFiles() as $f) {
236 $tpl->addJavaScript($f, true, 3);
237 }
238 }
239
243 public static function addCss(): void
244 {
245 global $DIC;
246
247 $tpl = $DIC["tpl"];
248
249 foreach (self::getLocalCssFiles() as $f) {
250 $tpl->addCss($f);
251 }
252 }
253
254 public static function getLocalJavascriptFiles(): array
255 {
256 $debug = false;
257 if ($debug) {
258 return array(
259 "../components/ILIAS/Accordion/resources/accordion.js"
260 );
261 }
262 return array(
263 "assets/js/accordion.js",
264 "assets/js" . self::$owl_js_path
265 );
266 }
267
268 public static function getLocalCssFiles(): array
269 {
270 return array(
271 "./components/ILIAS/Accordion/css/accordion.css",
272 self::$owl_path . self::$owl_css_path
273 );
274 }
275
276 public function addItem(
277 string $a_header,
278 string $a_content,
279 bool $a_force_open = false
280 ): void {
281 $this->items[] = array("header" => $a_header,
282 "content" => $a_content, "force_open" => $a_force_open);
283
284 if ($a_force_open) {
285 $this->force_open[] = sizeof($this->items);
286 }
287 }
288
289 public function getItems(): array
290 {
291 return $this->items;
292 }
293
294 public function getHTML(bool $async = false): string
295 {
296 $ilUser = $this->user;
297
298 self::$accordion_cnt++;
299
300 $or_short = ($this->getOrientation() == ilAccordionGUI::HORIZONTAL)
301 ? "H"
302 : "V";
303
304 $width = (int) $this->getContentWidth();
305 $height = (int) $this->getContentHeight();
306 if ($this->getOrientation() == ilAccordionGUI::HORIZONTAL) {
307 if ($width == 0) {
308 $width = 200;
309 }
310 if ($height == 0) {
311 $height = 100;
312 }
313 }
314
315 $this->addJavascript();
316 $this->addCss();
317
318 $tpl = new ilTemplate("tpl.accordion.html", true, true, "components/ILIAS/Accordion");
319 foreach ($this->getItems() as $item) {
320 $tpl->setCurrentBlock("item");
321 $tpl->setVariable("HEADER", $item["header"]);
322 $tpl->setVariable("CONTENT", $item["content"]);
323 $tpl->setVariable("HEADER_CLASS", $this->getHeaderClass()
324 ? $this->getHeaderClass() : "il_" . $or_short . "AccordionHead");
325 $tpl->setVariable("CONTENT_CLASS", $this->getContentClass()
326 ? $this->getContentClass() : "il_" . $or_short . "AccordionContent");
327
328 if ($this->getBehaviour() != self::FORCE_ALL_OPEN) {
329 $tpl->setVariable("HIDE_CONTENT_CLASS", "ilAccHideContent");
330 }
331
332 $tpl->setVariable("OR_SHORT", $or_short);
333
334 $tpl->setVariable("INNER_CONTAINER_CLASS", $this->getInnerContainerClass()
335 ? $this->getInnerContainerClass() : "il_" . $or_short . "AccordionInnerContainer");
336
337
338 if ($height > 0) {
339 $tpl->setVariable("HEIGHT", "height:" . $height . "px;");
340 }
341 if ($height > 0 && $this->getOrientation() == ilAccordionGUI::HORIZONTAL) {
342 $tpl->setVariable("HHEIGHT", "height:" . $height . "px;");
343 }
344 $tpl->parseCurrentBlock();
345 }
346
347 $tpl->setVariable("CONTAINER_CLASS", $this->getContainerClass()
348 ? $this->getContainerClass() : "il_" . $or_short . "AccordionContainer");
349
350 $options["orientation"] = $this->getOrientation();
351 $options["int_id"] = $this->getId();
352
353 if ($this->getUseSessionStorage() && $this->getId() != "") {
355
356 $ctab = $stor->getProperty(
357 $this->getId(),
358 $ilUser->getId(),
359 "opened"
360 );
361 $ctab_arr = explode(";", $ctab);
362
363 foreach ($this->force_open as $fo) {
364 if (!in_array($fo, $ctab_arr)) {
365 $ctab_arr[] = $fo;
366 }
367 }
368 $ctab = implode(";", $ctab_arr);
369
370 if ($ctab == "0") {
371 $ctab = "";
372 }
373
374 $options["initial_opened"] = $ctab;
375 $options["save_url"] = "./ilias.php?baseClass=ilaccordionpropertiesstoragegui&cmd=setOpenedTab" .
376 "&accordion_id=" . $this->getId() . "&user_id=" . $ilUser->getId();
377 }
378
379 $options["behaviour"] = $this->getBehaviour();
380 if ($this->getOrientation() == ilAccordionGUI::HORIZONTAL) {
381 $options["toggle_class"] = 'il_HAccordionToggleDef';
382 $options["toggle_act_class"] = 'il_HAccordionToggleActiveDef';
383 $options["content_class"] = 'il_HAccordionContentDef';
384 } else {
385 $options["toggle_class"] = 'il_VAccordionToggleDef';
386 $options["toggle_act_class"] = 'il_VAccordionToggleActiveDef';
387 $options["content_class"] = 'il_VAccordionContentDef';
388 }
389
390
391 if ($width > 0) {
392 $options["width"] = $width;
393 } else {
394 $options["width"] = null;
395 }
396 if ($width > 0 && $this->getOrientation() == ilAccordionGUI::VERTICAL) {
397 $tpl->setVariable("CWIDTH", 'style="width:' . $width . 'px;"');
398 }
399
400 if ($this->head_class_set) {
401 $options["active_head_class"] = $this->getActiveHeaderClass();
402 } else {
403 if ($this->getOrientation() == ilAccordionGUI::VERTICAL) {
404 $options["active_head_class"] = "il_HAccordionHeadActive";
405 } else {
406 $options["active_head_class"] = "il_VAccordionHeadActive";
407 }
408 }
409
410 $options["height"] = null;
411 $options["id"] = 'accordion_' . $this->getId() . '_' . self::$accordion_cnt;
412 $options["multi"] = (bool) $this->getAllowMultiOpened();
413 $options["show_all_element"] = $this->getShowAllElement();
414 $options["hide_all_element"] = $this->getHideAllElement();
415
416 $tpl->setVariable("ACC_ID", $options["id"]);
417
418 $html = $tpl->get();
419 $code = $this->getOnloadCode($options);
420 if (!$async) {
421 $this->main_tpl->addOnLoadCode($code);
422 } else {
423 $html .= "<script>$code</script>";
424 }
425 return $html;
426 }
427
428 protected function getOnloadCode(array $options): string
429 {
430 return 'il.Accordion.add(' . json_encode($options, JSON_THROW_ON_ERROR) . ');';
431 }
432}
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)
static string $owl_css_path
addItem(string $a_header, string $a_content, bool $a_force_open=false)
getHTML(bool $async=false)
setInnerContainerClass(string $a_containerclass)
static string $owl_js_path
setAllowMultiOpened(bool $a_val)
setHideAllElement(string $a_val)
setBehaviour(string $a_val)
Set behaviour "ForceAllOpen" | "FirstOpen" | "AllClosed".
static string $owl_path
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
static initConnection(?ilGlobalTemplateInterface $a_main_tpl=null)
Init YUI Connection module.
static initjQueryUI(?ilGlobalTemplateInterface $a_tpl=null)
inits and adds the jQuery-UI JS-File to the global template (see included_components....
global $DIC
Definition: shib_login.php:26