Source code for file /pattemplate/patTemplate/Reader/DB.php
Documentation is available at DB.php
* patTemplate Reader that reads from a database using PEAR::DB
* $Id: DB.php 47 2005-09-15 02:55:27Z rhuk $
// Check to ensure this file is within the rest of the framework
* PEAR::DB is not installed
define('PATTEMPLATE_READER_DB_ERROR_CLASS_NOT_FOUND', 'patTemplate::Reader::DB::001');
* Connection could not be established
define('PATTEMPLATE_READER_DB_ERROR_NO_CONNECTION', 'patTemplate::Reader::DB::002');
define('PATTEMPLATE_READER_DB_ERROR_NO_INPUT', 'patTemplate::Reader::DB::003');
define('PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT', 'patTemplate::Reader::DB::004');
* patTemplate Reader that reads from a database using PEAR::DB
* $Id: DB.php 47 2005-09-15 02:55:27Z rhuk $
* read templates from the database
* Input may either be an SQL query or a string defining the location
* of the template using the format:
* table[@key=value]/@templateField
* @param string file to parse
* @return array templates
$templates =
$this->parseString($content);
* fetch the template data from the database
* @param string input to read from
$input =
$this->parseInputStringToQuery($input, $db);
$content =
$db->getOne($input);
* Parse the template location syntax to a query
function parseInputStringToQuery($input, $db)
if (strstr($input, 'SELECT') !==
false) {
if (!preg_match('/^([a-z]+)\[([^]]+)\]\/@([a-z]+)$/i', $input, $matches)) {
$templateField =
$matches[3];
foreach ($tmp as $clause) {
array_push($where, $field .
'=' .
$db->quoteSmart($value));
$query =
sprintf('SELECT %s FROM %s WHERE %s', $templateField, $table, implode(' AND ', $where));
* load template from any input
* If the a template is loaded, the content will not get
* analyzed but the whole content is returned as a string.
* @abstract must be implemented in the template readers
* @param mixed input to load from.
* This can be a string, a filename, a resource or whatever the derived class needs to read from
* @return string template content