Rev 1 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
<?php
require_once "Template.class.php";
require_once "DBDatabase.class.php";
class DBTemplate extends Template {
public static $autoSave = true;
private $tableDef;
private $type; // the type of the template (view, edit, list) created
public function __construct( $tableDef, $type='view', $templatePath = null, $filename = null ) {
parent::__construct( $filename, $templatePath );
$this->tableDef = $tableDef;
$this->type = $type;
if ( ! isset( $this->filename ) ) $this->FileName( );
}
public function process( $dataset = null ) {
$this->dataset = array(
$this->tableDef->tableName => $this->tableDef->currentRecords
);
return parent::process(
$this->dataset
);
}
public function makeTemplate() {
switch ( $this->type ) {
case 'edit' : break;
case 'list' : $this->makeListTemplate();
break;
case 'view' : $this->makeViewTemplate();
break;
} //switch
if ( self::$autoSave ) {
$this->saveTemplate();
}
} // function makeTemplate
private function makeListTemplate() {
$columnHeaders = array();
$data = array();
foreach ( $this->tableDef->columns as $name => $definition ) {
$columnHeaders[] = $definition->displayName;
$data[] = "<datafield $definition->columnName>";
}
$this->template = "<table border='1'>\n";
$this->template .= "<tr><td>" . implode( '</td><td>', $columnHeaders ) . "</td></tr>\n";
$this->template .= "<loop " . $this->tableDef->tableName . ">\n";
$this->template .= "<tr><td>" . implode( '</td><td>', $data ) . "</td></tr>\n";
$this->template .= "</loop>\n</table>\n";
} // makeListTemplate
private function makeViewTemplate () {
$this->template = '';
foreach ( $this->tableDef->columns as $name => $definition ) {
$this->template .= "<tr><td>$definition->displayName</td><td>" .
"<datafield $definition->columnName></td></tr>\n";
}
$this->template = "\n$this->template</table>\n";
} // makeViewTemplate
/*
* name: loadTemplate
* parameters: $filename -- optional override of filename stored in instance
* returns: nothing
* description: Loads the template file from disk. This is an OVERLOAD of Templates::loadTemplate;
*/
public function loadTemplate ( $type = null, $filename = null ) {
if ( isset( $filename ) )
$this->FileName( $filename );
if ( isset( $type ) )
$this->type = $type;
try {
parent::loadTemplate( );
} catch ( Exception $e ) {
$this->makeTemplate();
}
} // loadTemplate
public function saveTemplate() {
$results = file_put_contents( $this->filename, $this->template );
if ( $results === false )
throw new Exception( 'Can not write to [' . $this->filename . '], template not saved', 16 );
return $results;
} // loadTemplate
public function FileName ( $filename = null ) {
return parent::FileName( isset( $filename ) ?
$filename :
$this->tableDef->tableName . '-' . $this->type . '.template'
);
}
}
?>