| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfWidgetFormSelectRadio extends sfWidgetFormChoiceBase |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* Constructor. |
|---|
| 23 |
* |
|---|
| 24 |
* Available options: |
|---|
| 25 |
* |
|---|
| 26 |
* * label_separator: The separator to use between the input radio and the label |
|---|
| 27 |
* * separator: The separator to use between each input radio |
|---|
| 28 |
* * class: The class to use for the main <ul> tag |
|---|
| 29 |
* * formatter: A callable to call to format the radio choices |
|---|
| 30 |
* The formatter callable receives the widget and the array of inputs as arguments |
|---|
| 31 |
* * template: The template to use when grouping option in groups (%group% %options%) |
|---|
| 32 |
* |
|---|
| 33 |
* @param array $options An array of options |
|---|
| 34 |
* @param array $attributes An array of default HTML attributes |
|---|
| 35 |
* |
|---|
| 36 |
* @see sfWidgetFormChoiceBase |
|---|
| 37 |
*/ |
|---|
| 38 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 39 |
{ |
|---|
| 40 |
parent::configure($options, $attributes); |
|---|
| 41 |
|
|---|
| 42 |
$this->addOption('class', 'radio_list'); |
|---|
| 43 |
$this->addOption('label_separator', ' '); |
|---|
| 44 |
$this->addOption('separator', "\n"); |
|---|
| 45 |
$this->addOption('formatter', array($this, 'formatter')); |
|---|
| 46 |
$this->addOption('template', '%group% %options%'); |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
* Renders the widget. |
|---|
| 51 |
* |
|---|
| 52 |
* @param string $name The element name |
|---|
| 53 |
* @param string $value The value selected in this widget |
|---|
| 54 |
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes |
|---|
| 55 |
* @param array $errors An array of errors for the field |
|---|
| 56 |
* |
|---|
| 57 |
* @return string An HTML tag string |
|---|
| 58 |
* |
|---|
| 59 |
* @see sfWidgetForm |
|---|
| 60 |
*/ |
|---|
| 61 |
public function render($name, $value = null, $attributes = array(), $errors = array()) |
|---|
| 62 |
{ |
|---|
| 63 |
if ('[]' != substr($name, -2)) |
|---|
| 64 |
{ |
|---|
| 65 |
$name .= '[]'; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
$choices = $this->getChoices(); |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
if (count($choices) && is_array(next($choices))) |
|---|
| 72 |
{ |
|---|
| 73 |
$parts = array(); |
|---|
| 74 |
foreach ($choices as $key => $option) |
|---|
| 75 |
{ |
|---|
| 76 |
$parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes))); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
return implode("\n", $parts); |
|---|
| 80 |
} |
|---|
| 81 |
else |
|---|
| 82 |
{ |
|---|
| 83 |
return $this->formatChoices($name, $value, $choices, $attributes); |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
protected function formatChoices($name, $value, $choices, $attributes) |
|---|
| 88 |
{ |
|---|
| 89 |
$inputs = array(); |
|---|
| 90 |
foreach ($choices as $key => $option) |
|---|
| 91 |
{ |
|---|
| 92 |
$baseAttributes = array( |
|---|
| 93 |
'name' => substr($name, 0, -2), |
|---|
| 94 |
'type' => 'radio', |
|---|
| 95 |
'value' => self::escapeOnce($key), |
|---|
| 96 |
'id' => $id = $this->generateId($name, self::escapeOnce($key)), |
|---|
| 97 |
); |
|---|
| 98 |
|
|---|
| 99 |
if (strval($key) == strval($value === false ? 0 : $value)) |
|---|
| 100 |
{ |
|---|
| 101 |
$baseAttributes['checked'] = 'checked'; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
$inputs[$id] = array( |
|---|
| 105 |
'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)), |
|---|
| 106 |
'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)), |
|---|
| 107 |
); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
return call_user_func($this->getOption('formatter'), $this, $inputs); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
public function formatter($widget, $inputs) |
|---|
| 114 |
{ |
|---|
| 115 |
$rows = array(); |
|---|
| 116 |
foreach ($inputs as $input) |
|---|
| 117 |
{ |
|---|
| 118 |
$rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
return !$rows ? '' : $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class'))); |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|