ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  function setFirstOptionalElement(&$doc, &$parent_node, $a_node_name, $a_successors,
43  $a_content, $a_attributes, $a_remove_childs = true)
44  {
45  $search = $a_successors;
46  $search[] = $a_node_name;
47 
48  $childs = $parent_node->child_nodes();
49  $cnt_childs = count($childs);
50  $found = false;
51 //echo "A";
52  foreach($childs as $child)
53  {
54  $child_name = $child->node_name();
55 //echo "B$child_name";
56  if (in_array($child_name, $search))
57  {
58 //echo "C";
59  $found = true;
60  break;
61  }
62  }
63  // didn't find element
64  if(!$found)
65  {
66  $new_node =& $doc->create_element($a_node_name);
67  $new_node =& $parent_node->append_child($new_node);
68  if ($a_content != "")
69  {
70  $new_node->set_content($a_content);
71  }
72  ilDOMUtil::set_attributes($new_node, $a_attributes);
73  }
74  else
75  {
76  if ($child_name == $a_node_name)
77  {
78  if ($a_remove_childs)
79  {
80  $childs2 = $child->child_nodes();
81  for($i=0; $i<count($childs2); $i++)
82  {
83  $child->remove_child($childs2[$i]);
84  }
85  }
86  if ($a_content != "")
87  {
88  $child->set_content($a_content);
89  }
90  ilDOMUtil::set_attributes($child, $a_attributes);
91  }
92  else
93  {
94  $new_node =& $doc->create_element($a_node_name);
95  $new_node =& $child->insert_before($new_node, $child);
96  if ($a_content != "")
97  {
98  $new_node->set_content($a_content);
99  }
100  ilDOMUtil::set_attributes($new_node, $a_attributes);
101  }
102  }
103  }
104 
111  function set_attributes(&$a_node, $a_attributes)
112  {
113  foreach ($a_attributes as $attribute => $value)
114  {
115  if ($value != "")
116  {
117  $a_node->set_attribute($attribute, $value);
118  }
119  else
120  {
121  if ($a_node->has_attribute($attibute))
122  {
123  $a_node->remove_attribute($attribute);
124  }
125  }
126  }
127  }
128 
132  function deleteAllChildsByName(&$a_parent, $a_node_names)
133  {
134  $childs = $a_parent->child_nodes();
135  foreach($childs as $child)
136  {
137  $child_name = $child->node_name();
138  if (in_array($child_name, $a_node_names))
139  {
140  $child->unlink_node();
141  }
142  }
143  }
144 
150  function addElementToList(&$doc, &$parent_node, $a_node_name, $a_successors,
151  $a_content, $a_attributes)
152  {
153  $search = $a_successors;
154 
155  $childs = $parent_node->child_nodes();
156  $cnt_childs = count($childs);
157  $found = false;
158  foreach($childs as $child)
159  {
160  $child_name = $child->node_name();
161  if (in_array($child_name, $search))
162  {
163  $found = true;
164  break;
165  }
166  }
167  // didn't successors -> append at the end
168  if(!$found)
169  {
170  $new_node = $doc->create_element($a_node_name);
171  $new_node = $parent_node->append_child($new_node);
172  if ($a_content != "")
173  {
174  $new_node->set_content($a_content);
175  }
176  ilDOMUtil::set_attributes($new_node, $a_attributes);
177  }
178  else
179  {
180  $new_node = $doc->create_element($a_node_name);
181  $new_node = $child->insert_before($new_node, $child);
182  if ($a_content != "")
183  {
184  $new_node->set_content($a_content);
185  }
186  ilDOMUtil::set_attributes($new_node, $a_attributes);
187  }
188 
189  return $new_node;
190  }
191 
192 
193 } // END class.ilDOMUtil
194 ?>