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 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 5
    protected function get_view_renderer() : view
19
    {
20 5
        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 103
            '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 40
                if (!empty($child->vars['start_fieldset']['css_group'])) {
67 31
                    $class = $child->vars['start_fieldset']['css_group'];
68
                } else {
69 9
                    $class = $child->vars['name'];
70
                }
71 40
                $string .= '<fieldset class="fieldset ' . $class . '">';
72 40
                if (!empty($child->vars['start_fieldset']['title'])) {
73 40
                    $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 43
                $end_fieldsets = max(1, (int) $child->vars['end_fieldset']);
79 43
                $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 ($view->vars['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 $key => $child) {
131 103
            $label = $this->renderer->humanize($view->vars['button-labels'][$key]);
132 103
            $string .= $this->renderer->widget($child, ['label' => $label]);
133
        }
134 103
        return $string . '</div>';
135
    }
136
137 2
    public function repeated_row(FormView $view, array $data)
138
    {
139 2
        $view->children['second']->vars['label'] = $this->renderer->humanize($view->children['first']->vars['label']) . ' ' . $this->renderer->humanize($view->children['second']->vars['label']);
140 2
        $string = '';
141 2
        foreach ($view->children as $child) {
142 2
            $string .= $this->form_row($child, $data);
143
        }
144 2
        return $string;
145
    }
146
147 7
    public function attachment_row(FormView $view, array $data)
148
    {
149 7
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css");
150 7
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/attachment.js');
151
152 7
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
153 7
        $string .= '<legend>';
154 7
        $string .= $data['value']['filename'] ?? $this->renderer->humanize('add new file');
155 7
        $string .= '</legend>';
156 7
        $class = ($data['errors']->count() > 0) ? ' error' : '';
157 7
        $string .= '<div class="attachment-container' . $class . '">';
158 7
        $string .= '<div class="attachment-preview">';
159 7
        if (!empty($data['value']['filename'])) {
160
            $ext = pathinfo($data['value']['filename'], PATHINFO_EXTENSION);
161
            $string .= '<a href="' . $data['value']['url'] . '" target="_blank" class="icon" title="' . $data['value']['filename'] . '">';
162
            $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

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