From 7f7f1534a8a27bc4aea1cda817042eb7102c0c6e Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 21 Jan 2006 06:59:11 +0000 Subject: [PATCH] Added in relations system --- api.php | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/api.php b/api.php index 6eb217e..8f074d6 100644 --- a/api.php +++ b/api.php @@ -52,6 +52,26 @@ if (!defined('REQ_AUTO')) * Index for verification type */ define('F_VERIFY', 2); + + /** + * Index for relation + */ + define('F_RELATION', 3); + + /** + * Relation index for file name, relative to ISSO->apppath + */ + define('F_RELATION_FILE', 0); + + /** + * Relation index for class name + */ + define('F_RELATION_CLASS', 1); + + /** + * Relation index for field-link alternate name + */ + define('F_RELATION_ALTFIELD', 2); } /** @@ -78,7 +98,7 @@ class API /** * Fields: used for verification and sanitization - * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method)) + * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method), RELATION => array(FILE, CLASS IN FILE, ALTERNATE FIELD NAME)) * @var array * @access protected */ @@ -98,6 +118,13 @@ class API */ var $setfields = array(); + /** + * An array of all of the processed relations on an object + * @var array + * @access public + */ + var $relations = array(); + /** * WHERE condition * @var string @@ -126,6 +153,13 @@ class API */ var $norunners = array(); + /** + * Prevent relations from being synced in these actions + * @var array + * @access public + */ + var $norelations = array(); + // ################################################################### /** * Constructor: cannot instantiate class directly @@ -303,6 +337,8 @@ class API $this->run_action_method('post_fetch'); $this->objdata = $result; + + $this->call_relations('fetch'); } // ################################################################### @@ -419,6 +455,45 @@ class API $actmethod = (method_exists($this, $method) ? $this->$method() : ''); } + // ################################################################### + /** + * Determines if it's safe to run a relation; if so, it will return + * the WHERE SQL clause + * + * @access public + * + * @param string Field name + * @param string Operation to run + */ + function call_relations($method) + { + if (in_array($method, $this->norelations)) + { + return; + } + + foreach ($this->fields AS $field => $info) + { + $value = (isset($this->values["$field"]) ? $this->values["$field"] : $this->objdata["$field"]); + if ($value == null OR !is_array($info[F_RELATION])) + { + continue; + } + + if (!file_exists($this->registry->get('apppath') . $info[F_RELATION][F_RELATION_FILE])) + { + trigger_error("Could not load the relation file for field '$field'"); + } + + require_once($this->registry->get('apppath') . $info[F_RELATION][F_RELATION_FILE]); + + $this->relations["$field"] = new $info[F_RELATION][F_RELATION_CLASS]($this->registry); + $this->relations["$field"]->set(($info[F_RELATION][F_RELATION_ALTFIELD] ? $info[F_RELATION][F_RELATION_ALTFIELD] : $field), $value); + $this->relations["$field"]->set_condition(); + $this->relations["$field"]->$method(); + } + } + // ################################################################### /** * Prepares a value for use in a SQL query; it encases and escapes -- 2.22.5