| 1 |
lars |
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 |
* sfWidgetFormChoice represents a choice widget.
|
|
|
13 |
*
|
|
|
14 |
* @package symfony
|
|
|
15 |
* @subpackage widget
|
|
|
16 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
17 |
* @version SVN: $Id: sfWidgetFormChoice.class.php 32835 2011-07-27 07:07:00Z fabien $
|
|
|
18 |
*/
|
|
|
19 |
class sfWidgetFormChoice extends sfWidgetFormChoiceBase
|
|
|
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 sfWidgetFormChoiceBase
|
|
|
40 |
*/
|
|
|
41 |
protected function configure($options = array(), $attributes = array())
|
|
|
42 |
{
|
|
|
43 |
parent::configure($options, $attributes);
|
|
|
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 |
* Renders the widget.
|
|
|
67 |
*
|
|
|
68 |
* @param string $name The element name
|
|
|
69 |
* @param string $value The value selected in this widget
|
|
|
70 |
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
|
|
|
71 |
* @param array $errors An array of errors for the field
|
|
|
72 |
*
|
|
|
73 |
* @return string An HTML tag string
|
|
|
74 |
*
|
|
|
75 |
* @see sfWidgetForm
|
|
|
76 |
*/
|
|
|
77 |
public function render($name, $value = null, $attributes = array(), $errors = array())
|
|
|
78 |
{
|
|
|
79 |
if ($this->getOption('multiple'))
|
|
|
80 |
{
|
|
|
81 |
$attributes['multiple'] = 'multiple';
|
|
|
82 |
|
|
|
83 |
if ('[]' != substr($name, -2))
|
|
|
84 |
{
|
|
|
85 |
$name .= '[]';
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
if (!$this->getOption('renderer') && !$this->getOption('renderer_class') && $this->getOption('expanded'))
|
|
|
90 |
{
|
|
|
91 |
unset($attributes['multiple']);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
return $this->getRenderer()->render($name, $value, $attributes, $errors);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Gets the stylesheet paths associated with the widget.
|
|
|
99 |
*
|
|
|
100 |
* @return array An array of stylesheet paths
|
|
|
101 |
*/
|
|
|
102 |
public function getStylesheets()
|
|
|
103 |
{
|
|
|
104 |
return $this->getRenderer()->getStylesheets();
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Gets the JavaScript paths associated with the widget.
|
|
|
109 |
*
|
|
|
110 |
* @return array An array of JavaScript paths
|
|
|
111 |
*/
|
|
|
112 |
public function getJavaScripts()
|
|
|
113 |
{
|
|
|
114 |
return $this->getRenderer()->getJavaScripts();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public function getRenderer()
|
|
|
118 |
{
|
|
|
119 |
if ($this->getOption('renderer'))
|
|
|
120 |
{
|
|
|
121 |
return $this->getOption('renderer');
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
if (!$class = $this->getOption('renderer_class'))
|
|
|
125 |
{
|
|
|
126 |
$type = !$this->getOption('expanded') ? '' : ($this->getOption('multiple') ? 'checkbox' : 'radio');
|
|
|
127 |
$class = sprintf('sfWidgetFormSelect%s', ucfirst($type));
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$options = $this->options['renderer_options'];
|
|
|
131 |
$options['choices'] = new sfCallable(array($this, 'getChoices'));
|
|
|
132 |
|
|
|
133 |
$renderer = new $class($options, $this->getAttributes());
|
|
|
134 |
|
|
|
135 |
// choices returned by the callback will already be translated (so we need to avoid double-translation)
|
|
|
136 |
if ($renderer->hasOption('translate_choices')) {
|
|
|
137 |
$renderer->setOption('translate_choices', false);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
$renderer->setParent($this->getParent());
|
|
|
141 |
|
|
|
142 |
return $renderer;
|
|
|
143 |
}
|
|
|
144 |
}
|