File indexing completed on 2025-03-02 05:29:19
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_Dojo 0017 * @subpackage View 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 /** Zend_View_Helper_HtmlElement */ 0024 // require_once 'Zend/View/Helper/HtmlElement.php'; 0025 0026 /** 0027 * Dojo dijit base class 0028 * 0029 * @uses Zend_View_Helper_Abstract 0030 * @package Zend_Dojo 0031 * @subpackage View 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 */ 0035 abstract class Zend_Dojo_View_Helper_Dijit extends Zend_View_Helper_HtmlElement 0036 { 0037 /** 0038 * @var Zend_Dojo_View_Helper_Dojo_Container 0039 */ 0040 public $dojo; 0041 0042 /** 0043 * Dijit being used 0044 * @var string 0045 */ 0046 protected $_dijit; 0047 0048 /** 0049 * Element type 0050 * @var string 0051 */ 0052 protected $_elementType; 0053 0054 /** 0055 * Parameters that should be JSON encoded 0056 * @var array 0057 */ 0058 protected $_jsonParams = array('constraints'); 0059 0060 /** 0061 * Dojo module to use 0062 * @var string 0063 */ 0064 protected $_module; 0065 0066 /** 0067 * Root node element type for layout elements 0068 * @var string 0069 */ 0070 protected $_rootNode = 'div'; 0071 0072 /** 0073 * Set view 0074 * 0075 * Set view and enable dojo 0076 * 0077 * @param Zend_View_Interface $view 0078 * @return Zend_Dojo_View_Helper_Dijit 0079 */ 0080 public function setView(Zend_View_Interface $view) 0081 { 0082 parent::setView($view); 0083 $this->dojo = $this->view->dojo(); 0084 $this->dojo->enable(); 0085 return $this; 0086 } 0087 0088 0089 /** 0090 * Get root node type 0091 * 0092 * @return string 0093 */ 0094 public function getRootNode() 0095 { 0096 return $this->_rootNode; 0097 } 0098 0099 /** 0100 * Set root node type 0101 * 0102 * @param string $value 0103 * @return Zend_Dojo_View_Helper_Dijit 0104 */ 0105 public function setRootNode($value) 0106 { 0107 $this->_rootNode = $value; 0108 return $this; 0109 } 0110 0111 /** 0112 * Whether or not to use declarative dijit creation 0113 * 0114 * @return bool 0115 */ 0116 protected function _useDeclarative() 0117 { 0118 return Zend_Dojo_View_Helper_Dojo::useDeclarative(); 0119 } 0120 0121 /** 0122 * Whether or not to use programmatic dijit creation 0123 * 0124 * @return bool 0125 */ 0126 protected function _useProgrammatic() 0127 { 0128 return Zend_Dojo_View_Helper_Dojo::useProgrammatic(); 0129 } 0130 0131 /** 0132 * Whether or not to use programmatic dijit creation w/o script creation 0133 * 0134 * @return bool 0135 */ 0136 protected function _useProgrammaticNoScript() 0137 { 0138 return Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript(); 0139 } 0140 0141 /** 0142 * Create a layout container 0143 * 0144 * @param int $id 0145 * @param string $content 0146 * @param array $params 0147 * @param array $attribs 0148 * @param string|null $dijit 0149 * @return string 0150 */ 0151 protected function _createLayoutContainer($id, $content, array $params, array $attribs, $dijit = null) 0152 { 0153 $attribs['id'] = $id; 0154 $attribs = $this->_prepareDijit($attribs, $params, 'layout', $dijit); 0155 0156 $nodeType = $this->getRootNode(); 0157 $html = '<' . $nodeType . $this->_htmlAttribs($attribs) . '>' 0158 . $content 0159 . "</$nodeType>\n"; 0160 0161 return $html; 0162 } 0163 0164 /** 0165 * Create HTML representation of a dijit form element 0166 * 0167 * @param string $id 0168 * @param string $value 0169 * @param array $params 0170 * @param array $attribs 0171 * @param string|null $dijit 0172 * @return string 0173 */ 0174 public function _createFormElement($id, $value, array $params, array $attribs, $dijit = null) 0175 { 0176 if (!array_key_exists('id', $attribs)) { 0177 $attribs['id'] = $id; 0178 } 0179 $attribs['name'] = $id; 0180 $attribs['value'] = (string) $value; 0181 $attribs['type'] = $this->_elementType; 0182 0183 $attribs = $this->_prepareDijit($attribs, $params, 'element', $dijit); 0184 0185 $html = '<input' 0186 . $this->_htmlAttribs($attribs) 0187 . $this->getClosingBracket(); 0188 return $html; 0189 } 0190 0191 /** 0192 * Merge attributes and parameters 0193 * 0194 * Also sets up requires 0195 * 0196 * @param array $attribs 0197 * @param array $params 0198 * @param string $type 0199 * @param string $dijit Dijit type to use (otherwise, pull from $_dijit) 0200 * @return array 0201 */ 0202 protected function _prepareDijit(array $attribs, array $params, $type, $dijit = null) 0203 { 0204 $this->dojo->requireModule($this->_module); 0205 0206 switch ($type) { 0207 case 'layout': 0208 $stripParams = array('id'); 0209 break; 0210 case 'element': 0211 $stripParams = array('id', 'name', 'value', 'type'); 0212 foreach (array('checked', 'disabled', 'readonly') as $attrib) { 0213 if (array_key_exists($attrib, $attribs)) { 0214 if ($attribs[$attrib]) { 0215 $attribs[$attrib] = $attrib; 0216 } else { 0217 unset($attribs[$attrib]); 0218 } 0219 } 0220 } 0221 break; 0222 case 'textarea': 0223 $stripParams = array('id', 'name', 'type', 'degrade'); 0224 break; 0225 default: 0226 } 0227 0228 foreach ($stripParams as $param) { 0229 if (array_key_exists($param, $params)) { 0230 unset($params[$param]); 0231 } 0232 } 0233 0234 // Normalize constraints, if present 0235 foreach ($this->_jsonParams as $param) { 0236 if (array_key_exists($param, $params)) { 0237 // require_once 'Zend/Json.php'; 0238 0239 if (is_array($params[$param])) { 0240 $values = array(); 0241 foreach ($params[$param] as $key => $value) { 0242 if (!is_scalar($value)) { 0243 continue; 0244 } 0245 $values[$key] = $value; 0246 } 0247 } elseif (is_string($params[$param])) { 0248 $values = (array) $params[$param]; 0249 } else { 0250 $values = array(); 0251 } 0252 $values = Zend_Json::encode($values); 0253 if ($this->_useDeclarative()) { 0254 $values = str_replace('"', "'", $values); 0255 } 0256 $params[$param] = $values; 0257 } 0258 } 0259 0260 $dijit = (null === $dijit) ? $this->_dijit : $dijit; 0261 if ($this->_useDeclarative()) { 0262 $attribs = array_merge($attribs, $params); 0263 if (isset($attribs['required'])) { 0264 $attribs['required'] = ($attribs['required']) ? 'true' : 'false'; 0265 } 0266 $attribs['dojoType'] = $dijit; 0267 } elseif (!$this->_useProgrammaticNoScript()) { 0268 $this->_createDijit($dijit, $attribs['id'], $params); 0269 } 0270 0271 return $attribs; 0272 } 0273 0274 /** 0275 * Create a dijit programmatically 0276 * 0277 * @param string $dijit 0278 * @param string $id 0279 * @param array $params 0280 * @return void 0281 */ 0282 protected function _createDijit($dijit, $id, array $params) 0283 { 0284 $params['dojoType'] = $dijit; 0285 0286 array_walk_recursive($params, array($this, '_castBoolToString')); 0287 0288 $this->dojo->setDijit($id, $params); 0289 } 0290 0291 /** 0292 * Cast a boolean to a string value 0293 * 0294 * @param mixed $item 0295 * @param string $key 0296 * @return void 0297 */ 0298 protected function _castBoolToString(&$item, $key) 0299 { 0300 if (!is_bool($item)) { 0301 return; 0302 } 0303 $item = ($item) ? "true" : "false"; 0304 } 0305 0306 /** 0307 * Render a hidden element to hold a value 0308 * 0309 * @param string $id 0310 * @param string|int|float $value 0311 * @return string 0312 */ 0313 protected function _renderHiddenElement($id, $value) 0314 { 0315 $hiddenAttribs = array( 0316 'name' => $id, 0317 'value' => (string) $value, 0318 'type' => 'hidden', 0319 ); 0320 return '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket(); 0321 } 0322 0323 /** 0324 * Create JS function for retrieving parent form 0325 * 0326 * @return void 0327 */ 0328 protected function _createGetParentFormFunction() 0329 { 0330 $function =<<<EOJ 0331 if (zend == undefined) { 0332 var zend = {}; 0333 } 0334 zend.findParentForm = function(elementNode) { 0335 while (elementNode.nodeName.toLowerCase() != 'form') { 0336 elementNode = elementNode.parentNode; 0337 } 0338 return elementNode; 0339 }; 0340 EOJ; 0341 0342 $this->dojo->addJavascript($function); 0343 } 0344 }