name = $name; $this->parent = $parent; $this->fields = array(); $this->field_vals = array(); $this->groups = array(); $this->group_vals = array(); } function __destruct() { } /****************************** 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(); } } } /****************************** add_field ********************************/ function add_field($field,$count) { $this->fields[$field] = $count; } /****************************** add_group ********************************/ function add_group($group) { $this->groups[$group->name] = $group; } /****************************** has_value ********************************/ function has_value($field) { return (array_key_exists($field,$this->field_vals)); } /****************************** assign_ids ********************************/ 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]++; } /****************************** add_value ********************************/ function add_value($field,$value) { if(array_key_exists($field,$this->fields)) { $this->field_vals[$field][] = $value; return true; } else { return false; } } /****************************** add_group_value ********************************/ function add_group_value($group) { $this->group_vals[$group->name][] = $group; $group->parent = $this; } /****************************** get_group_names ********************************/ 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; } /****************************** print_group_info ********************************/ 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(); } } /****************************** print_group_values ********************************/ 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(); } } } } /****************************** add_field ********************************/ function load_query_result($row,$data) { foreach($row as $field=>$val) { if($field == "template") { $group_names = $this->get_group_names(); $cols = explode(" ",$val); foreach($cols as $col) { $c = str_replace("/","",$col); if($this->contains_field($col) || in_array($c,$group_names)) { $template .= $col." "; } } $this->template = $template; } 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 = clone $group; $new_group->id = $row[$group->name."_id"]; $new_group->load_query_result($row,$data); $this->group_vals[$new_group->name][] = $new_group; unset($new_group); } } } } /****************************** destroy_subgroups ********************************/ 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 = ""; } /****************************** reset_values ********************************/ function reset_values() { unset($this->field_vals); $this->field_vals = array(); unset($this->group_vals); $this->group_vals = array(); $this->template = ""; } /****************************** unhtmlentitites ********************************/ function unhtmlentities($string) { $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); return strtr($string, $trans_tbl); } /****************************** to_ssv ********************************/ 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; } /****************************** find_group ********************************/ /* 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; } /****************************** make_tables_array ********************************/ 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); } } } /****************************** to_xml ********************************/ 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; } /****************************** to_xml_by_template ********************************/ function to_xml_by_template($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_by_template($subgroup_template,$with_ids); } } $xml .= "name>\n"; return $xml; } /****************************** contains_field ********************************/ 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; } /****************************** structure_to_xml ********************************/ 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; } /****************************** write_schema ********************************/ function write_schema($file_name) { $out = fopen($file_name,"w"); fwrite($out,$this->structure_to_xml()); fclose($out); } /****************************** set_parent ********************************/ function set_parent() { $names = array_keys($this->groups); foreach($names as $name) { $g = $this->groups[$name]; $g->parent = $this; $g->set_parent(); } } /****************************** get_field_names ********************************/ function get_field_names() { $fields = array(); foreach($this->fields as $field=>$count) { $fields[] = $field; } foreach($this->groups as $group) { $fields = array_merge($fields,$group->get_field_names()); } $fields = array_unique($fields); sort($fields); return $fields; } /****************************** assign_fields ********************************/ function assign_fields($existing_groups) { $this->fields = $existing_groups[$this->name]; foreach($this->groups as $group) { $group->assign_fields($existing_groups); } } } ?>