web/lib/Zend/Dojo/View/Helper/Slider.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Slider.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /** Zend_Dojo_View_Helper_Dijit */
       
    24 require_once 'Zend/Dojo/View/Helper/Dijit.php';
       
    25 
       
    26 /**
       
    27  * Abstract class for Dojo Slider dijits
       
    28  *
       
    29  * @uses       Zend_Dojo_View_Helper_Dijit
       
    30  * @package    Zend_Dojo
       
    31  * @subpackage View
       
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34   */
       
    35 abstract class Zend_Dojo_View_Helper_Slider extends Zend_Dojo_View_Helper_Dijit
       
    36 {
       
    37     /**
       
    38      * Dojo module to use
       
    39      * @var string
       
    40      */
       
    41     protected $_module = 'dijit.form.Slider';
       
    42 
       
    43     /**
       
    44      * Required slider parameters
       
    45      * @var array
       
    46      */
       
    47     protected $_requiredParams = array('minimum', 'maximum', 'discreteValues');
       
    48 
       
    49     /**
       
    50      * Slider type -- vertical or horizontal
       
    51      * @var string
       
    52      */
       
    53     protected $_sliderType;
       
    54 
       
    55     /**
       
    56      * dijit.form.Slider
       
    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      * @return string
       
    63      */
       
    64     public function prepareSlider($id, $value = null, array $params = array(), array $attribs = array())
       
    65     {
       
    66         $this->_sliderType = strtolower($this->_sliderType);
       
    67 
       
    68         // Prepare two items: a hidden element to store the value, and the slider
       
    69         $hidden = $this->_renderHiddenElement($id, $value);
       
    70         $hidden = preg_replace('/(name=")([^"]*)"/', 'id="$2" $1$2"', $hidden);
       
    71 
       
    72         foreach ($this->_requiredParams as $param) {
       
    73             if (!array_key_exists($param, $params)) {
       
    74                 require_once 'Zend/Dojo/View/Exception.php';
       
    75                 throw new Zend_Dojo_View_Exception('prepareSlider() requires minimally the "minimum", "maximum", and "discreteValues" parameters');
       
    76             }
       
    77         }
       
    78 
       
    79         $content = '';
       
    80         $params['value'] = $value;
       
    81 
       
    82         if (!array_key_exists('onChange', $attribs)) {
       
    83             $attribs['onChange'] = "dojo.byId('" . $id . "').value = arguments[0];";
       
    84         }
       
    85 
       
    86         $id  = str_replace('][', '-', $id);
       
    87         $id  = str_replace(array('[', ']'), '-', $id);
       
    88         $id  = rtrim($id, '-');
       
    89         $id .= '-slider';
       
    90 
       
    91         switch ($this->_sliderType) {
       
    92             case 'horizontal':
       
    93                 if (array_key_exists('topDecoration', $params)) {
       
    94                     $content .= $this->_prepareDecoration('topDecoration', $id, $params['topDecoration']);
       
    95                     unset($params['topDecoration']);
       
    96                 }
       
    97 
       
    98                 if (array_key_exists('bottomDecoration', $params)) {
       
    99                     $content .= $this->_prepareDecoration('bottomDecoration', $id, $params['bottomDecoration']);
       
   100                     unset($params['bottomDecoration']);
       
   101                 }
       
   102 
       
   103                 if (array_key_exists('leftDecoration', $params)) {
       
   104                     unset($params['leftDecoration']);
       
   105                 }
       
   106 
       
   107                 if (array_key_exists('rightDecoration', $params)) {
       
   108                     unset($params['rightDecoration']);
       
   109                 }
       
   110                 break;
       
   111             case 'vertical':
       
   112                 if (array_key_exists('leftDecoration', $params)) {
       
   113                     $content .= $this->_prepareDecoration('leftDecoration', $id, $params['leftDecoration']);
       
   114                     unset($params['leftDecoration']);
       
   115                 }
       
   116 
       
   117                 if (array_key_exists('rightDecoration', $params)) {
       
   118                     $content .= $this->_prepareDecoration('rightDecoration', $id, $params['rightDecoration']);
       
   119                     unset($params['rightDecoration']);
       
   120                 }
       
   121 
       
   122                 if (array_key_exists('topDecoration', $params)) {
       
   123                     unset($params['topDecoration']);
       
   124                 }
       
   125 
       
   126                 if (array_key_exists('bottomDecoration', $params)) {
       
   127                     unset($params['bottomDecoration']);
       
   128                 }
       
   129                 break;
       
   130             default:
       
   131                 require_once 'Zend/Dojo/View/Exception.php';
       
   132                 throw new Zend_Dojo_View_Exception('Invalid slider type; slider must be horizontal or vertical');
       
   133         }
       
   134 
       
   135         return $hidden . $this->_createLayoutContainer($id, $content, $params, $attribs);
       
   136     }
       
   137 
       
   138     /**
       
   139      * Prepare slider decoration
       
   140      *
       
   141      * @param  string $position
       
   142      * @param  string $id
       
   143      * @param  array $decInfo
       
   144      * @return string
       
   145      */
       
   146     protected function _prepareDecoration($position, $id, $decInfo)
       
   147     {
       
   148         if (!in_array($position, array('topDecoration', 'bottomDecoration', 'leftDecoration', 'rightDecoration'))) {
       
   149             return '';
       
   150         }
       
   151 
       
   152         if (!is_array($decInfo)
       
   153             || !array_key_exists('labels', $decInfo)
       
   154             || !is_array($decInfo['labels'])
       
   155         ) {
       
   156             return '';
       
   157         }
       
   158 
       
   159         $id .= '-' . $position;
       
   160 
       
   161         if (!array_key_exists('dijit', $decInfo)) {
       
   162             $dijit = 'dijit.form.' . ucfirst($this->_sliderType) . 'Rule';
       
   163         } else {
       
   164             $dijit = $decInfo['dijit'];
       
   165             if ('dijit.form.' != substr($dijit, 0, 10)) {
       
   166                 $dijit = 'dijit.form.' . $dijit;
       
   167             }
       
   168         }
       
   169 
       
   170         $params  = array();
       
   171         $attribs = array();
       
   172         $labels  = $decInfo['labels'];
       
   173         if (array_key_exists('params', $decInfo)) {
       
   174             $params = $decInfo['params'];
       
   175         }
       
   176         if (array_key_exists('attribs', $decInfo)) {
       
   177             $attribs = $decInfo['attribs'];
       
   178         }
       
   179 
       
   180         $containerParams = null;
       
   181         if (array_key_exists('container', $params)) {
       
   182             $containerParams = $params['container'];
       
   183             unset($params['container']);
       
   184         }
       
   185 
       
   186         if (array_key_exists('labels', $params)) {
       
   187             $labelsParams = $params['labels'];
       
   188             unset($params['labels']);
       
   189         } else {
       
   190             $labelsParams = $params;
       
   191         }
       
   192 
       
   193         if (null === $containerParams) {
       
   194             $containerParams = $params;
       
   195         }
       
   196 
       
   197         $containerAttribs = null;
       
   198         if (array_key_exists('container', $attribs)) {
       
   199             $containerAttribs = $attribs['container'];
       
   200             unset($attribs['container']);
       
   201         }
       
   202 
       
   203         if (array_key_exists('labels', $attribs)) {
       
   204             $labelsAttribs = $attribs['labels'];
       
   205             unset($attribs['labels']);
       
   206         } else {
       
   207             $labelsAttribs = $attribs;
       
   208         }
       
   209 
       
   210         if (null === $containerAttribs) {
       
   211             $containerAttribs = $attribs;
       
   212         }
       
   213 
       
   214         $containerParams['container'] = $position;
       
   215         $labelsParams['container']    = $position;
       
   216 
       
   217         $labelList = $this->_prepareLabelsList($id, $labelsParams, $labelsAttribs, $labels);
       
   218 
       
   219         $dijit = 'dijit.form.' . ucfirst($this->_sliderType) . 'Rule';
       
   220         $containerAttribs['id'] = $id;
       
   221         $containerAttribs = $this->_prepareDijit($containerAttribs, $containerParams, 'layout', $dijit);
       
   222         $containerHtml = '<div' . $this->_htmlAttribs($containerAttribs) . "></div>\n";
       
   223 
       
   224         switch ($position) {
       
   225             case 'topDecoration':
       
   226             case 'leftDecoration':
       
   227                 return $labelList . $containerHtml;
       
   228             case 'bottomDecoration':
       
   229             case 'rightDecoration':
       
   230                 return $containerHtml . $labelList;
       
   231         }
       
   232     }
       
   233 
       
   234     /**
       
   235      * Prepare slider label list
       
   236      *
       
   237      * @param  string $id
       
   238      * @param  array $params
       
   239      * @param  array $attribs
       
   240      * @param  array $labels
       
   241      * @return string
       
   242      */
       
   243     protected function _prepareLabelsList($id, array $params, array $attribs, array $labels)
       
   244     {
       
   245         $attribs['id'] = $id . '-labels';
       
   246         $dijit = 'dijit.form.' . ucfirst($this->_sliderType) . 'RuleLabels';
       
   247         $attribs = $this->_prepareDijit($attribs, $params, 'layout', $dijit);
       
   248 
       
   249         return $this->view->htmlList($labels, true, $attribs);
       
   250     }
       
   251 }