Cnz_Html
[ class tree: Cnz_Html ] [ index: Cnz_Html ] [ all elements ]

Source for file Database.php

Documentation is available at Database.php

  1. <?php
  2. /**
  3.  * CNZ Framework
  4.  *
  5.  * A Framework for Creating and Using Complex Web Elements
  6.  *
  7.  * The purpose of this framework is to provide a library of high-level objects
  8.  * to facilitate common HTML coding tasks, such as menus, tables, and forms.
  9.  * The intent is to reduce repetitive HTML coding as much as possible, replacing
  10.  * it with a combination of configuration files and style sheets with
  11.  * standardized naming conventions.
  12.  *
  13.  * This framework is built on and requires the
  14.  * {@link http://framework.zend.com/ Zend Framework}.
  15.  *
  16.  * @category Cnz
  17.  * @package Cnz_Html
  18.  * @subpackage Table
  19.  *
  20.  * @author    Lyle Frost <lfrost@cnz.com>
  21.  * @copyright Copyright (c) 2006-2007 Citadel Network <{@link http://www.citadelnetwork.com/}>
  22.  * @filesource
  23.  * @license   http://www.citadelnetwork.com/license/cnzframework New BSD License
  24.  * @version   $Id: Database.php 27 2007-07-19 18:47:54Z lfrost $
  25.  */
  26.  
  27. /**
  28.  * Required classes.
  29.  *
  30.  * @ignore
  31.  */
  32. Zend_Loader::loadClass('Cnz_Html_Table');
  33. Zend_Loader::loadClass('Zend_Filter');
  34. Zend_Loader::loadClass('Zend_Filter_Alnum');
  35. Zend_Loader::loadClass('Zend_Db_Select');
  36.  
  37. /**
  38.  * HTML Table Database class
  39.  *
  40.  * @category Cnz
  41.  * @package Cnz_Html
  42.  * @subpackage Table
  43.  *
  44.  * @see Cnz_Html_Table
  45.  */
  46. {
  47.     /**#@+ @var array */
  48.     private $filterArray        array();
  49.     private $joinArray        array();
  50.     /**#@-*/
  51.  
  52.     /**#@+ @var string */
  53.     private $columns        '*';
  54.     private $from            NULL;
  55.     private $query            NULL;
  56.     /**#@-*/
  57.  
  58.     /* Getters/Setters ===================================================*/
  59.  
  60.     /** @return string Query */
  61.     public function getQuery()
  62.     {
  63.         return $this->query;
  64.     }
  65.  
  66.     /**
  67.      * @param  string $query New query
  68.      * @return void 
  69.      */
  70.     public function setQuery($query)
  71.     {
  72.         $this->query $query;
  73.         return;
  74.     }
  75.  
  76.     /* Methods ===========================================================*/
  77.  
  78.     /**
  79.      * Option fields:
  80.      *   none
  81.      *
  82.      * Configuration fields:
  83.      *   cols
  84.      *   from     SQL from clause
  85.      *   filters
  86.      *   join
  87.      *
  88.      * @param  array       $options Options
  89.      * @param  Zend_Config $config  Configuration
  90.      */
  91.     public function __construct(array $options array())
  92.     {
  93.         parent::__construct($options);
  94.  
  95.         // Process configuration.
  96.         if (isset($this->config->cols)) $this->columns $this->config->cols;
  97.         if (isset($this->config->from)) $this->from $this->config->from;
  98.         if (isset($this->config->filters)) $this->filterArray Cnz_Html::listToArray($this->config->filters);
  99.  
  100.         // Process joins.
  101.         if (isset($this->config->join))
  102.         {
  103.             for ($this->config->join->rewind()$this->config->join->valid()$this->config->join->next())
  104.             {
  105.                 $joinConfig $this->config->join->current();
  106.                 $join array();
  107.                 $join['name'$this->config->join->key();
  108.                 if (isset($joinConfig->type)) $join['type'$joinConfig->type;
  109.                 // HACK:  Zend_Config_Ini does not allow " in values.
  110.                 if (isset($joinConfig->on)) $join['on'str_replace('`''"'$joinConfig->on);
  111.                 if (isset($joinConfig->columns)) $join['columns'explode(','$joinConfig->columns);
  112.                 $this->joinArray[$join;
  113.             }
  114.         }
  115.  
  116.         return;
  117.     }
  118.  
  119.     /**
  120.      * @return boolean Success
  121.      */
  122.     protected function readData()
  123.     {
  124.         $offset $this->offset;
  125.         $limit $this->limit;
  126.  
  127.         $db Cnz_Html::getDb();
  128.  
  129.         $order array();
  130.         foreach ($this->orderArray as $name => $dir)
  131.         {
  132.             if (!empty($dir))
  133.             {
  134.                 $dir strtoupper($dir);
  135.                 $name .= ' ' $dir;
  136.             }
  137.             $order[$name;
  138.         }
  139.         $columns array();
  140.         foreach ($this->columnArray as $column)
  141.         {
  142.             if (strlen($column->getExpression()) 1)
  143.             {
  144.                 $columns[$column->getName();
  145.             }
  146.             else
  147.             {
  148.                 $columns[new Zend_Db_Expr($column->getExpression(' AS ' $column->getName());
  149.             }
  150.         }
  151.  
  152.         // Primary key (change to be configurable)
  153.         $columns['id';
  154.  
  155.         // Count rows.
  156.         $select $db->select();
  157.         $select->from($this->from$columns)
  158.             ->limit($this->limit$this->offset)
  159.             ->order($order);
  160.         foreach ($this->joinArray as $join)
  161.         {
  162.             $select->join($join['name']$join['on']$join['columns']);
  163.         }
  164.  
  165.         // Filter.
  166.         foreach ($this->filterArray as $key => $value)
  167.         {
  168.             $select->where($key ' = ?'$_POST[$key]);
  169.         }
  170.  
  171.         $this->query $select->__toString();
  172.  
  173.         // FIX count for joins
  174.         $result $db->query('SELECT count(*) FROM ' $db->quoteIdentifier($this->from));
  175.         $rowSet $result->fetchAll();
  176.         $this->rowCount = (int)$rowSet[0]['count'];
  177.         $this->pageCount = (int)ceil($this->rowCount / $limit);
  178.         if ($this->pageCount < 1$this->pageCount = 1;
  179.         $this->pageNumber = $offset $limit 1;
  180.  
  181.         // Get rows.
  182.         $result $db->query($this->query);
  183.         $result->setFetchMode(Zend_Db::FETCH_ASSOC);
  184.         $this->rowArray = $result->fetchAll();
  185.  
  186.         return true;
  187.     }
  188. }

Documentation generated on Thu, 19 Jul 2007 15:01:59 -0400 by phpDocumentor 1.4.0RC2