Tryag File Manager
Home
-
Turbo Force
Current Path :
/
home
/
cluster1
/
data
/
bu01
/
1121861
/
html
/
jlex
/
undocumented code
/
Upload File :
New :
File
Dir
//home/cluster1/data/bu01/1121861/html/jlex/undocumented code/group.php4
<? class group { var $name; var $fields; var $field_vals; var $groups; var $group_vals; var $head_tag; var $id; var $main_id; var $parent_id; var $ids; var $parent; var $template = ""; function group($name, &$ids, &$parent) { $this->name = $name; $this->ids = &$ids; $this->parent = &$parent; $this->fields = array(); $this->field_vals = array(); $this->groups = array(); $this->group_vals = array(); } 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 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 = $this->ids[$this->name]; $this->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 add_group_value(&$group) { $this->group_vals[$group->name][] = &$group; $group->parent = &$this; } 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 "<B>Group: $this->name </B> main id: $this->main_id; parent_id: $this->parent_id;"; echo " id: this->id \n"; foreach($this->fields as $field=>$count) { echo "field: $field count: $count\n"; } foreach($this->groups as $group) { echo "group: $group->name : XX fields : ".count($group->groups)." groups \n"; } 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 \n"; } } } 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") { $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<BR>"; } } } $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; unset($new_group); } } } } function complete_destroy() { unset($this->parent); $this->parent = NULL; unset($this->field_vals); $this->field_vals = NULL; $names = array_keys($this->groups); foreach($names as $name) { $g = &$this->groups[$name]; $g->destroy_subgroups(); } $names = array_keys($this->group_vals); foreach($names as $name) { $num = count($this->group_vals[$name]); for($i=0;$i<$num;$i++) { $g = &$this->group_vals[$name][$i]; $g->destroy_subgroups(); } } } function destroy_subgroups() { unset($this->parent); $this->parent = NULL; unset($this->field_vals); $this->field_vals = NULL; if(is_array($this->group_vals)) { $names = array_keys($this->group_vals); foreach($names as $name) { $num = count($this->group_vals[$name]); for($i=0;$i<$num;$i++) { $g = &$this->group_vals[$name][$i]; $g->destroy_subgroups(); } } } $this->group_vals = NULL; $this->template = ""; } function reset_values() { unset($this->field_vals); $this->field_vals = array(); unset($this->group_vals); $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; if($this->group_vals != null) { 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."<BR>"; $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</$field>\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 .= "</$this->name>\n"; return $xml; } function to_xml_3($template, $with_ids) { $xml = "<$this->name>\n"; if($with_ids) { $xml .= "<".$this->name."_id>$this->id</".$this->name."_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</$cur_tag>\n"; } else if(array_key_exists($cur_tag,$this->group_vals)){ $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,$with_ids); } else { echo "No $cur_tag group within this group object \n"; die("Error!"); } } $xml .= "</$this->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 = "<group name=\"$this->name\">\n"; ksort($this->fields); foreach($this->fields as $field=>$count) { $xml .= "<field count=\"$count\">$field</field>\n"; } foreach($this->groups as $group) { $xml .= $group->structure_to_xml(); } $xml .= "</group>\n"; return $xml; } function write_schema($file_name) { $out = fopen($file_name,"w"); fwrite($out,$this->structure_to_xml()); fclose($out); } function set_parent() { $names = array_keys($this->groups); foreach($names as $name) { $g = &$this->groups[$name]; //echo "setting $g->name \n"; $g->parent = &$this; $g->set_parent(); } } 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; } } ?>