• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/Zend/View/Helper/FormSelect.php

00001 <?php
00027 require_once 'Zend/View/Helper/FormElement.php';
00028 
00029 
00039 class Zend_View_Helper_FormSelect extends Zend_View_Helper_FormElement
00040 {
00064     public function formSelect($name, $value = null, $attribs = null,
00065         $options = null, $listsep = "<br />\n")
00066     {
00067         $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
00068         extract($info); // name, id, value, attribs, options, listsep, disable
00069 
00070         // force $value to array so we can compare multiple values to multiple
00071         // options; also ensure it's a string for comparison purposes.
00072         $value = array_map('strval', (array) $value);
00073 
00074         // check if element may have multiple values
00075         $multiple = '';
00076 
00077         if (substr($name, -2) == '[]') {
00078             // multiple implied by the name
00079             $multiple = ' multiple="multiple"';
00080         }
00081 
00082         if (isset($attribs['multiple'])) {
00083             // Attribute set
00084             if ($attribs['multiple']) {
00085                 // True attribute; set multiple attribute
00086                 $multiple = ' multiple="multiple"';
00087 
00088                 // Make sure name indicates multiple values are allowed
00089                 if (!empty($multiple) && (substr($name, -2) != '[]')) {
00090                     $name .= '[]';
00091                 }
00092             } else {
00093                 // False attribute; ensure attribute not set
00094                 $multiple = '';
00095             }
00096             unset($attribs['multiple']);
00097         }
00098 
00099         // now start building the XHTML.
00100         $disabled = '';
00101         if (true === $disable) {
00102             $disabled = ' disabled="disabled"';
00103         }
00104 
00105         // Build the surrounding select element first.
00106         $xhtml = '<select'
00107                 . ' name="' . $this->view->escape($name) . '"'
00108                 . ' id="' . $this->view->escape($id) . '"'
00109                 . $multiple
00110                 . $disabled
00111                 . $this->_htmlAttribs($attribs)
00112                 . ">\n    ";
00113 
00114         // build the list of options
00115         $list       = array();
00116         $translator = $this->getTranslator();
00117         foreach ((array) $options as $opt_value => $opt_label) {
00118             if (is_array($opt_label)) {
00119                 $opt_disable = '';
00120                 if (is_array($disable) && in_array($opt_value, $disable)) {
00121                     $opt_disable = ' disabled="disabled"';
00122                 }
00123                 if (null !== $translator) {
00124                     $opt_value = $translator->translate($opt_value);
00125                 }
00126                 $list[] = '<optgroup'
00127                         . $opt_disable
00128                         . ' label="' . $this->view->escape($opt_value) .'">';
00129                 foreach ($opt_label as $val => $lab) {
00130                     $list[] = $this->_build($val, $lab, $value, $disable);
00131                 }
00132                 $list[] = '</optgroup>';
00133             } else {
00134                 $list[] = $this->_build($opt_value, $opt_label, $value, $disable);
00135             }
00136         }
00137 
00138         // add the options to the xhtml and close the select
00139         $xhtml .= implode("\n    ", $list) . "\n</select>";
00140 
00141         return $xhtml;
00142     }
00143 
00153     protected function _build($value, $label, $selected, $disable)
00154     {
00155         if (is_bool($disable)) {
00156             $disable = array();
00157         }
00158 
00159         $opt = '<option'
00160              . ' value="' . $this->view->escape($value) . '"'
00161              . ' label="' . $this->view->escape($label) . '"';
00162 
00163         // selected?
00164         if (in_array((string) $value, $selected)) {
00165             $opt .= ' selected="selected"';
00166         }
00167 
00168         // disabled?
00169         if (in_array($value, $disable)) {
00170             $opt .= ' disabled="disabled"';
00171         }
00172 
00173         $opt .= '>' . $this->view->escape($label) . "</option>";
00174 
00175         return $opt;
00176     }
00177 
00178 }

Generated on Thu Apr 19 2012 17:01:18 for openbiz by  doxygen 1.7.2