BootstrapFormExtension::setLabelCol()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of BraincraftedBootstrapBundle.
4
 * (c) 2012-2013 by Florian Eckerstorfer
5
 */
6
7
namespace Braincrafted\Bundle\BootstrapBundle\Twig;
8
9
use Twig_Extension;
10
use Twig_SimpleFunction;
11
12
13
/**
14
 * BootstrapFormExtension
15
 *
16
 * @package    BraincraftedBootstrapBundle
17
 * @subpackage Twig
18
 * @author     Florian Eckerstorfer <[email protected]>
19
 * @copyright  2012-2013 Florian Eckerstorfer
20
 * @license    http://opensource.org/licenses/MIT The MIT License
21
 * @link       http://bootstrap.braincrafted.com Bootstrap for Symfony2
22
 */
23
class BootstrapFormExtension extends Twig_Extension
24
{
25
    /** @var string */
26
    private $style;
27
28
    /** @var string */
29
    private $colSize = 'lg';
30
31
    /** @var integer */
32
    private $widgetCol = 10;
33
34
    /** @var integer */
35
    private $labelCol = 2;
36
37
    /** @var integer */
38
    private $simpleCol = false;
39
40
    /** @var array */
41
    private $settingsStack = array();
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getFunctions()
47
    {
48
        return array(
49
            new Twig_SimpleFunction('bootstrap_set_style', array($this, 'setStyle')),
50
            new Twig_SimpleFunction('bootstrap_get_style', array($this, 'getStyle')),
51
            new Twig_SimpleFunction('bootstrap_set_col_size', array($this, 'setColSize')),
52
            new Twig_SimpleFunction('bootstrap_get_col_size', array($this, 'getColSize')),
53
            new Twig_SimpleFunction('bootstrap_set_widget_col', array($this, 'setWidgetCol')),
54
            new Twig_SimpleFunction('bootstrap_get_widget_col', array($this, 'getWidgetCol')),
55
            new Twig_SimpleFunction('bootstrap_set_label_col', array($this, 'setLabelCol')),
56
            new Twig_SimpleFunction('bootstrap_get_label_col', array($this, 'getLabelCol')),
57
            new Twig_SimpleFunction('bootstrap_set_simple_col', array($this, 'setSimpleCol')),
58
            new Twig_SimpleFunction('bootstrap_get_simple_col', array($this, 'getSimpleCol')),
59
            new Twig_SimpleFunction('bootstrap_backup_form_settings', array($this, 'backupFormSettings')),
60
            new Twig_SimpleFunction('bootstrap_restore_form_settings', array($this, 'restoreFormSettings')),
61
            new Twig_SimpleFunction(
62
                'checkbox_row',
63
                null,
64
                array('is_safe' => array('html'), 'node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode')
65
            ),
66
            new Twig_SimpleFunction(
67
                'radio_row',
68
                null,
69
                array('is_safe' => array('html'), 'node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode')
70
            ),
71
            new Twig_SimpleFunction(
72
                'global_form_errors',
73
                null,
74
                array('is_safe' => array('html'), 'node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode')
75
            ),
76
            new Twig_SimpleFunction(
77
                'form_control_static',
78
                array($this, 'formControlStaticFunction'),
79
                array('is_safe' => array('html'))
80
            )
81
        );
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getName()
88
    {
89
        return 'braincrafted_bootstrap_form';
90
    }
91
92
    /**
93
     * Sets the style.
94
     *
95
     * @param string $style Name of the style
96
     */
97
    public function setStyle($style)
98
    {
99
        $this->style = $style;
100
    }
101
102
    /**
103
     * Returns the style.
104
     *
105
     * @return string Name of the style
106
     */
107
    public function getStyle()
108
    {
109
        return $this->style;
110
    }
111
112
    /**
113
     * Sets the column size.
114
     *
115
     * @param string $colSize Column size (xs, sm, md or lg)
116
     */
117
    public function setColSize($colSize)
118
    {
119
        $this->colSize = $colSize;
120
    }
121
122
    /**
123
     * Returns the column size.
124
     *
125
     * @return string Column size (xs, sm, md or lg)
126
     */
127
    public function getColSize()
128
    {
129
        return $this->colSize;
130
    }
131
132
    /**
133
     * Sets the number of columns of widgets.
134
     *
135
     * @param integer $widgetCol Number of columns.
136
     */
137
    public function setWidgetCol($widgetCol)
138
    {
139
        $this->widgetCol = $widgetCol;
140
    }
141
142
    /**
143
     * Returns the number of columns of widgets.
144
     *
145
     * @return integer Number of columns.Class
146
     */
147
    public function getWidgetCol()
148
    {
149
        return $this->widgetCol;
150
    }
151
152
    /**
153
     * Sets the number of columns of labels.
154
     *
155
     * @param integer $labelCol Number of columns.
156
     */
157
    public function setLabelCol($labelCol)
158
    {
159
        $this->labelCol = $labelCol;
160
    }
161
162
    /**
163
     * Returns the number of columns of labels.
164
     *
165
     * @return integer Number of columns.
166
     */
167
    public function getLabelCol()
168
    {
169
        return $this->labelCol;
170
    }
171
172
    /**
173
     * Sets the number of columns of simple widgets.
174
     *
175
     * @param integer $simpleCol Number of columns.
176
     */
177
    public function setSimpleCol($simpleCol)
178
    {
179
        $this->simpleCol = $simpleCol;
180
    }
181
182
    /**
183
     * Returns the number of columns of simple widgets.
184
     *
185
     * @return integer Number of columns.
186
     */
187
    public function getSimpleCol()
188
    {
189
        return $this->simpleCol;
190
    }
191
192
    /**
193
     * Backup the form settings to the stack.
194
     *
195
     * @internal Should only be used at the beginning of form_start. This allows
196
     *           a nested subform to change its settings without affecting its
197
     *           parent form.
198
     */
199
    public function backupFormSettings()
200
    {
201
        $settings = array(
202
            'style'     => $this->style,
203
            'colSize'   => $this->colSize,
204
            'widgetCol' => $this->widgetCol,
205
            'labelCol'  => $this->labelCol,
206
            'simpleCol' => $this->simpleCol,
207
        );
208
209
        array_push($this->settingsStack, $settings);
210
    }
211
212
    /**
213
     * Restore the form settings from the stack.
214
     *
215
     * @internal Should only be used at the end of form_end.
216
     * @see backupFormSettings
217
     */
218
    public function restoreFormSettings()
219
    {
220
        if (count($this->settingsStack) < 1) {
221
            return;
222
        }
223
224
        $settings = array_pop($this->settingsStack);
225
226
        $this->style     = $settings['style'];
227
        $this->colSize   = $settings['colSize'];
228
        $this->widgetCol = $settings['widgetCol'];
229
        $this->labelCol  = $settings['labelCol'];
230
        $this->simpleCol = $settings['simpleCol'];
231
    }
232
233
    /**
234
     * @param string $label
235
     * @param string $value
236
     *
237
     * @return string
238
     */
239
    public function formControlStaticFunction($label, $value)
240
    {
241
        return  sprintf(
242
            '<div class="form-group"><label class="col-sm-%s control-label">%s</label><div class="col-sm-%s"><p class="form-control-static">%s</p></div></div>',
243
            $this->getLabelCol(), $label, $this->getWidgetCol(), $value
244
        );
245
    }
246
}
247