3 * patTemplate Reader that reads from a database using PEAR::DB
5 * $Id: DB.php 440 2008-03-30 09:00:16Z fishbone $
9 * @author Stephan Schmidt <schst@php.net>
13 * PEAR::DB is not installed
15 define('PATTEMPLATE_READER_DB_ERROR_CLASS_NOT_FOUND', 'patTemplate::Reader::DB::001');
18 * Connection could not be established
20 define('PATTEMPLATE_READER_DB_ERROR_NO_CONNECTION', 'patTemplate::Reader::DB::002');
23 * Could not find input
25 define('PATTEMPLATE_READER_DB_ERROR_NO_INPUT', 'patTemplate::Reader::DB::003');
28 * Unknown input syntax
30 define('PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT', 'patTemplate::Reader::DB::004');
33 * patTemplate Reader that reads from a database using PEAR::DB
35 * $Id: DB.php 440 2008-03-30 09:00:16Z fishbone $
37 * @package patTemplate
39 * @author Stephan Schmidt <schst@php.net>
41 class patTemplate_Reader_DB extends patTemplate_Reader
51 * read templates from the database
53 * Input may either be an SQL query or a string defining the location
54 * of the template using the format:
56 * table[@key=value]/@templateField
61 * @param string file to parse
62 * @return array templates
64 function readTemplates($input)
66 $content = $this->getDataFromDb($input);
67 if (patErrorManager::isError($content)) {
70 $templates = $this->parseString($content);
75 * fetch the template data from the database
78 * @param string input to read from
80 function getDataFromDb($input)
83 if (!class_exists('DB')) {
84 @include_once 'DB.php';
85 if (!class_exists('DB')) {
86 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_CLASS_NOT_FOUND, 'This reader requires PEAR::DB which could not be found on your system.');
90 // establish connection
91 $db = &DB::connect($this->getTemplateRoot());
92 if (PEAR::isError($db)) {
93 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_NO_CONNECTION, 'Could not establish database connection: ' . $db->getMessage());
96 $input = $this->parseInputStringToQuery($input, $db);
97 if (patErrorManager::isError($input)) {
101 $content = $db->getOne($input);
102 if (PEAR::isError($content)) {
103 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_NO_INPUT, 'Could not fetch template: ' . $content->getMessage());
109 * Parse the template location syntax to a query
115 function parseInputStringToQuery($input, $db)
118 if (strstr($input, 'SELECT') !== false) {
123 if (!preg_match('/^([a-z]+)\[([^]]+)\]\/@([a-z]+)$/i', $input, $matches)) {
124 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT, 'Could not parse input string.');
127 $table = $matches[1];
128 $templateField = $matches[3];
130 $tmp = explode(',', $matches[2]);
131 foreach ($tmp as $clause) {
132 list($field, $value) = explode('=', trim($clause));
133 if ($field{0} !== '@') {
134 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT, 'Could not parse input string.');
136 $field = substr($field, 1);
137 array_push($where, $field . '=' . $db->quoteSmart($value));
140 $query = sprintf('SELECT %s FROM %s WHERE %s', $templateField, $table, implode(' AND ', $where));
145 * load template from any input
147 * If the a template is loaded, the content will not get
148 * analyzed but the whole content is returned as a string.
150 * @abstract must be implemented in the template readers
151 * @param mixed input to load from.
152 * This can be a string, a filename, a resource or whatever the derived class needs to read from
153 * @return string template content
155 function loadTemplate($input)
157 $content = $this->getDataFromDb($input);