Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: patTemplate

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /pattemplate/patTemplate/Reader/DB.php

Documentation is available at DB.php

  1. <?PHP
  2. /**
  3.  * patTemplate Reader that reads from a database using PEAR::DB
  4.  *
  5.  * $Id: DB.php 47 2005-09-15 02:55:27Z rhuk $
  6.  *
  7.  * @package        patTemplate
  8.  * @subpackage    Readers
  9.  * @author        Stephan Schmidt <[email protected]>
  10.  */
  11.  
  12. // Check to ensure this file is within the rest of the framework
  13. defined('JPATH_BASE'or die();
  14.  
  15. /**
  16.  * PEAR::DB is not installed
  17.  */
  18. define('PATTEMPLATE_READER_DB_ERROR_CLASS_NOT_FOUND''patTemplate::Reader::DB::001');
  19.  
  20. /**
  21.  * Connection could not be established
  22.  */
  23. define('PATTEMPLATE_READER_DB_ERROR_NO_CONNECTION''patTemplate::Reader::DB::002');
  24.  
  25. /**
  26.  * Could not find input
  27.  */
  28. define('PATTEMPLATE_READER_DB_ERROR_NO_INPUT''patTemplate::Reader::DB::003');
  29.  
  30. /**
  31.  * Unknown input syntax
  32.  */
  33. define('PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT''patTemplate::Reader::DB::004');
  34.  
  35. /**
  36.  * patTemplate Reader that reads from a database using PEAR::DB
  37.  *
  38.  * $Id: DB.php 47 2005-09-15 02:55:27Z rhuk $
  39.  *
  40.  * @package        patTemplate
  41.  * @subpackage    Readers
  42.  * @author        Stephan Schmidt <[email protected]>
  43.  */
  44. {
  45.    /**
  46.     * reader name
  47.     * @access    private
  48.     * @var        string 
  49.     */
  50.     var    $_name = 'DB';
  51.  
  52.    /**
  53.     * read templates from the database
  54.     *
  55.     * Input may either be an SQL query or a string defining the location
  56.     * of the template using the format:
  57.     * <code>
  58.     * table[@key=value]/@templateField
  59.     * </code>
  60.     *
  61.     * @final
  62.     * @access    public
  63.     * @param    string    file to parse
  64.     * @return    array    templates
  65.     */
  66.     function readTemplates($input)
  67.     {
  68.         $content $this->getDataFromDb($input);
  69.         if (patErrorManager::isError($content)) {
  70.             return $content;
  71.         }
  72.         $templates $this->parseString($content);
  73.         return $templates;
  74.     }
  75.  
  76.    /**
  77.     * fetch the template data from the database
  78.     *
  79.     * @access   protected
  80.     * @param    string      input to read from
  81.     */
  82.     function getDataFromDb($input)
  83.     {
  84.         // check for PEAR DB
  85.         if (!class_exists('DB')) {
  86.             @include_once 'DB.php';
  87.             if (!class_exists('DB')) {
  88.                 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_CLASS_NOT_FOUND'This reader requires PEAR::DB which could not be found on your system.');
  89.             }
  90.         }
  91.  
  92.         // establish connection
  93.         $db &DB::connect($this->getTemplateRoot());
  94.         if (PEAR::isError($db)) {
  95.             return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_NO_CONNECTION'Could not establish database connection: ' $db->getMessage());
  96.         }
  97.  
  98.         $input $this->parseInputStringToQuery($input$db);
  99.         if (patErrorManager::isError($input)) {
  100.             return $input;
  101.         }
  102.  
  103.         $content $db->getOne($input);
  104.         if (PEAR::isError($content)) {
  105.             return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_NO_INPUT'Could not fetch template: ' $content->getMessage());
  106.         }
  107.         return $content;
  108.     }
  109.  
  110.    /**
  111.     * Parse the template location syntax to a query
  112.     *
  113.     * @access  private
  114.     * @param   string 
  115.     * @param   DB_common 
  116.     */
  117.     function parseInputStringToQuery($input$db)
  118.     {
  119.         // Input is no query
  120.         if (strstr($input'SELECT'!== false{
  121.             return $input;
  122.         }
  123.  
  124.         $matches array();
  125.         if (!preg_match('/^([a-z]+)\[([^]]+)\]\/@([a-z]+)$/i'$input$matches)) {
  126.             return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT'Could not parse input string.');
  127.         }
  128.  
  129.         $table         $matches[1];
  130.         $templateField $matches[3];
  131.         $where         array();
  132.         $tmp explode(','$matches[2]);
  133.         foreach ($tmp as $clause{
  134.             list($field$valueexplode('='trim($clause));
  135.             if ($field{0!== '@'{
  136.                 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT'Could not parse input string.');
  137.             }
  138.             $field substr($field1);
  139.             array_push($where$field '=' $db->quoteSmart($value));
  140.         }
  141.  
  142.         $query sprintf('SELECT %s FROM %s WHERE %s'$templateField$tableimplode(' AND '$where));
  143.         return $query;
  144.     }
  145.  
  146.    /**
  147.     * load template from any input
  148.     *
  149.     * If the a template is loaded, the content will not get
  150.     * analyzed but the whole content is returned as a string.
  151.     *
  152.     * @abstract    must be implemented in the template readers
  153.     * @param    mixed    input to load from.
  154.     *                     This can be a string, a filename, a resource or whatever the derived class needs to read from
  155.     * @return    string  template content
  156.     */
  157.     function loadTemplate($input)
  158.     {
  159.         $content $this->getDataFromDb($input);
  160.         return $content;
  161.     }
  162. }
  163. ?>

Documentation generated on Mon, 05 Mar 2007 20:56:25 +0000 by phpDocumentor 1.3.1