Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00031 class ilXML2SQL
00032 {
00039 var $obj_id;
00040
00047 var $mapping;
00048
00055 var $ilias;
00056
00063 function ilXML2SQL ($a_xmltree,$a_lo_id)
00064 {
00065 global $ilias;
00066
00067 $this->ilias =& $ilias;
00068 $this->xmltree = $a_xmltree;
00069 $this->obj_id = $a_lo_id;
00070 }
00071
00072 function insertDocument ()
00073 {
00074
00075 foreach ($this->xmltree as $id => $node) {
00076 $node_id = $this->insertNode($node);
00077 $this->mapping[$id] = $node_id;
00078 }
00079
00080 foreach ($this->xmltree as $id => $node) {
00081 $this->xmltree[$id]["parent"] = $this->mapping[$node["parent"]];
00082 $this->xmltree[$id]["prev"] = $this->mapping[$node["prev"]];
00083 $this->xmltree[$id]["next"] = $this->mapping[$node["next"]];
00084 $this->xmltree[$id]["first"] = $this->mapping[$node["first"]];
00085 $this->xmltree[$id]["node"] = $this->mapping[$id];
00086 }
00087
00088 foreach ($this->xmltree as $id => $node) {
00089 $this->updateNode($node);
00090 $this->insertNodeData($node);
00091 }
00092
00093 return $this->xmltree;
00094 }
00095
00096 function insertNode ($a_node)
00097 {
00098 $q = "INSERT INTO lo_tree ".
00099 "(lo_id,lft,rgt,node_type_id,depth,struct) ".
00100 "VALUES ".
00101 "('".$this->obj_id."','".$a_node["left"].
00102 "','".$a_node["right"]."','".$a_node["type"].
00103 "','".$a_node["depth"]."','".$a_node["struct"]."') ";
00104 $this->ilias->db->query($q);
00105
00106 return $this->getLastInsertId();
00107 }
00108
00109 function updateNode ($a_node)
00110 {
00111 $q = "UPDATE lo_tree SET ".
00112 "parent_node_id = '".$a_node["parent"]."',".
00113 "prev_sibling_node_id = '".$a_node["prev"]."',".
00114 "next_sibling_node_id = '".$a_node["next"]."',".
00115 "first_child_node_id = '".$a_node["first"]."' ".
00116 "WHERE node_id = '".$a_node["node"]."' ".
00117 "AND lo_id = '".$this->obj_id."'";
00118 $this->ilias->db->query($q);
00119 }
00120
00121 function insertNodeData ($a_node)
00122 {
00123
00124 $a_node = $this->prepareData($a_node);
00125
00126
00127
00128 switch ($a_node["type"]) {
00129 case 1:
00130 $this->insertElement($a_node);
00131 $this->insertAttributes($a_node);
00132 break;
00133
00134 case 3:
00135 $this->insertText($a_node);
00136 break;
00137
00138 case 4:
00139
00140 break;
00141
00142 case 5:
00143
00144 break;
00145
00146 case 6:
00147
00148 break;
00149
00150 case 7:
00151
00152 break;
00153
00154 case 8:
00155 $this->insertComment($a_node);
00156 break;
00157
00158 default:
00159
00160 break;
00161 }
00162 }
00163
00169 function insertElement ($a_node)
00170 {
00171 $element_id = $this->getEntryId("lo_element_name","element","element_id",$a_node["name"]);
00172
00173
00174 if ($element_id == false)
00175 {
00176 $q = "INSERT INTO lo_element_name (element) ".
00177 "VALUES ('".$a_node["name"]."')";
00178 $this->ilias->db->query($q);
00179
00180 $element_id = $this->getLastInsertId();
00181 }
00182
00183
00184 $q = "INSERT INTO lo_element_idx (node_id,element_id) ".
00185 "VALUES ('".$a_node["node"]."','".$element_id."')";
00186 $this->ilias->db->query($q);
00187 }
00188
00194 function insertText ($a_node)
00195 {
00196
00197 $content = trimDeluxe($a_node["content"]);
00198
00199 $q = "INSERT INTO lo_text ".
00200 "(node_id,textnode) ".
00201 "VALUES ".
00202 "('".$a_node["node"]."','".$content."')";
00203 $this->ilias->db->query($q);
00204 }
00205
00211 function insertComment ($a_node)
00212 {
00213 $q = "INSERT INTO lo_comment ".
00214 "(node_id,comment) ".
00215 "VALUES ".
00216 "('".$a_node["node"]."','".$a_node["content"]."')";
00217 $this->ilias->db->query($q);
00218 }
00219
00226 function insertAttributes ($a_node)
00227 {
00228 if (is_array($a_node["attr_list"]))
00229 {
00230 foreach ($a_node["attr_list"] as $attr => $value)
00231 {
00232 $attribute_id = $this->getEntryId("lo_attribute_name","attribute","attribute_id",$attr);
00233
00234
00235 if ($attribute_id == false)
00236 {
00237 $q = "INSERT INTO lo_attribute_name (attribute) ".
00238 "VALUES ('".$attr."')";
00239 $this->ilias->db->query($q);
00240
00241 $attribute_id = $this->getLastInsertId();
00242 }
00243
00244 $value_id = $this->getEntryId("lo_attribute_value","value","value_id",$value);
00245
00246
00247 if ($value_id == false)
00248 {
00249 $q = "INSERT INTO lo_attribute_value (value) ".
00250 "VALUES ('".$value."')";
00251 $this->ilias->db->query($q);
00252
00253 $value_id = $this->getLastInsertId();
00254 }
00255
00256
00257 $q = "INSERT INTO lo_attribute_idx (node_id,attribute_id,value_id) ".
00258 "VALUES ".
00259 "('".$a_node["node"]."','".$attribute_id."','".$value_id."')";
00260 $this->ilias->db->query($q);
00261 }
00262
00263 return true;
00264 }
00265
00266 return false;
00267 }
00268
00278 function getEntryId ($a_table,$a_column,$a_return_value,$a_value)
00279 {
00280 $q = "SELECT DISTINCT ".$a_return_value." FROM ".$a_table." ".
00281 "WHERE ".$a_column."='".$a_value."'";
00282
00283 $res = $this->ilias->db->query($q,DB_FETCHMODE_ASSOC);
00284
00285 if ($res->numRows() == 0)
00286 {
00287 return false;
00288 }
00289
00290 $row = $res->fetchRow();
00291 return $row[0];
00292 }
00293
00299 function getLastInsertId ()
00300 {
00301 return $this->ilias->db->getLastInsertId();
00302 }
00303
00310 function prepareData ($a_data)
00311 {
00312 foreach ($a_data as $key => $value)
00313 {
00314 if (is_string($value))
00315 $data[$key] = addslashes($value);
00316 else
00317 $data[$key] = $value;
00318 }
00319
00320 return $data;
00321 }
00322
00323
00324
00325 function insertStructureIntoTree($a_nodes,$a_id)
00326 {
00327
00328 $lm_tree = new ilTree($a_id,$a_id);
00329
00330
00331 foreach ($a_nodes as $key => $nodes)
00332 {
00333 if (!is_array($nodes[key($nodes)]))
00334 {
00335 array_splice($a_nodes,$key);
00336 break;
00337 }
00338 }
00339
00340
00341 $parent_id = $a_id;
00342 $lm_tree->insertNode(key($a_nodes[0]),$parent_id,0);
00343
00344
00345 foreach ($a_nodes as $key => $nodes)
00346 {
00347 $parent_parent_id = $parent_id;
00348 $parent_id = key($nodes);
00349
00350 foreach (array_reverse($nodes[$parent_id]) as $child_id)
00351 {
00352 $lm_tree->insertNode($child_id,$parent_id,$parent_parent_id);
00353 }
00354 }
00355 }
00356 }
00357 ?>