Test Setup Failed
Pull Request — master (#190)
by
unknown
09:58
created

schema::build_form()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 10.75

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 5
nop 1
dl 0
loc 28
ccs 5
cts 20
cp 0.25
crap 10.75
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager;
7
8
use Symfony\Component\Form\FormBuilderInterface;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use Symfony\Component\Validator\Constraints\NotBlank;
12
use midcom;
13
use midcom_core_context;
14
use midcom\datamanager\extension\compat;
15
16
/**
17
 * Experimental schema class
18
 */
19
class schema
20
{
21
    private $defaults = array(
22
        'operations' => array('save' => '', 'cancel' => '')
23
    );
24
25
    private $config = array();
26
27
    /**
28
     *
29
     * @var string
30
     */
31
    private $name = 'default';
32
33 5
    public function __construct(array $config)
34
    {
35 5
        $this->config = array_merge($this->defaults, $config);
36 5
        $this->complete_fields();
37 5
    }
38
39
    /**
40
     *
41
     * @param string $name
42
     */
43 1
    public function set_name($name)
44
    {
45 1
        $this->name = $name;
46 1
    }
47
48
    /**
49
     *
50
     * @return string
51
     */
52 1
    public function get_name()
53
    {
54 1
        return $this->name;
55
    }
56
57
    /**
58
     *
59
     * @param FormBuilderInterface $builder
60
     * @return \Symfony\Component\Form\Form
61
     */
62 1
    public function build_form(FormBuilderInterface $builder)
63
    {
64 1
        foreach ($this->config['fields'] as $name => $config) {
65
            $options = array(
66
                'label' => $config['title'],
67
                'widget_config' => $config['widget_config'],
68
                'type_config' => $config['type_config'],
69
                'required' => $config['required'],
70
                'constraints' => $config['required'] ? array(new NotBlank()) : null,
71
                'dm2_type' => $config['type'],
72
                'start_fieldset' => $config['start_fieldset'],
73
                'end_fieldset' => $config['end_fieldset'],
74
                'index_method' => $config['index_method']
75
            );
76
77
            // Symfony < 2.8 compat
78
            if (compat::is_legacy()) {
79
                $options['read_only'] = $config['readonly'];
80
            } else {
81
                $options['attr']['readonly'] = $config['readonly'];
82
            }
83
84
            $builder->add($name, compat::get_type_name($config['widget']), $options);
85 1
        }
86
87 1
        $builder->add('form_toolbar', compat::get_type_name('toolbar'), array('operations' => $this->config['operations']));
88 1
        return $builder->getForm();
89
    }
90
91
    /**
92
     *
93
     * @return array
94
     */
95 5
    public function get_fields()
96
    {
97 5
        return $this->config['fields'];
98
    }
99
100
    /**
101
     *
102
     * @return \midcom_services_i18n_l10n
103
     */
104 1
    public function get_l10n()
105
    {
106
        // Populate the l10n_schema member
107 1
        if (array_key_exists('l10n_db', $this->config)) {
108
            $l10n_name = $this->config['l10n_db'];
109
        } else {
110 1
            $l10n_name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT);
111
        }
112 1
        if (!midcom::get()->componentloader->is_installed($l10n_name)) {
113 1
            $l10n_name = 'midcom';
114 1
        }
115 1
        return midcom::get()->i18n->get_l10n($l10n_name);
116
    }
117
118
119 5
    private function complete_fields()
120
    {
121 5
        foreach ($this->config['fields'] as $name => &$config) {
122 4
            $config = $this->resolve_field_options($config, $name);
123 5
        }
124 5
    }
125
126 4
    private function resolve_field_options(array $config, $name)
127
    {
128 4
        $resolver = new OptionsResolver();
129
130 4
        $resolver->setDefaults(array(
131 4
            'title' => '',
132 4
            'type' => null,
133 4
            'type_config' => array(),
134 4
            'widget' => null,
135 4
            'widget_config' => array(),
136 4
            'required' => false,
137 4
            'readonly' => false,
138 4
            'default' => null,
139 4
            'storage' => '__UNSET__',
140 4
            'index_method' => 'auto',
141 4
            'start_fieldset' => null,
142
            'end_fieldset' => null,
143 4
            'validation' => array()
144
        ));
145
146
        $normalize_widget = function (Options $options, $value) {
147 4
            if (   $value == 'images'
148 1
                || $value == 'downloads') {
149
                return 'subform';
150 3
            }
151 4
            return $value;
152
        };
153 4
154
        $normalize_storage = function (Options $options, $value) use ($name) {
155 4
            $default = array(
156 4
                'location' => 'parameter',
157
                'domain' => 'midcom.helper.datamanager2',
158 4
                'name' => $name
159 4
            );
160
            if ($value === '__UNSET__') {
161
                return $default;
162 4
            }
163 1
            if ($value === null) {
164
                return null;
165 3
            }
166 1
            if (is_string($value)) {
167
                if ($value === 'metadata') {
168
                    return array('location' => $value, 'name' => $name);
169 1
                }
170 1
                if ($value === 'parameter') {
171
                    return $default;
172
                }
173
                return array('location' => $value);
174 2
            }
175 2
            if (strtolower($value['location']) === 'parameter') {
176 2
                $value['location'] = strtolower($value['location']);
177
                if (!array_key_exists('domain', $value)) {
178
                    $value['domain'] = 'midcom.helper.datamanager2';
179 2
                }
180 2
            }
181
            if (strtolower($value['location']) === 'configuration') {
182
                $value['location'] = 'parameter';
183 2
            }
184 4
            return $value;
185
        };
186 4
187 4
        $resolver->setNormalizer('storage', $normalize_storage);
188
        $resolver->setNormalizer('widget', $normalize_widget);
189 4
190
        $config['validation'] = $this->complete_validation_field($config);
191
192
        return $resolver->resolve($config);
193
    }
194
195
    private function complete_validation_field($config)
196
    {
197
        $validation = array();
198
        if (array_key_exists('validation', $config)) {
199
            $validation = (array) $config['validation'];
200
        }
201
202
        foreach ($validation as $key => $rule) {
203
            if (!is_array($rule)) {
204
                $rule = array('type' => $rule);
205
            } elseif (!array_key_exists('type', $rule)) {
206
                throw new midcom_error("Missing validation rule type for rule {$key} on field {$config['name']}, this is a required option.");
207
            } elseif (   $rule['type'] == 'compare'
208
                    && !array_key_exists('compare_with', $rule)) {
209
                        throw new midcom_error("Missing compare_with option for compare type rule {$key} on field {$config['name']}, this is a required option.");
210
            }
211
212
            $defaults = array(
213
                            'message' => "validation failed: {$rule['type']}",
214
                            'format' => ''
215
                                    );
216
217
            $validation[$key] = array_merge($defaults, $rule);
218
        }
219
        return $validation;
220
    }
221
}
222