<?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
class Report_date_questions
{
var $questions;
var $question_elements;
var $existing_report_dates;
var $begin_and_end_display;
function Report_date_questions ( $begin_unix='', $end_unix='', $begin_and_end_displ='' )
{
GLOBAL $unix_date;
$this->begin_and_end_display = $begin_and_end_displ;
if ( !$begin_unix )
{
$begin_unix = $unix_date;
$end_unix = $unix_date;
}
$text_begin_month = safe_date("m", $begin_unix);
$text_begin_day = safe_date("d", $begin_unix);
$text_begin_year = safe_date("Y", $begin_unix);
if ( !$begin_and_end_displ )
{
$text_end_month = safe_date("m", $end_unix);
$text_end_day = safe_date("d", $end_unix);
$text_end_year = safe_date("Y", $end_unix);
}
$text_max_year = safe_date("Y", $end_unix)+2;
$this->questions["report_month_begin"] = array ("question_id" => "report_month_begin", "question_title" => "Month:", "question_type" => "number", "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "2", "question_range_bottom" => "1", "question_range_top" => "12", "question_display_order" => "1019", "question_required" => "yes", "report_type_id" => "client_identifier_entry", "question_default_value" => $text_begin_month, "question_visible" => "yes", "question_ecma" => "");
$this->questions["report_day_begin"] = array ("question_id" => "report_day_begin", "question_title" => "Day:", "question_type" => "number", "question_field_size" => "2", "question_range_bottom" => "1", "question_range_top" => "31", "question_display_order" => "1019", "question_required" => "yes", "question_default_value" => $text_begin_day, "question_visible" => "yes", "question_ecma" => "");
$this->questions["report_year_begin"] = array ("question_id" => "report_year_begin", "question_title" => "Year:", "question_type" => "number", "question_field_size" => "4", "question_range_bottom" => "1970", "question_range_top" => $text_begin_year, "question_display_order" => "1019", "question_required" => "yes", "question_default_value" => $text_begin_year, "question_visible" => "yes", "question_ecma" => "");
if ( $begin_and_end_displ != 'yes_begin_only' )
{
$this->questions["report_month_end"] = array ("question_id" => "report_month_end", "question_title" => "Month:", "question_type" => "number", "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "2", "question_range_bottom" => "1", "question_range_top" => "12", "question_display_order" => "1019", "question_required" => "no", "report_type_id" => "client_identifier_entry", "question_default_value" => $text_end_month, "question_visible" => "yes", "question_ecma" => "");
$this->questions["report_day_end"] = array ("question_id" => "report_day_end", "question_title" => "Day:", "question_type" => "number", "question_field_size" => "2", "question_range_bottom" => "1", "question_range_top" => "31", "question_display_order" => "1019", "question_required" => "no", "question_default_value" => $text_end_day, "question_visible" => "yes", "question_ecma" => "");
$this->questions["report_year_end"] = array ("question_id" => "report_year_end", "question_title" => "Year:", "question_type" => "number", "question_field_size" => "4", "question_range_bottom" => "1970", "question_range_top" => $text_max_year, "question_display_order" => "1019", "question_required" => "no", "question_default_value" => $text_end_year, "question_visible" => "yes", "question_ecma" => "");
}
}
/*
function insert_report_dates ($vetted_form_answer, $report_id) {
$begin_date = safe_mktime(0, 0, 0, $vetted_form_answer["report_month_begin"], $vetted_form_answer["report_day_begin"], $vetted_form_answer["report_year_begin"]);
$end_date = safe_mktime(0, 0, 0, $vetted_form_answer["report_month_end"], $vetted_form_answer["report_day_end"], $vetted_form_answer["report_year_end"]);
$sql = "UPDATE report_profile SET report_date_begin='".$begin_date."', report_date_end='".$end_date."' WHERE hh_report_id = '".$report_id."'";
run_query ($sql, "Insert report date begin");
}
*/
function translate_report_dates_to_unix_time ( $vetted_form_answer )
{
//safe_mktime fixes the issue we had with dates prior to 1970 but on form action and end dates may as well
//enforce a limit of 1970 anyways. A call to mktime() however with a negative timestamp (date prior to 1970)
//does cause errors in windows and linux.
if ($vetted_form_answer["report_year_begin"] && $vetted_form_answer["report_year_begin"] < 1970)
$vetted_form_answer["report_year_begin"] = '1970';
if ($vetted_form_answer["report_year_end"] && $vetted_form_answer["report_year_end"] < 1970)
$vetted_form_answer["report_year_end"] = '1970';
if ( !$vetted_form_answer )
{
//Windows doesn't like safe_mktime with NULLs (viewing a blank query_page)
$return_date["begin"] = "";
$return_date["end"] = "";
}
else
{
$return_date["begin"] = safe_mktime(0, 0, 0, $vetted_form_answer["report_month_begin"], $vetted_form_answer["report_day_begin"], $vetted_form_answer["report_year_begin"]);
if ( $this->begin_and_end_display != 'yes_begin_only' )
{
if( $vetted_form_answer["report_month_end"] || $vetted_form_answer["report_day_end"] || $vetted_form_answer["report_year_end"] )
{
$return_date["end"] = safe_mktime(0, 0, 0, $vetted_form_answer["report_month_end"], $vetted_form_answer["report_day_end"], $vetted_form_answer["report_year_end"]);
}
}
}
return $return_date;
}
function pull_existing_report_dates ($report_id, $report_type="")
{
if ( $report_type == 'client' )
{
$table = 'client_rpt_profile';
$report_id_column = 'client_rpt_id';
$c = 'c';
}
else
{
$table = 'report_profile';
$report_id_column = 'hh_report_id';
}
$sql = "SELECT report_date_begin".$c.", report_date_end".$c." FROM ".$table." WHERE ".$report_id_column." = '".$report_id."'";
$result = run_query ( $sql, 'Pulling existing report dates' );
$raw_unix_dates = fetch_array ( $result, 'Pulling exising report dates', '0' );
$begin = date_encrypted_translate_index_to_date_array ( $raw_unix_dates['report_date_begin'.$c] );
if ( $raw_unix_dates['report_date_end'.$c] != 0 )
$end = date_encrypted_translate_index_to_date_array ( $raw_unix_dates['report_date_end'.$c] );
$this->existing_report_dates["report_month_begin"] = $begin['month'];
$this->existing_report_dates["report_day_begin"] = $begin['day'];
$this->existing_report_dates["report_year_begin"] = $begin['year'];
$this->existing_report_dates["report_month_end"] = $end['month'];
$this->existing_report_dates["report_day_end"] = $end['day'];
$this->existing_report_dates["report_year_end"] = $end['year'];
return $this->existing_report_dates;
}
function validate_dates_exist ( $enforce_1970_date_limit="" )
{
//todo: Should put this function in Questions_answers_validation, and use in client report submission also. ($month_id, $day_id, $year_id)
//as stated above, no longer necessary to enforce 1970 date limit on form action and end dates but may as well set some limit just in case
//we need to pull safe_mktime and safe_date code. But once confident in this code we could kill all date limit enforcement related to negative
//timestamps on windows/linux. Also, the error validation/marking is inconsistant. Sometimes one function markes errors other times below
//marking active. Not well organized code but will do for now... ~Jeff
//echo " validate_dates_exist ( $enforce_1970_date_limit)<br>";
GLOBAL $form_answer, $question_validation_error, $message, $message_type;
$text_max_begin_year = safe_date("Y");
$text_max_end_year = safe_date("Y")+2;
//Validate 1970 date limit on begin date. End date checked below...
if ($enforce_1970_date_limit && ($form_answer["report_year_begin"] < 1970 || $form_answer["report_year_begin"]>=2038 ) )
{
//Error already marked...? todo: fix jeff's hack... why check this again?
//Previous check which markes the code would have caught this... but playing it safe for now...
if (!$question_validation_error["report_year_begin"])
question_error_marking ("report_year_begin", "Year must be between 1970 and $text_max_begin_year.");
if (!$message_set)
{
$message .= 'Action Date Year '.$form_answer['report_year_begin'].' is not valid. ';
$message_type = "error";
$message_set = "yes";
}
}
//If not a validate date, subtract one day and check again, repeat as necessary
if($form_answer["report_day_begin"] > 31) {$form_answer["report_day_begin"] = "31";}
if ($form_answer["report_month_begin"] && $form_answer["report_day_begin"] && $form_answer["report_year_begin"])
{
while(!checkdate ($form_answer["report_month_begin"], $form_answer["report_day_begin"], $form_answer["report_year_begin"]))
{
$form_answer["report_day_begin"]--;
$u++;
if ($u > 3)
{
question_error_marking ("report_month_begin", "This month-day-year combination is invalid.");
question_error_marking ("report_day_begin", "This month-day-year combination is invalid.");
question_error_marking ("report_year_begin", "This month-day-year combination is invalid.");
break 1;
echo "Break did not work<br>"; exit;
}
elseif (!$message_set)
{
$message .= "Begining day of query period adjusted because the entered day was not valid. ";
$message_type = "error";
$message_set = "yes";
}
}
}
if ( $this->begin_and_end_display != 'yes_begin_only' )
{
if( $form_answer["report_month_end"] || $form_answer["report_day_end"] || $form_answer["report_year_end"] )
{
//If not a validate date, subtract one day and check again, repeat as necessary
if($form_answer["report_day_end"] > 31) {$form_answer["report_day_end"] = "31";}
if ($form_answer["report_month_end"] && $form_answer["report_day_end"] && $form_answer["report_year_end"])
{
while(!checkdate ($form_answer["report_month_end"], $form_answer["report_day_end"], $form_answer["report_year_end"]))
{
$form_answer["report_day_end"]--;
$u++;
if ($u > 3)
{
question_error_marking ("report_month_end", "This month-day-year combination is invalid.");
question_error_marking ("report_day_end", "This month-day-year combination is invalid.");
question_error_marking ("report_year_end", "This month-day-year combination is invalid.");
break 1;
echo "Break did not work<br>"; exit;
}
elseif (!$message_set)
{
$message .= "Begining and/or end year of query period is not valid. See below... ";
$message_type = "error";
$message_set = "yes";
}
}
}
//Check to make sure the end is really after the begining
$test_begin_date = safe_mktime(0, 0, 0, $form_answer["report_month_begin"], $form_answer["report_day_begin"], $form_answer["report_year_begin"]);
$test_end_date = safe_mktime(0, 0, 0, $form_answer["report_month_end"], $form_answer["report_day_end"], $form_answer["report_year_end"]);
if($test_begin_date > $test_end_date)
{
question_error_marking ("report_month_begin", "The begining date must be before the end date. ");
question_error_marking ("report_day_begin", "The begining date must be before the end date. ");
question_error_marking ("report_year_begin", "The begining date must be before the end date. ");
}
if ($enforce_1970_date_limit && ($form_answer["report_year_end"] < 1970 || $form_answer["report_year_end"]>=2038) )
{
//Error already marked...? todo: fix jeff's hack... why check this again?
//Previous check which markes the code would have caught this... but playing it safe for now...
if (!$question_validation_error["report_year_end"]) //Error already marked...?
question_error_marking ('report_year_end', "Year must be between 1970 and $text_max_end_year.");
//if (!$message_set)
//{
$message .= 'End date year '.$form_answer['report_year_end'].' is not valid. ';
$message_type = "error";
$message_set = "yes";
//}
}
}
}
}
function build_report_date_table ($vetted_date_answers)
{
global $message;
$err = false;
/*if ($vetted_date_answers["report_year_begin"] < 1970)
{
$err = true;
$vetted_date_answers["report_year_begin"] = '1970';
}
if ($vetted_date_answers["report_year_end"] < 1970)
{
$err = true;
$vetted_date_answers["report_year_end"] = '1970';
}
if (!$message_set && $err)
{
$message .= "Begining and/or end year of query period adjusted because the year entered was not valid. Report end or action dates cannot be 1970 or prior to 1970.";
$message_type = "error";
$message_set = "yes";
}*/
$date_table_head = "<table border=0><tr><td class=\"genericsubtop\" colspan=\"3\">Action Date</td>";
$date_table_body = "<tr>";
$date_table_body .= question_display( $this->questions["report_month_begin"], $this->question_elements["report_month_begin"], $vetted_date_answers);
$date_table_body .= question_display( $this->questions["report_day_begin"], $this->question_elements["report_month_begin"], $vetted_date_answers);
$date_table_body .= question_display( $this->questions["report_year_begin"], $this->question_elements["report_month_begin"], $vetted_date_answers);
if ( $this->begin_and_end_display != 'yes_begin_only' )
{
$date_table_head .= "<td class=\"genericsubtop\" colspan=\"3\">End Date</td>";
$date_table_body .= question_display( $this->questions["report_month_end"], $this->question_elements["report_month_end"], $vetted_date_answers);
$date_table_body .= question_display( $this->questions["report_day_end"], $this->question_elements["report_day_end"], $vetted_date_answers);
$date_table_body .= question_display( $this->questions["report_year_end"], $this->question_elements["report_year_end"], $vetted_date_answers);
}
$date_table_head .= "</tr><tr>";
$date_table_body .= "</tr>";
$final_html .= $date_table_head.$date_table_body."</table>";
return $final_html;
}
}
?>