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_Abstract */
0022 // require_once 'Zend/Form/Decorator/Abstract.php';
0023 
0024 /**
0025  * Zend_Dojo_Form_Decorator_DijitContainer
0026  *
0027  * Render a dojo dijit layout container via a view helper
0028  *
0029  * Accepts the following options:
0030  * - helper:    the name of the view helper to use
0031  *
0032  * Assumes the view helper accepts four parameters, the id, content, dijit
0033  * parameters, and (X)HTML attributes; these will be provided by the element.
0034  *
0035  * @uses       Zend_Form_Decorator_Abstract
0036  * @package    Zend_Dojo
0037  * @subpackage Form_Decorator
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  * @version    $Id$
0041  */
0042 abstract class Zend_Dojo_Form_Decorator_DijitContainer extends Zend_Form_Decorator_Abstract
0043 {
0044     /**
0045      * View helper
0046      * @var string
0047      */
0048     protected $_helper;
0049 
0050     /**
0051      * Element attributes
0052      * @var array
0053      */
0054     protected $_attribs;
0055 
0056     /**
0057      * Dijit option parameters
0058      * @var array
0059      */
0060     protected $_dijitParams;
0061 
0062     /**
0063      * Container title
0064      * @var string
0065      */
0066     protected $_title;
0067 
0068     /**
0069      * Get view helper for rendering container
0070      *
0071      * @return string
0072      */
0073     public function getHelper()
0074     {
0075         if (null === $this->_helper) {
0076             // require_once 'Zend/Form/Decorator/Exception.php';
0077             throw new Zend_Form_Decorator_Exception('No view helper specified fo DijitContainer decorator');
0078         }
0079         return $this->_helper;
0080     }
0081 
0082     /**
0083      * Get element attributes
0084      *
0085      * @return array
0086      */
0087     public function getAttribs()
0088     {
0089         if (null === $this->_attribs) {
0090             $attribs = $this->getElement()->getAttribs();
0091             if (array_key_exists('dijitParams', $attribs)) {
0092                 unset($attribs['dijitParams']);
0093             }
0094             $this->_attribs = $attribs;
0095         }
0096         return $this->_attribs;
0097     }
0098 
0099     /**
0100      * Get dijit option parameters
0101      *
0102      * @return array
0103      */
0104     public function getDijitParams()
0105     {
0106         if (null === $this->_dijitParams) {
0107             $attribs = $this->getElement()->getAttribs();
0108             if (array_key_exists('dijitParams', $attribs)) {
0109                 $this->_dijitParams = $attribs['dijitParams'];
0110             } else {
0111                 $this->_dijitParams = array();
0112             }
0113 
0114             $options = $this->getOptions();
0115             if (array_key_exists('dijitParams', $options)) {
0116                 $this->_dijitParams = array_merge($this->_dijitParams, $options['dijitParams']);
0117                 $this->removeOption('dijitParams');
0118             }
0119         }
0120 
0121         // Ensure we have a title param
0122         if (!array_key_exists('title', $this->_dijitParams)) {
0123             $this->_dijitParams['title'] = $this->getTitle();
0124         }
0125 
0126         return $this->_dijitParams;
0127     }
0128 
0129     /**
0130      * Get title
0131      *
0132      * @return string
0133      */
0134     public function getTitle()
0135     {
0136         if (null === $this->_title) {
0137             $title = null;
0138             if (null !== ($element = $this->getElement())) {
0139                 if (method_exists($element, 'getLegend')) {
0140                     $title = $element->getLegend();
0141                 }
0142             }
0143             if (empty($title) && (null !== ($title = $this->getOption('legend')))) {
0144                 $this->removeOption('legend');
0145             }
0146             if (empty($title) && (null !== ($title = $this->getOption('title')))) {
0147                 $this->removeOption('title');
0148             }
0149 
0150             if (!empty($title)) {
0151                 if (null !== ($translator = $element->getTranslator())) {
0152                     $title = $translator->translate($title);
0153                 }
0154                 $this->_title = $title;
0155             }
0156         }
0157 
0158         return (empty($this->_title) ? '' : $this->_title);
0159     }
0160 
0161     /**
0162      * Render a dijit layout container
0163      *
0164      * Replaces $content entirely from currently set element.
0165      *
0166      * @param  string $content
0167      * @return string
0168      */
0169     public function render($content)
0170     {
0171         $element = $this->getElement();
0172         $view    = $element->getView();
0173         if (null === $view) {
0174             return $content;
0175         }
0176 
0177         $dijitParams = $this->getDijitParams();
0178         $attribs     = array_merge($this->getAttribs(), $this->getOptions());
0179 
0180         if (array_key_exists('legend', $attribs)) {
0181             if (!array_key_exists('title', $dijitParams) || empty($dijitParams['title'])) {
0182                 $dijitParams['title'] = $attribs['legend'];
0183             }
0184             unset($attribs['legend']);
0185         }
0186 
0187         $helper      = $this->getHelper();
0188         $id          = $element->getId() . '-' . $helper;
0189 
0190         if ($view->dojo()->hasDijit($id)) {
0191             trigger_error(sprintf('Duplicate dijit ID detected for id "%s; temporarily generating uniqid"', $id), E_USER_WARNING);
0192             $base = $id;
0193             do {
0194                 $id = $base . '-' . uniqid();
0195             } while ($view->dojo()->hasDijit($id));
0196         }
0197 
0198         return $view->$helper($id, $content, $dijitParams, $attribs);
0199     }
0200 }