Development

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

You must first sign up to be able to contribute.

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

Revision 30762, 3.9 kB (checked in by fabien, 4 years ago)

[1.3, 1.4] fixed phpdoc (closes #8931)

  • 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 sfWidgetFormChoiceBase
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 sfWidgetFormChoiceBase
38    */
39   protected function configure($options = array(), $attributes = array())
40   {
41     parent::configure($options, $attributes);
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    * Renders the widget.
52    *
53    * @param  string $name        The element name
54    * @param  string $value       The value selected in this widget
55    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
56    * @param  array  $errors      An array of errors for the field
57    *
58    * @return string An HTML tag string
59    *
60    * @see sfWidgetForm
61    */
62   public function render($name, $value = null, $attributes = array(), $errors = array())
63   {
64     if ('[]' != substr($name, -2))
65     {
66       $name .= '[]';
67     }
68
69     $choices = $this->getChoices();
70
71     // with groups?
72     if (count($choices) && is_array(next($choices)))
73     {
74       $parts = array();
75       foreach ($choices as $key => $option)
76       {
77         $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
78       }
79
80       return implode("\n", $parts);
81     }
82     else
83     {
84       return $this->formatChoices($name, $value, $choices, $attributes);
85     }
86   }
87
88   protected function formatChoices($name, $value, $choices, $attributes)
89   {
90     $inputs = array();
91     foreach ($choices as $key => $option)
92     {
93       $baseAttributes = array(
94         'name'  => substr($name, 0, -2),
95         'type'  => 'radio',
96         'value' => self::escapeOnce($key),
97         'id'    => $id = $this->generateId($name, self::escapeOnce($key)),
98       );
99
100       if (strval($key) == strval($value === false ? 0 : $value))
101       {
102         $baseAttributes['checked'] = 'checked';
103       }
104
105       $inputs[$id] = array(
106         'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
107         'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)),
108       );
109     }
110
111     return call_user_func($this->getOption('formatter'), $this, $inputs);
112   }
113
114   public function formatter($widget, $inputs)
115   {
116     $rows = array();
117     foreach ($inputs as $input)
118     {
119       $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
120     }
121
122     return !$rows ? '' : $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
123   }
124 }
125
Note: See TracBrowser for help on using the browser.