name = $name; $this->parent = $parent; } function __destruct() { } function load_xml($xmlstr) { $this->xml_obj = simplexml_load_string($xmlstr); } /* function init_ids() { $this->ids[$this->name] = 1; foreach($this->groups as $group) { if(!array_key_exists($group->name,$this->ids)) { $group->init_ids(); } } } */ function init_ids() { self::$ids[$this->name] = 1; foreach($this->groups as $group) { if(!array_key_exists($group->name,self::$ids)) { $group->init_ids(); } } } function add_field($field,$count) { $this->fields[$field] = $count; } function add_group($group) { $this->groups[$group->name] = $group; } function has_value($field) { return (array_key_exists($field,$this->field_vals)); } function assign_ids($main_id, $parent_id, $parent_name) { $this->main_id = $main_id; $this->parent_id = $parent_id."_".$parent_name; $this->id = self::$ids[$this->name]; self::$ids[$this->name]++; } function add_value($field,$value) { if(array_key_exists($field,$this->fields)) { $this->field_vals[$field][] = $value; return true; } else { return false; } } function get_group_names() { $names = array($this->name); foreach($this->groups as $group) { $subgroup_names = $group->get_group_names(); foreach($subgroup_names as $name) { if(!in_array($name,$names)) { $names[] = $name; } } } return $names; } function print_group_info() { echo "Group: $this->name main id: $this->main_id; parent_id: $this->parent_id;"; echo " id: this->id
"; foreach($this->fields as $field=>$count) { echo "field: $field count: $count
"; } foreach($this->groups as $group) { echo "group: $group->name : XX fields : ".count($group->groups)." groups
"; } foreach($this->groups as $group) { $group->print_group_info(); } } function print_group_values() { foreach($this->fields as $field=>$count) { if(array_key_exists($field,$this->field_vals)) { foreach($this->field_vals[$field] as $val) { echo "\\$field $val
"; } } } foreach($this->groups as $group) { if(array_key_exists($group->name, $this->group_vals)) { foreach($this->group_vals[$group->name] as $g) { $g->print_group_values(); } } } } function load_query_result($row,$data) { foreach($row as $field=>$val) { if($field == "template") { $this->template = $val; } else if((trim($val) != "") && !ereg("id",$field)) { $val = ereg_replace("&","&",$val); $index = strrpos($field,"_"); if($index) { $field = substr($field,0,$index); } if($this->add_value($field,$val)) { //echo "parent: ".$this->parent->name." : $this->name : $field=$val
"; } } } $id = $row[$this->name."_id"]."_".$this->name; foreach($this->groups as $group) { if(array_key_exists($id."_".$group->name, $data)) { foreach($data[$id."_".$group->name] as $row) { $new_group = $group; $new_group->id = $row[$group->name."_id"]; $new_group->load_query_result($row,$data); $this->group_vals[$new_group->name][] = $new_group; } } } } function destroy_subgroups() { unset($this->parent); $this->parent = false; unset($this->field_vals); $this->field_vals = array(); foreach($this->group_vals as $groups) { foreach($groups as $group) { $group->destroy_subgroups(); } } $this->group_vals = array(); $this->template = ""; } function unhtmlentities($string) { $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); return strtr($string, $trans_tbl); } function to_ssv($head_tag) { $row = array(); $row[$head_tag][0] = $this->main_id; if($this->name != $head_tag) { $row["parent_id"][0] = $this->parent_id; $row["id"][0] = $this->id; } else { $row["template"][] = $this->template; } foreach($this->fields as $field=>$count) { if(array_key_exists($field,$this->field_vals)) { foreach($this->field_vals[$field] as $val) { $val = $this->unhtmlentities($val); $row[$field][] = $val; } $num_fields = count($this->field_vals[$field]); for($i=$num_fields;$i<$count;$i++) { $row[$field][$i] = ""; } } else { for($i=0; $i<$count;$i++) { $row[$field][$i] = ""; } } } $s = ""; foreach($row as $field=>$vals) { foreach($vals as $val) { $s .= "^$val^ "; } } $result[$this->name][] = $s; foreach($this->groups as $group) { if(array_key_exists($group->name, $this->group_vals)) { foreach($this->group_vals[$group->name] as $g) { //echo "adding ".$g->name."
"; $subgroup_tabledata = $g->to_ssv($head_tag); foreach($subgroup_tabledata as $table=>$rows) { foreach($rows as $row) { $result[$table][] = $row; } } } } } return $result; } /* This returns an array of all groups containing $field. The array is ordered in key-value pairs of group_name=count where count is the amount of fields which may exist in that group. For example, if a group can have up to 3 lxa entries, the count is 3. */ function find_group($field) { $group_names = array(); if(array_key_exists($field,$this->fields) ) { $group_names[$this->name] = $this->fields[$field]; } foreach($this->groups as $group) { $subgroup_names = $group->find_group($field); foreach($subgroup_names as $name=>$count) { if(!array_key_exists($name,$group_names)) { $group_names[$name] = $count; } } } return $group_names; } function make_tables_array($tables) { $s = "\"$this->name\" => array("; foreach($this->fields as $field=>$count) { $s .= "\"$field\"=>$count,"; } $s = substr($s,0,-1).")"; $tables[$this->name] = $s; foreach($this->groups as $group) { if(!array_key_exists($group->name, $tables)) { $group->make_tables_array($tables); } } } function to_xml() { $xml = "<$this->name>\n"; foreach($this->field_vals as $field=>$vals) { foreach($vals as $val) { if(trim($val) != "") { $xml .= "<$field>$val\n"; } } } foreach($this->groups as $group) { if(array_key_exists($group->name, $this->group_vals)) { foreach($this->group_vals[$group->name] as $g) { $xml .= $g->to_xml(); } } } $xml .= "name>\n"; return $xml; } function to_xml_2($fields) { $xml = "<$this->name>\n"; //echo "FIELDS: ".implode(" ",$fields)."
"; $count = 0; $num_fields = count($fields); $indices = array(); while($count < $num_fields) { $field = $fields[$count]; if(array_key_exists($field,$this->field_vals)) { if(!array_key_exists($field,$indices)) { $indices[$field] = 0; } $index = $indices[$field]; $val = $this->field_vals[$field][$index]; //echo "adding $field=$val from $this->name
"; $xml .= "<$field>$val\n"; $indices[$field]++; $count++; } else { $groups = $this->find_group($field); $group_name = key($groups); if(!array_key_exists($group_name,$indices)) { $indices[$group_name] = 0; } $index = $indices[$group_name]; $group = $this->group_vals[$group_name][$index]; $first_field = $field; $sub_group_fields = array(); while($group->contains_field($field)) { $sub_group_fields[] = $field; $count++; $field = $fields[$count]; if(($count == $num_fields) || ($field == $first_field)) break; } $xml .= $group->to_xml_2($sub_group_fields); $indices[$group_name]++; } } $xml .= "name>\n"; return $xml; } function to_xml_3($template, $with_ids) { $xml = "<$this->name>\n"; if($with_ids) { $xml .= "<".$this->name."_id>$this->idname."_id>\n"; } $indices = array(); while(count($template) != 0) { $cur_tag = array_shift($template); if(!array_key_exists($cur_tag,$indices)) { $indices[$cur_tag] = 0; } $index = $indices[$cur_tag]; $indices[$cur_tag]++; if(array_key_exists($cur_tag,$this->fields)) { if(array_key_exists($cur_tag,$this->field_vals)) { $val = $this->field_vals[$cur_tag][$index]; } else { $val = ""; } $xml .= "<$cur_tag>$val\n"; } else { $group = $this->group_vals[$cur_tag][$index]; $subgroup_template = array(); while(($cur_tag = array_shift($template)) != "/".$group->name) { $subgroup_template[] = $cur_tag; } $xml .= $group->to_xml_3($subgroup_template); } } $xml .= "name>\n"; return $xml; } function contains_field($field) { if(array_key_exists($field,$this->fields)) { return true; } else { foreach($this->groups as $group) { $result = $group->contains_field($field); if($result) { return true; } } } return false; } function structure_to_xml() { $xml = "name\">\n"; ksort($this->fields); foreach($this->fields as $field=>$count) { $xml .= "$field\n"; } foreach($this->groups as $group) { $xml .= $group->structure_to_xml(); } $xml .= "\n"; return $xml; } function write_schema($file_name) { $out = fopen($file_name,"w"); fwrite($out,$this->structure_to_xml()); fclose($out); } } ?>