<<

. 79
( 132 .)



>>

$tpl->setVariable(˜form_action™, $_SERVER[˜PHP_SELF™]);
$tpl->parseCurrentBlock();

// actually display something, finally...
print start_page($default_page_title.™: Enter New Report™);
$tpl->show();
print end_page();
}

The form will be submitted to the page that called it. That page in turn ends up
calling the write_customer() function, which is discussed next.

write_customer.php
This function is long, if not terribly complicated. We will mention interesting parts
as they present themselves. It™s easiest to figure out by actually looking at it the
variety of actions this function can accomplish.
This page is largely a series of nested if...else statements. The simplest case is
that in which all or part of the customer record ” as identified by the
customer_id ” exists in the database already and when none of the information
Chapter 13: Problem-Tracking System 467

entered into the form conflicts with anything that™s in the database. If that™s so, we
just pick up the customer information and move along.
A more complicated situation exists when more than one record matches the
email and/or phone numbers entered into the form or when a single record matches
but differs in other respects from what™s in the database. The procedure here is to
show all matching records to the customer and allow him or her to take any of sev-
eral actions:

— Create a new record

— Choose one of the existing records to be used without modification
— Choose an existing record, merging it with the information in the form

Here is the complete listing of the function:

function write_customer(&$params)
{
$dbh = db_connect();

$output = TRUE;

$action = ˜insert™;
$duplicate = NULL;

// set up default variables for all the fields in customers table
// we end up with a key in params for every field in customers,
// set to NULL if we have no value
$customer_fields = $dbh->getCol(˜describe customers™);
foreach (array_diff($customer_fields, array_keys($params)) as $k)
$params[$k] = NULL;

// extract($params, EXTR_REFS);
// workaround for “BOGUS” bug #24630 with extract() :
// extracting with the EXTR_REFS flag will make any
// variables used in *creating* the array into
// references as well
foreach (array_keys($params) as $k)
$$k =& $params[$k];

// the hours for day and evening phones are filled in by default.
// if no phone number is given, set them to NULL

if (empty($day_prefix)) { $day_start = NULL; $day_end = NULL; }
if (empty($eve_prefix)) { $eve_start = NULL; $eve_end = NULL; }
468 Part IV: Not So Simple Applications

// the $action variable will be set to reflect what the script
// should do with the customer data as it passes through a
// myriad of tests. by default, create a new customer record

$errors = array();

// if we™ve been through this once already, the user may
// have passed on instructions
$dup_action = NULL;
if ($duplicate)
{
list($customer_id,$dup_action) = explode(˜-™,$duplicate);
}

// use the validate_email() function (defined in
// /book/functions/CheckEmail.php) to validate the format of the
// email address. (note: this does *not* verify that the email
// address is a real one, only that it looks like one.)

if (!validate_email($email)
&& empty($day_prefix)
&& empty($eve_prefix)
)
{
// we have to have either an email address or a phone number
// to contact the user. if we don™t, print out an error.
$errors[] = <<<EOQ

Without either a valid email address or a phone number,
we can™t contact you to resolve your problem.
Please enter at least one of these items
and submit your problem again. Thank you.

EOQ;
$action = ˜problems™;
}
elseif ($customer_id)
{
// we have a database ID for a specific customer record
$record = array(˜customer_id™ => $customer_id);
fetch_customer($record);

if ($dup_action == ˜merge™)
{
// the user has checked a radio button indicating
Chapter 13: Problem-Tracking System 469

// that we should combine the information in the form
// with the existing record in the customer table.

// begin parsing customer record.
foreach ($record as $field => $value)
{
if (is_string($value))
{
$value = trim($value);
}
if (is_string($$field))
{
$$field = trim($$field);
}
// for each column from the database record
if ($value != $$field && empty($$field))
{
// the value from the form ($$field -
// i.e. the variable with the same name
// as the column) is blank, and the
// current value of the column
// in the database is not blank.
// overwrite the form value with
// the value from the database.
$$field = $value;
}
}
// end parsing customer record.

// set $action to indicate that we should update
// the customer record, using the information from
// the form (which has been overwritten when called
// for above).
$action = ˜update™;
}
elseif ($dup_action == ˜override™)
{
// the user has checked a radio button indicating
// that all the information in the current database
// record should be overwritten by the values from
// the form.

// set $action to indicate that we should update
// the customer record using the information from
470 Part IV: Not So Simple Applications

// the form.

$customer_code = $record[˜customer_code™];
$action = ˜update™;
}
else
{
// use the customer™s record from the database
extract($record);
$action = ˜none™;
}
}
elseif ($dup_action == ˜add_as_new™)
{
// the user has clicked on a radio field indicating that
// a new customer record should be created.

// set $action to indicate that we should create a new record.
// with the information from the form.
$action = ˜insert™;
}
else
{
// either we haven™t checked for duplicate customer records yet,
// or the user didn™t tell us what to do about them.

// use the find_customer() function (defined in functions.php)
// to look for any existing customer records which might
// match the customer described in the form.

$result = find_customer($email
, $day_area, $day_prefix, $day_suffix
, $eve_area, $eve_prefix, $eve_suffix
);
if (count($result) == 0)
{
// either we don™t have any contact information,
// or no record was found matching this one.
// set $action to indicate that we should go ahead
// and create a new record.
$action = ˜insert™;
}
else
{
$action = ˜problems™;
Chapter 13: Problem-Tracking System 471

if (count($result) == 1)
{
// we found exactly one record which might match
// the form.
// get that record from the database, trimming
// strings and removing empty fields
$row = array_filter($result[0], ˜notempty™);

// do the same for current field values
$current = array_filter($params,™notempty™);

// check for differences
$diff = array_diff($row,$current);
foreach (array_keys($diff) as $k)
{
if (empty($current[$k]))
{
unset($diff[$k]);
}
}
if (!empty($diff))
{
// $errors[] = var_export($diff, TRUE);
$customer_id = 0;
}
else
{
$action = ˜none™;
extract($result[0]);
}
}
if ($action == ˜problems™)
{
$params[˜dup_results™] = $result;
}
}
}

if ($action == ˜problems™ || count($errors) > 0)
{
$params[˜error_messages™] = $errors;
return FALSE;
}

if ($action == ˜none™)
472 Part IV: Not So Simple Applications

{
// no change to existing customer record
}
else
{
// remove customer_id and customer_code from list of fields
// at first
$fields = array_diff(
$customer_fields
, array(˜customer_id™, ˜customer_code™)
);
$bind = array_key_value($params,$fields,NULL,™list™);
$where = NULL;
if ($action == ˜insert™)
{
// create a new customer record

<<

. 79
( 132 .)



>>