PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/zf/library/Zend/Dojo/View/Helper/ComboBox.php

http://github.com/eryx/php-framework-benchmark
PHP | 155 lines | 80 code | 12 blank | 63 comment | 15 complexity | fbc1c7fa333930b2c430e1f1bd16d9d0 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Dojo
 17 * @subpackage View
 18 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: ComboBox.php 23931 2011-05-02 19:32:51Z matthew $
 21 */
 22
 23/** Zend_Dojo_View_Helper_Dijit */
 24require_once 'Zend/Dojo/View/Helper/Dijit.php';
 25
 26/**
 27 * Dojo ComboBox dijit
 28 *
 29 * @uses       Zend_Dojo_View_Helper_Dijit
 30 * @package    Zend_Dojo
 31 * @subpackage View
 32 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 33 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 34  */
 35class Zend_Dojo_View_Helper_ComboBox extends Zend_Dojo_View_Helper_Dijit
 36{
 37    /**
 38     * Dijit being used
 39     * @var string
 40     */
 41    protected $_dijit  = 'dijit.form.ComboBox';
 42
 43    /**
 44     * HTML element type
 45     * @var string
 46     */
 47    protected $_elementType = 'text';
 48
 49    /**
 50     * Dojo module to use
 51     * @var string
 52     */
 53    protected $_module = 'dijit.form.ComboBox';
 54
 55    /**
 56     * dijit.form.ComboBox
 57     *
 58     * @param  int $id
 59     * @param  mixed $value
 60     * @param  array $params  Parameters to use for dijit creation
 61     * @param  array $attribs HTML attributes
 62     * @param  array|null $options Select options
 63     * @return string
 64     */
 65    public function comboBox($id, $value = null, array $params = array(), array $attribs = array(), array $options = null)
 66    {
 67        $html = '';
 68        if (!array_key_exists('id', $attribs)) {
 69            $attribs['id'] = $id;
 70        }
 71        if (array_key_exists('store', $params) && is_array($params['store'])) {
 72            // using dojo.data datastore
 73            if (false !== ($store = $this->_renderStore($params['store'], $id))) {
 74                $params['store'] = $params['store']['store'];
 75                if (is_string($store)) {
 76                    $html .= $store;
 77                }
 78                $html .= $this->_createFormElement($id, $value, $params, $attribs);
 79                return $html;
 80            }
 81            unset($params['store']);
 82        } elseif (array_key_exists('store', $params)) {
 83            if (array_key_exists('storeType', $params)) {
 84                $storeParams = array(
 85                    'store' => $params['store'],
 86                    'type'  => $params['storeType'],
 87                );
 88                unset($params['storeType']);
 89                if (array_key_exists('storeParams', $params)) {
 90                    $storeParams['params'] = $params['storeParams'];
 91                    unset($params['storeParams']);
 92                }
 93                if (false !== ($store = $this->_renderStore($storeParams, $id))) {
 94                    if (is_string($store)) {
 95                        $html .= $store;
 96                    }
 97                }
 98            }
 99            $html .= $this->_createFormElement($id, $value, $params, $attribs);
100            return $html;
101        }
102
103        // required for correct type casting in declerative mode 
104        if (isset($params['autocomplete'])) {
105            $params['autocomplete'] = ($params['autocomplete']) ? 'true' : 'false';
106        }
107        // do as normal select
108        $attribs = $this->_prepareDijit($attribs, $params, 'element');
109        return $this->view->formSelect($id, $value, $attribs, $options);
110    }
111
112    /**
113     * Render data store element
114     *
115     * Renders to dojo view helper
116     *
117     * @param  array $params
118     * @return string|false
119     */
120    protected function _renderStore(array $params, $id)
121    {
122        if (!array_key_exists('store', $params) || !array_key_exists('type', $params)) {
123            return false;
124        }
125
126        $this->dojo->requireModule($params['type']);
127
128        $extraParams = array();
129        $storeParams = array(
130            'dojoType' => $params['type'],
131            'jsId'     => $params['store'],
132        );
133
134        if (array_key_exists('params', $params)) {
135            $storeParams = array_merge($storeParams, $params['params']);
136            $extraParams = $params['params'];
137        }
138
139        if ($this->_useProgrammatic()) {
140            if (!$this->_useProgrammaticNoScript()) {
141                require_once 'Zend/Json.php';
142                $this->dojo->addJavascript('var ' . $storeParams['jsId'] . ";\n");
143                $js = $storeParams['jsId'] . ' = '
144                    . 'new ' . $storeParams['dojoType'] . '('
145                    .     Zend_Json::encode($extraParams)
146                    . ");\n";
147                $js = "function() {\n$js\n}";
148                $this->dojo->_addZendLoad($js);
149            }
150            return true;
151        }
152
153        return '<div' . $this->_htmlAttribs($storeParams) . '></div>';
154    }
155}