. 73
( 132 .)


$this->mapped_styles = $this->sort_list($values);
return $this->mapped_styles;

write_to_db() This method overwrites the write_to_db() method from the Base
class. It™s derived from the multi-statement transaction methods defined in Base,
because writing a product to the database means not just storing information about
the product as a record in the products table, but also storing mappings of that
product to a style or set of styles.
After setting empty numeric properties to NULL, this method calls the parent
write_to_db() method to save the product record itself. It then deletes all existing
mappings between the current product and any styles and recreates them based on
the values in the $status_id property (which is an array passed in from a form).

function write_to_db()
static $numeric_fields = array(
foreach ($numeric_fields as $f)
if (empty($this->$f))
$this->$f = NULL;
Chapter 12: Catalog 429

˜delete from product_style_map where product_id = ?™
, array($this->product_id)
foreach ((array)$this->status_id as $style_id => $status_id)
foreach ((array)$this->new_style_id as $nid)
$this->map_style($nid, Product::Available);

MAP_STYLE() You saw this method called from the write_to_db() method. It
does a simple insert into the product_style_map table, after checking that the status
of the mapping is not one derived from a parent of the current product:

function map_style($style_id=0,$status_id=Product::Available)
if (!$style_id)
˜insert into product_style_map
values (?,?,?)™
, array($this->product_id,$style_id,$status_id)

IMG() This method creates an instance of the CatalogImage class and stores it in
the $img property of the current object:

function img()
if (!isset($this->img))
$this->img = new CatalogImage(array(
430 Part IV: Not So Simple Applications

return $this->img;

upload() This method is a front end to the upload() method of the CatalogImage
object created by the img() method. It passes on any arguments received to the
image™s upload() method, getting back the ID value of the new record created in
the images table (if there is one), which it then stores in the $image_id property of
the current product:

function upload()
$args = func_get_args();
$func = array($this->img(), ˜upload™);
$image_id = call_user_func_array($func, $args);
if ($image_id)
$this->image_id = $image_id;
return $image_id;

thumbnail() This is another front end method to the CatalogImage object. It sets
the $alt property of the image to the name of the current product, so that the alt
attribute of the <IMG> tag, which will ultimately be returned, matches the product
being displayed:

function thumbnail($href=NULL)
$this->img()->alt = $this->product;
return $this->img()->thumbnail($href=NULL);

__get() and __call() These are special methods, called “overloading” methods.
Their names are predefined by PHP. If you access an undefined property of an
object, the __get() method is called. Similarly, accessing an undefined method
causes __call() to be run. These methods enable you to use the properties and
methods of the CatalogImage object directly, as if they were properties and methods
of the Product object.

function __get($property)
return $this->img()->$property;
Chapter 12: Catalog 431

function __call($method, $args)
$img = $this->img();
return call_user_func_array(array($img,$method),$args);

Now that you have seen one class in its entirety, and have a feel for how the data
structures are created, it would be a waste of paper, as well as your time, to lay out
all the other classes here. As we™ve said, they™re designed to work similarly. If you
understand one, you really understand all of them.

If you™d like more detail on any of the remaining classes, see the comments
within the files on the CD.

In this section we™re going to tell you what you need to know to make use of the
remaining classes. (Note that the Catalog class was described earlier.)

Class Name:
CatalogBase ” Extends Base

Default Properties:

— $parents;

— $parent_ids;

— $children;

— $child_ids;

— $depth = 0;

— $appname = ˜catalog™;


— get_parent ” Retrieves the parent record from the table specified in the
$table property for the ID specified in the $id parameter. If it finds a
parent record, it calls itself to get the parent of that record, walking back
up the tree of ancestors until it comes to a root-level record (one where
432 Part IV: Not So Simple Applications

the parent_id field is zero or NULL). The record is then stored in the
$parent_ids property, indexed by ID value, and returned. The result is
a sequential list of parent records in inheritance order.
— list_parents ” Calls get_parent() on the current record and stores the
result in the $parents property. Also sets the $depth property to reflect
how far down the current record is (for example, a depth of zero means
this is a root-level record, a depth of two means this is the child of a child
of a root-level record, and so on).
— get_parent_ids ” Returns the ID values of the parent records retrieved
by list_parents().
— get_child_ids ” A similar method to get_parent, except in the other
direction. This method gets the ID values of all the records descended
from the current one and returns them as an array.
— get_child ” Gets descendant records (as opposed to just ID values) of
the current record. Optionally stops at a specified maximum depth ” for
example, setting a maximum depth of zero results in the retrieval of only
the first generation of child records.
— list_children ” Calls get_child, limiting the result to the immediate
children of the current record.
— list_all_children ” Calls get_child with no maximum depth, retriev-
ing all descendant records of the current record.
— sort_list ” Turns a nested associative array into a flat list, setting a
depth element in each record to indicate how far into the original array™s
nesting it was found.

Class Name:
CatalogImage ” Extends Image

Default Properties:

— $table = ˜images™;

— $idfield = ˜image_id™;

— $what = ˜image™;

— $fields = array(˜image_id™,™image™,™width™,™height™,™alt™);

— $id = NULL;
Chapter 12: Catalog 433

— $image_id = NULL;

— $appname = ˜catalog™;

— $src_dir = ˜/book/catalog/images™;


— __construct ” Sets the $file_dir property (inherited from Image) to the
full file system path of the /images directory of the Catalog application. It
then calls the __construct and build methods of its parent classes and
sets the $id property as a reference to the $image_id property.
— upload ” Calls the upload() method of the Image class, with a default
field name of imagefile. If the upload succeeds, the upload() method
writes itself to the images table in the database and returns the ID value of
the database record.
— check ” Fetches information from the database for the current image and
then calls the check() method of the parent Image class.

Class Name:
Style ” Extends CatalogBase

Default Properties:

— $table = ˜styles™;

— $idfield = ˜style_id™;

— $what = ˜style™;

— $fields = array(˜style_id™,™parent_id™,™style™,™description™);

— $id = 0;

— $style_id = 0;

— $parent_id = 0;

— $style = ˜™;

— $description = ˜™;

— $price = NULL;

— $price_type_id = NULL;
434 Part IV: Not So Simple Applications


— __construct ” Calls the __construct and build methods of the parent
CatalogBase and Base classes, and then sets the $id property as a refer-
ence to the $style_id property.

Sample script
Now that you understand the classes available, we show you how they are put to


. 73
( 132 .)