ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilDOMUtil.php
Go to the documentation of this file.
1 <?php
2 
24 class ilDOMUtil
25 {
32  public static function setFirstOptionalElement(
33  php4DOMDocument $doc,
34  php4DOMElement $parent_node,
35  string $a_node_name,
36  array $a_successors,
37  string $a_content,
38  array $a_attributes,
39  bool $a_remove_childs = true
40  ): void {
41  $search = $a_successors;
42  $search[] = $a_node_name;
43  $child_name = "";
44  $child = null;
45 
46  $childs = $parent_node->child_nodes();
47  $found = false;
48  foreach ($childs as $child) {
49  $child_name = $child->node_name();
50  //echo "B$child_name";
51  if (in_array($child_name, $search)) {
52  //echo "C";
53  $found = true;
54  break;
55  }
56  }
57  // didn't find element
58  if (!$found) {
59  $new_node = $doc->create_element($a_node_name);
60  $new_node = $parent_node->append_child($new_node);
61  if ($a_content != "") {
62  $new_node->set_content($a_content);
63  }
64  ilDOMUtil::set_attributes($new_node, $a_attributes);
65  } else {
66  if ($child_name == $a_node_name) {
67  if ($a_remove_childs) {
68  $childs2 = $child->child_nodes();
69  for ($i = 0; $i < count($childs2); $i++) {
70  $child->remove_child($childs2[$i]);
71  }
72  }
73  if ($a_content != "") {
74  $child->set_content($a_content);
75  }
76  ilDOMUtil::set_attributes($child, $a_attributes);
77  } else {
78  $new_node = $doc->create_element($a_node_name);
79  $new_node = $child->insert_before($new_node, $child);
80  if ($a_content != "") {
81  $new_node->set_content($a_content);
82  }
83  ilDOMUtil::set_attributes($new_node, $a_attributes);
84  }
85  }
86  }
87 
92  public static function set_attributes(
93  php4DOMElement $a_node,
94  array $a_attributes
95  ): void {
96  foreach ($a_attributes as $attribute => $value) {
97  if ($value != "") {
98  $a_node->set_attribute($attribute, $value);
99  } else {
100  if ($a_node->has_attribute($attribute)) {
101  $a_node->remove_attribute($attribute);
102  }
103  }
104  }
105  }
106 
110  public static function deleteAllChildsByName(
111  php4DOMElement $a_parent,
112  array $a_node_names
113  ): void {
114  $childs = $a_parent->child_nodes();
115  foreach ($childs as $child) {
116  $child_name = $child->node_name();
117  if (in_array($child_name, $a_node_names)) {
118  $child->unlink_node();
119  }
120  }
121  }
122 
128  public static function addElementToList(
129  php4DOMDocument $doc,
130  php4DOMElement $parent_node,
131  string $a_node_name,
132  array $a_successors,
133  string $a_content,
134  array $a_attributes
135  ): php4DOMElement {
136  $search = $a_successors;
137  $child = null;
138  $childs = $parent_node->child_nodes();
139  $cnt_childs = count($childs);
140  $found = false;
141  foreach ($childs as $child) {
142  $child_name = $child->node_name();
143  if (in_array($child_name, $search)) {
144  $found = true;
145  break;
146  }
147  }
148  // didn't successors -> append at the end
149  $new_node = $doc->create_element($a_node_name);
150  if (!$found) {
151  $new_node = $parent_node->append_child($new_node);
152  } else {
153  $new_node = $child->insert_before($new_node, $child);
154  }
155  if ($a_content != "") {
156  $new_node->set_content($a_content);
157  }
158  ilDOMUtil::set_attributes($new_node, $a_attributes);
159 
160  return $new_node;
161  }
162 } // END class.ilDOMUtil
create_element(string $name)
static setFirstOptionalElement(php4DOMDocument $doc, php4DOMElement $parent_node, string $a_node_name, array $a_successors, string $a_content, array $a_attributes, bool $a_remove_childs=true)
searches for an element $a_node_name within the childs of $parent_node if no node is found...
append_child($newnode)
static addElementToList(php4DOMDocument $doc, php4DOMElement $parent_node, string $a_node_name, array $a_successors, string $a_content, array $a_attributes)
Places a new node $a_node_name directly before nodes with names of $a_successors. ...
set_attribute($name, $value)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static deleteAllChildsByName(php4DOMElement $a_parent, array $a_node_names)
delete all childs of a node by names in $a_node_names
php4DomElement
$i
Definition: metadata.php:41
static set_attributes(php4DOMElement $a_node, array $a_attributes)
set attributes of a node