grc276-blog-laravel /src/application/libraries/Zend/Dojo/Form/Decorator/DijitContainer.php

Language PHP Lines 201
MD5 Hash 661e8cb74874108abdd105f55d54e65c Estimated Cost $2,332 (why?)
Repository https://bitbucket.org/masnug/grc276-blog-laravel View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Form
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/** Zend_Form_Decorator_Abstract */
require_once 'Zend/Form/Decorator/Abstract.php';

/**
 * Zend_Dojo_Form_Decorator_DijitContainer
 *
 * Render a dojo dijit layout container via a view helper
 *
 * Accepts the following options:
 * - helper:    the name of the view helper to use
 *
 * Assumes the view helper accepts four parameters, the id, content, dijit
 * parameters, and (X)HTML attributes; these will be provided by the element.
 *
 * @uses       Zend_Form_Decorator_Abstract
 * @package    Zend_Dojo
 * @subpackage Form_Decorator
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: DijitContainer.php 23775 2011-03-01 17:25:24Z ralph $
 */
abstract class Zend_Dojo_Form_Decorator_DijitContainer extends Zend_Form_Decorator_Abstract
{
    /**
     * View helper
     * @var string
     */
    protected $_helper;

    /**
     * Element attributes
     * @var array
     */
    protected $_attribs;

    /**
     * Dijit option parameters
     * @var array
     */
    protected $_dijitParams;

    /**
     * Container title
     * @var string
     */
    protected $_title;

    /**
     * Get view helper for rendering container
     *
     * @return string
     */
    public function getHelper()
    {
        if (null === $this->_helper) {
            require_once 'Zend/Form/Decorator/Exception.php';
            throw new Zend_Form_Decorator_Exception('No view helper specified fo DijitContainer decorator');
        }
        return $this->_helper;
    }

    /**
     * Get element attributes
     *
     * @return array
     */
    public function getAttribs()
    {
        if (null === $this->_attribs) {
            $attribs = $this->getElement()->getAttribs();
            if (array_key_exists('dijitParams', $attribs)) {
                unset($attribs['dijitParams']);
            }
            $this->_attribs = $attribs;
        }
        return $this->_attribs;
    }

    /**
     * Get dijit option parameters
     *
     * @return array
     */
    public function getDijitParams()
    {
        if (null === $this->_dijitParams) {
            $attribs = $this->getElement()->getAttribs();
            if (array_key_exists('dijitParams', $attribs)) {
                $this->_dijitParams = $attribs['dijitParams'];
            } else {
                $this->_dijitParams = array();
            }

            $options = $this->getOptions();
            if (array_key_exists('dijitParams', $options)) {
                $this->_dijitParams = array_merge($this->_dijitParams, $options['dijitParams']);
                $this->removeOption('dijitParams');
            }
        }

        // Ensure we have a title param
        if (!array_key_exists('title', $this->_dijitParams)) {
            $this->_dijitParams['title'] = $this->getTitle();
        }

        return $this->_dijitParams;
    }

    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        if (null === $this->_title) {
            $title = null;
            if (null !== ($element = $this->getElement())) {
                if (method_exists($element, 'getLegend')) {
                    $title = $element->getLegend();
                }
            }
            if (empty($title) && (null !== ($title = $this->getOption('legend')))) {
                $this->removeOption('legend');
            }
            if (empty($title) && (null !== ($title = $this->getOption('title')))) {
                $this->removeOption('title');
            }

            if (!empty($title)) {
                if (null !== ($translator = $element->getTranslator())) {
                    $title = $translator->translate($title);
                }
                $this->_title = $title;
            }
        }

        return (empty($this->_title) ? '' : $this->_title);
    }

    /**
     * Render a dijit layout container
     *
     * Replaces $content entirely from currently set element.
     *
     * @param  string $content
     * @return string
     */
    public function render($content)
    {
        $element = $this->getElement();
        $view    = $element->getView();
        if (null === $view) {
            return $content;
        }

        $dijitParams = $this->getDijitParams();
        $attribs     = array_merge($this->getAttribs(), $this->getOptions());

        if (array_key_exists('legend', $attribs)) {
            if (!array_key_exists('title', $dijitParams) || empty($dijitParams['title'])) {
                $dijitParams['title'] = $attribs['legend'];
            }
            unset($attribs['legend']);
        }

        $helper      = $this->getHelper();
        $id          = $element->getId() . '-' . $helper;

        if ($view->dojo()->hasDijit($id)) {
            trigger_error(sprintf('Duplicate dijit ID detected for id "%s; temporarily generating uniqid"', $id), E_USER_WARNING);
            $base = $id;
            do {
                $id = $base . '-' . uniqid();
            } while ($view->dojo()->hasDijit($id));
        }

        return $view->$helper($id, $content, $dijitParams, $attribs);
    }
}
Back to Top