ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilDOMUtil.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
33 class ilDOMUtil
34 {
35 
42  public static function setFirstOptionalElement(
43  $doc,
44  $parent_node,
45  $a_node_name,
46  $a_successors,
47  $a_content,
48  $a_attributes,
49  $a_remove_childs = true
50  ) {
51  $search = $a_successors;
52  $search[] = $a_node_name;
53 
54  $childs = $parent_node->child_nodes();
55  $cnt_childs = count($childs);
56  $found = false;
57  //echo "A";
58  foreach ($childs as $child) {
59  $child_name = $child->node_name();
60  //echo "B$child_name";
61  if (in_array($child_name, $search)) {
62  //echo "C";
63  $found = true;
64  break;
65  }
66  }
67  // didn't find element
68  if (!$found) {
69  $new_node =&$doc->create_element($a_node_name);
70  $new_node =&$parent_node->append_child($new_node);
71  if ($a_content != "") {
72  $new_node->set_content($a_content);
73  }
74  ilDOMUtil::set_attributes($new_node, $a_attributes);
75  } else {
76  if ($child_name == $a_node_name) {
77  if ($a_remove_childs) {
78  $childs2 = $child->child_nodes();
79  for ($i=0; $i<count($childs2); $i++) {
80  $child->remove_child($childs2[$i]);
81  }
82  }
83  if ($a_content != "") {
84  $child->set_content($a_content);
85  }
86  ilDOMUtil::set_attributes($child, $a_attributes);
87  } else {
88  $new_node =&$doc->create_element($a_node_name);
89  $new_node =&$child->insert_before($new_node, $child);
90  if ($a_content != "") {
91  $new_node->set_content($a_content);
92  }
93  ilDOMUtil::set_attributes($new_node, $a_attributes);
94  }
95  }
96  }
97 
104  public static function set_attributes($a_node, $a_attributes)
105  {
106  foreach ($a_attributes as $attribute => $value) {
107  if ($value != "") {
108  $a_node->set_attribute($attribute, $value);
109  } else {
110  if ($a_node->has_attribute($attribute)) {
111  $a_node->remove_attribute($attribute);
112  }
113  }
114  }
115  }
116 
120  public static function deleteAllChildsByName($a_parent, $a_node_names)
121  {
122  $childs = $a_parent->child_nodes();
123  foreach ($childs as $child) {
124  $child_name = $child->node_name();
125  if (in_array($child_name, $a_node_names)) {
126  $child->unlink_node();
127  }
128  }
129  }
130 
136  public static function addElementToList(
137  $doc,
138  $parent_node,
139  $a_node_name,
140  $a_successors,
141  $a_content,
142  $a_attributes
143  ) {
144  $search = $a_successors;
145 
146  $childs = $parent_node->child_nodes();
147  $cnt_childs = count($childs);
148  $found = false;
149  foreach ($childs as $child) {
150  $child_name = $child->node_name();
151  if (in_array($child_name, $search)) {
152  $found = true;
153  break;
154  }
155  }
156  // didn't successors -> append at the end
157  if (!$found) {
158  $new_node = $doc->create_element($a_node_name);
159  $new_node = $parent_node->append_child($new_node);
160  if ($a_content != "") {
161  $new_node->set_content($a_content);
162  }
163  ilDOMUtil::set_attributes($new_node, $a_attributes);
164  } else {
165  $new_node = $doc->create_element($a_node_name);
166  $new_node = $child->insert_before($new_node, $child);
167  if ($a_content != "") {
168  $new_node->set_content($a_content);
169  }
170  ilDOMUtil::set_attributes($new_node, $a_attributes);
171  }
172 
173  return $new_node;
174  }
175 } // END class.ilDOMUtil
static set_attributes($a_node, $a_attributes)
set attributes of a node
class for DOM utilities
static deleteAllChildsByName($a_parent, $a_node_names)
delete all childs of a node by names in $a_node_names
$a_content
Definition: workflow.php:93
static addElementToList( $doc, $parent_node, $a_node_name, $a_successors, $a_content, $a_attributes)
Places a new node $a_node_name directly before nodes with names of $a_successors. ...
$i
Definition: disco.tpl.php:19
static setFirstOptionalElement( $doc, $parent_node, $a_node_name, $a_successors, $a_content, $a_attributes, $a_remove_childs=true)
searches for an element $a_node_name within the childs of $parent_node if no node is found...