view::checkbox_widget()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace midcom\datamanager\template;
3
4
use Symfony\Component\Form\FormView;
5
use midcom;
6
use midcom_helper_formatter;
7
use Michelf\MarkdownExtra;
8
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
9
use midcom\datamanager\renderer;
10
11
class view extends base
12
{
13
    private bool $skip_empty = false;
14
15
    /**
16
     * Define the quotes behavior when htmlspecialchars() is called
17
     *
18
     * @see http://www.php.net/htmlspecialchars
19
     */
20
    private int $specialchars_quotes = ENT_QUOTES;
21
22
    /**
23
     * Define the charset to use when htmlspecialchars() is called
24
     *
25
     * @see http://www.php.net/htmlspecialchars
26
     */
27
    private string $specialchars_charset = 'UTF-8';
28
29 57
    public function __construct(renderer $renderer, bool $skip_empty = false)
30
    {
31 57
        parent::__construct($renderer);
32 57
        $this->skip_empty = $skip_empty;
33
    }
34
35 13
    public function form_start(FormView $view, array $data)
36
    {
37 13
        return "<div class=\"midcom_helper_datamanager2_view\">";
38
    }
39
40 13
    public function form_end(FormView $view, array $data)
41
    {
42 13
        return "</div>";
43
    }
44
45 13
    public function form_errors(FormView $view, array $data)
46
    {
47 13
        return '';
48
    }
49
50 22
    public function form_rows(FormView $view, array $data)
51
    {
52 22
        $string = '';
53 22
        foreach ($view as $child) {
54 22
            if (!empty($child->vars['hidden'])) {
55 2
                $child->setRendered();
56 2
                continue;
57
            }
58
59 22
            if (isset($child->vars['start_fieldset'])) {
60 4
                $string .= '<div class="fieldset">';
61 4
                if (!empty($child->vars['start_fieldset']['title'])) {
62 4
                    $string .= '<h2>' . $this->renderer->humanize($child->vars['start_fieldset']['title']) . '</h2>';
63
                }
64
            }
65
66 22
            $string .= $this->renderer->row($child);
67
68 22
            if (isset($child->vars['end_fieldset'])) {
69 4
                $end_fieldsets = max(1, (int) $child->vars['end_fieldset']);
70 4
                $string .= str_repeat('</div>', $end_fieldsets);
71
            }
72
        }
73 22
        return $string;
74
    }
75
76 22
    public function form_rest(FormView $view, array $data)
77
    {
78 22
        return '';
79
    }
80
81 2
    public function hidden_row(FormView $view, array $data)
82
    {
83 2
        return '';
84
    }
85
86
    public function button_row(FormView $view, array $data)
87
    {
88
        return '';
89
    }
90
91 13
    public function toolbar_row(FormView $view, array $data)
92
    {
93 13
        return '';
94
    }
95
96 22
    public function form_row(FormView $view, array $data)
97
    {
98 22
        $content = $this->renderer->widget($view);
99 22
        if ($this->skip_empty && trim($content) === '') {
100 10
            return '';
101
        }
102
103 20
        $class = 'field field_' . $view->vars['block_prefixes'][count($view->vars['block_prefixes']) - 2];
104
105 20
        $string = '<div class="' . $class . '">';
106 20
        $string .= $this->renderer->label($view);
107 20
        $string .= '<div class="value">';
108 20
        $string .= $content;
109 20
        return $string . '</div></div>';
110
    }
111
112
    public function attachment_row(FormView $view, array $data)
113
    {
114
        if (empty($data['value']['url'])) {
115
            return '';
116
        }
117
        return '<a href="' . $data['value']['url'] . '">' . $data['value']['title'] . '</a>';
118
    }
119
120 20
    public function form_label(FormView $view, array $data)
121
    {
122 20
        if ($data['label'] === false) {
123
            return '';
124
        }
125 20
        $label_attr = $data['label_attr'];
126 20
        $label_attr['class'] = trim('title ' . ($label_attr['class'] ?? ''));
127 20
        if (!$data['label']) {
128 9
            $data['label'] = $data['name'];
129
        }
130 20
        return '<div ' . $this->attributes($label_attr) . '>' . $this->renderer->humanize($data['label']) . '</div>';
131
    }
132
133 4
    public function subform_widget(FormView $view, array $data)
134
    {
135 4
        if (empty($view->vars['data'])) {
136 4
            return '';
137
        }
138
        $string = '<div ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
139
        $string .= $this->renderer->block($view, 'form_rows');
140
        $string .= $this->renderer->rest($view);
141
        return $string . '</div>';
142
    }
143
144 14
    public function form_widget_simple(FormView $view, array $data)
145
    {
146 14
        if (   !empty($data['value'])
147 14
            || is_numeric($data['value'])) {
148 12
            return $this->escape($data['value']);
149
        }
150 7
        return '';
151
    }
152
153 9
    public function email_widget(FormView $view, array $data)
154
    {
155 9
        if (!empty($data['value'])) {
156 3
            return '<a href="mailto:' . $data['value'] . '">' . $this->escape($data['value']) . '</a>';
157
        }
158 7
        return '';
159
    }
160
161 1
    public function password_widget(FormView $view, array $data)
162
    {
163 1
        if (!empty($data['value'])) {
164
            return '******';
165
        }
166 1
        return '';
167
    }
168
169 6
    public function url_widget(FormView $view, array $data)
170
    {
171 6
        if (!empty($data['value'])) {
172
            return '<a href="' . $data['value'] . '">' . $this->escape($data['value']) . '</a>';
173
        }
174 6
        return '';
175
    }
176
177 57
    public function text_widget(FormView $view, array $data)
178
    {
179 57
        if (empty($view->vars['output_mode'])) {
180 54
            $view->vars['output_mode'] = 'specialchars';
181
        }
182
183 57
        switch ($view->vars['output_mode']) {
184 57
            case 'code':
185 3
                return '<pre style="overflow:auto">' . htmlspecialchars($data['value'], $this->specialchars_quotes, $this->specialchars_charset) . '</pre>';
186
187 57
            case 'pre':
188 2
                return '<pre style="white-space: pre-wrap">' . htmlspecialchars($data['value'], $this->specialchars_quotes, $this->specialchars_charset) . '</pre>';
189
190 57
            case 'specialchars':
191 54
                return htmlspecialchars($data['value'], $this->specialchars_quotes, $this->specialchars_charset);
192
193 41
            case 'nl2br':
194 1
                return nl2br(htmlentities($data['value'], $this->specialchars_quotes, $this->specialchars_charset));
195
196 40
            case 'midgard_f':
197
                return midcom_helper_formatter::format($data['value'], 'f');
198
199 40
            case 'markdown':
200 25
                return MarkdownExtra::defaultTransform($data['value']);
201
202 15
            case (str_starts_with($view->vars['output_mode'], 'x')):
203
                // Run the contents through a custom formatter registered via mgd_register_filter
204
                return midcom_helper_formatter::format($data['value'], $view->vars['output_mode']);
205
206 15
            case 'html':
207 15
                return $data['value'];
208
        }
209
    }
210
211 6
    public function choice_widget_expanded(FormView $view, array $data)
212
    {
213 6
        $ret = [];
214 6
        foreach ($view->children as $child) {
215 6
            if ($child->vars['checked']) {
216 6
                $ret[] = $this->renderer->humanize($child->vars['label']);
217
            }
218
        }
219 6
        return implode(', ', $ret);
220
    }
221
222 12
    public function image_widget(FormView $view, array $data)
223
    {
224 12
        if (!array_key_exists('main', $data['value']['objects'])) {
225 12
            $ret = "";
226 12
            if (!empty($data['value']['objects'])) {
227
                $ret .= $this->renderer->humanize('could not figure out which image to show, listing files') . "<ul>";
228
                foreach ($data['value']['objects'] as $info) {
229
                    $ret .= "<li><a href='{$info['url']}'>{$info['filename']}</a></li>";
230
                }
231
                $ret .= "</ul>";
232
            }
233 12
            return $ret;
234
        }
235
236
        $identifier = 'main';
237
        $linkto = false;
238
        if (array_key_exists('view', $data['value']['objects'])) {
239
            $identifier = 'view';
240
            $linkto = 'main';
241
        } elseif (array_key_exists('thumbnail', $data['value']['objects'])) {
242
            $identifier = 'thumbnail';
243
            $linkto = 'main';
244
        } elseif (array_key_exists('archival', $data['value']['objects'])) {
245
            $linkto = 'archival';
246
        }
247
248
        $img = $data['value']['objects'][$identifier];
249
        $return = '<div class="midcom_helper_datamanager2_type_photo">';
250
        $img_tag = "<img src='{$img['url']}' {$img['size_line']} class='photo {$identifier}' />";
251
        if ($linkto) {
252
            $linked = $data['value']['objects'][$linkto];
253
            $return .= "<a href='{$linked['url']}' target='_blank' class='{$linkto} {$linked['mimetype']}'>{$img_tag}</a>";
254
        } else {
255
            $return .= $img_tag;
256
        }
257
        if (array_key_exists('archival', $data['value']['objects'])) {
258
            $arch = $data['value']['objects']['archival'];
259
            $return .= "<br/><a href='{$arch['url']}' target='_blank' class='archival {$arch['mimetype']}'>" . $this->renderer->humanize('archived image') . '</a>';
260
        }
261
        return $return . '</div>';
262
    }
263
264 40
    public function autocomplete_widget(FormView $view, array $data)
265
    {
266 40
        return implode(', ', $data['handler_options']['preset']);
267
    }
268
269 26
    public function choice_widget_collapsed(FormView $view, array $data)
270
    {
271 26
        if (!empty($view->vars['multiple'])) {
272 8
            $selection = $data['data'];
273
        } else {
274 18
            $selection = (string) $data['data'];
275
        }
276 26
        $selected = [];
277 26
        foreach ($data['choices'] as $choice) {
278 18
            if ($choice instanceof ChoiceGroupView) {
279
                foreach ($choice->choices as $option) {
280
                    if ($data['is_selected']($option->value, $selection)) {
281
                        $selected[] = $this->renderer->humanize($option->label);
282
                    }
283
                }
284 18
            } elseif ($data['is_selected']($choice->value, $selection)) {
285 14
                $selected[] = $this->renderer->humanize($choice->label);
286
            }
287
        }
288 26
        return implode(', ', $selected);
289
    }
290
291 15
    public function checkbox_widget(FormView $view, array $data)
292
    {
293 15
        if ($data['checked']) {
294 7
            return '<img src="' . MIDCOM_STATIC_URL . '/stock-icons/16x16/ok.png" alt="selected" />';
295
        }
296 8
        return '<img src="' . MIDCOM_STATIC_URL . '/stock-icons/16x16/cancel.png" alt="not selected" />';
297
    }
298
299
    public function codemirror_widget(FormView $view, array $data)
300
    {
301
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>';
302
        $string .= $data['value'] . '</textarea>';
303
        if (!empty($data['codemirror_snippet'])) {
304
            $this->add_head_elements_for_codemirror($data['modes']);
305
            $snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);
306
            $snippet = str_replace('{$read_only}', 'true', $snippet);
307
            $string .= $this->jsinit($snippet);
308
        }
309
        return $string;
310
    }
311
312 12
    public function tinymce_widget(FormView $view, array $data)
313
    {
314 12
        return $data['value'];
315
    }
316
317 17
    public function jsdate_widget(FormView $view, array $data)
318
    {
319 17
        if (empty($data['value']['date'])) {
320 9
            return '';
321
        }
322
323 10
        $time_format = 'none';
324 10
        if (isset($view['time'])) {
325 5
            $time_format = (isset($data['value']['seconds'])) ? 'medium' : 'short';
326
        }
327 10
        return midcom::get()->i18n->get_l10n()->get_formatter()->datetime($data['value']['date'], 'medium', $time_format);
328
    }
329
}
330