Location: PHPKode > projects > Homeless Mangement Information System > hmis/dump_entered_data.php
<?php
//header('Content-Type: application/octet-stream');
//header('Content-Disposition: filename=dump_entered_data.php');
//*Client Data System, Copyright (C) 2000, 2001, 2002 Tedd Kelleher.  This is free software, subject to the 
//*GNU GENERAL PUBLIC LICENSE, Version 2, June 1991 (in file named gpl.txt), which should accompany 
//*any distribution of this file.  Tedd Kelleher can be contacted at hide@address.com
//This page will dump the data entered in the course of using the system (client data, user data, etc) into a series of SQL INSERT statements

//$page_title = "Client Page";
//$page_id = "clients";
$page_access_levels = ":10:";
//$page_profile = "View Client Information";
//$instructions .= "View client information by inputting their ID below. ";

////Header for every page that finds the include directory, connects to db, authenticates user access 
include("initialize_pointer.php");
if(!include($include_root."authenticate_public.inc")){echo "No Authentication"; exit; };

//Key is table name, use a "1" in the Value to preserve the system generated sequence count
$dump_tables = array (
"gate" => "1", 
"groups" => "1",  
"organizations" => "1",
"user_info" => "0",
 
"question_optional" => "1", 
"table_elements_optional" => "1", 

"vacancy_report_profile" => "0",
"vacancy_answers" => "0",

"clients" => "1",
"client_rpt_profile" => "1",   
"client_rpt_answers" => "1", 
"org_generated_client_ids" => "1", 

"report_profile" => "1", 
"report_relationship" => "1",
"report_answers" => "1",
"textarea_answers" => "1", 

"referrals" => "1",

"per_group_profile" => "1",
"per_group_associated_orgs" => "0", 
"per_associated_users" => "0", 
"per_group_associated_reports" => "0",
"per_associated_users_notificati" => "0",

"org_client_status" => "0",
"services_provided_by_org" => "0"
);

//$dump_tables = array (
// "pre_built_queries" => "0" );


foreach ( $dump_tables AS $table_name => $contains_serial)	{
 
	$question_table_column_names = return_column_names_array($table_name, "");

	$sql = "SELECT ".$question_table_column_names["list"]." FROM ".$table_name.' ORDER BY '.$question_table_column_names["array"][0];
	$questions_result = run_query($sql, "Pulling all question result");
	$question_count = num_rows($questions_result);

	for($i=0; $i < $question_count; $i++)   {
    	$question_attributes_array = fetch_array($questions_result, "Fetching question array data for dump", $i);

    	for ($z=0; $z < $question_table_column_names["column_count"]; $z++) {

				if ( $question_attributes_array[$z] == NULL )	{
					$question_attributes_array[$z] = "NULL";	
				
				}
				else {
					$question_attributes_array[$z] = "'".addslashes($question_attributes_array[$z])."'";
				} 	

        	$values_list .= $question_attributes_array[$z];
        	if ($z != $question_table_column_names["column_count"]-1)   {
        	$values_list .= ", "; 
        	}
    	}

	$dump_result .= "run_query(\"INSERT INTO ".$table_name." (".$question_table_column_names["list"].") VALUES (".$values_list.")\",
	\"Error inserting values into ".$table_name.", row #".$question_attributes_array[0]."\");\n";
	unset($values_list);
	//$dump_result .= build_question_element_creation_sql ($question_attributes_array["question_id"], $question_attributes_array["question_type"]);
	}

    //Set the serial seq value to the next increment, to prevent PgSQL trying to start from 1 again when inserting
    if ( $contains_serial == 1 )    {

        //Find the highest increment in the column
        $sql = "SELECT ".$question_table_column_names["array"][0]." FROM ".$table_name." ORDER BY ".$question_table_column_names["array"][0]." DESC";
        $highest_increment = run_query_return_single_row ($sql, $error_message);


        //Find the name of the sequence
        if ( strlen ( $question_table_column_names["array"][0] ) < 13 )  {
            $bonus_len = 13 - strlen ( $question_table_column_names["array"][0] );
        }
        else {
            $bonus_len = 0;
        }
        //echo "Bonus lengt is ".$bonus_len."<p>";
        $seq_table_name = substr ($table_name, 0, 13 + $bonus_len);
        //$seq_table_name = substr ($table_name, 0, 13 );
        $part_two_length = ( 13 - strlen($seq_table_name) ) + 13;
	    
        $seq_serial_col_name = substr ($question_table_column_names["array"][0], 0, $part_two_length);
        //echo $seq_table_name."_".$seq_serial_col_name."_seq<p>";
        if  ( $highest_increment[0] ) {
        
        
        $dump_result .= "if ( \$db_version == 'pg_7.3' )	{\n\n";
        $dump_result .= "run_query(\"SELECT setval ('".$table_name."_".$question_table_column_names["array"][0]."_seq', ".$highest_increment[0].")\", \"Error setting sequence for ".$table_name."\");\n\n";
        $dump_result .= "}\n\n";
        $dump_result .= "if ( \$db_version == 'pg_7.2' )	{\n\n";
        $dump_result .= "run_query(\"SELECT setval ('".$seq_table_name."_".$seq_serial_col_name."_seq', ".$highest_increment[0].")\", \"Error setting sequence for ".$table_name."\");\n\n";
        $dump_result .= "}\n\n";
        }
        
        //echo $seq_table_name."_".$seq_serial_col_name."_seq<p>";

    }

}



echo $dump_result;

?>
Return current item: Homeless Mangement Information System