Code Duplication    Length = 94-97 lines in 3 locations

lib/widgets/sfWidgetFormI18nSelect2ChoiceCountry.class.php 1 location

@@ 13-109 (lines=97) @@
10
 * @author      Ing. Gerhard Schranz <[email protected]>
11
 * @version     0.1 2013-03-11
12
 */
13
class sfWidgetFormI18nSelect2ChoiceCountry extends sfWidgetFormI18nChoiceCountry
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('width', sfConfig::get('sf_sfSelect2Widgets_width'));
32
33
        parent::configure($options, $attributes);
34
35
        if (!$this->getOption('culture')) {
36
            $this->setOption('culture', sfContext::getInstance()->getUser()->getCulture());
37
        }
38
    }
39
40
    public function getChoices()
41
    {
42
        $choices = parent::getChoices();
43
44
        if (count($choices) > 0 && isset($choices['']) && $choices[''] == '') {
45
            $choices[''] = '&nbsp;';
46
        }
47
48
        return $choices;
49
    }
50
51
    /**
52
     * @param  string $name        The element name
53
     * @param  string $value       The date displayed 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
        $id = $this->generateId($name);
64
65
        $return = parent::render($name, $value, $attributes, $errors);
66
67
        $return .= sprintf(<<<EOF
68
<script type="text/javascript">
69
function formatResult(item)
70
{
71
    return item.text;
72
}
73
74
jQuery("#%s").select2(
75
{
76
    width:              '%s',
77
    allowClear:         %s,
78
});
79
</script>
80
EOF
81
            ,
82
            $id,
83
            $this->getOption('width'),
84
            $this->getOption('add_empty') == true ? 'true' : 'false'
85
        );
86
87
        return $return;
88
    }
89
90
    /**
91
     * Gets the stylesheet paths associated with the widget.
92
     *
93
     * @return array An array of stylesheet paths
94
     */
95
    public function getStylesheets()
96
    {
97
        return Select2::addStylesheets();
98
    }
99
100
    /**
101
     * Gets the JavaScript paths associated with the widget.
102
     *
103
     * @return array An array of JavaScript paths
104
     */
105
    public function getJavascripts()
106
    {
107
        return Select2::addJavascripts($this->getOption('culture'));
108
    }
109
}
110

lib/widgets/sfWidgetFormI18nSelect2ChoiceLanguage.class.php 1 location

@@ 13-109 (lines=97) @@
10
 * @author      Ing. Gerhard Schranz <[email protected]>
11
 * @version     0.1 2013-03-11
12
 */
13
class sfWidgetFormI18nSelect2ChoiceLanguage extends sfWidgetFormI18nChoiceLanguage
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('width', sfConfig::get('sf_sfSelect2Widgets_width'));
32
33
        parent::configure($options, $attributes);
34
35
        if (!$this->getOption('culture')) {
36
            $this->setOption('culture', sfContext::getInstance()->getUser()->getCulture());
37
        }
38
    }
39
40
    public function getChoices()
41
    {
42
        $choices = parent::getChoices();
43
44
        if (count($choices) > 0 && isset($choices['']) && $choices[''] == '') {
45
            $choices[''] = '&nbsp;';
46
        }
47
48
        return $choices;
49
    }
50
51
    /**
52
     * @param  string $name        The element name
53
     * @param  string $value       The date displayed 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
        $id = $this->generateId($name);
64
65
        $return = parent::render($name, $value, $attributes, $errors);
66
67
        $return .= sprintf(<<<EOF
68
<script type="text/javascript">
69
function formatResult(item)
70
{
71
    return item.text;
72
}
73
74
jQuery("#%s").select2(
75
{
76
    width:              '%s',
77
    allowClear:         %s,
78
});
79
</script>
80
EOF
81
            ,
82
            $id,
83
            $this->getOption('width'),
84
            $this->getOption('add_empty') == true ? 'true' : 'false'
85
        );
86
87
        return $return;
88
    }
89
90
    /**
91
     * Gets the stylesheet paths associated with the widget.
92
     *
93
     * @return array An array of stylesheet paths
94
     */
95
    public function getStylesheets()
96
    {
97
        return Select2::addStylesheets();
98
    }
99
100
    /**
101
     * Gets the JavaScript paths associated with the widget.
102
     *
103
     * @return array An array of JavaScript paths
104
     */
105
    public function getJavascripts()
106
    {
107
        return Select2::addJavascripts($this->getOption('culture'));
108
    }
109
}
110

lib/widgets/sfWidgetFormSelect2PropelChoice.class.php 1 location

@@ 13-106 (lines=94) @@
10
 * @author      Ing. Gerhard Schranz <[email protected]>
11
 * @version     0.1 2013-03-11
12
 */
13
class sfWidgetFormSelect2PropelChoice extends sfWidgetFormPropelChoice
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[''] = '&nbsp;';
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
        $return = parent::render($name, $value, $attributes, $errors);
63
64
        $return .= sprintf(<<<EOF
65
<script type="text/javascript">
66
function formatResult(item)
67
{
68
    return item.text;
69
}
70
71
jQuery("#%s").select2(
72
{
73
    width:              '%s',
74
    allowClear:         %s
75
});
76
</script>
77
EOF
78
            ,
79
            $id,
80
            $this->getOption('width'),
81
            $this->getOption('add_empty') == true ? 'true' : 'false'
82
        );
83
84
        return $return;
85
    }
86
87
    /**
88
     * Gets the stylesheet paths associated with the widget.
89
     *
90
     * @return array An array of stylesheet paths
91
     */
92
    public function getStylesheets()
93
    {
94
        return Select2::addStylesheets();
95
    }
96
97
    /**
98
     * Gets the JavaScript paths associated with the widget.
99
     *
100
     * @return array An array of JavaScript paths
101
     */
102
    public function getJavascripts()
103
    {
104
        return Select2::addJavascripts($this->getOption('culture'));
105
    }
106
}
107