Passed
Push — master ( a4dff3...a51da9 )
by Andreas
16:37 queued 06:39
created

form::hidden_widget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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