Passed
Push — master ( b1c1e8...d0f685 )
by Andreas
32:35
created

view::url_widget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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