<<

. 64
( 132 .)



>>

{
static $_defaults = array(
˜table™ => NULL
, ˜key™ => NULL
, ˜value™ => NULL
, ˜columns™ => ˜*™
, ˜extra™ => NULL
, ˜key_op™ => ˜=™
, ˜key_join™ => ˜ and ˜
, ˜order_by™ => NULL
);
static $_simple = array(˜table™, ˜key™, ˜value™);
$args = func_get_args();
extract($_defaults);
$p = parse_arguments($args, $_simple, $_defaults);
Chapter 11: Content-Management System 371

extract($p, EXTR_IF_EXISTS);

$query = ˜select ! from !™;
$bind = array($columns,$table);
$where = NULL;
if (!empty($key) && !empty($value))
{
$where .= implode($key_join, array_fill(0, count($key), “!
$key_op ?”));
if (is_array($key) && is_array($value))
{
foreach ($key as $i => $k)
{
$bind[] = $k;
$bind[] = $value[$i];
}
}
else
{
$bind[] = $key;
$bind[] = $value;
}
}
if ($extra)
{
if ($where)
{
$where = “ ($where) and “;
}
$where .= “ ($extra) “;
}
if ($where)
{
$query .= ˜ where ˜.$where;
}
$order_by = (array)$order_by;
if (count($order_by) > 0)
{
$query .= ˜ order by ˜.implode(˜,™,$order_by);
}
$result = db_connect()->getAll($query, $bind,
DB_FETCHMODE_ASSOC);
if (!$result)
{
372 Part IV: Not So Simple Applications

$private_error = ˜could not fetch record: ˜
.™ query=™.$query
.™ bind=™.$bind
.™ result=™.$result
;
user_error(“Could not fetch $table record”, E_USER_ERROR);
exit;
}
if (count($result) == 1)
{
$result = array_shift($result);
}
return $result;
}

If the resulting data set has only one row, that row is returned directly. Otherwise,
the entire data set is returned. In either case, the constant DB_FETCHMODE_ASSOC
(defined by the DB library) tells the DB::getAll() method to return each row of
data as an associative array, with the column names from the query as keys.

db_values_array()
The db_values_array() function is similar to db_fetch_record() in that it™s a
shorthand for writing out a whole query. In this case, though, a list of values is
always returned and a particular table structure is assumed: that the name of the
table is the plural of the name of the label column, and that the name of the key
column is the name of the label column plus _id. You can pass in corrections to
these assumptions as arguments to the function (a common example from our code
here: the name of a status lookup table is usually ˜status™, not ˜statuss™).

function db_values_array ()
{
static $_defaults = array(
˜label™ => NULL
, ˜table™ => NULL
, ˜value™ => NULL
, ˜sort™ => NULL
, ˜where™ => NULL
);
static $_simple = array(˜label™,™table™);
$p = func_get_args();
extract($_defaults);
$p = parse_arguments($p, $_simple, $_defaults);
extract($p, EXTR_IF_EXISTS);

if (empty($label))
Chapter 11: Content-Management System 373

{
$label = str_replace(˜_id™,™™,$value);
}
elseif (empty($value))
{
$value = $label.™_id™;
}
if (empty($table))
{
$table = $label.™s™;
}
if (empty($sort))
{
$sort = $label;
}
if (empty($where))
{
$where = ˜1=1™;
}

$output = db_connect()->getAssoc(
˜select !, ! from ! where ! order by !™
, FALSE
, array($value,$label,$table,$where,$sort)
);
return $output;
}

The most common use of db_values_array() is to generate a list of values
from a database table for use in a SELECT field or group of option fields (radio but-
tons or checkboxes).

nullop()
The nullop() function returns either is or is not if the value being checked is
equal to NULL, and either = or <> otherwise. We use <> rather than != because the !
character has special meaning to the DB code (see the db_fetch_record() func-
tion, described previously in the chapter):

function nullop($value=NULL,$op=™=™)
{
if ($value === NULL)
{
if (strstr($op,™!=™))
{
$op = ˜is not™;
374 Part IV: Not So Simple Applications

}
else
{
$op = ˜is™;
}
}
else
{
if (strstr($op, ˜!=™))
{
$op = ˜<>™;
}
}
return $op;
}


Functions from /content/functions
These functions will be used throughout the application. This section will contain
many references to Chapter 9 because in that chapter we first used many of the
functions we™ll call upon here.

connect_validate_login()
In this example we are using MySQL™s own user and password tables to set up
accounts that can be used with this application. The success or failure of the
attempted connection to a MySQL server tells us if a username is valid or not. We
do this by splitting the authenticate() function used in previous examples into
several pieces, so that we can drop in our own validation code ” in this case,
connect_validate_login().

function connect_validate_login()
{
// if we haven™t started a session yet, the references to
$_SESSION
// will come up invalid, so start one if we need to.
check_session();
if (!isset($_SESSION))
{
global $_SESSION;
}

static $_defaults = array(
˜application™ => ˜content™
, ˜username™ => NULL
, ˜password™ => NULL
Chapter 11: Content-Management System 375

, ˜database™ => ˜netsloth™
);
if ($_defaults[˜username™] === NULL)
{
if (isset($_SESSION[˜PHP_AUTH_USER™]))
{
$_defaults[˜username™] = $_SESSION[˜PHP_AUTH_USER™];
}
if (isset($_SESSION[˜PHP_AUTH_PW™]))
{
$_defaults[˜password™] = $_SESSION[˜PHP_AUTH_PW™];
}
}
static $_simple = array(˜username™,™password™);
$args = func_get_args();
$p = parse_arguments($args, $_simple, $_defaults);

$ok = FALSE;

$p[˜db_error_level™] = E_USER_NOTICE;
ob_start();
$dbh = @db_connect($p);
ob_end_clean();
if ($dbh)
{
$ok = TRUE;
}
else
{
$p[˜error_message™] = “Could not connect\n”;
}
return $ok;
}

fetch_story()

This function enables us to get the record for a story.

function fetch_story ($args=NULL)
{
$story_id=NULL;
if (isset($_SESSION) && isset($_SESSION[˜PHP_AUTH_USER™]))
{
$this_username=$_SESSION[˜PHP_AUTH_USER™];
}
376 Part IV: Not So Simple Applications

else
{
$this_username = NULL;
}
if (is_assoc($args))
{
extract($args, EXTR_IF_EXISTS);
}
elseif (is_numeric($args))
{
$story_id = $args;
}

$query = <<<EOQ
select m.user_id as is_ok
, s.*
, date_format(s.publish_dt, ˜%Y™) as publish_yr
, date_format(s.publish_dt, ˜%m™) as publish_mn
, date_format(s.publish_dt, ˜%d™) as publish_dy

<<

. 64
( 132 .)



>>