Completed
Push — master ( 784146...1583b2 )
by Andreas
20:04
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
        if ($view->vars['readonly']) {
267 2
            $string = $this->get_view_renderer()->autocomplete_widget($view, $data);
268 2
            return $string .  $this->renderer->widget($view['selection']);
269
        }
270
271 57
        autocomplete::add_head_elements($data['handler_options']['creation_mode_enabled'], $data['handler_options']['sortable']);
272
273 57
        $element_id = $view->vars['id'];
274 57
        $jsinit = 'window.' . $element_id . '_handler_options = ' . json_encode($data['handler_options']) . ";\n";
275 57
        $jsinit .= "midcom_helper_datamanager2_autocomplete.create_dm2_widget('{$element_id}_search_input', {$data['min_chars']});\n";
276
277 57
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
278 57
        $string .=  $this->renderer->widget($view['selection']);
279 57
        $string .= ' ' . $this->renderer->widget($view['search_input']);
280 57
        $string .= '</fieldset>';
281 57
        return $string . $this->jsinit($jsinit);
282
    }
283
284 57
    protected function prepare_widget_attributes($type, FormView $view, array &$data)
285
    {
286 57
        $data['attr']['type'] = $type;
287 57
        if (isset($data['value'])) {
288 57
            $data['attr']['value'] = $data['value'];
289
        }
290 57
        if ($data['checked']) {
291 33
            $data['attr']['checked'] = 'checked';
292
        }
293 57
    }
294
295 29
    public function radio_widget(FormView $view, array $data)
296
    {
297 29
        $this->prepare_widget_attributes('radio', $view, $data);
298 29
        if ($view->vars['readonly']) {
299
            $data['attr']['disabled'] = true;
300
        }
301
302 29
        return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
303
    }
304
305 36
    public function checkbox_widget(FormView $view, array $data)
306
    {
307 36
        if ($view->vars['readonly']) {
308
            $string = $this->get_view_renderer()->checkbox_widget($view, $data);
309
            if ($data['checked']) {
310
                $string .= $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
311
            }
312
            return $string;
313
        }
314 36
        $this->prepare_widget_attributes('checkbox', $view, $data);
315
316 36
        return '<input ' . $this->renderer->block($view, 'widget_attributes', $data) . ' />';
317
    }
318
319 46
    public function choice_widget_collapsed(FormView $view, array $data)
320
    {
321 46
        if ($view->vars['readonly'] && empty($view->vars['multiple'])) {
322
            $string = $this->get_view_renderer()->choice_widget_collapsed($view, $data);
323
            return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
324
        }
325
326 46
        $string = '<select ';
327 46
        if (   $data['required']
328 46
            && null === $data['placeholder']
329 46
            && $data['placeholder_in_choices'] === false
330 46
            && $data['multiple'] === false) {
331 3
            $data['required'] = false;
332
        }
333
334 46
        if ($data['multiple']) {
335 4
            $data['attr']['multiple'] = 'multiple';
336
        }
337
338 46
        $string .= $this->renderer->block($view, 'widget_attributes', $data) . '>';
339 46
        if (null !== $data['placeholder']) {
340
            $string .= '<option value=""';
341
            if (   $data['required']
342
                && empty($data['value'])
343
                && "0" !== $data['value']) {
344
                $string .= ' selected="selected"';
345
            }
346
            $string .= '>' . $data['placeholder'] . '</option>';
347
        }
348 46
        if (count($data['preferred_choices']) > 0) {
349
            $string .= $this->renderer->block($view, 'choice_widget_options', ['choices' => $data['preferred_choices']]);
350
            if (count($data['choices']) > 0 && null !== $data['separator']) {
351
                $string .= '<option disabled="disabled">' . $data['separator'] . '</option>';
352
            }
353
        }
354 46
        $string .= $this->renderer->block($view, 'choice_widget_options', ['choices' => $data['choices']]);
355 46
        return $string . '</select>';
356
    }
357
358 4
    public function privilege_widget(FormView $view, array $data)
359
    {
360 4
        if (isset($view->vars['effective_value'])) {
361 4
            if ($view->vars['effective_value']) {
362 2
                $label = $this->renderer->humanize('widget privilege: allow');
363
            } else {
364 3
                $label = $this->renderer->humanize('widget privilege: deny');
365
            }
366 4
            $new_label = sprintf($this->renderer->humanize('widget privilege: inherit %s'), $label);
367 4
            $view->children[2]->vars['label'] = $new_label;
368
        }
369 4
        return $this->choice_widget_expanded($view, $data);
370
    }
371
372 1
    public function privilegeselection_widget(FormView $view, array $data)
373
    {
374 1
        midcom::get()->head->enable_jquery();
375 1
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/privilege/jquery.privilege.css');
376 1
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/privilege/jquery.privilege.js');
377
378 1
        $string = '<div class="holder">' . $this->choice_widget_collapsed($view, $data) . '</div>';
379 1
        return $string . $this->jsinit($view->vars['jsinit']);
380
    }
381
382 29
    public function choice_widget_expanded(FormView $view, array $data)
383
    {
384 29
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
385 29
        foreach ($view as $child) {
386 29
            $string .= $this->renderer->widget($child);
387 29
            $string .= '<label for="' . $child->vars['id'] . '">' . $this->renderer->humanize($child->vars['label']) . '</label>';
388
        }
389 29
        return $string . '</fieldset>';
390
    }
391
392 46
    public function choice_widget_options(FormView $view, array $data)
393
    {
394 46
        $string = '';
395 46
        foreach ($data['choices'] as $index => $choice) {
396 44
            if (is_array($choice) || $choice instanceof ChoiceGroupView) {
397
                $string .= '<optgroup label="' . $index . '">';
398
                $string .= $this->renderer->block($view, 'choice_widget_options', ['choices' => $choice]);
399
                $string .= '</optgroup>';
400
            } else {
401 44
                $choice->attr['value'] = $choice->value;
402 44
                if ($data['is_selected']($choice->value, $data['value'])) {
403 23
                    $choice->attr['selected'] = 'selected';
404
                } else {
405 44
                    unset($choice->attr['selected']);
406
                }
407
408 44
                $string .= '<option ' . $this->attributes($choice->attr);
409 44
                $string .= '>' . $this->renderer->humanize($choice->label) . '</option>';
410
            }
411
        }
412 46
        return $string;
413
    }
414
415 2
    public function other_widget(FormView $view, array $data)
416
    {
417 2
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
418 2
        $string .= $this->renderer->widget($view->children['select']);
419 2
        $string .= $this->renderer->humanize($view->children['other']->vars['label']) . ': ' . $this->renderer->widget($view->children['other'], ['attr' => ['class' => 'other']]);
420 2
        return $string . '</fieldset>';
421
    }
422
423
    public function captcha_widget(FormView $view, array $data)
424
    {
425
        $alt = $this->renderer->humanize('captcha image alt text');
426
        $string = '<fieldset class="captcha">';
427
        $string .= "<img src='{$view->vars['captcha_url']}' alt='{$alt}' text='{$alt}' class='captcha'><br>";
428
        $string .= $this->renderer->humanize('captcha message');
429
        $data['attr']['class'] = 'captcha';
430
        $data['value'] = '';
431
        $string .= $this->form_widget_simple($view, $data);
432
        return $string . '</fieldset>';
433
    }
434
435 3
    public function codemirror_widget(FormView $view, array $data)
436
    {
437
        // we set required to false, because codemirror doesn't play well with html5 validation..
438 3
        $data['required'] = false;
439 3
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>';
440 3
        $string .= $data['value'] . '</textarea>';
441 3
        if (!empty($data['codemirror_snippet'])) {
442 3
            $this->add_head_elements_for_codemirror($data['modes']);
443 3
            $snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);
444 3
            $snippet = str_replace('{$read_only}', 'false', $snippet);
445 3
            $string .= $this->jsinit($snippet);
446
        }
447 3
        return $string;
448
    }
449
450 29
    public function jsdate_widget(FormView $view, array $data)
451
    {
452 29
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
453
454 29
        if ($view->vars['readonly']) {
455 2
            $string .= $this->get_view_renderer()->jsdate_widget($view, $data);
456 2
            $string .= $this->renderer->widget($view['date'], ['type' => 'hidden']);
457 2
            if (isset($view['time'])) {
458 2
                $string .= $this->renderer->widget($view['time'], ['type' => 'hidden']);
459
            }
460
        } else {
461 29
            midcom::get()->head->enable_jquery_ui(['datepicker']);
462
463 29
            $string .= $this->renderer->widget($view['date'], ['type' => 'hidden']);
464 29
            $string .= $this->renderer->widget($view['input'], ['attr' => ['class' => 'jsdate']]);
465 29
            if (isset($view['time'])) {
466 7
                $string .= ' ' . $this->renderer->widget($view['time']);
467
            }
468 29
            $string .= $data['jsinit'];
469
        }
470 29
        return $string . '</fieldset>';
471
    }
472
473 5
    public function image_widget(FormView $view, array $data)
474
    {
475 5
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/image.css');
476 5
        midcom::get()->head->enable_jquery();
477 5
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/image.js');
478
479 5
        $string = '<div ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
480 5
        $string .= '<table class="midcom_datamanager_table_photo"><tr><td>';
481 5
        $preview = null;
482 5
        $objects = $data['value']['objects'] ?? [];
483 5
        foreach ($objects as $identifier => $info) {
484
            $preview = $info;
485
            if ($identifier == 'thumbnail') {
486
                break;
487
            }
488
        }
489 5
        if (!empty($preview)) {
490
            if ($preview['id'] === 0) {
491
                $preview['url'] = \midcom_connection::get_url('self') . 'midcom-exec-midcom.datamanager/preview-tmpfile.php?identifier=' . substr($preview['identifier'], strlen('tmpfile-'));
492
            }
493
494
            $string .= '<img src="' . $preview['url'] . '" class="preview-image">';
495
        }
496 5
        $string .= '</td><td>';
497
498 5
        if (!empty($objects)) {
499
            $string .= '<label class="midcom_datamanager_photo_label">' . $this->renderer->humanize('delete photo') . ' ' . $this->renderer->widget($data['form']['delete']) . '</label>';
500
            $string .= '<ul>';
501
            foreach ($objects as $identifier => $info) {
502
                if (   $info['size_x']
503
                    && $info['size_y']) {
504
                    $size = "{$info['size_x']}&times;{$info['size_y']}";
505
                } else {
506
                    $size = 'unknown';
507
                }
508
                $string .= "<li title=\"{$info['guid']}\">{$size}: <a href='{$info['url']}' target='_new'>{$info['formattedsize']}</a></li>";
509
            }
510
            $string .= '</ul>';
511
        }
512 5
        $string .= $this->renderer->errors($data['form']['file']);
513 5
        $string .= $this->renderer->widget($data['form']['file']);
514
515 5
        if (array_key_exists('title', $view->children)) {
516 1
            $string .= $this->renderer->widget($view->children['title'], ['attr' => ['placeholder' => $this->renderer->humanize('title')]]);
517
        }
518 5
        if (array_key_exists('description', $view->children)) {
519
            $string .= $this->renderer->widget($view->children['description'], ['attr' => ['placeholder' => $this->renderer->humanize('description')]]);
520
        }
521 5
        if (array_key_exists('score', $view->children)) {
522
            $string .= $this->renderer->widget($view->children['score']);
523
        }
524 5
        $string .= '</td></tr></table></div>';
525 5
        $string .= $this->renderer->row($data['form']['identifier']);
526
527 5
        return $string . $this->jsinit('init_image_widget("' . $view->vars['id'] .'");');
528
    }
529
530 7
    public function subform_widget(FormView $view, array $data)
531
    {
532 7
        $head = midcom::get()->head;
533 7
        $head->enable_jquery();
534 7
        if ($view->vars['sortable'] == 'true') {
535
            $head->enable_jquery_ui(['mouse', 'sortable']);
536
        }
537 7
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/subform.js');
538
539 7
        $data['attr']['data-prototype'] = $this->escape($this->renderer->row($view->vars['prototype']));
540 7
        $data['attr']['data-max-count'] = $view->vars['max_count'];
541 7
        $string = $this->renderer->widget($data['form'], $data);
542 7
        return $string . $this->jsinit('init_subform("' . $view->vars['id'] . '", ' . $view->vars['sortable'] . ');');
543
    }
544
545 101
    public function submit_widget(FormView $view, array $data)
546
    {
547 101
        $data['type'] = $data['type'] ?? 'submit';
548 101
        return $this->renderer->block($view, 'button_widget', $data);
549
    }
550
551
    public function delete_widget(FormView $view, array $data)
552
    {
553
        $data['type'] = $data['type'] ?? 'delete';
554
        return $this->renderer->block($view, 'button_widget', $data);
555
    }
556
557 28
    public function textarea_widget(FormView $view, array $data)
558
    {
559 28
        if ($view->vars['readonly']) {
560 1
            $view->vars['output_mode'] = 'nl2br';
561 1
            $string = $this->get_view_renderer()->text_widget($view, $data);
562 1
            return $string . $this->renderer->block($view, 'form_widget_simple', ['type' => "hidden"]);
563
        }
564 27
        $data['attr']['class'] = 'longtext';
565 27
        $data['attr']['cols'] = 50;
566 27
        return '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
567
    }
568
569 33
    public function markdown_widget(FormView $view, array $data)
570
    {
571 33
        $head = midcom::get()->head;
572 33
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css');
573 33
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/simplemde/simplemde.min.css');
574 33
        $head->enable_jquery();
575 33
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/simplemde/simplemde.min.js');
576
577 33
        $data['required'] = false;
578 33
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
579 33
        return $string . $this->jsinit('var simplemde = new SimpleMDE({ element: document.getElementById("' . $view->vars['id'] . '"), status: false });');
580
    }
581
582 9
    public function tinymce_widget(FormView $view, array $data)
583
    {
584 9
        if ($view->vars['readonly']) {
585
            $string = $this->get_view_renderer()->text_widget($view, $data);
586
            $data['type'] = 'hidden';
587
            return $string . $this->renderer->block($view, 'form_widget_simple', $data);
588
        }
589
590 9
        midcom::get()->head->enable_jquery();
591 9
        midcom::get()->head->add_jsfile($data['tinymce_url']);
592 9
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/tinymce.custom.js');
593
594
        // we set required to false, because tinymce doesn't play well with html5 validation..
595 9
        $data['required'] = false;
596 9
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>' . $data['value'] . '</textarea>';
597 9
        return $string . $this->jsinit($data['tinymce_snippet']);
598
    }
599
600 100
    public function form_label(FormView $view, array $data)
601
    {
602 100
        if ($data['label'] === false) {
603
            return '';
604
        }
605 100
        if (!$data['label']) {
606 9
            $data['label'] = $data['name'];
607
        }
608 100
        $data['label'] = $this->renderer->humanize($data['label']);
609
610 100
        $label_attr = $data['label_attr'];
611 100
        if ($data['required'] && !$view->vars['readonly']) {
612 70
            $label_attr['class'] = trim(($label_attr['class'] ?? '') . ' required');
613 70
            $data['label'] .= ' <span class="field_required_start">*</span>';
614
        }
615 100
        if (!$data['compound']) {
616 95
            $label_attr['for'] = $data['id'];
617
        }
618 100
        return '<label ' . $this->attributes($label_attr) . '><span class="field_text">' . $data['label'] . '</span></label>';
619
    }
620
}
621