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

Source for file Select.php

Documentation is available at Select.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 Form
  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: Select.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_Form_Item');
  33.  
  34. /**
  35.  * HTML Form Select class
  36.  *
  37.  * This class automates the creation and display of a selection list.  The list
  38.  * can be populated from either a static list or from a database.
  39.  *
  40.  * Example INI for a static list:
  41.  *
  42.  * <pre><samp>
  43.  * item.title.label = Title
  44.  * item.title.type  = select
  45.  * item.title.list  = "
  46.  *         ,
  47.  *         Mr.,
  48.  *         Mrs.,
  49.  *         Ms.,
  50.  *         Dr.
  51.  * "
  52.  * </samp></pre>
  53.  *
  54.  * If two fields are specified for a list item, the first is taken as the value
  55.  * and the second as the label for that item.
  56.  
  57.  *
  58.  * Example INI for database lookup:
  59.  *
  60.  * <pre><samp>
  61.  * item.user_id.label    = Name
  62.  * item.user_id.type     = select
  63.  * item.user_id.db.from  = user
  64.  * item.user_id.db.order = name_last
  65.  * item.user_id.db.value = id
  66.  * item.user_id.db.label = "COALESCE(name_first, '')||' '||name_last"
  67.  * </samp></pre>
  68.  *
  69.  * @category   Cnz
  70.  * @package    Cnz_Html
  71.  * @subpackage Form
  72.  */
  73. {
  74.     /**#@+ @var array */
  75.  
  76.     /** Associative array value => label */
  77.     private $itemArray    NULL;
  78.  
  79.     /**#@-*/
  80.  
  81.     /**#@+ @var string */
  82.     private $dbFrom        NULL;
  83.     private $dbOrder    NULL;
  84.     private $dbLabel    NULL;
  85.     private $dbValue    NULL;
  86.     /**#@-*/
  87.  
  88.     /* Methods ===========================================================*/
  89.  
  90.     /**
  91.      * Option fields:
  92.      *   none
  93.      *
  94.      * Configuration fields:
  95.      *   dbFrom   Table from which to query the list
  96.      *   dbOrder  Column(s) on which to order the list
  97.      *   dbValue  Column for value
  98.      *   dbLabel  SQL expression for label
  99.      *
  100.      * @param  array       $options Options
  101.      * @param  Zend_Config $config  Configuration
  102.      */
  103.     public function __construct(array $options array()Zend_Config $config NULL)
  104.     {
  105.         parent::__construct($options$config);
  106.         if (isset($config->list)) $this->itemArray Cnz_Html::listToArray($config->list);
  107.         elseif (isset($config->db))
  108.         {
  109.             if (isset($config->db->from)) $this->dbFrom $config->db->from;
  110.             if (isset($config->db->order)) $this->dbOrder $config->db->order;
  111.             if (isset($config->db->value)) $this->dbValue $config->db->value;
  112.             // HACK:  Zend_Config_Ini does not allow " in values.
  113.             if (isset($config->db->label)) $this->dbLabel str_replace('`''"'$config->db->label);
  114.         }
  115.         else
  116.         {
  117.             Cnz_Html::getLogger()->err(__METHOD__ . '[' . __LINE__ . '] ' 'No list data specified for ' $this->name . '.');
  118.         }
  119.         return;
  120.     }
  121.  
  122.     /**
  123.      * @param  string $style Value for outermost style attribute
  124.      * @return void 
  125.      */
  126.     public function display($style NULL)
  127.     {
  128.         $is Cnz_Html::getIndentString();
  129.         $indent Cnz_Html::indentGenerate();
  130.  
  131.         // If database, retrieve list.
  132.         if (!empty($this->dbFrom))
  133.         {
  134.             $db Cnz_Html::getDb();
  135.             $select $db->select();
  136.             $select
  137.                 ->from($this->dbFromarray($this->dbValuenew Zend_Db_Expr($this->dbLabel ' AS ' 'label')))
  138.                 ->order($this->dbOrder);
  139.             $stmt $select->query();
  140.             $rowArray $stmt->fetchAll();
  141.             $this->itemArray array();
  142.             $this->itemArray[''NULL;    // blank selection
  143.             foreach ($rowArray as $row)
  144.             {
  145.                 $this->itemArray[$row[$this->dbValue]] $row['label'];
  146.             }
  147.         }
  148.  
  149.         echo $indent'<div class = "'$this->genStyles('label')'"><label for = "'$this->genId()'">'empty($this->label'&nbsp;' $this->label'</label></div>'"\n";
  150.         echo $indent'<div class = "'$this->genStyles('field')'">';
  151.  
  152.         echo '<select id = "'$this->genId()'" name = "'$this->name'">'"\n";
  153.         foreach ($this->itemArray as $value => $label)
  154.         {
  155.             echo $indent$is'<option';
  156.             if (!empty($this->initial&& $this->initial == $value)
  157.             {
  158.                 echo ' selected = "selected"';
  159.             }
  160.             echo ' value = "'htmlentities($value)'">';
  161.             if (empty($label)) echo $valueelse echo $label;
  162.             echo '</option>'"\n";
  163.         }
  164.         echo $indent'</select>';
  165.         echo '</div>'"\n";
  166.         return;
  167.     }
  168. }

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