Calcuating Age VT Approach
From PikaDocs
| Table of contents |
The Problem
One thing that has been frustrating for us is that if a screener does not get the DOB during the very beginning of screening (when getting name, SSN and then clicking Search Names and Browse Names), but instead gets the DOB after they have already created a client record, the age on the case is not auto filled. As best as I can tell, this is because the process of creating a new client also creates the case once you have clicked the Continue button on that initial screen.
There are a couple ways of dealing with this. When I first spoke to Aaron about it, he indicated his preferred approach would be some PHP code that would check if the age field on the Eligibility tab was not filled, and fill it in if it could. This sounds fine to me, but after a couple more weeks of testing I find we are creating many cases without the age filled in. I decided to see if I could cobble something together myself.
Basically I took Matt's code for the automatic zip code lookup and adapted it to do an automatic age lookup. It uses an iframe and javascript, as I understand it, something of a simpler precursor to AJAX. But don't get me wrong - it works well!
Two Roads Diverged in the Code
While working on the code, I had some problems and happened to search Pika Docs for iframe for help. That lead me to discover that LSNC had already come across this problem and solved it. However, their approach to ages and DOBs is different enough from ours that it doesn't really work for us. They keep the age field and DOB field together on the contact screen (/subtemplates/contact_full.html). They worked up an age-server.php to do the age lookup, in fact they appear to have started from Matt's code as well. We, however, want to use Pika's distinction between having the DOB with the contact and the age with the case. Thus, our code needs to deal with that.
Modifying the subtemplate
I started with zip-server.php, renamed it to vt-age-server.php just to avoid naming it age-server.php. The latter name makes perfect sense, but is what LSNC used so I didn't want to duplicate. If this ever makes it into mainstream Pika, Aaron will get dictate a name of course, such as this-car-is-powered-by-AJAX.php or whatever.
Next step, edit /subtemplates/case-elig.html: 1) At the very end of the file, add a new iframe:
<iframe name="ageframe" src="%%[base_url]%%/vt-age-server.php" style="display: none" width="300" height="50" frameborder="0"></iframe>
Yes, the width and height aren't really needed. But if you need to troubleshoot and see what's in there, you can remove the style="display: none" and see what's going on.
2) Just above the iframe in the javascript section, add a new function:
function update_age()
{
parent.ageframe.location.href = '%%[base_url]%%/vt-age-server.php?go=1&case_id=' + document.ws.case_id.value;
return;
}
3) Now you need to make sure this function gets called. So find the words "Client Age" in the form, and make them a hyperlink as follows:
<a href="#" onClick="update_age();" title="Click to Fill Client's Age">Client Age</a>:<br/>
Summary of how this works:
1) User clicks the hyperlink "Client Age", which
2) Calls the javascript function update_age(), which
3) calls a new URL in the hidden iframe, which (code for this follows below)
4) using the case_id, looks up the client_id
5) using the client_id, looks up the DOB
6) calculates the age
7) writes the age into the form on the eligibility tab
The PHP code
Create a new file in the root of Pika called vt-age-server.php (see above for discussion of alternate names). Paste all the following code into it.
<?php
/**********************************/
/* Pika CMS (C) 2002 Aaron Worley */
/* http://pikasoftware.net */
/**********************************/
// completely new file
// Andrew Cameron 2006/3/13
require_once ('pika_cms.php');
$go = pl_grab_var('go', 'GET', '0');
$go = mysql_escape_string($go);
$case_id = pl_grab_var('case_id', 'GET', '');
$case_id = mysql_escape_string($case_id);
$nodob = 0;
$r = pl_query("SELECT client_id FROM cases WHERE case_id='$case_id' LIMIT 1");
$s = $r->fetchRow();
$client_id = $s['client_id'];
$r2 = pl_query("SELECT birth_date FROM contacts WHERE contact_id='$client_id' LIMIT 1");
$s2 = $r2->fetchRow();
$dob = $s2['birth_date'];
// if birth_date is NULL then flag it and use below to warn user
if ($dob=="")
$nodob = 1;
else
$age = pl_calc_age($dob);
?>
<html>
<body>
<?php
// for troubleshooting
echo $_SERVER['REQUEST_URI'];
if ($go=='1')
{
?>
<form name=ageform>
<input name=dob type=text value="<?php echo $dob; ?>">
<input name=client_age type=text value="<?php echo $age; ?>">
<input name=nodob type=text value="<?php echo $nodob; ?>">
</form>
</form>
<script language="JavaScript" type="text/javascript"><!--
function updateAge()
{
//alert('boop');
if (parent.document.ws.client_age.value.length != 0)
alert ("Client's Age is already filled in. If you really want to recalculate it, you must erase the existing value first");
else if (document.ageform.nodob.value=="1")
alert ("The client's DOB is not filled in. Please click on the client's name to add the DOB, then come back here to calculate the age.");
else
parent.document.ws.client_age.value = document.ageform.client_age.value;
return;
}
updateAge();
//--></script>
<?php
}
?>
</html>
