Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/import_data.inc
<?php
//The group id number for the imported records
$group_id_num = 2;


include($include_root."client_id_class.inc");
include($include_root."add_organization_class.inc");
include($include_root."questions_display.inc"); //Add in the secondary question validation needed by add_organization.inc
include($include_root."Questions_into_array_class.inc");
include($include_root."client_profile_class.inc");
include($include_root."insert_household_report_class.inc");

class Import_data {

    var $lines;
    var $elements;
    var $birth_year;
    var $gender;
    var $households;


    function Import_data ($path) {
        $this->lines = file ($path);
        //echo $this->lines[1];
        
        $this->switchyard();
    }



    function find_distinct_elements_seperated_by_commas ($line_number)    {
    
        $line_elements = explode(",", $this->lines[$line_number]);
        
        return $line_elements;
    
    
    }

    
    function switchyard ()  {
        GLOBAL $group_id_num, $client_info;
    
        $cli_id = new Client_id;
    
        //Loop through the lines
        foreach ( $this->lines AS $key_line => $cur_line) {
            
            $current_line_elements = $this->find_distinct_elements_seperated_by_commas ($key_line);
            
            //Third character of last name for middle initial
            $middle_name =  strtoupper( substr($current_line_elements[2], 2, 1) ); 
            
            $client_identifier = $this->assemble_client_id ($current_line_elements[1], $middle_name, $current_line_elements[2], $current_line_elements[3], $current_line_elements[11]);
                          
            $ethnicity = $this->convert_ethnicity ($current_line_elements[10], $current_line_elements[7]);
         
            //Find the organization id
            $org_id_num = $this->find_org_id ($current_line_elements[9]);
            //echo "org id  is: ".$org_id_num."<p>";           
               
            //Put the client id, and org-specific id into the global variable for processing
            $client_info[0]["client_identifier"] = $client_identifier;
            $client_info[0]["org_generated_client_id"] = $current_line_elements[1]." ".$current_line_elements[2];
            $cli_id->client_id_assign ($org_id_num);
            $client_id_num = $client_info[0]["client_id"];
            echo "Client id # is: ".$client_id_num."<br/>";
            //Run it again to insert the social sec # as an org specific client id
            $client_info[0]["org_generated_client_id"] = $current_line_elements[12];
            $cli_id->client_id_assign ($org_id_num);
         
            //Insert a client profile
            $this->insert_client_profile ($client_id_num, $this->gender, $this->birth_year, $ethnicity, $org_id_num, $group_id_num);
            
            //Insert household report
            $this->insert_household_report ($client_id_num, $org_id_num, $group_id_num, $current_line_elements);
            
        }
        
        
        
    }
          
  
    function assemble_client_id ($first_name, $middle_name, $last_name, $birth_date, $gender_raw)   {
   
        $first_initial = substr($first_name, 0, 1);
        $middle_initial = substr($middle_name, 0, 1);
        $last_initial = substr($last_name, 0, 1);
        
        if (!$first_initial)   {
            $first_initial = "-";
        }
        
        if (!$middle_initial)   {
            $middle_initial = "-";
        }
        
        if (!$last_initial)   {
            $last_initial = "-";
        }
        
        $intials = strtoupper($first_initial.$middle_initial.$last_initial);
        
        
        $birthdate_elements = explode("/", $birth_date);
        
        //Strip off the time from birthyears
        $birthdate_elements[2] = substr( $birthdate_elements[2], 0, -8); 
        
        //Fix two digit years
        if ( strlen ($birthdate_elements[2]) < 3 )  {
        
            if ($birthdate_elements[2] < 3) {
                $birth_year = $birthdate_elements[2] + 2000;
            
            }    
            else {
            
                $birth_year = $birthdate_elements[2] + 1900;
            }
        
        }
        else    {
        
            $birth_year = $birthdate_elements[2];
            
        } 
        $this->birth_year = $birth_year;
        
     
        $birth_month = substr($birthdate_elements[0] + 100, 1);
        $birth_date = substr($birthdate_elements[1] + 100, 1);
        
        if ($gender_raw == "Male")  {
            
            $gender = "male";
        
        }
        
        else    {
        
            $gender = "female";
        
        }
        $this->gender = $gender;
        
        
        $client_identifier = $first_initial.$middle_initial.$last_initial.$birth_month.$birth_date.$birth_year.$gender;
                 
        echo $client_identifier."<br/>";
        
        return $client_identifier;
   
    
    }
    
    
    
    function convert_ethnicity ($race_num, $hispanic_yes_or_no)    {
        
        if ( $hispanic_yes_or_no == "1" )  {
        
            $return_array[0] = "hispanic/latino";
            $array_count = 1;
        
        }
        else {
            $array_count = 0;
        }
        
        switch ($race_num)  {
        
            case 1:
                $return_array[$array_count] = "white";                
                break;
            case 2:
                $return_array[$array_count] = "african-american";                
                break; 
            case 3:
                $return_array[$array_count] = "asian";                
                break; 
            case 4:
                $return_array[$array_count] = "hawaiian_native_or_pacific_islander";                
                break; 
            case 5:
                $return_array[$array_count] = "american_indian";                
                break; 
            default:
                $return_array[$array_count] = "white";
        }
  
        return $return_array;
    }  


    function find_org_id ( $org_name ) {
        GLOBAL $unique_seq, $group_id_num;
            
        $sql = "SELECT org_id FROM organizations WHERE org_name LIKE '".$org_name."'";
        $org_id_raw = run_query_return_single_row ($sql, "Pulling out existing organizations");
        //echo $sql."<p>";
        $org_id = $org_id_raw[0];
        
        
        if (!$org_id)   {
            echo "Org does not exist ".$org_name."<p>";
            
            $vetted_values["login"] = substr ($org_name, 0, 24);
            $vetted_values["password"] = time();
            $vetted_values["org_name"] = substr ($org_name, 0, 74);
            $vetted_values["first_name"] = substr ($org_name, 0, 6)."Firstname";
            $vetted_values["last_name"] = substr ($org_name, 0, 6)."Lastname";
            $vetted_values["email_address"] = "hide@address.com";
            $vetted_values["user_phone"] = "(555) 555-5555";
            
            $add_org = new Add_organization ($vetted_values, $group_id_num);
            $org_id = $add_org->new_org_id;
            
            //Deactivate the new account
            $sql = "UPDATE organizations SET account_status = 'inactive' WHERE org_id = '".$org_id."'";
            run_query ($sql, "Organizations deactivation");
        
        
        }
    
    
        return $org_id;
    
    }
    
    

    function insert_client_profile ($client_id_num, $gender, $birth_year, $ethnicity, $organization_number, $client_status = '2')
	{
        GLOBAL $group_id_num;
    
        $client_profile_questions = new Questions_into_array ("client_profile", "40", $organization_number, $group_id_num,
     "display");
    
        $client_info[0]["client_id"] = $client_id_num;
		$client_info[0][98] = $client_status;
        $client_info[0][100] = $birth_year;
        $client_info[0][101] = $gender;
        $client_info[0][102] = $ethnicity;
        $client_info[0]["client_rpt_sharing_permission"] = "yes";
    
        $cli_prof = new Client_profile;
        $cli_prof->insert_client_profile_answers ("client_profile", $client_info, $client_profile_questions->questions, $organization_number);
    }


    function insert_household_report ($client_id_num, $org_id_num, $group_id_num, $current_line_elements)   {
        
        GLOBAL $client_info, $_SESSION;
        $client_info = "";
    
        $report_dates["begin"] = strtotime($current_line_elements[6]);
        //if there is no end date, make the end date one day past the begin date
        if ( !$current_line_elements[13] )  {
            $report_dates["end"] = $report_dates["begin"] + 86400;        
        }
        else {
            $report_dates["end"] = strtotime($current_line_elements[13]);
        }
        //$report_dates["begin"] = "1222";
        //$report_dates["end"] = "1222";
    
        //If they are head of houshold, or there is no family id....
        if ($current_line_elements[5] == "-1" || $current_line_elements[4] < 1)  {
            $hh_rpt = new Insert_household_report;
            $client_info[0]["client_id"] = $client_id_num;
            $client_info[0]["client_relationship_to_lead"] = "head";
            //Put in the value say the person/family received emergency shelter
            $vetted_values[135] = "emergency_shelter";
        
            $pull_questions = new Questions_into_array ("household_service", "40", $org_id_num, $group_id_num, "display");
        
            //..insert a new household service report
            $this->households[$current_line_elements[4]]["report_id"] = $hh_rpt->insert_report_answers ("household_service", $client_info, $pull_questions->questions, $vetted_values, $org_id_num, "yes", "", $report_dates);
    
    
        }
        //...they are part of family, and a report id has been assigned to that family, so only add an association
        // with the existing report
        elseif ( $current_line_elements[4] > 0 && $this->households[$current_line_elements[4]]["report_id"] > 0 )   {
            $sql = "INSERT INTO report_relationship
             ( hh_report_id, report_relationship_client_id, client_relationship_to_lead )
             VALUES ( '".$this->households[$current_line_elements[4]]["report_id"]."', '".$client_id_num."', 'friend' )";
             run_query ($sql, "Inserting secondary family member");
        }
        else {
            echo "Mistake";
        }
        $client_info = "";
        $_SESSION['client_infoB'] = "";
    
    }  
    
    
}


?>
Return current item: Homeless Mangement Information System