Completed
Push — master ( 600290...8bc3d2 )
by Andreas
16:56
created

form::image_widget()   C

Complexity

Conditions 12
Paths 144

Size

Total Lines 55
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 24.8958

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 12
eloc 38
c 1
b 0
f 1
nc 144
nop 2
dl 0
loc 55
ccs 21
cts 38
cp 0.5526
crap 24.8958
rs 6.6

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
namespace midcom\datamanager\template;
3
4
use Symfony\Component\Form\FormView;
5
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
6
use midcom\datamanager\renderer;
7
use midcom;
8
use midcom\datamanager\helper\autocomplete;
9
10
class form extends base
11
{
12 101
    public function __construct(renderer $renderer)
13
    {
14 101
        parent::__construct($renderer);
15 101
        midcom::get()->head->prepend_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/default.css');
16 101
    }
17
18 3
    private function get_view_renderer() : view
19
    {
20 3
        return new view($this->renderer);
21
    }
22
23 101
    public function form_start(FormView $view, array $data)
24
    {
25 101
        $attributes = $data['attr'];
26 101
        $attributes['name'] = $data['name'];
27 101
        $attributes['id'] = $data['name'];
28 101
        $attributes['method'] = strtolower($data['method']);
29 101
        $attributes['action'] = $data['action'];
30 101
        $attributes['class'] = 'datamanager2';
31
32 101
        if ($data['multipart']) {
33 16
            $attributes['enctype'] = 'multipart/form-data';
34
        }
35
36 101
        return '<form ' . $this->attributes($attributes) . '>';
37
    }
38
39 100
    public function form_errors(FormView $view, array $data)
40
    {
41 100
        $errors = [];
42 100
        foreach ($data['errors'] as $error) {
43
            $params = $error->getMessageParameters();
44
            $message = $error->getMessage();
45
            foreach ($params as $param) {
46
                if (strpos($message, (string) $param)) {
47
                    $message = str_replace($param, $this->renderer->humanize($param), $message);
48
                }
49
            }
50
            $errors[] = '<div class="field_error">' . $this->renderer->humanize($message) . '</div>';
51
        }
52
53 100
        return implode('', $errors);
54
    }
55
56 100
    public function form_rows(FormView $view, array $data)
57
    {
58 100
        $string = '';
59 100
        foreach ($view as $child) {
60 100
            if (!empty($child->vars['hidden'])) {
61 19
                $child->setRendered();
62 19
                continue;
63
            }
64 100
            if ($child->vars['start_fieldset'] !== null) {
65 39
                if (!empty($child->vars['start_fieldset']['css_group'])) {
66 32
                    $class = $child->vars['start_fieldset']['css_group'];
67
                } else {
68 7
                    $class = $child->vars['name'];
69
                }
70 39
                $string .= '<fieldset class="fieldset ' . $class . '">';
71 39
                if (!empty($child->vars['start_fieldset']['title'])) {
72 39
                    $string .= '<legend>' . $this->renderer->humanize($child->vars['start_fieldset']['title']) . '</legend>';
73
                }
74
            }
75 100
            $string .= $this->renderer->row($child);
76 100
            if ($child->vars['end_fieldset'] !== null) {
77 42
                $end_fieldsets = max(1, (int) $child->vars['end_fieldset']);
78 42
                $string .= str_repeat('</fieldset>', $end_fieldsets);
79
            }
80
        }
81 100
        return $string;
82
    }
83
84 101
    public function form_end(FormView $view, array $data)
85
    {
86 101
        $string = '';
87 101
        if (   !isset($data['render_rest'])
88 101
            || $data['render_rest']) {
89 101
            $string .= $this->renderer->rest($view);
90
        }
91 101
        return $string . '</form>';
92
    }
93
94 98
    public function form_row(FormView $view, array $data)
95
    {
96 98
        $class = 'element element_' . $view->vars['block_prefixes'][count($view->vars['block_prefixes']) - 2];
97
98 98
        if ($view->vars['required']) {
99 67
            $class .= ' required';
100
        }
101
102 98
        if ($data['errors']->count() > 0) {
103
            $class .= ' error';
104
        }
105
106 98
        $string = '<div class="' . $class . '">';
107 98
        $string .= $this->renderer->label($view);
108 98
        $string .= '<div class="input">';
109 98
        $string .= $this->renderer->errors($view);
110 98
        $string .= $this->renderer->widget($view);
111 98
        return $string . '</div></div>';
112
    }
113
114 57
    public function autocomplete_row(FormView $view, array $data)
115
    {
116 57
        $class = 'element element_' . $view->vars['block_prefixes'][count($view->vars['block_prefixes']) - 2];
117
118 57
        if ($view->vars['required']) {
119 20
            $class .= ' required';
120
        }
121
122 57
        if ($data['errors']->count() > 0) {
123
            $class .= ' error';
124
        }
125
126 57
        $string = '<div class="' . $class . '">';
127 57
        $string .= $this->renderer->label($view);
128 57
        $string .= '<div class="input">';
129
130 57
        if ($data['errors']->count() > 0) {
131
            $string .= $this->renderer->errors($view) . '<br>';
132
        }
133 57
        $string .= $this->renderer->widget($view);
134 57
        return $string . '</div></div>';
135
    }
136
137
    public function button_row(FormView $view, array $data)
138
    {
139
        $string = '<div>';
140
        $string .= $this->renderer->widget($view);
141
        return $string . '</div>';
142
    }
143
144 30
    public function hidden_row(FormView $view, array $data)
145
    {
146 30
        return $this->renderer->widget($view);
147
    }
148
149 101
    public function toolbar_row(FormView $view, array $data)
150
    {
151 101
        $string = '<div class="form_toolbar">';
152 101
        foreach ($view as $child) {
153 101
            $string .= $this->renderer->widget($child);
154
        }
155 101
        return $string . '</div>';
156
    }
157
158 2
    public function repeated_row(FormView $view, array $data)
159
    {
160 2
        $view->children['second']->vars['label'] = $this->renderer->humanize($view->children['first']->vars['label']) . ' ' . $this->renderer->humanize($view->children['second']->vars['label']);
161 2
        $string = '';
162 2
        foreach ($view->children as $child) {
163 2
            $string .= $this->form_row($child, $data);
164
        }
165 2
        return $string;
166
    }
167
168 7
    public function attachment_row(FormView $view, array $data)
169
    {
170 7
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css");
171 7
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/attachment.js');
172
173 7
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
174 7
        $string .= '<legend>';
175 7
        $string .= (!empty($data['value']['filename'])) ? $data['value']['filename'] : $this->renderer->humanize('add new file');
176 7
        $string .= '</legend>';
177
178 7
        $string .= '<div class="attachment-container">';
179 7
        $string .= '<div class="attachment-preview">';
180 7
        if (!empty($data['value']['filename'])) {
181
            $ext = pathinfo($data['value']['filename'], PATHINFO_EXTENSION);
182
            $string .= '<a href="' . $data['value']['url'] . '" target="_blank" class="icon" title="' . $data['value']['filename'] . '">';
183
            $string .= '<i class="fa fa-file-o"></i><span class="extension">' . $ext . '</span></a>';
184
        } else {
185 7
            $string .= '<span class="icon no-file"><i class="fa fa-file-o"></i></span>';
186
        }
187
188 7
        $string .= '</div><div class="attachment-input">';
189 7
        foreach ($view->children as $child) {
190 7
            $string .= $this->renderer->row($child);
191
        }
192
193 7
        $string .= '</div></div>';
194 7
        $string .= $this->jsinit('dm_attachment_init("' . $data['form']['file']->vars['id'] . '")');
195 7
        return $string . '</fieldset>';
196
    }
197
198 5
    public function image_row(FormView $view, array $data)
199
    {
200 5
        if (!in_array('subform', $view->parent->vars['block_prefixes'])) {
201 4
            return $this->form_row($view, $data);
202
        }
203
204 1
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
205 1
        $string .= '<legend>';
206 1
        $string .= (!empty($data['value']['objects']['main']['filename'])) ? preg_replace('/^.+?\-/', '', $data['value']['objects']['main']['filename']) : $this->renderer->humanize('add new file');
207 1
        $string .= '</legend>';
208
209 1
        $string .= $this->renderer->widget($view);
210
211 1
        return $string . '</fieldset>';
212
    }
213
214 94
    public function form_widget_simple(FormView $view, array $data)
215
    {
216 94
        $type = $data['type'] ?? 'text';
217 94
        if ($view->vars['readonly'] && $type !== 'hidden') {
218 4
            $data['type'] = 'hidden';
219 4
            return $data['value'] . $this->renderer->block($view, 'form_widget_simple', $data);
220
        }
221
222 94
        if (empty($data['attr']['class']) && in_array($type, ['text', 'password', 'email', 'url'])) {
223 94
            $data['attr']['class'] = 'shorttext';
224
        }
225
226 94
        if (   !empty($data['value'])
227 94
            || is_numeric($data['value'])) {
228 80
            $data['attr']['value'] = $data['value'];
229
        }
230 94
        $data['attr']['type'] = $type;
231 94
        return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
232
    }
233
234 101
    public function button_widget(FormView $view, array $data)
235
    {
236 101
        $type = $data['type'] ?? 'button';
237 101
        if (!$data['label']) {
238
            $data['label'] = $data['name'];
239
        }
240
241 101
        return '<button type="' . $type . '" ' . $this->renderer->block($view, 'button_attributes') . '>' . $this->renderer->humanize($data['label']) . '</button>';
242
    }
243
244 70
    public function hidden_widget(FormView $view, array $data)
245
    {
246 70
        return $this->renderer->block($view, 'form_widget_simple', ['type' => $data['type'] ?? "hidden"]);
247
    }
248
249 18
    public function email_widget(FormView $view, array $data)
250
    {
251 18
        return $this->renderer->block($view, 'form_widget_simple', ['type' => $data['type'] ?? "email"]);
252
    }
253
254 4
    public function password_widget(FormView $view, array $data)
255
    {
256 4
        return $this->renderer->block($view, 'form_widget_simple', ['type' => $data['type'] ?? "password"]);
257
    }
258
259 8
    public function url_widget(FormView $view, array $data)
260
    {
261 8
        return $this->renderer->block($view, 'form_widget_simple', ['type' => $data['type'] ?? "url"]);
262
    }
263
264 57
    public function autocomplete_widget(FormView $view, array $data)
265
    {
266 57
        autocomplete::add_head_elements($data['handler_options']['creation_mode_enabled'], $data['handler_options']['sortable']);
267
268 57
        $element_id = $view->vars['id'];
269 57
        $jsinit = 'window.' . $element_id . '_handler_options = ' . json_encode($data['handler_options']) . ";\n";
270 57
        $jsinit .= "midcom_helper_datamanager2_autocomplete.create_dm2_widget('{$element_id}_search_input', {$data['min_chars']});\n";
271
272 57
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
273 57
        $string .=  $this->renderer->widget($view['selection']);
274 57
        $string .= ' ' . $this->renderer->widget($view['search_input']);
275 57
        $string .= '</fieldset>';
276 57
        return $string . $this->jsinit($jsinit);
277
    }
278
279 57
    protected function prepare_widget_attributes($type, FormView $view, array &$data)
280
    {
281 57
        $data['attr']['type'] = $type;
282 57
        if (isset($data['value'])) {
283 57
            $data['attr']['value'] = $data['value'];
284
        }
285 57
        if ($data['checked']) {
286 33
            $data['attr']['checked'] = 'checked';
287
        }
288 57
    }
289
290 29
    public function radio_widget(FormView $view, array $data)
291
    {
292 29
        $this->prepare_widget_attributes('radio', $view, $data);
293 29
        if ($view->vars['readonly']) {
294
            $data['attr']['disabled'] = true;
295
        }
296
297 29
        return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
298
    }
299
300 36
    public function checkbox_widget(FormView $view, array $data)
301
    {
302 36
        if ($view->vars['readonly']) {
303
            $string = $this->get_view_renderer()->checkbox_widget($view, $data);
304
            if ($data['checked']) {
305
                $string .= $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
306
            }
307
            return $string;
308
        }
309 36
        $this->prepare_widget_attributes('checkbox', $view, $data);
310
311 36
        return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
312
    }
313
314 46
    public function choice_widget_collapsed(FormView $view, array $data)
315
    {
316 46
        if ($view->vars['readonly'] && empty($view->vars['multiple'])) {
317
            $string = $this->get_view_renderer()->choice_widget_collapsed($view, $data);
318
            return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
319
        }
320
321 46
        $string = '<select ';
322 46
        if (   $data['required']
323 46
            && null === $data['placeholder']
324 46
            && $data['placeholder_in_choices'] === false
325 46
            && $data['multiple'] === false) {
326 3
            $data['required'] = false;
327
        }
328
329 46
        if ($data['multiple']) {
330 4
            $data['attr']['multiple'] = 'multiple';
331
        }
332
333 46
        $string .= $this->renderer->block($view, 'widget_attributes', $data) . '>';
334 46
        if (null !== $data['placeholder']) {
335
            $string .= '<option value=""';
336
            if (   $data['required']
337
                && empty($data['value'])
338
                && "0" !== $data['value']) {
339
                $string .= ' selected="selected"';
340
            }
341
            $string .= '>' . $data['placeholder'] . '</option>';
342
        }
343 46
        if (count($data['preferred_choices']) > 0) {
344
            $string .= $this->renderer->block($view, 'choice_widget_options', ['choices' => $data['preferred_choices']]);
345
            if (count($data['choices']) > 0 && null !== $data['separator']) {
346
                $string .= '<option disabled="disabled">' . $data['separator'] . '</option>';
347
            }
348
        }
349 46
        $string .= $this->renderer->block($view, 'choice_widget_options', ['choices' => $data['choices']]);
350 46
        return $string . '</select>';
351
    }
352
353 4
    public function privilege_widget(FormView $view, array $data)
354
    {
355 4
        if (isset($view->vars['effective_value'])) {
356 4
            if ($view->vars['effective_value']) {
357 2
                $label = $this->renderer->humanize('widget privilege: allow');
358
            } else {
359 3
                $label = $this->renderer->humanize('widget privilege: deny');
360
            }
361 4
            $new_label = sprintf($this->renderer->humanize('widget privilege: inherit %s'), $label);
362 4
            $view->children[2]->vars['label'] = $new_label;
363
        }
364 4
        return $this->choice_widget_expanded($view, $data);
365
    }
366
367 1
    public function privilegeselection_widget(FormView $view, array $data)
368
    {
369 1
        midcom::get()->head->enable_jquery();
370 1
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/privilege/jquery.privilege.css');
371 1
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/privilege/jquery.privilege.js');
372
373 1
        $string = '<div class="holder">' . $this->choice_widget_collapsed($view, $data) . '</div>';
374 1
        return $string . $this->jsinit($view->vars['jsinit']);
375
    }
376
377 29
    public function choice_widget_expanded(FormView $view, array $data)
378
    {
379 29
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
380 29
        foreach ($view as $child) {
381 29
            $string .= $this->renderer->widget($child);
382 29
            $string .= '<label for="' . $child->vars['id'] . '">' . $this->renderer->humanize($child->vars['label']) . '</label>';
383
        }
384 29
        return $string . '</fieldset>';
385
    }
386
387 46
    public function choice_widget_options(FormView $view, array $data)
388
    {
389 46
        $string = '';
390 46
        foreach ($data['choices'] as $index => $choice) {
391 44
            if (is_array($choice) || $choice instanceof ChoiceGroupView) {
392
                $string .= '<optgroup label="' . $index . '">';
393
                $string .= $this->renderer->block($view, 'choice_widget_options', ['choices' => $choice]);
394
                $string .= '</optgroup>';
395
            } else {
396 44
                $choice->attr['value'] = $choice->value;
397 44
                if ($data['is_selected']($choice->value, $data['value'])) {
398 23
                    $choice->attr['selected'] = 'selected';
399
                } else {
400 44
                    unset($choice->attr['selected']);
401
                }
402
403 44
                $string .= '<option ' . $this->attributes($choice->attr);
404 44
                $string .= '>' . $this->renderer->humanize($choice->label) . '</option>';
405
            }
406
        }
407 46
        return $string;
408
    }
409
410 2
    public function other_widget(FormView $view, array $data)
411
    {
412 2
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
413 2
        $string .= $this->renderer->widget($view->children['select']);
414 2
        $string .= $this->renderer->humanize($view->children['other']->vars['label']) . ': ' . $this->renderer->widget($view->children['other'], ['attr' => ['class' => 'other']]);
415 2
        return $string . '</fieldset>';
416
    }
417
418
    public function captcha_widget(FormView $view, array $data)
419
    {
420
        $alt = $this->renderer->humanize('captcha image alt text');
421
        $string = '<fieldset class="captcha">';
422
        $string .= "<img src='{$view->vars['captcha_url']}' alt='{$alt}' text='{$alt}' class='captcha'><br>";
423
        $string .= $this->renderer->humanize('captcha message');
424
        $data['attr']['class'] = 'captcha';
425
        $data['value'] = '';
426
        $string .= $this->form_widget_simple($view, $data);
427
        return $string . '</fieldset>';
428
    }
429
430 3
    public function codemirror_widget(FormView $view, array $data)
431
    {
432
        // we set required to false, because codemirror doesn't play well with html5 validation..
433 3
        $data['required'] = false;
434 3
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>';
435 3
        $string .= $data['value'] . '</textarea>';
436 3
        if (!empty($data['codemirror_snippet'])) {
437 3
            $this->add_head_elements_for_codemirror($data['modes']);
438 3
            $snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);
439 3
            $snippet = str_replace('{$read_only}', 'false', $snippet);
440 3
            $string .= $this->jsinit($snippet);
441
        }
442 3
        return $string;
443
    }
444
445 29
    public function jsdate_widget(FormView $view, array $data)
446
    {
447 29
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
448
449 29
        if ($view->vars['readonly']) {
450 2
            $string .= $this->get_view_renderer()->jsdate_widget($view, $data);
451 2
            $string .= $this->renderer->widget($view['date'], ['type' => 'hidden']);
452 2
            if (isset($view['time'])) {
453 2
                $string .= $this->renderer->widget($view['time'], ['type' => 'hidden']);
454
            }
455
        } else {
456 29
            midcom::get()->head->enable_jquery_ui(['datepicker']);
457
458 29
            $string .= $this->renderer->widget($view['date'], ['type' => 'hidden']);
459 29
            $string .= $this->renderer->widget($view['input'], ['attr' => ['class' => 'jsdate']]);
460 29
            if (isset($view['time'])) {
461 7
                $string .= ' ' . $this->renderer->widget($view['time']);
462
            }
463 29
            $string .= $data['jsinit'];
464
        }
465 29
        return $string . '</fieldset>';
466
    }
467
468 5
    public function image_widget(FormView $view, array $data)
469
    {
470 5
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/image.css');
471 5
        midcom::get()->head->enable_jquery();
472 5
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/image.js');
473
474 5
        $string = '<div ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
475 5
        $string .= '<table class="midcom_datamanager_table_photo"><tr><td>';
476 5
        $preview = null;
477 5
        $objects = $data['value']['objects'] ?? [];
478 5
        foreach ($objects as $identifier => $info) {
479
            $preview = $info;
480
            if ($identifier == 'thumbnail') {
481
                break;
482
            }
483
        }
484 5
        if (!empty($preview)) {
485
            if ($preview['id'] === 0) {
486
                $preview['url'] = \midcom_connection::get_url('self') . 'midcom-exec-midcom.datamanager/preview-tmpfile.php?identifier=' . substr($preview['identifier'], strlen('tmpfile-'));
487
            }
488
489
            $string .= '<img src="' . $preview['url'] . '" class="preview-image">';
490
        }
491 5
        $string .= '</td><td>';
492
493 5
        if (!empty($objects)) {
494
            $string .= '<label class="midcom_datamanager_photo_label">' . $this->renderer->humanize('delete photo') . ' ' . $this->renderer->widget($data['form']['delete']) . '</label>';
495
            $string .= '<ul>';
496
            foreach ($objects as $identifier => $info) {
497
                if (   $info['size_x']
498
                    && $info['size_y']) {
499
                    $size = "{$info['size_x']}&times;{$info['size_y']}";
500
                } else {
501
                    $size = 'unknown';
502
                }
503
                $string .= "<li title=\"{$info['guid']}\">{$size}: <a href='{$info['url']}' target='_new'>{$info['formattedsize']}</a></li>";
504
            }
505
            $string .= '</ul>';
506
        }
507 5
        $string .= $this->renderer->errors($data['form']['file']);
508 5
        $string .= $this->renderer->widget($data['form']['file']);
509
510 5
        if (array_key_exists('title', $view->children)) {
511 1
            $string .= $this->renderer->widget($view->children['title'], ['attr' => ['placeholder' => $this->renderer->humanize('title')]]);
512
        }
513 5
        if (array_key_exists('description', $view->children)) {
514
            $string .= $this->renderer->widget($view->children['description'], ['attr' => ['placeholder' => $this->renderer->humanize('description')]]);
515
        }
516 5
        if (array_key_exists('score', $view->children)) {
517
            $string .= $this->renderer->widget($view->children['score']);
518
        }
519 5
        $string .= '</td></tr></table></div>';
520 5
        $string .= $this->renderer->row($data['form']['identifier']);
521
522 5
        return $string . $this->jsinit('init_image_widget("' . $view->vars['id'] .'");');
523
    }
524
525 7
    public function subform_widget(FormView $view, array $data)
526
    {
527 7
        $head = midcom::get()->head;
528 7
        $head->enable_jquery();
529 7
        if ($view->vars['sortable'] == 'true') {
530
            $head->enable_jquery_ui(['mouse', 'sortable']);
531
        }
532 7
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/subform.js');
533
534 7
        $data['attr']['data-prototype'] = $this->escape($this->renderer->row($view->vars['prototype']));
535 7
        $data['attr']['data-max-count'] = $view->vars['max_count'];
536 7
        $string = $this->renderer->widget($data['form'], $data);
537 7
        return $string . $this->jsinit('init_subform("' . $view->vars['id'] . '", ' . $view->vars['sortable'] . ');');
538
    }
539
540 101
    public function submit_widget(FormView $view, array $data)
541
    {
542 101
        $data['type'] = $data['type'] ?? 'submit';
543 101
        return $this->renderer->block($view, 'button_widget', $data);
544
    }
545
546
    public function delete_widget(FormView $view, array $data)
547
    {
548
        $data['type'] = $data['type'] ?? 'delete';
549
        return $this->renderer->block($view, 'button_widget', $data);
550
    }
551
552 28
    public function textarea_widget(FormView $view, array $data)
553
    {
554 28
        if ($view->vars['readonly']) {
555 1
            $view->vars['output_mode'] = 'nl2br';
556 1
            $string = $this->get_view_renderer()->text_widget($view, $data);
557 1
            return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
558
        }
559 27
        $data['attr']['class'] = 'longtext';
560 27
        $data['attr']['cols'] = 50;
561 27
        return '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
562
    }
563
564 33
    public function markdown_widget(FormView $view, array $data)
565
    {
566 33
        $head = midcom::get()->head;
567 33
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css');
568 33
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/simplemde/simplemde.min.css');
569 33
        $head->enable_jquery();
570 33
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/simplemde/simplemde.min.js');
571
572 33
        $data['required'] = false;
573 33
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
574 33
        return $string . $this->jsinit('var simplemde = new SimpleMDE({ element: document.getElementById("' . $view->vars['id'] . '"), status: false });');
575
    }
576
577 9
    public function tinymce_widget(FormView $view, array $data)
578
    {
579 9
        if ($view->vars['readonly']) {
580
            $string = $this->get_view_renderer()->text_widget($view, $data);
581
            $data['type'] = 'hidden';
582
            return $string . $this->renderer->block($view, 'form_widget_simple', $data);
583
        }
584
585 9
        midcom::get()->head->enable_jquery();
586 9
        midcom::get()->head->add_jsfile($data['tinymce_url']);
587 9
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/tinymce.custom.js');
588
589
        // we set required to false, because tinymce doesn't play well with html5 validation..
590 9
        $data['required'] = false;
591 9
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
592 9
        return $string . $this->jsinit($data['tinymce_snippet']);
593
    }
594
595 100
    public function form_label(FormView $view, array $data)
596
    {
597 100
        if ($data['label'] === false) {
598
            return '';
599
        }
600 100
        if (!$data['label']) {
601 9
            $data['label'] = $data['name'];
602
        }
603 100
        $data['label'] = $this->renderer->humanize($data['label']);
604
605 100
        $label_attr = $data['label_attr'];
606 100
        if ($data['required'] && !$view->vars['readonly']) {
607 70
            $label_attr['class'] = trim(($label_attr['class'] ?? '') . ' required');
608 70
            $data['label'] .= ' <span class="field_required_start">*</span>';
609
        }
610 100
        if (!$data['compound']) {
611 95
            $label_attr['for'] = $data['id'];
612
        }
613 100
        return '<label ' . $this->attributes($label_attr) . '><span class="field_text">' . $data['label'] . '</span></label>';
614
    }
615
}
616