form::form_widget_simple()   B
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10.1167

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 20
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 31
ccs 17
cts 19
cp 0.8947
crap 10.1167
rs 7.6666

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

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