ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPCTabs.php
Go to the documentation of this file.
1 <?php
2 
24 class ilPCTabs extends ilPageContent
25 {
26  public const ACCORDION_HOR = "HorizontalAccordion";
27  public const ACCORDION_VER = "VerticalAccordion";
28  public const CAROUSEL = "Carousel";
29 
30  public function init(): void
31  {
32  $this->setType("tabs");
33  }
34 
35  public function create(
36  ilPageObject $a_pg_obj,
37  string $a_hier_id,
38  string $a_pc_id = ""
39  ): void {
40  $this->createInitialChildNode($a_hier_id, $a_pc_id, "Tabs");
41  }
42 
43  protected function setTabsAttribute(
44  string $a_attr,
45  string $a_value
46  ): void {
47  $this->dom_util->setAttribute($this->getChildNode(), $a_attr, $a_value);
48  }
49 
53  public function setTabType(
54  string $a_type = "HorizontalTabs"
55  ): void {
56  switch ($a_type) {
59  case ilPCTabs::CAROUSEL:
60  $this->setTabsAttribute("Type", $a_type);
61  break;
62  }
63  }
64 
65  public function getTabType(): string
66  {
67  return $this->getChildNode()->getAttribute("Type");
68  }
69 
70  public function setContentWidth(string $a_val): void
71  {
72  $this->setTabsAttribute("ContentWidth", $a_val);
73  }
74 
75  public function getContentWidth(): string
76  {
77  return $this->getChildNode()->getAttribute("ContentWidth");
78  }
79 
80  public function setContentHeight(string $a_val): void
81  {
82  $this->setTabsAttribute("ContentHeight", $a_val);
83  }
84 
85  public function getContentHeight(): string
86  {
87  return $this->getChildNode()->getAttribute("ContentHeight");
88  }
89 
90  public function setHorizontalAlign(string $a_val): void
91  {
92  $this->setTabsAttribute("HorizontalAlign", $a_val);
93  }
94 
95  public function getHorizontalAlign(): string
96  {
97  return $this->getChildNode()->getAttribute("HorizontalAlign");
98  }
99 
100  public function setBehavior(string $a_val): void
101  {
102  $this->setTabsAttribute("Behavior", $a_val);
103  }
104 
105  public function getBehavior(): string
106  {
107  return $this->getChildNode()->getAttribute("Behavior");
108  }
109 
110  public function getCaptions(): array
111  {
112  $captions = array();
113  $k = 0;
114  foreach ($this->getChildNode()->childNodes as $child) {
115  if ($child->nodeName == "Tab") {
116  $pc_id = $child->getAttribute("PCID");
117  $hier_id = $child->getAttribute("HierId");
118  $current_caption = "";
119  foreach ($child->childNodes as $tab_child) {
120  if ($tab_child->nodeName == "TabCaption") {
121  $current_caption = $this->dom_util->getContent($tab_child);
122  }
123  }
124  $captions[] = array("pos" => $k,
125  "caption" => $current_caption, "pc_id" => $pc_id, "hier_id" => $hier_id);
126  $k++;
127  }
128  }
129 
130  return $captions;
131  }
132 
133  public function getCaption(
134  string $a_hier_id,
135  string $a_pc_id
136  ): string {
137  foreach ($this->getCaptions() as $cap) {
138  if ($cap["pc_id"] === $a_pc_id && $cap["hier_id"] === $a_hier_id) {
139  return $cap["caption"];
140  }
141  }
142  return "";
143  }
144 
148  public function savePositions(
149  array $a_pos
150  ): void {
151  asort($a_pos);
152 
153  // File Item
154  $nodes = array();
155  foreach ($this->getChildNode()->childNodes as $child) {
156  if ($child->nodeName == "Tab") {
157  $pc_id = $child->getAttribute("PCID");
158  $hier_id = $child->getAttribute("HierId");
159  $nodes[$hier_id . ":" . $pc_id] = $child;
160  }
161  }
162  $this->dom_util->deleteAllChildsByName($this->getChildNode(), ["Tab"]);
163 
164  foreach ($a_pos as $k => $v) {
165  if (is_object($nodes[$k])) {
166  $nodes[$k] = $this->getChildNode()->appendChild($nodes[$k]);
167  }
168  }
169  }
170 
171  public function saveCaptions(array $a_captions): void
172  {
173  // iterate all tab nodes
174  foreach ($this->getChildNode()->childNodes as $child) {
175  if ($child->nodeName == "Tab") {
176  $pc_id = $child->getAttribute("PCID");
177  $hier_id = $child->getAttribute("HierId");
178  $k = $hier_id . ":" . $pc_id;
179  // if caption given, set it, otherwise delete caption subitem
180  if ($a_captions[$k] != "") {
181  $this->dom_util->setFirstOptionalElement(
182  $child,
183  "TabCaption",
184  array(),
185  $a_captions[$k],
186  array()
187  );
188  } else {
189  $this->dom_util->deleteAllChildsByName($child, array("TabCaption"));
190  }
191  }
192  }
193  }
194 
195  public function deleteTab(
196  string $a_hier_id,
197  string $a_pc_id
198  ): void {
199  // File Item
200  foreach ($this->getChildNode()->childNodes as $child) {
201  if ($child->nodeName == "Tab") {
202  if ($a_pc_id == $child->getAttribute("PCID") &&
203  $a_hier_id == $child->getAttribute("HierId")) {
204  $child->parentNode->removeChild($child);
205  }
206  }
207  }
208  }
209 
210  public function addTab(string $a_caption): void
211  {
212  $new_item = $this->dom_doc->createElement("Tab");
213  $new_item = $this->getChildNode()->appendChild($new_item);
214  $this->dom_util->setFirstOptionalElement(
215  $new_item,
216  "TabCaption",
217  array(),
218  $a_caption,
219  array()
220  );
221  }
222 
223  public function setTemplate(string $a_template): void
224  {
225  $this->setTabsAttribute("Template", $a_template);
226  }
227 
228  public function getTemplate(): string
229  {
230  return $this->getChildNode()->getAttribute("Template");
231  }
232 
233  public static function getLangVars(): array
234  {
235  return array("pc_vacc", "pc_hacc", "pc_carousel");
236  }
237 
238  public function setAutoTime(?int $a_val): void
239  {
240  $this->setTabsAttribute("AutoAnimWait", (string) $a_val);
241  }
242 
243  public function getAutoTime(): ?int
244  {
245  $val = $this->getChildNode()->getAttribute("AutoAnimWait");
246  if ($val) {
247  return (int) $val;
248  }
249  return null;
250  }
251 
252  public function setRandomStart(bool $a_val): void
253  {
254  $this->setTabsAttribute("RandomStart", $a_val);
255  }
256 
257  public function getRandomStart(): bool
258  {
259  return (bool) $this->getChildNode()->getAttribute("RandomStart");
260  }
261 
262  public function getJavascriptFiles(string $a_mode): array
263  {
265  }
266 
267  public function getCssFiles(string $a_mode): array
268  {
270  }
271 
272  public function saveCaption(string $pc_id, string $caption): void
273  {
274  $tab_nodes = $this->getChildNode()->childNodes;
275  foreach ($tab_nodes as $tab_node) {
276  if ($tab_node->nodeName === "Tab") {
277  $current_pc_id = $tab_node->getAttribute("PCID");
278  if ($current_pc_id === $pc_id) {
279  if ($caption !== "") {
280  $this->dom_util->setFirstOptionalElement(
281  $tab_node,
282  "TabCaption",
283  [],
284  $caption,
285  []
286  );
287  } else {
288  $this->dom_util->deleteAllChildsByName($tab_node, ["TabCaption"]);
289  }
290  }
291  }
292  }
293  }
294 
295  public function addAbove(string $pc_id, string $caption = ""): void
296  {
297  $dom = $this->getPage()->getDomDoc();
298  $new_tab = $dom->createElement("Tab");
299  $tab = $this->getPage()->getDomNodeForPCId($pc_id);
300  if (!is_null($tab)) {
301  $new_tab = $tab->parentNode->insertBefore($new_tab, $tab);
302  if ($caption !== "") {
303  $dom_util = $this->domain->domUtil();
304  $dom_util->setFirstOptionalElement(
305  $new_tab,
306  "TabCaption",
307  array(),
308  $caption,
309  array()
310  );
311  }
312  }
313  }
314 
315  public function addBelow(string $pc_id, string $caption = ""): void
316  {
317  $dom = $this->getPage()->getDomDoc();
318  $new_tab = $dom->createElement("Tab");
319  $tab = $this->getPage()->getDomNodeForPCId($pc_id);
320  if (!is_null($tab)) {
321  if ($next = $tab->nextSibling) {
322  $new_tab = $next->parentNode->insertBefore($new_tab, $next);
323  } else {
324  $new_tab = $tab->parentNode->appendChild($new_tab);
325  }
326 
327  if ($caption !== "") {
328  $dom_util = $this->domain->domUtil();
329  $dom_util->setFirstOptionalElement(
330  $new_tab,
331  "TabCaption",
332  array(),
333  $caption,
334  array()
335  );
336  }
337  }
338  }
339 
340  public function moveUp(string $pc_id): void
341  {
342  $dom = $this->getPage()->getDomDoc();
343  $new_tab = $dom->createElement("Tab");
344  $tab = $this->getPage()->getDomNodeForPCId($pc_id);
345  if (!is_null($tab)) {
346  $prev = $tab->previousSibling;
347  if ($prev) {
348  $tab->parentNode->removeChild($tab);
349  $tab = $prev->parentNode->insertBefore($tab, $prev);
350  }
351  }
352  }
353 
354  public function moveDown(string $pc_id): void
355  {
356  $dom = $this->getPage()->getDomDoc();
357  $new_tab = $dom->createElement("Tab");
358  $tab = $this->getPage()->getDomNodeForPCId($pc_id);
359  if (!is_null($tab)) {
360  $next = $tab->nextSibling;
361  if ($next) {
362  $next2 = $next->nextSibling;
363  $tab->parentNode->removeChild($tab);
364  if ($next2) {
365  $tab = $next2->parentNode->insertBefore($tab, $next2);
366  } else {
367  $tab = $next->parentNode->appendChild($tab);
368  }
369  }
370  }
371  }
372 
373  public function moveTop(string $pc_id): void
374  {
375  $dom = $this->getPage()->getDomDoc();
376  $new_tab = $dom->createElement("Tab");
377  $tab = $this->getPage()->getDomNodeForPCId($pc_id);
378  if (!is_null($tab)) {
379  $prev = $tab->previousSibling;
380  if ($prev) {
381  $tab->parentNode->removeChild($tab);
382  $first = $prev->parentNode->childNodes->item(0);
383  $tab = $prev->parentNode->insertBefore($tab, $first);
384  }
385  }
386  }
387 
388  public function moveBottom(string $pc_id): void
389  {
390  $dom = $this->getPage()->getDomDoc();
391  $new_tab = $dom->createElement("Tab");
392  $tab = $this->getPage()->getDomNodeForPCId($pc_id);
393  if (!is_null($tab)) {
394  $next = $tab->nextSibling;
395  if ($next) {
396  $tab->parentNode->removeChild($tab);
397  $tab = $next->parentNode->appendChild($tab);
398  }
399  }
400  }
401 
402  public function deletePanel(string $pc_id): void
403  {
404  $dom = $this->getPage()->getDomDoc();
405  $new_tab = $dom->createElement("Tab");
406  $tab = $this->getPage()->getDomNodeForPCId($pc_id);
407  if (!is_null($tab)) {
408  $tab->parentNode->removeChild($tab);
409  }
410  }
411 
412  public function getNodeXml(string $pc_id): string
413  {
414  $tab = $this->getPage()->getDomNodeForPCId($pc_id);
415  if (!is_null($tab)) {
416  $xml = "";
417  foreach ($tab->childNodes as $node) {
418  if ($node->nodeName === "PageContent") {
419  $xml .= $node->ownerDocument->saveXml($node);
420  }
421  }
422  return $xml;
423  }
424  return "";
425  }
426 
427 }
create(ilPageObject $a_pg_obj, string $a_hier_id, string $a_pc_id="")
setTabType(string $a_type="HorizontalTabs")
savePositions(array $a_pos)
Save positions of tabs.
saveCaption(string $pc_id, string $caption)
setType(string $a_type)
Set Type.
addAbove(string $pc_id, string $caption="")
getJavascriptFiles(string $a_mode)
static getLangVars()
getCssFiles(string $a_mode)
setContentHeight(string $a_val)
setRandomStart(bool $a_val)
setTemplate(string $a_template)
setBehavior(string $a_val)
setContentWidth(string $a_val)
deleteTab(string $a_hier_id, string $a_pc_id)
Content object of ilPageObject (see ILIAS DTD).
moveBottom(string $pc_id)
moveUp(string $pc_id)
static getLocalJavascriptFiles()
const ACCORDION_HOR
getCaption(string $a_hier_id, string $a_pc_id)
moveTop(string $pc_id)
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
setTabsAttribute(string $a_attr, string $a_value)
addBelow(string $pc_id, string $caption="")
const ACCORDION_VER
addTab(string $a_caption)
moveDown(string $pc_id)
setAutoTime(?int $a_val)
saveCaptions(array $a_captions)
getHorizontalAlign()
deletePanel(string $pc_id)
const CAROUSEL
ILIAS COPage Dom DomUtil $dom_util
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setHorizontalAlign(string $a_val)
getNodeXml(string $pc_id)
createInitialChildNode(string $hier_id, string $pc_id, string $child, array $child_attributes=[])