Passed
Push — master ( c4eeb8...d8ebc1 )
by Andreas
17:42
created

view::__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 2
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 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 54
    public function __construct(renderer $renderer, bool $skip_empty = false)
30
    {
31 54
        parent::__construct($renderer);
32 54
        $this->skip_empty = $skip_empty;
33 54
    }
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 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
66 22
            $string .= $this->renderer->row($child);
67
68 22
            if (isset($child->vars['end_fieldset'])) {
69 5
                $end_fieldsets = max(1, (int) $child->vars['end_fieldset']);
70 5
                $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 && $content === '') {
100 4
            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 52
    public function text_widget(FormView $view, array $data)
178
    {
179 52
        if (empty($view->vars['output_mode'])) {
180 49
            $view->vars['output_mode'] = 'html';
181
        }
182
183 52
        switch ($view->vars['output_mode']) {
184 52
            case 'code':
185
                return '<pre style="overflow:auto">' . htmlspecialchars($data['value'], $this->specialchars_quotes, $this->specialchars_charset) . '</pre>';
186
187 52
            case 'pre':
188 2
                return '<pre style="white-space: pre-wrap">' . htmlspecialchars($data['value'], $this->specialchars_quotes, $this->specialchars_charset) . '</pre>';
189
190 52
            case 'specialchars':
191
                return htmlspecialchars($data['value'], $this->specialchars_quotes, $this->specialchars_charset);
192
193 52
            case 'nl2br':
194 1
                return nl2br(htmlentities($data['value'], $this->specialchars_quotes, $this->specialchars_charset));
195
196 51
            case 'midgard_f':
197
                return midcom_helper_formatter::format($data['value'], 'f');
198
199 51
            case 'markdown':
200 25
                return MarkdownExtra::defaultTransform($data['value']);
201
202 50
            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 50
            case 'html':
207 50
                return $data['value'];
208
        }
209
    }
210
211 6
    public function radiocheckselect_widget(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 (isset($data['data'])) {
272 26
            if (!empty($view->vars['multiple'])) {
273 8
                $selection = $data['data'];
274
            } else {
275 18
                $selection = (string) $data['data'];
276
            }
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
                            return $this->renderer->humanize($option->label);
282
                        }
283
                    }
284 18
                } elseif ($data['is_selected']($choice->value, $selection)) {
285 13
                    return $this->renderer->humanize($choice->label);
286
                }
287
            }
288
        }
289 19
        return '';
290
    }
291
292 14
    public function checkbox_widget(FormView $view, array $data)
293
    {
294 14
        if ($data['checked']) {
295 6
            return '<img src="' . MIDCOM_STATIC_URL . '/stock-icons/16x16/ok.png" alt="selected" />';
296
        }
297 8
        return '<img src="' . MIDCOM_STATIC_URL . '/stock-icons/16x16/cancel.png" alt="not selected" />';
298
    }
299
300 3
    public function codemirror_widget(FormView $view, array $data)
301
    {
302 3
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', $data) . '>';
303 3
        $string .= $data['value'] . '</textarea>';
304 3
        if (!empty($data['codemirror_snippet'])) {
305 3
            $this->add_head_elements_for_codemirror($data['modes']);
306 3
            $snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);
307 3
            $snippet = str_replace('{$read_only}', 'true', $snippet);
308 3
            $string .= $this->jsinit($snippet);
309
        }
310 3
        return $string;
311
    }
312
313 15
    public function tinymce_widget(FormView $view, array $data)
314
    {
315 15
        return $data['value'];
316
    }
317
318 17
    public function jsdate_widget(FormView $view, array $data)
319
    {
320 17
        if (empty($data['value']['date'])) {
321 9
            return '';
322
        }
323
324 10
        $time_format = 'none';
325 10
        if (isset($view['time'])) {
326 5
            $time_format = (isset($data['value']['seconds'])) ? 'medium' : 'short';
327
        }
328 10
        return midcom::get()->i18n->get_l10n()->get_formatter()->datetime($data['value']['date'], 'medium', $time_format);
329
    }
330
}
331