[ Indice ]

Riferimento incrociato di Joomla! 1.5.14 - VM 1.1.4

Servizio fornito da VMItalia
Classe:   Funzione:   Variabile:   Costante:  
Storico Ricerche +

titolo

Corpo

[chiudi]

/libraries/pattemplate/patTemplate/Reader/ -> DB.php (sorgente)

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


Generato il: Mon Oct 19 20:29:27 2009 Generato con PHPXref 0.7