BootstrapCollectionType::getParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of BraincraftedBootstrapBundle.
4
 * (c) 2012-2013 by Florian Eckerstorfer
5
 */
6
7
namespace Braincrafted\Bundle\BootstrapBundle\Form\Type;
8
9
use Symfony\Component\Form\AbstractType;
10
use Symfony\Component\Form\Extension\Core\Type\TextType;
11
use Symfony\Component\Form\FormView;
12
use Symfony\Component\Form\FormInterface;
13
use Symfony\Component\HttpKernel\Kernel;
14
use Symfony\Component\OptionsResolver\Options;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
use Braincrafted\Bundle\BootstrapBundle\Util\LegacyFormHelper;
18
19
/**
20
 * BootstrapCollectionType
21
 *
22
 * @package    BraincraftedBootstrapBundle
23
 * @subpackage Form
24
 * @author     Florian Eckerstorfer <[email protected]>
25
 * @copyright  2012-2013 Florian Eckerstorfer
26
 * @license    http://opensource.org/licenses/MIT The MIT License
27
 * @link       http://bootstrap.braincrafted.com Bootstrap for Symfony2
28
 */
29
class BootstrapCollectionType extends AbstractType
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function buildView(FormView $view, FormInterface $form, array $options)
35
    {
36
        $view->vars = array_replace(
37
            $view->vars,
38
            array(
39
                'allow_add'          => $options['allow_add'],
40
                'allow_delete'       => $options['allow_delete'],
41
                'add_button_text'    => $options['add_button_text'],
42
                'add_button_class'   => $options['add_button_class'],
43
                'delete_button_text' => $options['delete_button_text'],
44
                'delete_button_class'=> $options['delete_button_class'],
45
                'sub_widget_col'     => $options['sub_widget_col'],
46
                'button_col'         => $options['button_col'],
47
                'prototype_name'     => $options['prototype_name']
48
            )
49
        );
50
51
        if (false === $view->vars['allow_delete']) {
52
            $view->vars['sub_widget_col'] += $view->vars['button_col'];
53
        }
54
55
        if ($form->getConfig()->hasAttribute('prototype')) {
56
            $view->vars['prototype'] = $form->getConfig()->getAttribute('prototype')->createView($view);
57
        }
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function configureOptions(OptionsResolver $resolver)
64
    {
65
        $optionsNormalizer = function (Options $options, $value) {
66
            // @codeCoverageIgnoreStart
67
            $value['block_name'] = 'entry';
68
69
            return $value;
70
            // @codeCoverageIgnoreEnd
71
        };
72
73
        $defaults =  array(
74
            'allow_add'          => false,
75
            'allow_delete'       => false,
76
            'prototype'          => true,
77
            'prototype_name'     => '__name__',
78
            'add_button_text'    => 'Add',
79
            'add_button_class'   => 'btn btn-primary btn-sm',
80
            'delete_button_text' => 'Delete',
81
            'delete_button_class'=> 'btn btn-danger btn-sm',
82
            'sub_widget_col'     => 10,
83
            'button_col'         => 2,
84
            'options'            => array(),
85
        );
86
87
88
        if (Kernel::VERSION_ID>=20800) {
89
            $defaults['entry_type'] = 'Symfony\Component\Form\Extension\Core\Type\TextType';
90
        } else {
91
            // map old class to new one using LegacyFormHelper
92
            $defaults['type'] = LegacyFormHelper::getType('text');
93
        }
94
95
        $resolver->setDefaults($defaults);
96
97
        $resolver->setNormalizer('options', $optionsNormalizer);
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    public function getParent()
104
    {
105
        // map old class to new one using LegacyFormHelper
106
        return LegacyFormHelper::getType('collection');
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    public function getBlockPrefix()
113
    {
114
        return 'bootstrap_collection';
115
    }
116
117
    /**
118
     * Backward compatibility for SF < 3.0
119
     *
120
     * @return null|string
121
     */
122
    public function getName() {
123
        return $this->getBlockPrefix();
124
    }
125
}
126