Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

corey

By: a guest on Jul 8th, 2009  |  syntax: PHP  |  size: 4.27 KB  |  views: 585  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. class MW_View_Helper_FormMultiRadio extends Zend_View_Helper_FormElement {
  3.   protected $_inputType = 'radio';
  4.   protected $_isArray = false;
  5.   protected $_filter;
  6.   protected $_labelAttribs;
  7.   protected $_labelPlacement;
  8.   protected $_endTag;
  9.   protected $_id;
  10.   protected $_name;
  11.   protected $_attribs;
  12.  
  13.   public function formMultiRadio($name, $value = null, $attribs = null,
  14.       $options = null, $listsep = "<br />\n")
  15.   {
  16.     $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
  17.     extract($info); // name, value, attribs, options, listsep, disable
  18.  
  19.     // retrieve attributes for labels (prefixed with 'label_' or 'label')
  20.     $this->_label_attribs = array();
  21.     foreach ($attribs as $key => $val) {
  22.         $tmp    = false;
  23.         $keyLen = strlen($key);
  24.         if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
  25.             $tmp = substr($key, 6);
  26.         } elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
  27.             $tmp = substr($key, 5);
  28.         }
  29.  
  30.         if ($tmp) {
  31.             // make sure first char is lowercase
  32.             $tmp[0] = strtolower($tmp[0]);
  33.             $this->_label_attribs[$tmp] = $val;
  34.             unset($attribs[$key]);
  35.         }
  36.     }
  37.  
  38.     $this->_labelPlacement = 'append';
  39.     foreach ($this->_label_attribs as $key => $val) {
  40.         switch (strtolower($key)) {
  41.             case 'placement':
  42.                 unset($this->_label_attribs[$key]);
  43.                 $val = strtolower($val);
  44.                 if (in_array($val, array('prepend', 'append'))) {
  45.                     $this->_labelPlacement = $val;
  46.                 }
  47.                 break;
  48.         }
  49.     }
  50.  
  51.     // the radio button values and labels
  52.     $options = (array) $options;
  53.  
  54.     // build the element
  55.     $xhtml = '';
  56.     $list  = array();
  57.  
  58.     // should the name affect an array collection?
  59.     $name = $this->view->escape($name);
  60.     if ($this->_isArray && ('[]' != substr($name, -2))) {
  61.         $name .= '[]';
  62.     }
  63.  
  64.     // ensure value is an array to allow matching multiple times
  65.     $value = (array) $value;
  66.  
  67.     // XHTML or HTML end tag?
  68.     $this->_endTag = ' />';
  69.     if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
  70.         $this->_endTag= '>';
  71.     }
  72.     $this->_id = $id;
  73.     $this->_name = $name;
  74.     $this->_attribs = $attribs;
  75.  
  76.     // add radio buttons to the list.
  77.     require_once 'Zend/Filter/Alnum.php';
  78.     $this->_filter = new Zend_Filter_Alnum();
  79.    
  80.     foreach ($options as $opt_value => $opt_label) {
  81.       if (is_array($opt_label))
  82.       {
  83.         $list[] = $opt_value;
  84.         foreach ($opt_label as $val => $lab) {
  85.             $list[] = $this->_build($val, $lab, $value, $disable, $escape);
  86.         }
  87.       } else {
  88.         $list[] = $this->_build($opt_value, $opt_label, $value, $disable, $escape);
  89.       }
  90.     }
  91.  
  92.     // done!
  93.     $xhtml .= implode($listsep, $list);
  94.     return $xhtml;
  95.    
  96.    
  97.   }
  98.  
  99.   protected function _build($opt_value, $opt_label, $value, $disable, $escape)
  100.   {
  101.     // Should the label be escaped?
  102.     if ($escape) {
  103.         $opt_label = $this->view->escape($opt_label);
  104.     }
  105.  
  106.     // is it disabled?
  107.     $disabled = '';
  108.     if (true === $disable) {
  109.         $disabled = ' disabled="disabled"';
  110.     } elseif (is_array($disable) && in_array($opt_value, $disable)) {
  111.         $disabled = ' disabled="disabled"';
  112.     }
  113.  
  114.     // is it checked?
  115.     $checked = '';
  116.     if (in_array($opt_value, $value)) {
  117.         $checked = ' checked="checked"';
  118.     }
  119.  
  120.     // generate ID
  121.     $optId = $this->_id . '-' . $this->_filter->filter($opt_value);
  122.  
  123.     // Wrap the radios in labels
  124.     $radio = '<label'
  125.             . $this->_htmlAttribs($this->_label_attribs) . ' for="' . $optId . '">'
  126.             . (('prepend' == $this->_labelPlacement) ? $opt_label : '')
  127.             . '<input type="' . $this->_inputType . '"'
  128.             . ' name="' . $this->_name . '"'
  129.             . ' id="' . $optId . '"'
  130.             . ' value="' . $this->view->escape($opt_value) . '"'
  131.             . $checked
  132.             . $disabled
  133.             . $this->_htmlAttribs($this->_attribs)
  134.             . $this->_endTag
  135.             . (('append' == $this->_labelPlacement) ? $opt_label : '')
  136.             . '</label>';
  137.     return $radio;
  138.   }
  139. }