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_Form
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  */
0020 
0021 /** Zend_Form_Decorator_ViewHelper */
0022 // require_once 'Zend/Form/Decorator/ViewHelper.php';
0023 
0024 /**
0025  * Zend_Dojo_Form_Decorator_DijitElement
0026  *
0027  * Render a dojo dijit element via a view helper
0028  *
0029  * Accepts the following options:
0030  * - separator: string with which to separate passed in content and generated content
0031  * - placement: whether to append or prepend the generated content to the passed in content
0032  * - helper:    the name of the view helper to use
0033  *
0034  * Assumes the view helper accepts three parameters, the name, value, and
0035  * optional attributes; these will be provided by the element.
0036  *
0037  * @package    Zend_Dojo
0038  * @subpackage Form_Decorator
0039  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0040  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0041  * @version    $Id$
0042  */
0043 class Zend_Dojo_Form_Decorator_DijitElement extends Zend_Form_Decorator_ViewHelper
0044 {
0045     /**
0046      * Element attributes
0047      * @var array
0048      */
0049     protected $_attribs;
0050 
0051     /**
0052      * Element types that represent buttons
0053      * @var array
0054      */
0055     protected $_buttonTypes = array(
0056         'Zend_Dojo_Form_Element_Button',
0057         'Zend_Form_Element_Button',
0058         'Zend_Form_Element_Reset',
0059         'Zend_Form_Element_Submit',
0060     );
0061 
0062     /**
0063      * Dijit option parameters
0064      * @var array
0065      */
0066     protected $_dijitParams = array();
0067 
0068     /**
0069      * Get element attributes
0070      *
0071      * @return array
0072      */
0073     public function getElementAttribs()
0074     {
0075         if (null === $this->_attribs) {
0076             $this->_attribs = parent::getElementAttribs();
0077             if (array_key_exists('dijitParams', $this->_attribs)) {
0078                 $this->setDijitParams($this->_attribs['dijitParams']);
0079                 unset($this->_attribs['dijitParams']);
0080             }
0081         }
0082 
0083         return $this->_attribs;
0084     }
0085 
0086     /**
0087      * Set a single dijit option parameter
0088      *
0089      * @param  string $key
0090      * @param  mixed $value
0091      * @return Zend_Dojo_Form_Decorator_DijitContainer
0092      */
0093     public function setDijitParam($key, $value)
0094     {
0095         $this->_dijitParams[(string) $key] = $value;
0096         return $this;
0097     }
0098 
0099     /**
0100      * Set dijit option parameters
0101      *
0102      * @param  array $params
0103      * @return Zend_Dojo_Form_Decorator_DijitContainer
0104      */
0105     public function setDijitParams(array $params)
0106     {
0107         $this->_dijitParams = array_merge($this->_dijitParams, $params);
0108         return $this;
0109     }
0110 
0111     /**
0112      * Retrieve a single dijit option parameter
0113      *
0114      * @param  string $key
0115      * @return mixed|null
0116      */
0117     public function getDijitParam($key)
0118     {
0119         $this->getElementAttribs();
0120         $key = (string) $key;
0121         if (array_key_exists($key, $this->_dijitParams)) {
0122             return $this->_dijitParams[$key];
0123         }
0124 
0125         return null;
0126     }
0127 
0128     /**
0129      * Get dijit option parameters
0130      *
0131      * @return array
0132      */
0133     public function getDijitParams()
0134     {
0135         $this->getElementAttribs();
0136         return $this->_dijitParams;
0137     }
0138 
0139     /**
0140      * Render an element using a view helper
0141      *
0142      * Determine view helper from 'helper' option, or, if none set, from
0143      * the element type. Then call as
0144      * helper($element->getName(), $element->getValue(), $element->getAttribs())
0145      *
0146      * @param  string $content
0147      * @return string
0148      * @throws Zend_Form_Decorator_Exception if element or view are not registered
0149      */
0150     public function render($content)
0151     {
0152         $element = $this->getElement();
0153         $view = $element->getView();
0154         if (null === $view) {
0155             // require_once 'Zend/Form/Decorator/Exception.php';
0156             throw new Zend_Form_Decorator_Exception('DijitElement decorator cannot render without a registered view object');
0157         }
0158 
0159         $options = null;
0160         $helper    = $this->getHelper();
0161         $separator = $this->getSeparator();
0162         $value     = $this->getValue($element);
0163         $attribs   = $this->getElementAttribs();
0164         $name      = $element->getFullyQualifiedName();
0165 
0166         $dijitParams = $this->getDijitParams();
0167         $dijitParams['required'] = $element->isRequired();
0168 
0169         $id = $element->getId();
0170         if ($view->dojo()->hasDijit($id)) {
0171             trigger_error(sprintf('Duplicate dijit ID detected for id "%s; temporarily generating uniqid"', $id), E_USER_NOTICE);
0172             $base = $id;
0173             do {
0174                 $id = $base . '-' . uniqid();
0175             } while ($view->dojo()->hasDijit($id));
0176         }
0177         $attribs['id'] = $id;
0178 
0179         if (array_key_exists('options', $attribs)) {
0180                $options = $attribs['options'];
0181         }
0182 
0183         $elementContent = $view->$helper($name, $value, $dijitParams, $attribs, $options);
0184         switch ($this->getPlacement()) {
0185             case self::APPEND:
0186                 return $content . $separator . $elementContent;
0187             case self::PREPEND:
0188                 return $elementContent . $separator . $content;
0189             default:
0190                 return $elementContent;
0191         }
0192     }
0193 }