Development

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

You must first sign up to be able to contribute.

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

Revision 17068, 3.4 kB (checked in by fabien, 6 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     if (!is_array($value))
85     {
86       $value = array($value);
87     }
88
89     $value = array_map('strval', array_values($value));
90     $value_set = array_flip($value);
91     
92     $options = array();
93     foreach ($choices as $key => $option)
94     {
95       if (is_array($option))
96       {
97         $options[] = $this->renderContentTag('optgroup', implode("\n", $this->getOptionsForSelect($value, $option)), array('label' => self::escapeOnce($key)));
98       }
99       else
100       {
101         $attributes = array('value' => self::escapeOnce($key));
102         if (isset($value_set[strval($key)]))
103         {
104           $attributes['selected'] = 'selected';
105         }
106
107         $options[] = $this->renderContentTag('option', self::escapeOnce($option), $attributes);
108       }
109     }
110
111     $this->attributes = $mainAttributes;
112
113     return $options;
114   }
115
116   public function __clone()
117   {
118     if ($this->getOption('choices') instanceof sfCallable)
119     {
120       $callable = $this->getOption('choices')->getCallable();
121       $class = __CLASS__;
122       if (is_array($callable) && $callable[0] instanceof $class)
123       {
124         $callable[0] = $this;
125         $this->setOption('choices', new sfCallable($callable));
126       }
127     }
128   }
129 }
130
Note: See TracBrowser for help on using the browser.