Development

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

You must first sign up to be able to contribute.

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

Revision 27989, 4.3 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.2] backported r27738 xss fix

  • 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  * sfWidgetFormSelectRadio represents radio HTML tags.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfWidgetFormSelectRadio extends sfWidgetForm
20 {
21   /**
22    * Constructor.
23    *
24    * Available options:
25    *
26    *  * choices:         An array of possible choices (required)
27    *  * label_separator: The separator to use between the input radio and the label
28    *  * separator:       The separator to use between each input radio
29    *  * class:           The class to use for the main <ul> tag
30    *  * formatter:       A callable to call to format the radio choices
31    *                     The formatter callable receives the widget and the array of inputs as arguments
32    *  * template:        The template to use when grouping option in groups (%group% %options%)
33    *
34    * @param array $options     An array of options
35    * @param array $attributes  An array of default HTML attributes
36    *   
37    * @see sfWidgetForm
38    */
39   protected function configure($options = array(), $attributes = array())
40   {
41     $this->addRequiredOption('choices');
42
43     $this->addOption('class', 'radio_list');
44     $this->addOption('label_separator', '&nbsp;');
45     $this->addOption('separator', "\n");
46     $this->addOption('formatter', array($this, 'formatter'));
47     $this->addOption('template', '%group% %options%');
48   }
49
50   /**
51    * @param  string $name        The element name
52    * @param  string $value       The value selected in this widget
53    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
54    * @param  array  $errors      An array of errors for the field
55    *
56    * @return string An HTML tag string
57    *
58    * @see sfWidgetForm
59    */
60   public function render($name, $value = null, $attributes = array(), $errors = array())
61   {
62     if ('[]' != substr($name, -2))
63     {
64       $name .= '[]';
65     }
66
67     $choices = $this->getOption('choices');
68     if ($choices instanceof sfCallable)
69     {
70       $choices = $choices->call();
71     }
72
73     // with groups?
74     if (count($choices) && is_array(next($choices)))
75     {
76       $parts = array();
77       foreach ($choices as $key => $option)
78       {
79         $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
80       }
81
82       return implode("\n", $parts);
83     }
84     else
85     {
86       return $this->formatChoices($name, $value, $choices, $attributes);;
87     }
88   }
89
90   protected function formatChoices($name, $value, $choices, $attributes)
91   {
92     $inputs = array();
93     foreach ($choices as $key => $option)
94     {
95       $baseAttributes = array(
96         'name'  => substr($name, 0, -2),
97         'type'  => 'radio',
98         'value' => self::escapeOnce($key),
99         'id'    => $id = $this->generateId($name, self::escapeOnce($key)),
100       );
101
102       if (strval($key) == strval($value === false ? 0 : $value))
103       {
104         $baseAttributes['checked'] = 'checked';
105       }
106
107       $inputs[] = array(
108         'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
109         'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)),
110       );
111     }
112
113     return call_user_func($this->getOption('formatter'), $this, $inputs);
114   }
115
116   public function formatter($widget, $inputs)
117   {
118     $rows = array();
119     foreach ($inputs as $input)
120     {
121       $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
122     }
123
124     return $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
125   }
126
127   public function __clone()
128   {
129     if ($this->getOption('choices') instanceof sfCallable)
130     {
131       $callable = $this->getOption('choices')->getCallable();
132       $class = __CLASS__;
133       if (is_array($callable) && $callable[0] instanceof $class)
134       {
135         $callable[0] = $this;
136         $this->setOption('choices', new sfCallable($callable));
137       }
138     }
139   }
140 }
141
Note: See TracBrowser for help on using the browser.