Completed
Push — master ( b0bf1d...042f37 )
by Andreas
14:04
created

form::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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