• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilDOMUtil.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00032 class ilDOMUtil
00033 {
00034 
00041         function setFirstOptionalElement(&$doc, &$parent_node, $a_node_name, $a_successors,
00042                 $a_content, $a_attributes, $a_remove_childs = true)
00043         {
00044                 $search = $a_successors;
00045                 $search[] = $a_node_name;
00046 
00047                 $childs = $parent_node->child_nodes();
00048                 $cnt_childs = count($childs);
00049                 $found = false;
00050 //echo "A";
00051                 foreach($childs as $child)
00052                 {
00053                         $child_name = $child->node_name();
00054 //echo "B$child_name";
00055                         if (in_array($child_name, $search))
00056                         {
00057 //echo "C";
00058                                 $found = true;
00059                                 break;
00060                         }
00061                 }
00062                 // didn't find element
00063                 if(!$found)
00064                 {
00065                         $new_node =& $doc->create_element($a_node_name);
00066                         $new_node =& $parent_node->append_child($new_node);
00067                         if ($a_content != "")
00068                         {
00069                                 $new_node->set_content($a_content);
00070                         }
00071                         ilDOMUtil::set_attributes($new_node, $a_attributes);
00072                 }
00073                 else
00074                 {
00075                         if ($child_name == $a_node_name)
00076                         {
00077                                 if ($a_remove_childs)
00078                                 {
00079                                         $childs2 = $child->child_nodes();
00080                                         for($i=0; $i<count($childs2); $i++)
00081                                         {
00082                                                 $child->remove_child($childs2[$i]);
00083                                         }
00084                                 }
00085                                 if ($a_content != "")
00086                                 {
00087                                         $child->set_content($a_content);
00088                                 }
00089                                 ilDOMUtil::set_attributes($child, $a_attributes);
00090                         }
00091                         else
00092                         {
00093                                 $new_node =& $doc->create_element($a_node_name);
00094                                 $new_node =& $child->insert_before($new_node, $child);
00095                                 if ($a_content != "")
00096                                 {
00097                                         $new_node->set_content($a_content);
00098                                 }
00099                                 ilDOMUtil::set_attributes($new_node, $a_attributes);
00100                         }
00101                 }
00102         }
00103 
00110         function set_attributes(&$a_node, $a_attributes)
00111         {
00112                 foreach ($a_attributes as $attribute => $value)
00113                 {
00114                         if ($value != "")
00115                         {
00116                                 $a_node->set_attribute($attribute, $value);
00117                         }
00118                         else
00119                         {
00120                                 if ($a_node->has_attribute($attibute))
00121                                 {
00122                                         $a_node->remove_attribute($attribute);
00123                                 }
00124                         }
00125                 }
00126         }
00127 
00131         function deleteAllChildsByName(&$a_parent, $a_node_names)
00132         {
00133                 $childs = $a_parent->child_nodes();
00134                 foreach($childs as $child)
00135                 {
00136                         $child_name = $child->node_name();
00137                         if (in_array($child_name, $a_node_names))
00138                         {
00139                                 $child->unlink_node();
00140                         }
00141                 }
00142         }
00143 
00144 } // END class.ilDOMUtil
00145 ?>

Generated on Fri Dec 13 2013 09:06:33 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1