ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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 = "./libs/bower/bower_components/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  {
96  return $this->containerclass;
97  }
98 
99  public function setInnerContainerClass(string $a_containerclass): void
100  {
101  $this->icontainerclass = $a_containerclass;
102  }
103 
104  public function getInnerContainerClass(): string
105  {
106  return $this->icontainerclass;
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  {
156  return $this->contentheight;
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  return array(
257  "./Services/Accordion/js/accordion.js",
258  self::$owl_path . self::$owl_js_path
259  );
260  }
261 
262  public static function getLocalCssFiles(): array
263  {
264  return array(
265  "./Services/Accordion/css/accordion.css",
266  self::$owl_path . self::$owl_css_path
267  );
268  }
269 
270  public function addItem(
271  string $a_header,
272  string $a_content,
273  bool $a_force_open = false
274  ): void {
275  $this->items[] = array("header" => $a_header,
276  "content" => $a_content, "force_open" => $a_force_open);
277 
278  if ($a_force_open) {
279  $this->force_open[] = sizeof($this->items);
280  }
281  }
282 
283  public function getItems(): array
284  {
285  return $this->items;
286  }
287 
288  public function getHTML(bool $async = false): string
289  {
291 
292  self::$accordion_cnt++;
293 
294  $or_short = ($this->getOrientation() == ilAccordionGUI::HORIZONTAL)
295  ? "H"
296  : "V";
297 
298  $width = (int) $this->getContentWidth();
299  $height = (int) $this->getContentHeight();
300  if ($this->getOrientation() == ilAccordionGUI::HORIZONTAL) {
301  if ($width == 0) {
302  $width = 200;
303  }
304  if ($height == 0) {
305  $height = 100;
306  }
307  }
308 
309  $this->addJavascript();
310  $this->addCss();
311 
312  $tpl = new ilTemplate("tpl.accordion.html", true, true, "Services/Accordion");
313  foreach ($this->getItems() as $item) {
314  $tpl->setCurrentBlock("item");
315  $tpl->setVariable("HEADER", $item["header"]);
316  $tpl->setVariable("CONTENT", $item["content"]);
317  $tpl->setVariable("HEADER_CLASS", $this->getHeaderClass()
318  ? $this->getHeaderClass() : "il_" . $or_short . "AccordionHead");
319  $tpl->setVariable("CONTENT_CLASS", $this->getContentClass()
320  ? $this->getContentClass() : "il_" . $or_short . "AccordionContent");
321 
322  if ($this->getBehaviour() != self::FORCE_ALL_OPEN) {
323  $tpl->setVariable("HIDE_CONTENT_CLASS", "ilAccHideContent");
324  }
325 
326  $tpl->setVariable("OR_SHORT", $or_short);
327 
328  $tpl->setVariable("INNER_CONTAINER_CLASS", $this->getInnerContainerClass()
329  ? $this->getInnerContainerClass() : "il_" . $or_short . "AccordionInnerContainer");
330 
331 
332  if ($height > 0) {
333  $tpl->setVariable("HEIGHT", "height:" . $height . "px;");
334  }
335  if ($height > 0 && $this->getOrientation() == ilAccordionGUI::HORIZONTAL) {
336  $tpl->setVariable("HHEIGHT", "height:" . $height . "px;");
337  }
338  $tpl->parseCurrentBlock();
339  }
340 
341  $tpl->setVariable("CONTAINER_CLASS", $this->getContainerClass()
342  ? $this->getContainerClass() : "il_" . $or_short . "AccordionContainer");
343 
344  $options["orientation"] = $this->getOrientation();
345  $options["int_id"] = $this->getId();
346 
347  if ($this->getUseSessionStorage() && $this->getId() != "") {
348  $stor = new ilAccordionPropertiesStorageGUI();
349 
350  $ctab = $stor->getProperty(
351  $this->getId(),
352  $ilUser->getId(),
353  "opened"
354  );
355  $ctab_arr = explode(";", $ctab);
356 
357  foreach ($this->force_open as $fo) {
358  if (!in_array($fo, $ctab_arr)) {
359  $ctab_arr[] = $fo;
360  }
361  }
362  $ctab = implode(";", $ctab_arr);
363 
364  if ($ctab == "0") {
365  $ctab = "";
366  }
367 
368  $options["initial_opened"] = $ctab;
369  $options["save_url"] = "./ilias.php?baseClass=ilaccordionpropertiesstoragegui&cmd=setOpenedTab" .
370  "&accordion_id=" . $this->getId() . "&user_id=" . $ilUser->getId();
371  }
372 
373  $options["behaviour"] = $this->getBehaviour();
374  if ($this->getOrientation() == ilAccordionGUI::HORIZONTAL) {
375  $options["toggle_class"] = 'il_HAccordionToggleDef';
376  $options["toggle_act_class"] = 'il_HAccordionToggleActiveDef';
377  $options["content_class"] = 'il_HAccordionContentDef';
378  } else {
379  $options["toggle_class"] = 'il_VAccordionToggleDef';
380  $options["toggle_act_class"] = 'il_VAccordionToggleActiveDef';
381  $options["content_class"] = 'il_VAccordionContentDef';
382  }
383 
384 
385  if ($width > 0) {
386  $options["width"] = $width;
387  } else {
388  $options["width"] = null;
389  }
390  if ($width > 0 && $this->getOrientation() == ilAccordionGUI::VERTICAL) {
391  $tpl->setVariable("CWIDTH", 'style="width:' . $width . 'px;"');
392  }
393 
394  if ($this->head_class_set) {
395  $options["active_head_class"] = $this->getActiveHeaderClass();
396  } else {
397  if ($this->getOrientation() == ilAccordionGUI::VERTICAL) {
398  $options["active_head_class"] = "il_HAccordionHeadActive";
399  } else {
400  $options["active_head_class"] = "il_VAccordionHeadActive";
401  }
402  }
403 
404  $options["height"] = null;
405  $options["id"] = 'accordion_' . $this->getId() . '_' . self::$accordion_cnt;
406  $options["multi"] = (bool) $this->getAllowMultiOpened();
407  $options["show_all_element"] = $this->getShowAllElement();
408  $options["hide_all_element"] = $this->getHideAllElement();
409 
410  $tpl->setVariable("ACC_ID", $options["id"]);
411 
412  $html = $tpl->get();
413  $code = $this->getOnloadCode($options);
414  if (!$async) {
415  $this->main_tpl->addOnLoadCode($code);
416  } else {
417  $html .= "<script>$code</script>";
418  }
419  return $html;
420  }
421 
422  protected function getOnloadCode(array $options): string
423  {
424  return 'il.Accordion.add(' . json_encode($options, JSON_THROW_ON_ERROR) . ');';
425  }
426 }
getHTML(bool $async=false)
addItem(string $a_header, string $a_content, bool $a_force_open=false)
setId(string $a_val)
setBehaviour(string $a_val)
Set behaviour "ForceAllOpen" | "FirstOpen" | "AllClosed".
special template class to simplify handling of ITX/PEAR
getOnloadCode(array $options)
static string $owl_css_path
setHideAllElement(string $a_val)
setContentClass(string $a_contentclass)
static getLocalJavascriptFiles()
setOrientation(string $a_orientation)
global $DIC
Definition: feed.php:28
static string $owl_path
static addJavaScript(ilGlobalTemplate $main_tpl=null)
Add javascript files that are necessary to run accordion.
setContentHeight(?int $a_contentheight)
setContainerClass(string $a_containerclass)
static addCss()
Add required css.
ilGlobalTemplateInterface $main_tpl
static initjQueryUI(ilGlobalTemplateInterface $a_tpl=null)
inits and adds the jQuery-UI JS-File to the global template (see included_components.txt for included components)
setShowAllElement(string $a_val)
setContentWidth(?int $a_contentwidth)
setAllowMultiOpened(bool $a_val)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ilUser
Definition: imgupload.php:34
static string $owl_js_path
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
setInnerContainerClass(string $a_containerclass)
setUseSessionStorage(bool $a_val)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setHeaderClass(string $a_headerclass)
static initConnection(?ilGlobalTemplateInterface $a_main_tpl=null)
Init YUI Connection module.
setActiveHeaderClass(string $a_h_class)