/**
* The required files
*/
require_once(HTML_BASIC_UTIL_PATH.'/Message.php');
if (defined('HTML_LOG_UTIL_PATH')) {
require_once(HTML_LOG_UTIL_PATH.'/Log.php');
}
/**
* The Data Transfer Object used as a data carrier between systems.
* The Data Reader towards the mysql database
* and the rendering of the ViewXyz table/form components
* <code>
* Usage:
* $datareader = new DataReader();
* // fill data into the datareader object from a call to mysql
*
* // Print the result
* print $datareader->getNumRows();
* print $datareader->getFieldName($name);
* </code>
* @package dto
*/
class DataReader {
/**
* @var int $numRows The number of rows returned in the query
*/
var $numRows = '';
/**
* @var boolean $rc The return code returned in the query
*/
var $rc = '';
/**
* @var array $rows The array of rows returned in the query
*/
var $rows = array(); // of rows
/**
* @var int $numFields The number of fields returned in the query
*/
var $numFields = '';
/**
* @var array $fieldName The array of names for each field
*/
var $fieldName = array();
/**
* @var array $fieldLen The array of length for each field
*/
var $fieldLen = array();
/**
* @var array $fieldType The array of types for each field
*/
var $fieldType = array();
/**
* @var array $fieldFlags The array of field flags for each field
*/
var $fieldFlags = array();
/**
* @var array $fieldTable The array of table names for each field
*/
var $fieldTable = array();
/**
* @var String $msg The message about the dto
*/
var $msg = '';
/**
* Returns the name of the instanciated class
* @return String The name of the class
*/
function getClassName() {
return "DataReader"; // __CLASS__
}
/**
* Set the named attribute (key) of a class to specified value
* Display an error message and exit, If not a known attribute
* i.e. You MUST define the attribute at class scope as ... var $myattr='';
* @param String $key The key to set with the value specified
* @param String $value The value of the key
*/
function set($key,$value) {
if (array_key_exists($key,get_class_vars(get_class($this)))) {
$this->$key = $value;
} else {
$msg = $this->getClassName()."->set() not a valid key<br />\r\n";
$msg .= "Format: class->key=value<br />\r\n ";
$msg .= 'Found : '.$this->getClassName().'->'.$key.'='.(isset($value)?''.$value:gettype($value));
$this->error($msg,__FILE__,__LINE__);
}
}
/**
* Get the complete html for a key
* Display an error message and exit, If not a known attribute
* i.e. You MUST define the 'key' at class scope as ... var $mykey='';
* @param String $key The value to return for the specified index
* @param String $index The index to use, if an array
* @param String $default The default value to use if no match
* @return String the html for the attribute (key)
*/
function get($key,$index='',$default='') {
$html = '';
$vars = get_object_vars($this);
if (array_key_exists($key,get_class_vars(get_class($this))) || array_key_exists($key, $vars)) {
if ($index!='' && is_array($this->$key)) {
if (array_key_exists($index, $this->$key)) {
$html = $this->{$key}[$index];
} else {
$msg = $this->getClassName()."->get('$index') not a valid key<br />\r\n";
$msg .= "Format: rc = class->key[index]<br />\r\n ";
$msg .= 'Found : '.$this->getClassName().'->'.$key.'['.(isset($index)?''.$index:gettype($index)).']'.' this->key='.$this->$key;
$this->error($msg,__FILE__,__LINE__);
//27-May-2007 13:56:51|4|DataReader->get(email) not a valid key<br />
//Format: rc = class->key[index]<br />
//Found : DataReader->fieldLen[email] this->key=Array|/var/www/html/myphp-1.10/myphp-1.10-dto/html/util/DataReader.php|141|80.166.135.151|http://www.finnrasmussen.dk/bryllup/guestlist/|Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)||
// TODO WHAT ???
$html = $this->{$key}[$index];
//die('DataReader.'.$html."- $key $index");
}
} else {
$html = $this->$key;
}
} else {
// Ignore
}
if ($html=='') {
$html .= $default;
}
return $html;
}
/**
* Add a row
* @param String $key The key to use in the array
* @param Object $value The value to add to array
* @param String $name The name to use in the class
*/
function add($key,$value,$name=DATA_READER_ROWS) {
$this->{$name}[$key]=$value;
}
/**
* Return the array of rows
* @param int $index The index to use
* @return array The array of rows
*/
function getRows($index='') {
return $this->get(DATA_READER_ROWS,$index);
}
/**
* Print or log error messages
* @param String $msg The message
* @param String $file The file name
* @param String $line The line number
*/
function error($msg,$file,$line) {
$this->setMsg("$msg file=$file in line=$line");
if (defined('HTML_LOG_UTIL_PATH')) {
Log::debug($file,$line,$msg);
} else {
Message::add($msg,__FILE__,__LINE__);
}
}
/**
* Helper functions
*/
function getRc() {
return $this->get(DATA_READER_RC);
}
function setRc($value) {
$this->set(DATA_READER_RC,$value);
}
/**
* Bugfix, when testing
*/
function getNumRows() {
return $this->get(DATA_READER_NUM_ROWS);
}
function setNumRows($value) {
$this->set(DATA_READER_NUM_ROWS,$value);
}
function getNumFields() {
return $this->get(DATA_READER_NUM_FIELDS);
}
function setNumFields($value) {
$this->set(DATA_READER_NUM_FIELDS,$value);
}
/**
* Get the FieldName, FieldLen, FieldType, FieldFlags, FieldTable
* @param int $index The index to get the value for
* @return String The requested value
*/
function getFieldName($index='') {
return $this->get(DATA_READER_FIELD_NAME,$index);
}
function getFieldLen($index='') {
return $this->get(DATA_READER_FIELD_LEN,$index);
}
function getFieldType($index='') {
return $this->get(DATA_READER_FIELD_TYPE,$index);
}
function getFieldFlags($index='') {
return $this->get(DATA_READER_FIELD_FLAGS,$index);
}
function getFieldTable($index='') {
return $this->get(DATA_READER_FIELD_TABLE,$index);
}
/**
* Add the FieldName, FieldLen, FieldType, FieldFlags, FieldTable
* @param String $key The key to use
* @param String $value The value to add for the key
*/
function addFieldName($key,$value) {
$this->add($key,$value,DATA_READER_FIELD_NAME);
}
function addFieldLen($key,$value) {
$this->add($key,$value,DATA_READER_FIELD_LEN);
}
function addFieldType($key,$value) {
$this->add($key,$value,DATA_READER_FIELD_TYPE);
}
function addFieldFlags($key,$value) {
$this->add($key,$value,DATA_READER_FIELD_FLAGS);
}
function addFieldTable($key,$value) {
$this->add($key,$value,DATA_READER_FIELD_TABLE);
}
/**
* Set the message
* @param String $msg The message
*/
function setMsg($msg) {
$this->msg = $msg;
}
/**
* Get the message
* @return String The message
*/
function getMsg() {
return $this->msg;
}
/**
* Get the html code
* @return String The html code
*/
function getHtml() {
return $this->getNumRows();
}
}
?>