Completed
Push — master ( 0ce313...924bce )
by Marko
15:21
created

CKEditorType::buildView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 3
dl 0
loc 24
ccs 0
cts 11
cp 0
crap 6
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the FOSCKEditor Bundle.
5
 *
6
 * (c) 2018 - present  Friends of Symfony
7
 * (c) 2009 - 2017     Eric GELOEN <[email protected]>
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace FOS\CKEditorBundle\Form\Type;
14
15
use FOS\CKEditorBundle\Model\ConfigManagerInterface;
16
use FOS\CKEditorBundle\Model\PluginManagerInterface;
17
use FOS\CKEditorBundle\Model\StylesSetManagerInterface;
18
use FOS\CKEditorBundle\Model\TemplateManagerInterface;
19
use FOS\CKEditorBundle\Model\ToolbarManagerInterface;
20
use Symfony\Component\Form\AbstractType;
21
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
22
use Symfony\Component\Form\FormBuilderInterface;
23
use Symfony\Component\Form\FormInterface;
24
use Symfony\Component\Form\FormView;
25
use Symfony\Component\OptionsResolver\Options;
26
use Symfony\Component\OptionsResolver\OptionsResolver;
27
28
/**
29
 * @author GeLo <[email protected]>
30
 */
31
class CKEditorType extends AbstractType
32
{
33
    /**
34
     * @var ConfigManagerInterface
35
     */
36
    private $configManager;
37
38
    /**
39
     * @var PluginManagerInterface
40
     */
41
    private $pluginManager;
42
43
    /**
44
     * @var StylesSetManagerInterface
45
     */
46
    private $stylesSetManager;
47
48
    /**
49
     * @var TemplateManagerInterface
50
     */
51
    private $templateManager;
52
53
    /**
54
     * @var ToolbarManagerInterface
55
     */
56
    private $toolbarManager;
57
58
    /**
59
     * @var array
60
     */
61
    private $config;
62
63
    public function __construct(
64
        ConfigManagerInterface $configManager,
65
        PluginManagerInterface $pluginManager,
66
        StylesSetManagerInterface $stylesSetManager,
67
        TemplateManagerInterface $templateManager,
68
        ToolbarManagerInterface $toolbarManager,
69
        array $config
70
    ) {
71
        $this->configManager = $configManager;
72
        $this->pluginManager = $pluginManager;
73
        $this->stylesSetManager = $stylesSetManager;
74
        $this->templateManager = $templateManager;
75
        $this->toolbarManager = $toolbarManager;
76
        $this->config = $config;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function buildForm(FormBuilderInterface $builder, array $options)
83
    {
84
        $builder->setAttribute('enable', $options['enable']);
85
86
        if (!$options['enable']) {
87
            return;
88
        }
89
90
        $builder->setAttribute('async', $options['async']);
91
        $builder->setAttribute('autoload', $options['autoload']);
92
        $builder->setAttribute('auto_inline', $options['auto_inline']);
93
        $builder->setAttribute('inline', $options['inline']);
94
        $builder->setAttribute('jquery', $options['jquery']);
95
        $builder->setAttribute('require_js', $options['require_js']);
96
        $builder->setAttribute('input_sync', $options['input_sync']);
97
        $builder->setAttribute('filebrowsers', $options['filebrowsers']);
98
        $builder->setAttribute('base_path', $options['base_path']);
99
        $builder->setAttribute('js_path', $options['js_path']);
100
        $builder->setAttribute('jquery_path', $options['jquery_path']);
101
102
        $configManager = clone $this->configManager;
103
        $pluginManager = clone $this->pluginManager;
104
        $stylesSetManager = clone $this->stylesSetManager;
105
        $templateManager = clone $this->templateManager;
106
107
        $config = $options['config'];
108
109
        if (null === $options['config_name']) {
110
            $options['config_name'] = uniqid('fos', true);
111
            $configManager->setConfig($options['config_name'], $config);
112
        } else {
113
            $configManager->mergeConfig($options['config_name'], $config);
114
        }
115
116
        $pluginManager->setPlugins($options['plugins']);
117
        $stylesSetManager->setStylesSets($options['styles']);
118
        $templateManager->setTemplates($options['templates']);
119
120
        $config = $configManager->getConfig($options['config_name']);
121
122
        if (isset($config['toolbar']) && is_string($config['toolbar'])) {
123
            $config['toolbar'] = $this->toolbarManager->resolveToolbar($config['toolbar']);
124
        }
125
126
        $builder->setAttribute('config', $config);
127
        $builder->setAttribute('plugins', $pluginManager->getPlugins());
128
        $builder->setAttribute('styles', $stylesSetManager->getStylesSets());
129
        $builder->setAttribute('templates', $templateManager->getTemplates());
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function buildView(FormView $view, FormInterface $form, array $options)
136
    {
137
        $config = $form->getConfig();
138
        $view->vars['enable'] = $config->getAttribute('enable');
139
140
        if (!$view->vars['enable']) {
141
            return;
142
        }
143
144
        $view->vars['async'] = $config->getAttribute('async');
145
        $view->vars['autoload'] = $config->getAttribute('autoload');
146
        $view->vars['auto_inline'] = $config->getAttribute('auto_inline');
147
        $view->vars['inline'] = $config->getAttribute('inline');
148
        $view->vars['jquery'] = $config->getAttribute('jquery');
149
        $view->vars['require_js'] = $config->getAttribute('require_js');
150
        $view->vars['input_sync'] = $config->getAttribute('input_sync');
151
        $view->vars['filebrowsers'] = $config->getAttribute('filebrowsers');
152
        $view->vars['base_path'] = $config->getAttribute('base_path');
153
        $view->vars['js_path'] = $config->getAttribute('js_path');
154
        $view->vars['jquery_path'] = $config->getAttribute('jquery_path');
155
        $view->vars['config'] = $config->getAttribute('config');
156
        $view->vars['plugins'] = $config->getAttribute('plugins');
157
        $view->vars['styles'] = $config->getAttribute('styles');
158
        $view->vars['templates'] = $config->getAttribute('templates');
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function configureOptions(OptionsResolver $resolver)
165
    {
166
        $resolver
167
            ->setDefaults([
168
                'enable' => $this->config['enable'],
169
                'async' => $this->config['async'],
170
                'autoload' => $this->config['autoload'],
171
                'auto_inline' => $this->config['auto_inline'],
172
                'inline' => $this->config['inline'],
173
                'jquery' => $this->config['jquery'],
174
                'require_js' => $this->config['require_js'],
175
                'input_sync' => $this->config['input_sync'],
176
                'filebrowsers' => $this->config['filebrowsers'],
177
                'base_path' => $this->config['base_path'],
178
                'js_path' => $this->config['js_path'],
179
                'jquery_path' => $this->config['jquery_path'],
180
                'config_name' => $this->configManager->getDefaultConfig(),
181
                'config' => [],
182
                'plugins' => [],
183
                'styles' => [],
184
                'templates' => [],
185
            ])
186
            ->addAllowedTypes('enable', 'bool')
187
            ->addAllowedTypes('async', 'bool')
188
            ->addAllowedTypes('autoload', 'bool')
189
            ->addAllowedTypes('auto_inline', 'bool')
190
            ->addAllowedTypes('inline', 'bool')
191
            ->addAllowedTypes('jquery', 'bool')
192
            ->addAllowedTypes('require_js', 'bool')
193
            ->addAllowedTypes('input_sync', 'bool')
194
            ->addAllowedTypes('filebrowsers', 'array')
195
            ->addAllowedTypes('config_name', ['string', 'null'])
196
            ->addAllowedTypes('base_path', 'string')
197
            ->addAllowedTypes('js_path', 'string')
198
            ->addAllowedTypes('jquery_path', 'string')
199
            ->addAllowedTypes('config', 'array')
200
            ->addAllowedTypes('plugins', 'array')
201
            ->addAllowedTypes('styles', 'array')
202
            ->addAllowedTypes('templates', 'array')
203
            ->setNormalizer('base_path', function (Options $options, $value) {
204
                if ('/' !== substr($value, -1)) {
205
                    $value .= '/';
206
                }
207
208
                return $value;
209
            });
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function getParent()
216
    {
217
        return method_exists(AbstractType::class, 'getBlockPrefix') ? TextareaType::class : 'textarea';
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function getName()
224
    {
225
        return $this->getBlockPrefix();
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function getBlockPrefix()
232
    {
233
        return 'ckeditor';
234
    }
235
}
236