Passed
Push — master ( e56cb7...335385 )
by Andreas
17:50
created

form   F

Complexity

Total Complexity 118

Size/Duplication

Total Lines 587
Duplicated Lines 0 %

Test Coverage

Coverage 81.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 326
c 1
b 0
f 0
dl 0
loc 587
ccs 292
cts 360
cp 0.8111
rs 2
wmc 118

40 Methods

Rating   Name   Duplication   Size   Complexity  
A radio_widget() 0 8 2
A checkbox_widget() 0 12 3
A prepare_widget_attributes() 0 8 3
A choice_widget_expanded() 0 8 2
A choice_widget_options() 0 21 5
A other_widget() 0 6 1
A captcha_widget() 0 10 1
A form_end() 0 8 3
A __construct() 0 4 1
A form_start() 0 14 2
A form_row() 0 18 3
A form_errors() 0 15 4
A get_view_renderer() 0 3 1
B form_rows() 0 26 7
A subform_widget() 0 13 2
A button_widget() 0 8 2
A submit_widget() 0 4 1
C choice_widget_collapsed() 0 37 15
A button_row() 0 5 1
A markdown_widget() 0 11 1
A jsdate_widget() 0 22 4
A image_row() 0 14 3
A email_widget() 0 3 1
A repeated_row() 0 8 2
A password_widget() 0 3 1
C image_widget() 0 55 12
A hidden_widget() 0 3 1
B form_widget_simple() 0 18 7
A delete_widget() 0 4 1
A autocomplete_widget() 0 18 2
A hidden_row() 0 3 1
A toolbar_row() 0 7 2
A privilegeselection_widget() 0 8 1
A attachment_row() 0 28 4
A privilege_widget() 0 12 3
A form_label() 0 19 6
A url_widget() 0 3 1
A codemirror_widget() 0 13 2
A tinymce_widget() 0 16 2
A textarea_widget() 0 10 2

How to fix   Complexity   

Complex Class

Complex classes like form often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use form, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace midcom\datamanager\template;
3
4
use Symfony\Component\Form\FormView;
5
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
6
use midcom\datamanager\renderer;
7
use midcom;
8
use midcom\datamanager\helper\autocomplete;
9
10
class form extends base
11
{
12 101
    public function __construct(renderer $renderer)
13
    {
14 101
        parent::__construct($renderer);
15 101
        midcom::get()->head->prepend_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/default.css');
16 101
    }
17
18 3
    private function get_view_renderer() : view
19
    {
20 3
        return new view($this->renderer);
21
    }
22
23 101
    public function form_start(FormView $view, array $data)
24
    {
25 101
        $attributes = $data['attr'];
26 101
        $attributes['name'] = $data['name'];
27 101
        $attributes['id'] = $data['name'];
28 101
        $attributes['method'] = strtolower($data['method']);
29 101
        $attributes['action'] = $data['action'];
30 101
        $attributes['class'] = 'datamanager2';
31
32 101
        if ($data['multipart']) {
33 16
            $attributes['enctype'] = 'multipart/form-data';
34
        }
35
36 101
        return '<form ' . $this->attributes($attributes) . '>';
37
    }
38
39 100
    public function form_errors(FormView $view, array $data)
40
    {
41 100
        $errors = [];
42 100
        foreach ($data['errors'] as $error) {
43
            $params = $error->getMessageParameters();
44
            $message = $error->getMessage();
45
            foreach ($params as $param) {
46
                if (str_contains($message, (string) $param)) {
47
                    $message = str_replace($param, $this->renderer->humanize($param), $message);
48
                }
49
            }
50
            $errors[] = '<div class="field_error">' . $this->renderer->humanize($message) . '</div>';
51
        }
52
53 100
        return implode('', $errors);
54
    }
55
56 100
    public function form_rows(FormView $view, array $data)
57
    {
58 100
        $string = '';
59 100
        foreach ($view as $child) {
60 100
            if (!empty($child->vars['hidden'])) {
61 19
                $child->setRendered();
62 19
                continue;
63
            }
64 100
            if ($child->vars['start_fieldset'] !== null) {
65 39
                if (!empty($child->vars['start_fieldset']['css_group'])) {
66 32
                    $class = $child->vars['start_fieldset']['css_group'];
67
                } else {
68 7
                    $class = $child->vars['name'];
69
                }
70 39
                $string .= '<fieldset class="fieldset ' . $class . '">';
71 39
                if (!empty($child->vars['start_fieldset']['title'])) {
72 39
                    $string .= '<legend>' . $this->renderer->humanize($child->vars['start_fieldset']['title']) . '</legend>';
73
                }
74
            }
75 100
            $string .= $this->renderer->row($child);
76 100
            if ($child->vars['end_fieldset'] !== null) {
77 42
                $end_fieldsets = max(1, (int) $child->vars['end_fieldset']);
78 42
                $string .= str_repeat('</fieldset>', $end_fieldsets);
79
            }
80
        }
81 100
        return $string;
82
    }
83
84 101
    public function form_end(FormView $view, array $data)
85
    {
86 101
        $string = '';
87 101
        if (   !isset($data['render_rest'])
88 101
            || $data['render_rest']) {
89 101
            $string .= $this->renderer->rest($view);
90
        }
91 101
        return $string . '</form>';
92
    }
93
94 99
    public function form_row(FormView $view, array $data)
95
    {
96 99
        $class = 'element element_' . $view->vars['block_prefixes'][count($view->vars['block_prefixes']) - 2];
97
98 99
        if ($view->vars['required']) {
99 70
            $class .= ' required';
100
        }
101
102 99
        if ($data['errors']->count() > 0) {
103
            $class .= ' error';
104
        }
105
106 99
        $string = '<div class="' . $class . '">';
107 99
        $string .= $this->renderer->label($view);
108 99
        $string .= '<div class="input">';
109 99
        $string .= $this->renderer->errors($view);
110 99
        $string .= $this->renderer->widget($view);
111 99
        return $string . '</div></div>';
112
    }
113
114
    public function button_row(FormView $view, array $data)
115
    {
116
        $string = '<div>';
117
        $string .= $this->renderer->widget($view);
118
        return $string . '</div>';
119
    }
120
121 30
    public function hidden_row(FormView $view, array $data)
122
    {
123 30
        return $this->renderer->widget($view);
124
    }
125
126 101
    public function toolbar_row(FormView $view, array $data)
127
    {
128 101
        $string = '<div class="form_toolbar">';
129 101
        foreach ($view as $child) {
130 101
            $string .= $this->renderer->widget($child);
131
        }
132 101
        return $string . '</div>';
133
    }
134
135 2
    public function repeated_row(FormView $view, array $data)
136
    {
137 2
        $view->children['second']->vars['label'] = $this->renderer->humanize($view->children['first']->vars['label']) . ' ' . $this->renderer->humanize($view->children['second']->vars['label']);
138 2
        $string = '';
139 2
        foreach ($view->children as $child) {
140 2
            $string .= $this->form_row($child, $data);
141
        }
142 2
        return $string;
143
    }
144
145 7
    public function attachment_row(FormView $view, array $data)
146
    {
147 7
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css");
148 7
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/attachment.js');
149
150 7
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
151 7
        $string .= '<legend>';
152 7
        $string .= (!empty($data['value']['filename'])) ? $data['value']['filename'] : $this->renderer->humanize('add new file');
153 7
        $string .= '</legend>';
154
155 7
        $string .= '<div class="attachment-container">';
156 7
        $string .= '<div class="attachment-preview">';
157 7
        if (!empty($data['value']['filename'])) {
158
            $ext = pathinfo($data['value']['filename'], PATHINFO_EXTENSION);
159
            $string .= '<a href="' . $data['value']['url'] . '" target="_blank" class="icon" title="' . $data['value']['filename'] . '">';
160
            $string .= '<i class="fa fa-file-o"></i><span class="extension">' . $ext . '</span></a>';
0 ignored issues
show
Bug introduced by
Are you sure $ext of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
            $string .= '<i class="fa fa-file-o"></i><span class="extension">' . /** @scrutinizer ignore-type */ $ext . '</span></a>';
Loading history...
161
        } else {
162 7
            $string .= '<span class="icon no-file"><i class="fa fa-file-o"></i></span>';
163
        }
164
165 7
        $string .= '</div><div class="attachment-input">';
166 7
        foreach ($view->children as $child) {
167 7
            $string .= $this->renderer->row($child);
168
        }
169
170 7
        $string .= '</div></div>';
171 7
        $string .= $this->jsinit('dm_attachment_init("' . $data['form']['file']->vars['id'] . '")');
172 7
        return $string . '</fieldset>';
173
    }
174
175 5
    public function image_row(FormView $view, array $data)
176
    {
177 5
        if (!in_array('subform', $view->parent->vars['block_prefixes'])) {
178 4
            return $this->form_row($view, $data);
179
        }
180
181 1
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
182 1
        $string .= '<legend>';
183 1
        $string .= (!empty($data['value']['objects']['main']['filename'])) ? preg_replace('/^.+?\-/', '', $data['value']['objects']['main']['filename']) : $this->renderer->humanize('add new file');
184 1
        $string .= '</legend>';
185
186 1
        $string .= $this->renderer->widget($view);
187
188 1
        return $string . '</fieldset>';
189
    }
190
191 94
    public function form_widget_simple(FormView $view, array $data)
192
    {
193 94
        $type = $data['type'] ?? 'text';
194 94
        if ($view->vars['readonly'] && $type !== 'hidden') {
195 4
            $data['type'] = 'hidden';
196 4
            return $data['value'] . $this->renderer->block($view, 'form_widget_simple', $data);
197
        }
198
199 94
        if (empty($data['attr']['class']) && in_array($type, ['text', 'password', 'email', 'url'])) {
200 94
            $data['attr']['class'] = 'shorttext';
201
        }
202
203 94
        if (   !empty($data['value'])
204 94
            || is_numeric($data['value'])) {
205 80
            $data['attr']['value'] = $data['value'];
206
        }
207 94
        $data['attr']['type'] = $type;
208 94
        return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
209
    }
210
211 101
    public function button_widget(FormView $view, array $data)
212
    {
213 101
        $type = $data['type'] ?? 'button';
214 101
        if (!$data['label']) {
215
            $data['label'] = $data['name'];
216
        }
217
218 101
        return '<button type="' . $type . '" ' . $this->renderer->block($view, 'button_attributes') . '>' . $this->renderer->humanize($data['label']) . '</button>';
219
    }
220
221 70
    public function hidden_widget(FormView $view, array $data)
222
    {
223 70
        return $this->renderer->block($view, 'form_widget_simple', ['type' => $data['type'] ?? "hidden"]);
224
    }
225
226 18
    public function email_widget(FormView $view, array $data)
227
    {
228 18
        return $this->renderer->block($view, 'form_widget_simple', ['type' => $data['type'] ?? "email"]);
229
    }
230
231 4
    public function password_widget(FormView $view, array $data)
232
    {
233 4
        return $this->renderer->block($view, 'form_widget_simple', ['type' => $data['type'] ?? "password"]);
234
    }
235
236 8
    public function url_widget(FormView $view, array $data)
237
    {
238 8
        return $this->renderer->block($view, 'form_widget_simple', ['type' => $data['type'] ?? "url"]);
239
    }
240
241 57
    public function autocomplete_widget(FormView $view, array $data)
242
    {
243 57
        if ($view->vars['readonly']) {
244 2
            $string = $this->get_view_renderer()->autocomplete_widget($view, $data);
245 2
            return $string .  $this->renderer->widget($view['selection']);
246
        }
247
248 57
        autocomplete::add_head_elements($data['handler_options']['creation_mode_enabled'], $data['handler_options']['sortable']);
249
250 57
        $element_id = $view->vars['id'];
251 57
        $jsinit = 'window.' . $element_id . '_handler_options = ' . json_encode($data['handler_options']) . ";\n";
252 57
        $jsinit .= "midcom_helper_datamanager2_autocomplete.create_dm2_widget('{$element_id}_search_input', {$data['min_chars']});\n";
253
254 57
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
255 57
        $string .=  $this->renderer->widget($view['selection']);
256 57
        $string .= ' ' . $this->renderer->widget($view['search_input']);
257 57
        $string .= '</fieldset>';
258 57
        return $string . $this->jsinit($jsinit);
259
    }
260
261 57
    protected function prepare_widget_attributes(string $type, FormView $view, array &$data)
262
    {
263 57
        $data['attr']['type'] = $type;
264 57
        if (isset($data['value'])) {
265 57
            $data['attr']['value'] = $data['value'];
266
        }
267 57
        if ($data['checked']) {
268 33
            $data['attr']['checked'] = 'checked';
269
        }
270 57
    }
271
272 29
    public function radio_widget(FormView $view, array $data)
273
    {
274 29
        $this->prepare_widget_attributes('radio', $view, $data);
275 29
        if ($view->vars['readonly']) {
276
            $data['attr']['disabled'] = true;
277
        }
278
279 29
        return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
280
    }
281
282 36
    public function checkbox_widget(FormView $view, array $data)
283
    {
284 36
        if ($view->vars['readonly']) {
285
            $string = $this->get_view_renderer()->checkbox_widget($view, $data);
286
            if ($data['checked']) {
287
                $string .= $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
288
            }
289
            return $string;
290
        }
291 36
        $this->prepare_widget_attributes('checkbox', $view, $data);
292
293 36
        return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
294
    }
295
296 46
    public function choice_widget_collapsed(FormView $view, array $data)
297
    {
298 46
        if ($view->vars['readonly'] && empty($view->vars['multiple'])) {
299
            $string = $this->get_view_renderer()->choice_widget_collapsed($view, $data);
300
            return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
301
        }
302
303 46
        $string = '<select ';
304 46
        if (   $data['required']
305 46
            && null === $data['placeholder']
306 46
            && $data['placeholder_in_choices'] === false
307 46
            && $data['multiple'] === false) {
308 3
            $data['required'] = false;
309
        }
310
311 46
        if ($data['multiple']) {
312 4
            $data['attr']['multiple'] = 'multiple';
313
        }
314
315 46
        $string .= $this->renderer->block($view, 'widget_attributes', $data) . '>';
316 46
        if (null !== $data['placeholder']) {
317
            $string .= '<option value=""';
318
            if (   $data['required']
319
                && empty($data['value'])
320
                && "0" !== $data['value']) {
321
                $string .= ' selected="selected"';
322
            }
323
            $string .= '>' . $data['placeholder'] . '</option>';
324
        }
325 46
        if (count($data['preferred_choices']) > 0) {
326
            $string .= $this->renderer->block($view, 'choice_widget_options', ['choices' => $data['preferred_choices']]);
327
            if (count($data['choices']) > 0 && null !== $data['separator']) {
328
                $string .= '<option disabled="disabled">' . $data['separator'] . '</option>';
329
            }
330
        }
331 46
        $string .= $this->renderer->block($view, 'choice_widget_options', ['choices' => $data['choices']]);
332 46
        return $string . '</select>';
333
    }
334
335 4
    public function privilege_widget(FormView $view, array $data)
336
    {
337 4
        if (isset($view->vars['effective_value'])) {
338 4
            if ($view->vars['effective_value']) {
339 2
                $label = $this->renderer->humanize('widget privilege: allow');
340
            } else {
341 3
                $label = $this->renderer->humanize('widget privilege: deny');
342
            }
343 4
            $new_label = sprintf($this->renderer->humanize('widget privilege: inherit %s'), $label);
344 4
            $view->children[2]->vars['label'] = $new_label;
345
        }
346 4
        return $this->choice_widget_expanded($view, $data);
347
    }
348
349 1
    public function privilegeselection_widget(FormView $view, array $data)
350
    {
351 1
        midcom::get()->head->enable_jquery();
352 1
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/privilege/jquery.privilege.css');
353 1
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/privilege/jquery.privilege.js');
354
355 1
        $string = '<div class="holder">' . $this->choice_widget_collapsed($view, $data) . '</div>';
356 1
        return $string . $this->jsinit($view->vars['jsinit']);
357
    }
358
359 29
    public function choice_widget_expanded(FormView $view, array $data)
360
    {
361 29
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
362 29
        foreach ($view as $child) {
363 29
            $string .= $this->renderer->widget($child);
364 29
            $string .= '<label for="' . $child->vars['id'] . '">' . $this->renderer->humanize($child->vars['label']) . '</label>';
365
        }
366 29
        return $string . '</fieldset>';
367
    }
368
369 46
    public function choice_widget_options(FormView $view, array $data)
370
    {
371 46
        $string = '';
372 46
        foreach ($data['choices'] as $index => $choice) {
373 44
            if (is_array($choice) || $choice instanceof ChoiceGroupView) {
374
                $string .= '<optgroup label="' . $index . '">';
375
                $string .= $this->renderer->block($view, 'choice_widget_options', ['choices' => $choice]);
376
                $string .= '</optgroup>';
377
            } else {
378 44
                $choice->attr['value'] = $choice->value;
379 44
                if ($data['is_selected']($choice->value, $data['value'])) {
380 23
                    $choice->attr['selected'] = 'selected';
381
                } else {
382 44
                    unset($choice->attr['selected']);
383
                }
384
385 44
                $string .= '<option ' . $this->attributes($choice->attr);
386 44
                $string .= '>' . $this->renderer->humanize($choice->label) . '</option>';
387
            }
388
        }
389 46
        return $string;
390
    }
391
392 2
    public function other_widget(FormView $view, array $data)
393
    {
394 2
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
395 2
        $string .= $this->renderer->widget($view->children['select']);
396 2
        $string .= $this->renderer->humanize($view->children['other']->vars['label']) . ': ' . $this->renderer->widget($view->children['other'], ['attr' => ['class' => 'other']]);
397 2
        return $string . '</fieldset>';
398
    }
399
400
    public function captcha_widget(FormView $view, array $data)
401
    {
402
        $alt = $this->renderer->humanize('captcha image alt text');
403
        $string = '<fieldset class="captcha">';
404
        $string .= "<img src='{$view->vars['captcha_url']}' alt='{$alt}' text='{$alt}' class='captcha'><br>";
405
        $string .= $this->renderer->humanize('captcha message');
406
        $data['attr']['class'] = 'captcha';
407
        $data['value'] = '';
408
        $string .= $this->form_widget_simple($view, $data);
409
        return $string . '</fieldset>';
410
    }
411
412 1
    public function codemirror_widget(FormView $view, array $data)
413
    {
414
        // we set required to false, because codemirror doesn't play well with html5 validation..
415 1
        $data['required'] = false;
416 1
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>';
417 1
        $string .= $data['value'] . '</textarea>';
418 1
        if (!empty($data['codemirror_snippet'])) {
419 1
            $this->add_head_elements_for_codemirror($data['modes']);
420 1
            $snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);
421 1
            $snippet = str_replace('{$read_only}', 'false', $snippet);
422 1
            $string .= $this->jsinit($snippet);
423
        }
424 1
        return $string;
425
    }
426
427 29
    public function jsdate_widget(FormView $view, array $data)
428
    {
429 29
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
430
431 29
        if ($view->vars['readonly']) {
432 2
            $string .= $this->get_view_renderer()->jsdate_widget($view, $data);
433 2
            $string .= $this->renderer->widget($view['date'], ['type' => 'hidden']);
434 2
            if (isset($view['time'])) {
435 2
                $string .= $this->renderer->widget($view['time'], ['type' => 'hidden']);
436
            }
437
        } else {
438 29
            midcom::get()->head->enable_jquery_ui(['datepicker']);
439 29
            midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/datepicker.js');
440
441 29
            $string .= $this->renderer->widget($view['date'], ['type' => 'hidden']);
442 29
            $string .= $this->renderer->widget($view['input'], ['attr' => ['class' => 'jsdate']]);
443 29
            if (isset($view['time'])) {
444 7
                $string .= ' ' . $this->renderer->widget($view['time']);
445
            }
446 29
            $string .= $data['jsinit'];
447
        }
448 29
        return $string . '</fieldset>';
449
    }
450
451 5
    public function image_widget(FormView $view, array $data)
452
    {
453 5
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/image.css');
454 5
        midcom::get()->head->enable_jquery();
455 5
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/image.js');
456
457 5
        $string = '<div ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
458 5
        $string .= '<table class="midcom_datamanager_table_photo"><tr><td>';
459 5
        $preview = null;
460 5
        $objects = $data['value']['objects'] ?? [];
461 5
        foreach ($objects as $identifier => $info) {
462
            $preview = $info;
463
            if ($identifier == 'thumbnail') {
464
                break;
465
            }
466
        }
467 5
        if (!empty($preview)) {
468
            if ($preview['id'] === 0) {
469
                $preview['url'] = \midcom_connection::get_url('self') . 'midcom-exec-midcom.datamanager/preview-tmpfile.php?identifier=' . substr($preview['identifier'], strlen('tmpfile-'));
470
            }
471
472
            $string .= '<img src="' . $preview['url'] . '" class="preview-image">';
473
        }
474 5
        $string .= '</td><td>';
475
476 5
        if (!empty($objects)) {
477
            $string .= '<label class="midcom_datamanager_photo_label">' . $this->renderer->humanize('delete photo') . ' ' . $this->renderer->widget($data['form']['delete']) . '</label>';
478
            $string .= '<ul>';
479
            foreach ($objects as $info) {
480
                if (   $info['size_x']
481
                    && $info['size_y']) {
482
                    $size = "{$info['size_x']}&times;{$info['size_y']}";
483
                } else {
484
                    $size = 'unknown';
485
                }
486
                $string .= "<li title=\"{$info['guid']}\">{$size}: <a href='{$info['url']}' target='_new'>{$info['formattedsize']}</a></li>";
487
            }
488
            $string .= '</ul>';
489
        }
490 5
        $string .= $this->renderer->errors($data['form']['file']);
491 5
        $string .= $this->renderer->widget($data['form']['file']);
492
493 5
        if (array_key_exists('title', $view->children)) {
494 1
            $string .= $this->renderer->widget($view->children['title'], ['attr' => ['placeholder' => $this->renderer->humanize('title')]]);
495
        }
496 5
        if (array_key_exists('description', $view->children)) {
497
            $string .= $this->renderer->widget($view->children['description'], ['attr' => ['placeholder' => $this->renderer->humanize('description')]]);
498
        }
499 5
        if (array_key_exists('score', $view->children)) {
500
            $string .= $this->renderer->widget($view->children['score']);
501
        }
502 5
        $string .= '</td></tr></table></div>';
503 5
        $string .= $this->renderer->row($data['form']['identifier']);
504
505 5
        return $string . $this->jsinit('init_image_widget("' . $view->vars['id'] .'");');
506
    }
507
508 7
    public function subform_widget(FormView $view, array $data)
509
    {
510 7
        $head = midcom::get()->head;
511 7
        $head->enable_jquery();
512 7
        if ($view->vars['sortable'] == 'true') {
513
            $head->enable_jquery_ui(['mouse', 'sortable']);
514
        }
515 7
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/subform.js');
516
517 7
        $data['attr']['data-prototype'] = $this->escape($this->renderer->row($view->vars['prototype']));
518 7
        $data['attr']['data-max-count'] = $view->vars['max_count'];
519 7
        $string = $this->renderer->widget($data['form'], $data);
520 7
        return $string . $this->jsinit('init_subform("' . $view->vars['id'] . '", ' . $view->vars['sortable'] . ');');
521
    }
522
523 101
    public function submit_widget(FormView $view, array $data)
524
    {
525 101
        $data['type'] = $data['type'] ?? 'submit';
526 101
        return $this->renderer->block($view, 'button_widget', $data);
527
    }
528
529
    public function delete_widget(FormView $view, array $data)
530
    {
531
        $data['type'] = $data['type'] ?? 'delete';
532
        return $this->renderer->block($view, 'button_widget', $data);
533
    }
534
535 28
    public function textarea_widget(FormView $view, array $data)
536
    {
537 28
        if ($view->vars['readonly']) {
538 1
            $view->vars['output_mode'] = 'nl2br';
539 1
            $string = $this->get_view_renderer()->text_widget($view, $data);
540 1
            return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
541
        }
542 27
        $data['attr']['class'] = 'longtext';
543 27
        $data['attr']['cols'] = 50;
544 27
        return '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
545
    }
546
547 33
    public function markdown_widget(FormView $view, array $data)
548
    {
549 33
        $head = midcom::get()->head;
550 33
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css');
551 33
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/simplemde/simplemde.min.css');
552 33
        $head->enable_jquery();
553 33
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/simplemde/simplemde.min.js');
554
555 33
        $data['required'] = false;
556 33
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
557 33
        return $string . $this->jsinit('var simplemde = new SimpleMDE({ element: document.getElementById("' . $view->vars['id'] . '"), status: false });');
558
    }
559
560 5
    public function tinymce_widget(FormView $view, array $data)
561
    {
562 5
        if ($view->vars['readonly']) {
563
            $string = $this->get_view_renderer()->text_widget($view, $data);
564
            $data['type'] = 'hidden';
565
            return $string . $this->renderer->block($view, 'form_widget_simple', $data);
566
        }
567
568 5
        midcom::get()->head->enable_jquery();
569 5
        midcom::get()->head->add_jsfile($data['tinymce_url']);
570 5
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/tinymce.custom.js');
571
572
        // we set required to false, because tinymce doesn't play well with html5 validation..
573 5
        $data['required'] = false;
574 5
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
575 5
        return $string . $this->jsinit($data['tinymce_snippet']);
576
    }
577
578 100
    public function form_label(FormView $view, array $data)
579
    {
580 100
        if ($data['label'] === false) {
581
            return '';
582
        }
583 100
        if (!$data['label']) {
584 9
            $data['label'] = $data['name'];
585
        }
586 100
        $data['label'] = $this->renderer->humanize($data['label']);
587
588 100
        $label_attr = $data['label_attr'];
589 100
        if ($data['required'] && !$view->vars['readonly']) {
590 70
            $label_attr['class'] = trim(($label_attr['class'] ?? '') . ' required');
591 70
            $data['label'] .= ' <span class="field_required_start">*</span>';
592
        }
593 100
        if (!$data['compound']) {
594 95
            $label_attr['for'] = $data['id'];
595
        }
596 100
        return '<label ' . $this->attributes($label_attr) . '><span class="field_text">' . $data['label'] . '</span></label>';
597
    }
598
}
599