| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
|
|---|