Completed
Push — master ( f489a4...a656f3 )
by Andreas
14:04
created

form   F

Complexity

Total Complexity 121

Size/Duplication

Total Lines 608
Duplicated Lines 0 %

Test Coverage

Coverage 81.23%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
eloc 338
c 6
b 0
f 2
dl 0
loc 608
ccs 303
cts 373
cp 0.8123
rs 2
wmc 121

41 Methods

Rating   Name   Duplication   Size   Complexity  
A textarea_widget() 0 10 2
A subform_widget() 0 13 2
A button_widget() 0 8 2
A submit_widget() 0 4 1
C choice_widget_collapsed() 0 37 15
A button_row() 0 5 1
A markdown_widget() 0 11 1
A jsdate_widget() 0 21 4
A choice_widget_options() 0 21 5
B form_rows() 0 26 7
A image_row() 0 14 3
A email_widget() 0 3 1
A radio_widget() 0 8 2
A form_end() 0 8 3
A repeated_row() 0 8 2
A password_widget() 0 3 1
A __construct() 0 8 1
C image_widget() 0 55 12
A form_start() 0 14 2
A hidden_widget() 0 3 1
B form_widget_simple() 0 18 7
A choice_widget_expanded() 0 8 2
A delete_widget() 0 4 1
A autocomplete_widget() 0 13 1
A hidden_row() 0 3 1
A toolbar_row() 0 7 2
A privilegeselection_widget() 0 8 1
A attachment_row() 0 28 4
A privilege_widget() 0 12 3
A form_row() 0 18 3
A form_label() 0 19 6
A other_widget() 0 6 1
A get_view_renderer() 0 3 1
A form_errors() 0 15 4
A checkbox_widget() 0 12 3
A autocomplete_row() 0 21 4
A url_widget() 0 3 1
A codemirror_widget() 0 13 2
A tinymce_widget() 0 16 2
A captcha_widget() 0 10 1
A prepare_widget_attributes() 0 8 3

How to fix   Complexity   

Complex Class

Complex classes like form often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use form, and based on these observations, apply Extract Interface, too.

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