Development

/branches/1.1/lib/widget/sfWidgetFormSelect.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/widget/sfWidgetFormSelect.class.php

Revision 17068, 3.6 kB (checked in by fabien, 5 years ago)

[1.1, 1.2, 1.3] fixed clone() in all choice widgets (closes #4417)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfWidgetFormSelect represents a select HTML tag.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfWidgetFormSelect extends sfWidgetForm
20 {
21   /**
22    * Constructor.
23    *
24    * Available options:
25    *
26    *  * choices:  An array of possible choices (required)
27    *  * multiple: true if the select tag must allow multiple selections
28    *
29    * @param array $options     An array of options
30    * @param array $attributes  An array of default HTML attributes
31    *
32    * @see sfWidgetForm
33    */
34   protected function configure($options = array(), $attributes = array())
35   {
36     $this->addRequiredOption('choices');
37     $this->addOption('multiple', false);
38   }
39
40   /**
41    * @param  string $name        The element name
42    * @param  string $value       The value selected in this widget
43    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
44    * @param  array  $errors      An array of errors for the field
45    *
46    * @return string An HTML tag string
47    *
48    * @see sfWidgetForm
49    */
50   public function render($name, $value = null, $attributes = array(), $errors = array())
51   {
52     if ($this->getOption('multiple'))
53     {
54       $attributes['multiple'] = 'multiple';
55
56       if ('[]' != substr($name, -2))
57       {
58         $name .= '[]';
59       }
60     }
61
62     $choices = $this->getOption('choices');
63     if ($choices instanceof sfCallable)
64     {
65       $choices = $choices->call();
66     }
67
68     return $this->renderContentTag('select', "\n".implode("\n", $this->getOptionsForSelect($value, $choices))."\n", array_merge(array('name' => $name), $attributes));
69   }
70
71   /**
72    * Returns an array of option tags for the given choices
73    *
74    * @param  string $value    The selected value
75    * @param  array  $choices  An array of choices
76    *
77    * @return array  An array of option tags
78    */
79   protected function getOptionsForSelect($value, $choices)
80   {
81     $mainAttributes = $this->attributes;
82     $this->attributes = array();
83
84     $options = array();
85     foreach ($choices as $key => $option)
86     {
87       if (is_array($option))
88       {
89         $options[] = $this->renderContentTag('optgroup', implode("\n", $this->getOptionsForSelect($value, $option)), array('label' => self::escapeOnce($key)));
90       }
91       else
92       {
93         $attributes = array('value' => self::escapeOnce($key));
94         if ((is_array($value) && in_array(strval($key), $value)) || strval($key) == strval($value))
95         {
96           $attributes['selected'] = 'selected';
97         }
98
99         $options[] = $this->renderContentTag('option', self::escapeOnce($option), $attributes);
100       }
101     }
102
103     $this->attributes = $mainAttributes;
104
105     return $options;
106   }
107
108   /**
109    * @see sfWidget
110    *
111    * We always generate an attribute for the value.
112    */
113   protected function attributesToHtmlCallback($k, $v)
114   {
115     return is_null($v) || ('' === $v && 'value' != $k) ? '' : sprintf(' %s="%s"', $k, $this->escapeOnce($v));
116   }
117
118   public function __clone()
119   {
120     if ($this->getOption('choices') instanceof sfCallable)
121     {
122       $callable = $this->getOption('choices')->getCallable();
123       $class = __CLASS__;
124       if (is_array($callable) && $callable[0] instanceof $class)
125       {
126         $callable[0] = $this;
127         $this->setOption('choices', new sfCallable($callable));
128       }
129     }
130   }
131 }
132
Note: See TracBrowser for help on using the browser.