File indexing completed on 2024-12-22 05:37:13
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_View 0017 * @subpackage Helper 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * @see Zend_View_Helper_HtmlElement 0025 */ 0026 // require_once 'Zend/View/Helper/HtmlElement.php'; 0027 0028 /** 0029 * Base helper for form elements. Extend this, don't use it on its own. 0030 * 0031 * @category Zend 0032 * @package Zend_View 0033 * @subpackage Helper 0034 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0035 * @license http://framework.zend.com/license/new-bsd New BSD License 0036 */ 0037 abstract class Zend_View_Helper_FormElement extends Zend_View_Helper_HtmlElement 0038 { 0039 /** 0040 * @var Zend_Translate_Adapter|null 0041 */ 0042 protected $_translator; 0043 0044 /** 0045 * Get translator 0046 * 0047 * @return Zend_Translate_Adapter|null 0048 */ 0049 public function getTranslator() 0050 { 0051 return $this->_translator; 0052 } 0053 0054 /** 0055 * Set translator 0056 * 0057 * @param Zend_Translate|Zend_Translate_Adapter|null $translator 0058 * @return Zend_View_Helper_FormElement 0059 */ 0060 public function setTranslator($translator = null) 0061 { 0062 if (null === $translator) { 0063 $this->_translator = null; 0064 } elseif ($translator instanceof Zend_Translate_Adapter) { 0065 $this->_translator = $translator; 0066 } elseif ($translator instanceof Zend_Translate) { 0067 $this->_translator = $translator->getAdapter(); 0068 } else { 0069 // require_once 'Zend/View/Exception.php'; 0070 $e = new Zend_View_Exception('Invalid translator specified'); 0071 $e->setView($this->view); 0072 throw $e; 0073 } 0074 return $this; 0075 } 0076 0077 /** 0078 * Converts parameter arguments to an element info array. 0079 * 0080 * E.g, formExample($name, $value, $attribs, $options, $listsep) is 0081 * the same thing as formExample(array('name' => ...)). 0082 * 0083 * Note that you cannot pass a 'disable' param; you need to pass 0084 * it as an 'attribs' key. 0085 * 0086 * @access protected 0087 * 0088 * @return array An element info array with keys for name, value, 0089 * attribs, options, listsep, disable, and escape. 0090 */ 0091 protected function _getInfo($name, $value = null, $attribs = null, 0092 $options = null, $listsep = null 0093 ) { 0094 // the baseline info. note that $name serves a dual purpose; 0095 // if an array, it's an element info array that will override 0096 // these baseline values. as such, ignore it for the 'name' 0097 // if it's an array. 0098 $info = array( 0099 'name' => is_array($name) ? '' : $name, 0100 'id' => is_array($name) ? '' : $name, 0101 'value' => $value, 0102 'attribs' => $attribs, 0103 'options' => $options, 0104 'listsep' => $listsep, 0105 'disable' => false, 0106 'escape' => true, 0107 ); 0108 0109 // override with named args 0110 if (is_array($name)) { 0111 // only set keys that are already in info 0112 foreach ($info as $key => $val) { 0113 if (isset($name[$key])) { 0114 $info[$key] = $name[$key]; 0115 } 0116 } 0117 0118 // If all helper options are passed as an array, attribs may have 0119 // been as well 0120 if (null === $attribs) { 0121 $attribs = $info['attribs']; 0122 } 0123 } 0124 0125 $attribs = (array)$attribs; 0126 0127 // Normalize readonly tag 0128 if (array_key_exists('readonly', $attribs)) { 0129 $attribs['readonly'] = 'readonly'; 0130 } 0131 0132 // Disable attribute 0133 if (array_key_exists('disable', $attribs)) { 0134 if (is_scalar($attribs['disable'])) { 0135 // disable the element 0136 $info['disable'] = (bool)$attribs['disable']; 0137 } else if (is_array($attribs['disable'])) { 0138 $info['disable'] = $attribs['disable']; 0139 } 0140 } 0141 0142 // Set ID for element 0143 if (array_key_exists('id', $attribs)) { 0144 $info['id'] = (string)$attribs['id']; 0145 } else if ('' !== $info['name']) { 0146 $info['id'] = trim(strtr($info['name'], 0147 array('[' => '-', ']' => '')), '-'); 0148 } 0149 0150 // Remove NULL name attribute override 0151 if (array_key_exists('name', $attribs) && is_null($attribs['name'])) { 0152 unset($attribs['name']); 0153 } 0154 0155 // Override name in info if specified in attribs 0156 if (array_key_exists('name', $attribs) && $attribs['name'] != $info['name']) { 0157 $info['name'] = $attribs['name']; 0158 } 0159 0160 // Determine escaping from attributes 0161 if (array_key_exists('escape', $attribs)) { 0162 $info['escape'] = (bool)$attribs['escape']; 0163 } 0164 0165 // Determine listsetp from attributes 0166 if (array_key_exists('listsep', $attribs)) { 0167 $info['listsep'] = (string)$attribs['listsep']; 0168 } 0169 0170 // Remove attribs that might overwrite the other keys. We do this LAST 0171 // because we needed the other attribs values earlier. 0172 foreach ($info as $key => $val) { 0173 if (array_key_exists($key, $attribs)) { 0174 unset($attribs[$key]); 0175 } 0176 } 0177 $info['attribs'] = $attribs; 0178 0179 // done! 0180 return $info; 0181 } 0182 0183 /** 0184 * Creates a hidden element. 0185 * 0186 * We have this as a common method because other elements often 0187 * need hidden elements for their operation. 0188 * 0189 * @access protected 0190 * 0191 * @param string $name The element name. 0192 * @param string $value The element value. 0193 * @param array $attribs Attributes for the element. 0194 * 0195 * @return string A hidden element. 0196 */ 0197 protected function _hidden($name, $value = null, $attribs = null) 0198 { 0199 return '<input type="hidden"' 0200 . ' name="' . $this->view->escape($name) . '"' 0201 . ' value="' . $this->view->escape($value) . '"' 0202 . $this->_htmlAttribs($attribs) . $this->getClosingBracket(); 0203 } 0204 }