| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfWidgetFormChoice 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 |
* * expanded: true to display an expanded widget |
|---|
| 29 |
* if expanded is false, then the widget will be a select |
|---|
| 30 |
* if expanded is true and multiple is false, then the widget will be a list of radio |
|---|
| 31 |
* if expanded is true and multiple is true, then the widget will be a list of checkbox |
|---|
| 32 |
* * renderer_class: The class to use instead of the default ones |
|---|
| 33 |
* * renderer_options: The options to pass to the renderer constructor |
|---|
| 34 |
* * renderer: A renderer widget (overrides the expanded and renderer_options options) |
|---|
| 35 |
* The choices option must be: new sfCallable($thisWidgetInstance, 'getChoices') |
|---|
| 36 |
* @param array $options An array of options |
|---|
| 37 |
* @param array $attributes An array of default HTML attributes |
|---|
| 38 |
* |
|---|
| 39 |
* @see sfWidgetForm |
|---|
| 40 |
*/ |
|---|
| 41 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 42 |
{ |
|---|
| 43 |
$this->addRequiredOption('choices'); |
|---|
| 44 |
|
|---|
| 45 |
$this->addOption('multiple', false); |
|---|
| 46 |
$this->addOption('expanded', false); |
|---|
| 47 |
$this->addOption('renderer_class', false); |
|---|
| 48 |
$this->addOption('renderer_options', array()); |
|---|
| 49 |
$this->addOption('renderer', false); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
* Sets the format for HTML id attributes. This is made avaiable to the renderer, |
|---|
| 54 |
* as this widget does not render itself, but delegates to the renderer instead. |
|---|
| 55 |
* |
|---|
| 56 |
* @param string $format The format string (must contain a %s for the id placeholder) |
|---|
| 57 |
* |
|---|
| 58 |
* @see sfWidgetForm |
|---|
| 59 |
*/ |
|---|
| 60 |
public function setIdFormat($format) |
|---|
| 61 |
{ |
|---|
| 62 |
$this->options['renderer_options']['id_format'] = $format; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
* @param string $name The element name |
|---|
| 67 |
* @param string $value The value selected in this widget |
|---|
| 68 |
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes |
|---|
| 69 |
* @param array $errors An array of errors for the field |
|---|
| 70 |
* |
|---|
| 71 |
* @return string An HTML tag string |
|---|
| 72 |
* |
|---|
| 73 |
* @see sfWidgetForm |
|---|
| 74 |
*/ |
|---|
| 75 |
public function render($name, $value = null, $attributes = array(), $errors = array()) |
|---|
| 76 |
{ |
|---|
| 77 |
if ($this->getOption('multiple')) |
|---|
| 78 |
{ |
|---|
| 79 |
$attributes['multiple'] = 'multiple'; |
|---|
| 80 |
|
|---|
| 81 |
if ('[]' != substr($name, -2)) |
|---|
| 82 |
{ |
|---|
| 83 |
$name .= '[]'; |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
if (!$this->getOption('renderer') && !$this->getOption('renderer_class') && $this->getOption('expanded')) |
|---|
| 88 |
{ |
|---|
| 89 |
unset($attributes['multiple']); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
return $this->getRenderer()->render($name, $value, $attributes, $errors); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
* Gets the stylesheet paths associated with the widget. |
|---|
| 97 |
* |
|---|
| 98 |
* @return array An array of stylesheet paths |
|---|
| 99 |
*/ |
|---|
| 100 |
public function getStylesheets() |
|---|
| 101 |
{ |
|---|
| 102 |
return $this->getRenderer()->getStylesheets(); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
* Gets the JavaScript paths associated with the widget. |
|---|
| 107 |
* |
|---|
| 108 |
* @return array An array of JavaScript paths |
|---|
| 109 |
*/ |
|---|
| 110 |
public function getJavaScripts() |
|---|
| 111 |
{ |
|---|
| 112 |
return $this->getRenderer()->getJavaScripts(); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
public function getChoices() |
|---|
| 116 |
{ |
|---|
| 117 |
$choices = $this->getOption('choices'); |
|---|
| 118 |
if ($choices instanceof sfCallable) |
|---|
| 119 |
{ |
|---|
| 120 |
$choices = $choices->call(); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
return $choices; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
public function getRenderer() |
|---|
| 127 |
{ |
|---|
| 128 |
if ($this->getOption('renderer')) |
|---|
| 129 |
{ |
|---|
| 130 |
return $this->getOption('renderer'); |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
if (!$class = $this->getOption('renderer_class')) |
|---|
| 134 |
{ |
|---|
| 135 |
$type = !$this->getOption('expanded') ? '' : ($this->getOption('multiple') ? 'checkbox' : 'radio'); |
|---|
| 136 |
$class = sprintf('sfWidgetFormSelect%s', ucfirst($type)); |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
return new $class(array_merge(array('choices' => new sfCallable(array($this, 'getChoices'))), $this->options['renderer_options']), $this->getAttributes()); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
public function __clone() |
|---|
| 143 |
{ |
|---|
| 144 |
if ($this->getOption('choices') instanceof sfCallable) |
|---|
| 145 |
{ |
|---|
| 146 |
$callable = $this->getOption('choices')->getCallable(); |
|---|
| 147 |
$class = __CLASS__; |
|---|
| 148 |
if (is_array($callable) && $callable[0] instanceof $class) |
|---|
| 149 |
{ |
|---|
| 150 |
$callable[0] = $this; |
|---|
| 151 |
$this->setOption('choices', new sfCallable($callable)); |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|