<?php
//*Client Data System, Copyright (C) 2000, 2001 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
class Relationships extends Client_profile {
var $related_reports_array;
var $related_clients_profile_array;
var $return_added_client_checked_value;
function Relationships ( $client_id, $hh_report_id = '' )
{
if ( !$client_id )
{
$client_id = '0';
}
$this->pull_relationships ( $client_id, $hh_report_id );
}
function display_other_related_clients_table ( $client_overview = '' )
{
GLOBAL $client_info, $unix_date;
$related_clients_array = $this->related_clients_profile_array;
//Compare the clients in $client_info with those related to the head of household in other reports and drop out the onces aready in $client_info
if(is_array($related_clients_array))
{
foreach($related_clients_array AS $key => $current_related_client_array)
{
if(!$client_overview)
{
foreach($client_info AS $current_client_info)
{
if ($current_client_info["client_id"] == $current_related_client_array["client_id"])
{
//echo "There is a match for client #".$current_client_info["client_id"]."<br>";
unset ($related_clients_array[$key]);
}
}
}
}
$final_html = $this->display_client_profile_table ( $related_clients_array,
'Clients related to the profile listed above.',
'clients_related' );
}
//Not an array (i.e., no related clients)
else {
$final_html .= "<table><tr><td>No Related Clients</td></tr></table>"; //$count_of_rows++;
}
//if($count_of_rows == 0) {$final_html .= "<table><tr><td>No Related Clients</td></tr>";}
//$final_html .= "</table>";
return $final_html;
}
function pull_relationships ( $client_id, $hh_report_id = '' )
{
//$sql = "SELECT DISTINCT(b.client_id), b.client_relationship_to_lead FROM report_relationship AS a, report_relationship AS b WHERE a.client_id = '".$client_id."' AND a.hh_report_id = b.hh_report_id AND b.client_id != '".$client_id."'";
if ($hh_report_id)
{
//grab specific hh report.
$sql = "
SELECT
rrb.client_id, rrb.client_relationship_to_lead, rrb.hh_report_id
FROM report_relationship
INNER JOIN report_relationship AS rrb ON ( report_relationship.hh_report_id = rrb.hh_report_id )
WHERE
report_relationship.client_id = '".$client_id."'
AND rrb.hh_report_id = '".$hh_report_id."'
ORDER BY rrb.hh_report_id DESC
";
}
else
{
//Order by ensures that rows from the last report entered into the system is the first listed.
$sql = "
SELECT
rrb.client_id, rrb.client_relationship_to_lead, rrb.hh_report_id
FROM report_relationship
INNER JOIN report_relationship AS rrb ON ( report_relationship.hh_report_id = rrb.hh_report_id )
WHERE
report_relationship.client_id = '".$client_id."'
ORDER BY rrb.hh_report_id DESC
";
}
//echo $sql." =========== <br/>";
$related_clients_result = run_query ($sql, "Pull reports related to client");
$related_clients_count = num_rows($related_clients_result);
$first_time = TRUE;
$hh_report_id_to_use = 0;
//echo "<br>There are matching clients: ".$related_clients_count."<br>";
for ($x = 0; $x < $related_clients_count; $x++)
{
//echo ' '.$x.'-';
$related_client_id_array = fetch_array ($related_clients_result, "Client related", $x);
$current_client_profile = $this->pull_client_profile_array ($related_client_id_array[0]);
$current_client_profile['client_relationship_to_lead'] = $related_client_id_array[1];
//echo '<table border=1><tr><td>current client</td><td>'.pretty_var_dump($current_client_profile).'</td></tr></table>';
//this line of code must come before what's below... jeffgu-bugfix--2-19-04
if ($first_time)
{
$hh_report_id_to_use = $related_client_id_array['hh_report_id'];
//echo ' hh_rep_id_to_use is now: '.$hh_report_id_to_use;
$first_time=FALSE;
}
//echo "<hr color=blue><font size=1>";
//echo "related_client_id_array: ".pretty_var_dump($related_client_id_array);
//echo "The Relationship is: ".$current_client_profile["client_relationship_to_lead"]."<br/>";
//echo "The Client ID is: ".$current_client_profile["client_id"]."<br/>";
//echo "The Year Born is is: ".$current_client_profile["100"]."<br/>";
//echo "The Gender is: ".$current_client_profile["101"]."<p/>";
if (!$first_time && $related_client_id_array['hh_report_id']!=$hh_report_id_to_use)
{
break;
}
else
{
$this->related_clients_profile_array[$current_client_profile['client_id']] = $current_client_profile;
}
//echo "<br>Match Relationship: ". $this->related_clients_profile_array[$current_client_profile["client_id"]]["client_gender"]."</font><hr color=blue>";
}
}
function process_added_clients_checkboxes ($form_answer_added_client)
{
GLOBAL $client_info;
//Set a high number for the client key (format $client_info(0)). The head of household starts at "0", so 100 should be plenty high to eliminate duplication
$client_info_key = 100;
//$this->pull_relationships ($client_id);
if(is_array($this->related_clients_profile_array)) {
foreach($this->related_clients_profile_array AS $key => $value) {
//echo "Looking at related client: ".$key.", whose client id is:".$value["client_id"]."<br/>";
if($form_answer_added_client[$key] == "yes")
{
//echo "It equals yes:, client info key is:".$client_info_key." <br/>";
$client_info[$client_info_key] = $value;
$client_info[$client_info_key]['client_relationship_to_lead'] =
$this->find_relationship_to_lead( $client_info[0]['client_id'], $client_info[$client_info_key]['client_id'] );
$_SESSION['client_infoB'][$client_info_key] = $client_info[$client_info_key];
$this->return_added_client_checked_value[$key] = 'yes';
}
$client_info_key++;
}
}
//return $this->return_added_client_checked_value;
}
function find_relationship_to_lead ( $lead_client_id, $associated_client_id, $get_current_relationship='' )
{
/*
$sql = "
SELECT b.client_relationship_to_lead
FROM
report_relationship AS a,
report_relationship AS b
WHERE
a.client_id = '".$lead_client_id."'
AND b.client_id != '".$associated_client_id."'";
*/
$sql = "
SELECT rrb.client_relationship_to_lead, rrb.hh_report_id
FROM
report_relationship
INNER JOIN report_relationship AS rrb ON ( report_relationship.hh_report_id = rrb.hh_report_id )
WHERE
report_relationship.client_id = '".$lead_client_id."'
AND rrb.client_id = '".$associated_client_id."'
ORDER BY rrb.hh_report_id DESC
";
//echo '<hr color=red>'.$sql;
$related_clients_result = run_query ( $sql, 'Pull relationship type to client' );
if ( num_rows ( $related_clients_result ) > 0 )
{
$related_client_id_array = fetch_array ( $related_clients_result, 'Client related', '0' );
//echo 'yowan'.pretty_var_dump($related_client_id_array);
//if get current then unset any client entry that has a decremented hh_report_id
if ($get_current_relationship)
{
$sql = "SELECT MAX(report_relationship.hh_report_id) FROM report_relationship where report_relationship.client_id = '".$lead_client_id."'";
$hh_rpt_id_result_to_match = run_query ( $sql, 'Pull last or MAX hh_report_id for lead client' );
//If there is an existing report, depreciate the old report by putting the new report id into the succeeded_by column
if( num_rows ($hh_rpt_id_result_to_match) > 0 )
{$hh_rpt_id_to_match = fetch_result ($hh_rpt_id_result_to_match, 'Pull matching hh_report_id');}
else
{echo 'error finding relationship to lead.';exit;}
if ($related_client_id_array['hh_report_id']!=$hh_rpt_id_to_match)
{
return 'unknown';
}
}
//echo "<br>Relationship is: ".$related_client_id_array[0]."<hr color=red>";
}
return $related_client_id_array['client_relationship_to_lead'];
}
//todo: remove? this function is no longer called...
function assign_looked_up_client_as_lead ()
{
GLOBAL $client_info;
$temp_holder = $client_info[0];
$client_info = "";
$client_info[0] = $temp_holder;
$client_info[0]['client_relationship_to_lead'] = 'head of household';
$_SESSION['client_infoB'] = $client_info;
}
}
?>