Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/utility.inc
<?php

//*Client Data System, Copyright (C) 2000, 2001, 2002, 2003 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

include_once($include_root."adodb_time.inc");

//$arr = get_defined_vars();
//echo "<font color=navy>";
// var_dump($session_variable_key);
// echo "</font><br><br>";
// print all the server vars
// print_r($arr["_SERVER"]);
// print all the available keys for the arrays of variables
//print_r(array_keys(get_defined_vars()));

///////////////////////
// UTILITY FUNCTIONS //
///////////////////////

/********************************************************************************************
 * Name: safe_date: 
 * Description: Replacement for date called if windows.  
 * 
 * Details and Notes: Wrapper that handles negative timestamps, ie dates before 1970.
 * See adodb_time.inc for more info.  
 * 
 * @param: $fmt,$d,$is_gmt
 * @global: $os_of_webserver_is_windows_or_linux set in initilize.inc
 * @return: 
 * 
 * History: Created by Jeff on 3/11/04.
 ********************************************************************************************/
function safe_date($fmt,$d=false,$is_gmt=false)
{
    global $os_of_webserver_is_windows_or_linux;
    //echo "<br>safe_date($fmt,$d,$is_gmt) called";
    if ($os_of_webserver_is_windows_or_linux)
    {
        return adodb_date($fmt,$d,$is_gmt);
    }
    else
    {
        return date($fmt,$d);
    }
}

/********************************************************************************************
 * Name: safe_mktime: 
 * Description: Replacement for mktime called if windows.  
 * 
 * Details and Notes: Wrapper that handles negative timestamps, ie dates before 1970.  
 * See adodb_time.inc for more info.  
 * Additional possible wrapper can be found at http://us2.php.net/mktime.
 * 
 * @param: $hr,$min,$sec,$mon,$day,$year,$is_dst,$is_gmt
 * @global: $os_of_webserver_is_windows_or_linux set in initilize.inc
 * @return: 
 * 
 * History: Created by Jeff on 3/11/04.
 ********************************************************************************************/
function safe_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst=false,$is_gmt=false)
{
    global $os_of_webserver_is_windows_or_linux;
    //echo "<br>safe_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,$is_gmt) called";
    if ($os_of_webserver_is_windows_or_linux)
    {
        return adodb_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,$is_gmt);
    }
    else
    {
        return mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,$is_gmt);
    }
}

/********************************************************************************************
 * Name: safe_getdate: 
 * Description: Replacement for getdate called if windows.  
 * 
 * Details and Notes: Wrapper that handles negative timestamps, ie dates before 1970.  
 * See adodb_time.inc for more info.  
 * Additional possible wrapper can be found at http://us2.php.net/mktime.
 * 
 * @param: $d,$fast
 * @global: $os_of_webserver_is_windows_or_linux set in initilize.inc
 * @return: 
 * 
 * History: Created by Jeff on 3/11/04.
 ********************************************************************************************/
function safe_getdate($d=false,$fast=false)
{
    global $os_of_webserver_is_windows_or_linux;
    if ($os_of_webserver_is_windows_or_linux)
    {
        return adodb_getdate($d,$fast);
    }
    else
    {
        return getdate($d,$fast);
    }
}

/////////////////////
// DEBUG FUNCTIONS //
/////////////////////

// Below recursive function is an improvement and debugged version of a function posted on php.net by "rudigier at noxx dot"
function pretty_var_dump($vInput, $iLevel = 0)
{
	$bg[0] = "#DDDDDD";
	$bg[1] = "#C4F0FF";
	$bg[2] = "#BDFAFF";
	$bg[3] = "#FFF1CA";
	
	$return = "<table border='0' cellpadding='5' cellspacing='1' style='font-size:10px'>\n".
	          "<tr>".
	          "<td align='left' bgcolor=".$bg[$iLevel].">\n";
	
	if (is_int($vInput))
	{
		$return .= "int (<b>".intval($vInput)."</b>) </td>";
	}
	else if (is_float($vInput))
	{
		$return .= "float (<b>".doubleval($vInput)."</b>) </td>";
	}
	else if (is_string($vInput))
	{
		$return .= "string (" . strlen($vInput) . ") \"<b>" . $vInput . "</b>\"</td>";
	}
	else if (is_bool($vInput))
	{
		$return .= "bool(<b>" . ($vInput ? "true" : "false") . "</b>)</td>";
	}
	else if (is_array($vInput))
	{
	
		$return .= "array count = [<b>" . count($vInput) . "</b>] dimension = [<b>{$iLevel}</b>]</td></tr><tr><td>";
		$return .=  "<table border='0' cellpadding='5' cellspacing='1' style='font-size:10px'>";
	
		while (list($vKey, $vVal) = each($vInput))
		{
			$return .= "<tr><td align='left' bgcolor='".$bg[$iLevel]."'><b>";
			$return .= (is_int($vKey)) ? "" : "\"";
			$return .= $vKey;
			$return .= (is_int($vKey)) ? "" : "\"";
			$return .= "</b></td><td bgcolor='".$bg[$iLevel]."'>=></td><td bgcolor='".$bg[$iLevel]."'><b>";
			
			$return .= pretty_var_dump($vVal, ($iLevel + 1)) . "</b></td></tr>";
		
		}
		
		$return .= "</table>";
	
	}
	else 
	{
		$return .= "NULL</td>";
	}
	
	$return .= "</table>";
	
	return $return;
}





//TODO: for future referral feature...  this function is currently not called from anywhere and contains a call to a decremented function direct_to_page
/********************************************************************************************
 * Name: display_referral_report():
 * Description: display's client referral form/report.
 * 
 * Details and Notes:
 *  
 * @param: $report_type_profile - array - form profile.
 * @param: $empty_form - string - if set is empty form.
 * @param: $edit_rpt - string -
 * @param: $ref_to_orgid - string -
 * @return: Nothing returned
 * 
 * History: Edited 1/21/04 by Jeff.
 ********************************************************************************************/
function display_referral_report ($report_type_profile, $empty_form="", $edit_rpt="", $ref_to_orgid="")
{
    //echo "In display_referral_report<br>";
    GLOBAL $client_info, $tag_values, $access_level, $org_id, $org_name, $user_name, $user_id, $group_id,
           $question_validation_error, $html_template, $page_id, $page_title, $form_answer, $form_answer_added_client, 
		   $message, $message_type, $head_dynamic_ecma, $unix_date;
    
    //Modify the title to tell user if they are editing the report
    if ( $edit_rpt )
	{
        $title_type = "Edit existing ";
    }
    else
	{
        $title_type = "Submit new ";
    }
    
	$page_title = $title_type.$report_type_profile["report_title"]." form";
    
    $pull_questions = new Questions_into_array ($report_type_profile["report_type_id"], $access_level, $org_id, $group_id, "display");
    
    if ( !$hh_rpt )
	{
        $hh_rpt = new Reports;   
    }
    
    if ( $report_type_profile["report_on_timeperiod"] == "yes" && !$date_quest)
	{
        $report_date_questions = new Report_date_questions ();
    }
    
    //if ( !$ref ) {
        $ref = new Referrals;
    //}
        
    //If coming from an edit household report request, set/reset the client to match report....   
    if ( $edit_rpt && $empty_form )
	{
        $edit_rpt += 0;
             
        //Find out if we are allowed to view this report
        //if ( is_household_report_shared ($edit_rpt, $org_id) == 1 )    {
        $perms = report_permissions_household ( $edit_rpt, $org_id, $user_id );
        if( $perms['read_rpt'] == 1 )
		{
            //Pull out the client info for the clients associated with the report, and put them in the session variable
            $client_prof = new Client_profile;
            $client_prof->put_client_profiles_associated_with_household_report_into_client_info_session_variable ($edit_rpt);
            
            //Pull out the exisiting report answers
            $report_answers = $hh_rpt->pull_existing_report_answers($edit_rpt, $pull_questions->questions,
                               $pull_questions->question_elements);
            
            //...and pull the date answers if needed 
            //echo "Pulling exsitng date answere<p>";
            if ( $report_type_profile["report_on_timeperiod"] == "yes" )
			{
                $report_date_questions->pull_existing_report_dates ($edit_rpt, "household");
            }
        }
        else
		{
            echo "Not allowed to view form"; exit;
        }
    }
    
    $relationships = new Relationships ($client_info["0"]["client_id"]);
    
    //If a fresh form is not requested, validate the answers
    if(!$empty_form)
	{
        //Validate the answers against the questions attributes pulled into an array above
		//echo "<br><br><font color=green>";
		//var_dump($form_answer);
        $validation = new Questions_answers_validation($pull_questions->questions, $pull_questions->question_elements, $form_answer);
		//var_dump($validation);
		//echo "</font><br><br>";
        
        $report_answers = $validation->vetted_form_answer;
           
        if ( $report_type_profile["report_on_timeperiod"] == "yes" )
		{
            $validation_date = new Questions_answers_validation($report_date_questions->questions,
            $report_date_questions->question_elements, $form_answer);
            $report_date_questions->validate_dates_exist ();
        }
        
        $share_answer = $hh_rpt->validate_household_share_answer ($form_answer); 
        
        $relationships->process_added_clients_checkboxes ($form_answer_added_client);
        
        question_set_validation($report_type_profile["report_type_id"], $validation->vetted_form_answer);
    }
    
    //If no form error, and a fresh form was not requested, insert the data, give a OK message, and go to next page/form
    if (!$question_validation_error && !$empty_form )
	{
        $insert_answers = new Insert_household_report;
           
        if ( $report_type_profile["report_on_timeperiod"] == "yes" )
		{
               $insert_dates = $report_date_questions->translate_report_dates_to_unix_time($validation_date->vetted_value);
               //echo "value of date";
               //display_value ($insert_dates); 
        }
        
        $new_report_id = $insert_answers->insert_report_answers ($report_type_profile["report_type_id"], $client_info,
        $pull_questions->questions, $validation->vetted_value, $org_id, $share_answer, $edit_rpt, $insert_dates);
                       
        $ref->insert_referral ($org_id, $user_id, $ref_to_orgid, $new_report_id);
                                     
        //If we are editing a report, send the new report id into the direct to page function
        if ( $edit_rpt || $report_type_profile["report_that_follows"] == "none" )    {
            $return_to_this_report = $new_report_id;
        }
        
        direct_to_page ($report_type_profile["report_type_id"], "yes_empty_form", "yes_following_page",
        $return_to_this_report, $report_unit); //bugbug, decremented direct_to_page
    }
    else //There is an error, or fresh form indicator, return report entry form
	{
        //$page_id = "household_referral_entry";
        $page_id = $report_type_profile["report_type_id"];
        $html_template = "client_form.html";
        
        $tag_values["{FORM_END}"] .= display_hidden_form_values("report_name", $report_type_profile["report_type_id"]);
        
        $reports = new Reports;
        
        //If viewing/editing an the report, place the report id of the report we are editing as a hidden post value,
        // and give a button to submit with if we are allowed to submit edits...
        if ($edit_rpt)
		{
            $tag_values["{FORM_END}"] .= display_hidden_form_values("edit_report_id", $edit_rpt);
            $tag_values["{FORM_END}"] .= display_hidden_form_values("report_unit", "household");
            $exist_rpt_prof = $reports->pull_existing_report_profile ($edit_rpt);
            $tag_values["{REPORT_PROFILE}"] = $reports->display_report_profile ("Edit ".$report_type_profile["report_title"],
            $exist_rpt_prof["user_name_first"]." ".$exist_rpt_prof["user_name_last"], $exist_rpt_prof["org_name"],
            $exist_rpt_prof["report_date_begin"]);
            //if (is_allowed_to_edit_household_report ($edit_rpt, $org_id) == 1)    {
            if( $perms['edit_rpt'] == 1 )
            {
                $tag_values["{FORM_END}"] .= form_end ('Click Here to Submit Edited Household Form', 1);
            }
            else
		    {
                $tag_values["{FORM_END}"] .= 'You are not allowed to change this form';
            }
        }
        //..we are not editing, so give us a submit button
        else
		{
            $tag_values["{FORM_END}"] .= display_hidden_form_values("ref_to_org_id", $ref_to_orgid);
            //echo "Ref to org id is: ".$ref_to_orgid."<p>";
            $tag_values["{FORM_END}"] .= form_end ("Click Here to Submit New Household Form", 1);
            $tag_values["{REPORT_PROFILE}"] = $reports->display_report_profile ($report_type_profile["report_title"], $user_name,
            $org_name, $unix_date);
        }
        
        if ( $report_type_profile["report_on_timeperiod"] == "yes" )
		{
            if ($report_date_questions->existing_report_dates)
			{
                   $valid_date_form_answers = $report_date_questions->existing_report_dates;
            }
            else
			{
                  //echo "showing validated date questions<br>";
                  $valid_date_form_answers = $validation_date->vetted_form_answer;
            }
            //echo "Report date Profile<br>";
            $date_html = $report_date_questions->build_report_date_table ($valid_date_form_answers);
        }
        
        $profile = new Client_profile;
        $tag_values["{CLIENT PROFILE}"] = $profile->display_client_profile_table ($client_info);
        
        $tag_values["{RELATIONSHIPS}"] = $relationships->display_other_related_clients_table ();
        $tag_values["{EXISTING REPORTS}"] = $reports->display_household_reports_table ($client_info[0]["client_id"], $org_id);
        
        $tag_values["{HOUSEHOLD_REPORT}"] .= $reports->build_household_report ($pull_questions->questions,
                                              $pull_questions->question_elements, $report_answers, $date_html, $share_answer);
        
        display_ecma($report_type);
    }
    
    //echo "END display_referral_report<br>";
}


?>
Return current item: Homeless Mangement Information System