Tryag File Manager
Home
-
Turbo Force
Current Path :
/
home
/
cluster1
/
data
/
bu01
/
1121861
/
html
/
old_jlex_versions
/
php
/
Upload File :
New :
File
Dir
/home/cluster1/data/bu01/1121861/html/old_jlex_versions/php/group.php4
<? class group { var $name; var $fields = array(); var $field_vals = array(); var $groups = array(); var $group_vals = array(); 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; } 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 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 <BR>"; foreach($this->fields as $field=>$count) { echo "field: $field count: $count<BR>"; } foreach($this->groups as $group) { echo "group: $group->name : XX fields : ".count($group->groups)." groups <BR>"; } 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 <BR>"; } } } 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<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; } } } } 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] = ""; } } } $out = fopen("sql_data/$this->name.sql","a"); $s = ""; foreach($row as $field=>$vals) { foreach($vals as $val) { $s .= "^$val^ "; } } fwrite($out,trim($s)."\n"); fclose($out); //$tables[$this->name][] = $row; 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>"; $g->to_ssv($head_tag); } } } } function create_sql_table($head_tag) { $table = "CREATE TABLE $this->name \n("; if($this->name != $head_tag) { $table .= $head_tag."_id int unsigned,\n"; $table .= "parent_id text,\n"; } $table .= $this->name."_id int unsigned not null auto_increment primary key,\n"; if($this->name == $head_tag) { $table .= "template text,\n"; } foreach($this->fields as $field=>$num) { for($i=0;$i<$num;$i++) { $table .= $field."_$i text,\n"; } } $table = substr(trim($table),0,-1).")"; $tables[$this->name] = $table; foreach($this->groups as $group) { if(!array_key_exists($group->name,$tables)) { $tables = array_merge($tables,$group->create_sql_table($head_tag)); } } return $tables; } /* function create_sql_table(&$tables, $head_tag) { $table = "CREATE TABLE $this->name \n("; if($this->name != $head_tag) { $table .= $head_tag."_id int unsigned,\n"; $table .= "parent_id text,\n"; } $table .= $this->name."_id int unsigned not null auto_increment primary key,\n"; if($this->name == $head_tag) { $table .= "template text,\n"; } foreach($this->fields as $field=>$num) { for($i=0;$i<$num;$i++) { $table .= $field."_$i text,\n"; } } $table = substr(trim($table),0,-1).")"; $tables[$this->name] = $table; foreach($this->groups as $group) { if(!array_key_exists($group->name,$tables)) { $group->create_sql_table(&$tables,$head_tag); } } } */ /* 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_2($fields) { $xml = "<$this->name>\n"; //echo "FIELDS: ".implode(" ",$fields)."<BR>"; $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<BR>"; $xml .= "<$field>$val</$field>\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 .= "</$this->name>\n"; return $xml; } function to_xml_3($template) { $xml = "<$this->name>"; $xml .= "<".$this->name."_id>$this->id</".$this->name."_id>"; $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>"; } 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 .= "</$this->name>"; 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; } } ?>