1
|
|
|
<?php
|
2
|
|
|
require_once(dirname(__FILE__) . '/../select2/Select2.class.php');
|
3
|
|
|
|
4
|
|
|
/**
|
5
|
|
|
* This widget is designed to generate more user friendly autocomplete widgets.
|
6
|
|
|
*
|
7
|
|
|
* @package symfony
|
8
|
|
|
* @subpackage widget
|
9
|
|
|
* @link https://github.com/19Gerhard85/sfSelect2WidgetsPlugin
|
10
|
|
|
* @author Ing. Gerhard Schranz <[email protected]>
|
11
|
|
|
* @version 0.1 2013-03-11
|
12
|
|
|
*/
|
13
|
|
|
class sfWidgetFormSelect2Choice extends sfWidgetFormChoice
|
14
|
|
|
{
|
15
|
|
|
/**
|
16
|
|
|
* Configures the current widget.
|
17
|
|
|
*
|
18
|
|
|
* Available options:
|
19
|
|
|
*
|
20
|
|
|
* * url: The URL to call to get the choices to use (required)
|
21
|
|
|
* * config: A JavaScript array that configures the JQuery autocompleter widget
|
22
|
|
|
* * value_callback: A callback that converts the value before it is displayed
|
23
|
|
|
*
|
24
|
|
|
* @param array $options An array of options
|
25
|
|
|
* @param array $attributes An array of default HTML attributes
|
26
|
|
|
*
|
27
|
|
|
* @see sfWidgetForm
|
28
|
|
|
*/
|
29
|
|
|
protected function configure($options = array(), $attributes = array())
|
30
|
|
|
{
|
31
|
|
|
$this->addOption('culture', sfContext::getInstance()->getUser()->getCulture());
|
32
|
|
|
$this->addOption('width', sfConfig::get('sf_sfSelect2Widgets_width'));
|
33
|
|
|
|
34
|
|
|
parent::configure($options, $attributes);
|
35
|
|
|
}
|
36
|
|
|
|
37
|
|
|
public function getChoices()
|
38
|
|
|
{
|
39
|
|
|
$choices = parent::getChoices();
|
40
|
|
|
|
41
|
|
|
if (count($choices) > 0 && isset($choices['']) && $choices[''] == '') {
|
42
|
|
|
$choices[''] = ' ';
|
43
|
|
|
}
|
44
|
|
|
|
45
|
|
|
return $choices;
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* @param string $name The element name
|
50
|
|
|
* @param string $value The date displayed in this widget
|
51
|
|
|
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
|
52
|
|
|
* @param array $errors An array of errors for the field
|
53
|
|
|
*
|
54
|
|
|
* @return string An HTML tag string
|
55
|
|
|
*
|
56
|
|
|
* @see sfWidgetForm
|
57
|
|
|
*/
|
58
|
|
|
public function render($name, $value = null, $attributes = array(), $errors = array())
|
59
|
|
|
{
|
60
|
|
|
$id = $this->generateId($name);
|
61
|
|
|
|
62
|
|
|
$choices = $this->getChoices();
|
63
|
|
|
|
64
|
|
|
$return = parent::render($name, $value, $attributes, $errors);
|
65
|
|
|
|
66
|
|
|
$return .= sprintf(<<<EOF
|
67
|
|
|
<script type="text/javascript">
|
68
|
|
|
function formatResult(item)
|
69
|
|
|
{
|
70
|
|
|
return item.text;
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
jQuery("#%s").select2(
|
74
|
|
|
{
|
75
|
|
|
width: '%s',
|
76
|
|
|
allowClear: %s
|
77
|
|
|
});
|
78
|
|
|
</script>
|
79
|
|
|
EOF
|
80
|
|
|
,
|
81
|
|
|
$id,
|
82
|
|
|
$this->getOption('width'),
|
83
|
|
|
isset($choices['']) ? 'true' : 'false'
|
84
|
|
|
);
|
85
|
|
|
|
86
|
|
|
return $return;
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
/**
|
90
|
|
|
* Gets the stylesheet paths associated with the widget.
|
91
|
|
|
*
|
92
|
|
|
* @return array An array of stylesheet paths
|
93
|
|
|
*/
|
94
|
|
|
public function getStylesheets()
|
95
|
|
|
{
|
96
|
|
|
return Select2::addStylesheets();
|
97
|
|
|
}
|
98
|
|
|
|
99
|
|
|
/**
|
100
|
|
|
* Gets the JavaScript paths associated with the widget.
|
101
|
|
|
*
|
102
|
|
|
* @return array An array of JavaScript paths
|
103
|
|
|
*/
|
104
|
|
|
public function getJavascripts()
|
105
|
|
|
{
|
106
|
|
|
return Select2::addJavascripts($this->getOption('culture'));
|
107
|
|
|
}
|
108
|
|
|
}
|
109
|
|
|
|