Source for file Select.php
Documentation is available at Select.php
* A Framework for Creating and Using Complex Web Elements
* The purpose of this framework is to provide a library of high-level objects
* to facilitate common HTML coding tasks, such as menus, tables, and forms.
* The intent is to reduce repetitive HTML coding as much as possible, replacing
* it with a combination of configuration files and style sheets with
* standardized naming conventions.
* This framework is built on and requires the
* {@link http://framework.zend.com/ Zend Framework}.
* @author Lyle Frost <lfrost@cnz.com>
* @copyright Copyright (c) 2006-2007 Citadel Network <{@link http://www.citadelnetwork.com/}>
* @license http://www.citadelnetwork.com/license/cnzframework New BSD License
* @version $Id: Select.php 27 2007-07-19 18:47:54Z lfrost $
Zend_Loader::loadClass('Cnz_Html_Form_Item');
* This class automates the creation and display of a selection list. The list
* can be populated from either a static list or from a database.
* Example INI for a static list:
* item.title.label = Title
* item.title.type = select
* If two fields are specified for a list item, the first is taken as the value
* and the second as the label for that item.
* Example INI for database lookup:
* item.user_id.label = Name
* item.user_id.type = select
* item.user_id.db.from = user
* item.user_id.db.order = name_last
* item.user_id.db.value = id
* item.user_id.db.label = "COALESCE(name_first, '')||' '||name_last"
/** Associative array value => label */
private $itemArray = NULL;
/* Methods ===========================================================*/
* dbFrom Table from which to query the list
* dbOrder Column(s) on which to order the list
* dbValue Column for value
* dbLabel SQL expression for label
* @param array $options Options
* @param Zend_Config $config Configuration
public function __construct(array $options = array(), Zend_Config $config = NULL)
elseif (isset ($config->db))
if (isset ($config->db->from)) $this->dbFrom = $config->db->from;
if (isset ($config->db->order)) $this->dbOrder = $config->db->order;
if (isset ($config->db->value)) $this->dbValue = $config->db->value;
// HACK: Zend_Config_Ini does not allow " in values.
if (isset ($config->db->label)) $this->dbLabel = str_replace('`', '"', $config->db->label);
Cnz_Html::getLogger()->err(__METHOD__ . '[' . __LINE__ . '] ' . 'No list data specified for ' . $this->name . '.');
* @param string $style Value for outermost style attribute
public function display($style = NULL)
// If database, retrieve list.
if (!empty($this->dbFrom))
->from($this->dbFrom, array($this->dbValue, new Zend_Db_Expr($this->dbLabel . ' AS ' . 'label')))
$stmt = $select->query();
$rowArray = $stmt->fetchAll();
$this->itemArray = array();
$this->itemArray[''] = NULL; // blank selection
foreach ($rowArray as $row)
$this->itemArray[$row[$this->dbValue]] = $row['label'];
echo $indent, '<div class = "', $this->genStyles('label'), '"><label for = "', $this->genId(), '">', empty($this->label) ? ' ' : $this->label, '</label></div>', "\n";
echo $indent, '<div class = "', $this->genStyles('field'), '">';
echo '<select id = "', $this->genId(), '" name = "', $this->name, '">', "\n";
foreach ($this->itemArray as $value => $label)
echo $indent, $is, '<option';
echo ' selected = "selected"';
if (empty($label)) echo $value; else echo $label;
echo $indent, '</select>';
|