Passed
Push — master ( 9f04e9...9c2d83 )
by Andreas
20:35
created

form::choice_widget_collapsed()   D

Complexity

Conditions 18
Paths 73

Size

Total Lines 51
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 37.8016

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 18
eloc 32
c 2
b 0
f 0
nc 73
nop 2
dl 0
loc 51
ccs 20
cts 33
cp 0.6061
crap 37.8016
rs 4.8666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

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