Passed
Push — master ( 225b91...4e3bf7 )
by Andreas
27:54
created

form::captcha_widget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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

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