Completed
Branch master (e8947e)
by Andreas
15:09
created

form::choice_widget_expanded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace midcom\datamanager\template;
3
4
use Symfony\Component\Form\FormView;
5
6
class form extends base
7
{
8
    public function form_start(FormView $view, array $data)
9
    {
10
        $attributes = $data['attr'];
11
        $attributes['name'] = $data['name'];
12
        $attributes['id'] = $data['name'];
13
        $attributes['method'] = strtolower($data['method']);
14
        $attributes['action'] = $data['action'];
15
        $attributes['class'] = 'datamanager2';
16
17
        if ($data['multipart'])
18
        {
19
            $attributes['enctype'] = 'multipart/form-data';
20
        }
21
22
        return '<form' . $this->attributes($attributes) . '>';
23
    }
24
25
    public function form_errors(FormView $view, array $data)
26
    {
27
        $string = '';
28
29
        foreach ($data['errors'] as $error)
30
        {
31
            $string .= '<span class="field_error">' . $error->getMessage() . '</span>';
32
        }
33
34
        return $string;
35
    }
36
37 View Code Duplication
    public function form_rows(FormView $view, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $string = '';
40
        foreach ($view as $child)
41
        {
42
            if (    array_key_exists('start_fieldset', $child->vars)
43
                && $child->vars['start_fieldset'] !== null)
44
            {
45
                $string .= '<fieldset class="fieldset">';
46
                if (!empty($child->vars['start_fieldset']['title']))
47
                {
48
                    $string .= '<legend>' . $child->vars['start_fieldset']['title'] . '</legend>';
49
                }
50
            }
51
            $string .= $this->renderer->row($child);
52
            if (    array_key_exists('end_fieldset', $child->vars)
53
                && $child->vars['end_fieldset'] !== null)
54
            {
55
                $end_fieldsets = max(1, (int) $child->vars['end_fieldset']);
56
                for ($i = 0; $i < $end_fieldsets; $i++)
57
                {
58
                    $string .= '</fieldset>';
59
                }
60
            }
61
        }
62
        return $string;
63
    }
64
65 View Code Duplication
    public function form_end(FormView $view, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $string = '';
68
        if (   !isset($data['render_rest'])
69
            || $data['render_rest'])
70
        {
71
            $string .= $this->renderer->rest($view);
72
        }
73
        return $string . '</form>';
74
    }
75
76
    public function form_row(FormView $view, array $data)
77
    {
78
        $class = 'element element_' . $view->vars['block_prefixes'][count($view->vars['block_prefixes']) - 2];
79
80
        if ($view->vars['required'])
81
        {
82
            $class .= ' required';
83
        }
84
85
        if ($data['errors']->count() > 0)
86
        {
87
            $class .= ' error';
88
        }
89
90
        $string = '<div class="' . $class . '">';
91
        $string .= $this->renderer->label($view);
92
        $string .= $this->renderer->errors($view);
93
        $string .= '<div class="input">';
94
        $string .= $this->renderer->widget($view);
95
        return $string . '</div></div>';
96
    }
97
98
    public function button_row(FormView $view, array $data)
99
    {
100
        $string = '<div>';
101
        $string .= $this->renderer->widget($view);
102
        return $string . '</div>';
103
    }
104
105
    public function hidden_row(FormView $view, array $data)
106
    {
107
        return $this->renderer->widget($view);
108
    }
109
110 View Code Duplication
    public function toolbar_row(FormView $view, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $string = '<div class="form_toolbar">';
113
        foreach ($view as $child)
114
        {
115
            $string .= $this->renderer->widget($child);
116
        }
117
        return $string . '</div>';
118
    }
119
120
    public function images_row(FormView $view, array $data)
121
    {
122
        $string = '<fieldset' . $this->renderer->block($view, 'widget_container_attributes') . '>';
123
        $string .= '<legend>';
124
        $string .= (!empty($data['value']['filename'])) ? $data['value']['filename'] : $this->renderer->humanize('add new file');
125
        $string .= '</legend>';
126
        $string .= $this->renderer->widget($view);
127
        return $string . '</fieldset>';
128
    }
129
130
    public function blobs_row(FormView $view, array $data)
131
    {
132
        $string = '<fieldset' . $this->renderer->block($view, 'widget_container_attributes') . '>';
133
        $string .= '<legend>';
134
        $string .= (!empty($data['value']['filename'])) ? $data['value']['filename'] : $this->renderer->humanize('add new file');
135
        $string .= '</legend>';
136
137
        $string .= '<div class="attachment-container">';
138
        $string .= '<div class="attachment-preview">';
139
        if (!empty($data['value']['filename']))
140
        {
141
            $icon = \midcom_helper_misc::get_mime_icon($data['value']['mimetype']);
142
143
            $string .= '<a href="' . $data['value']['url'] . '" target="_blank"><img alt="' . $data['value']['filename'] . '" src="' . $icon . '" /></a>';
144
        }
145
146
        $string .= '</div><div class="attachment-input">';
147
        $string .= $this->renderer->row($data['form']['title']);
148
        $string .= $this->renderer->row($data['form']['file']);
149
        $string .= $this->renderer->row($data['form']['identifier']);
150
        $string .= '</div></div>';
151
152
        return $string . '</fieldset>';
153
    }
154
155
    public function form_widget_simple(FormView $view, array $data)
156
    {
157
        $type = isset($data['type']) ? $data['type'] : 'text';
158
        if (   $type == 'text'
159
            || $type == 'password'
160
            || $type == 'email')
161
        {
162
            $view->vars['attr']['class'] = 'shorttext';
163
        }
164
165
        $string = '<input type="' . $type . '"';
166
        $string .= $this->renderer->block($view, 'widget_attributes');
167
        if (   !empty($data['value'])
168
            || is_numeric($data['value']))
169
        {
170
            $string .= ' value="' . $this->escape($data['value']) . '"';
171
        }
172
        return $string . ' />';
173
    }
174
175
    public function button_widget(FormView $view, array $data)
176
    {
177
        $type = isset($data['type']) ? $data['type'] : 'button';
178
        if (!$data['label'])
179
        {
180
            $data['label'] = $this->renderer->humanize($data['name']);
181
        }
182
        return '<button type="' . $type . '" ' . $this->renderer->block($view, 'button_attributes') . '>' . $data['label'] . '</button>';
183
    }
184
185
    public function hidden_widget(FormView $view, array $data)
186
    {
187
         return $this->renderer->block($view, 'form_widget_simple', array('type' => isset($data['type']) ? $data['type'] : "hidden"));
188
    }
189
190
    public function email_widget(FormView $view, array $data)
191
    {
192
         return $this->renderer->block($view, 'form_widget_simple', array('type' => isset($data['type']) ? $data['type'] : "email"));
193
    }
194
195
    public function autocomplete_widget(FormView $view, array $data)
196
    {
197
        $element_id = $view->vars['id'];
198
        $jsinit = 'window.' . $element_id . '_handler_options = ' . $data['handler_options'] . ";\n";
199
        $jsinit .= "midcom_helper_datamanager2_autocomplete.create_dm2_widget('{$element_id}_search_input', {$data['min_chars']});\n";
200
201
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
202
        $string .=  $this->renderer->widget($view['selection']);
203
        $string .= ' ' . $this->renderer->widget($view['search_input']);
204
        $string .= '</fieldset>';
205
        return $string . $this->jsinit($jsinit);
206
    }
207
208 View Code Duplication
    public function radio_widget(FormView $view, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
    {
210
        $string = '<input type="radio"';
211
        $string .= $this->renderer->block($view, 'widget_attributes');
212
        if (strlen($data['value']) > 0)
213
        {
214
            $string .= ' value="' . $data['value'] . '"';
215
        }
216
        if ($data['checked'])
217
        {
218
            $string .= 'checked="checked"';
219
        }
220
        return $string . ' />';
221
    }
222
223 View Code Duplication
    public function checkbox_widget(FormView $view, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
    {
225
        $string = '<input type="checkbox"';
226
        $string .= $this->renderer->block($view, 'widget_attributes');
227
        if (strlen($data['value']) > 0)
228
        {
229
            $string .= ' value="' . $data['value'] . '"';
230
        }
231
        if ($data['checked'])
232
        {
233
            $string .= 'checked="checked"';
234
        }
235
        return $string . ' />';
236
    }
237
238
    public function choice_widget_collapsed(FormView $view, array $data)
239
    {
240
        $string = '<select';
241
        if (   $data['required']
242
            && null === $data['empty_value']
243
            && $data['empty_value_in_choices'] === false
244
            && $data['multiple'] === false)
245
        {
246
            $data['required'] = false;
247
        }
248
        $string .= $this->renderer->block($view, 'widget_attributes', array('required' => $data['required']));
249
        if ($data['multiple'])
250
        {
251
            $string .= ' multiple="multiple"';
252
        }
253
        $string .= '>';
254
        if (null !== $data['empty_value'])
255
        {
256
            $string .= '<option value=""';
257
            if (   $data['required']
258
                && empty($data['value'])
259
                && "0" !== $data['value'])
260
            {
261
                $string .= ' selected="selected"';
262
            }
263
            $string .= '>' . $data['empty_value'] . '</option>';
264
        }
265
        if (count($data['preferred_choices']) > 0)
266
        {
267
            $string .= $this->renderer->block($view, 'choice_widget_options', array('choices' => $data['preferred_choices']));
268
            if (count($data['choices']) > 0 && null !== $data['separator'])
269
            {
270
                $string .= '<option disabled="disabled">' . $data['separator'] . '</option>';
271
            }
272
        }
273
        $string .= $this->renderer->block($view, 'choice_widget_options', array('choices' => $data['choices']));
274
        return $string . '</select>';
275
    }
276
277
    public function radiocheckselect_widget(FormView $view, array $data)
278
    {
279
        return $this->choice_widget_expanded($view, $data);
280
    }
281
282
    public function choice_widget_expanded(FormView $view, array $data)
283
    {
284
        $string = '<fieldset ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
285
        foreach ($view as $child)
286
        {
287
            $string .= $this->renderer->widget($child);
288
            $string .= $this->renderer->label($child);
289
        }
290
291
        return $string . '</fieldset>';
292
    }
293
294
    public function choice_widget_options(FormView $view, array $data)
295
    {
296
        $string = '';
297
        foreach ($data['choices'] as $index => $choice)
298
        {
299
            if (is_array($choice))
300
            {
301
                $string .= '<optgroup label="' . $index . '">';
302
                $string .= $this->renderer->block($view, 'choice_widget_options', array('choices' => $choice));
303
                $string .= '</optgroup>';
304
            }
305
            else
306
            {
307
                $string .= '<option value="' . $choice->value . '"';
308
                if ($data['is_selected']($choice->value, $data['value']))
309
                {
310
                    $string .= ' selected="selected"';
311
                }
312
                $string .= '>' . $choice->label . '</option>';
313
            }
314
        }
315
        return $string;
316
    }
317
318 View Code Duplication
    public function codemirror_widget(FormView $view, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
319
    {
320
        //we set required to false, because codemirror doesn't play well with html5 validation..
321
        $string = '<textarea ' . $this->renderer->block($view, 'widget_attributes', array('required' => false)) . '>';
322
        $string .= $data['value'] . '</textarea>';
323
        if (!empty($data['codemirror_snippet']))
324
        {
325
            $snippet = str_replace('{$id}', $data['id'], $data['codemirror_snippet']);
326
            $snippet = str_replace('{$read_only}', 'false', $snippet);
327
            $string .= $this->jsinit($snippet);
328
        }
329
        return $string;
330
    }
331
332
    public function jsdate_widget(FormView $view, array $data)
333
    {
334
        $string = '<fieldset' . $this->renderer->block($view, 'widget_container_attributes') . '>';
335
        $string .= $this->renderer->widget($view['date']);
336
337
        if (isset($view['time']))
338
        {
339
            $string .= ' '. $this->renderer->widget($view['time']);
340
        }
341
        $string .= $data['jsinit'];
342
        return $string . '</fieldset>';
343
    }
344
345
    public function images_widget(FormView $view, array $data)
346
    {
347
        $string = '<div' . $this->renderer->block($view, 'widget_container_attributes') . '>';
348
        $string .= '<table><tr><td>';
349
350
        if (!empty($data['value']))
351
        {
352
            $string .= '<a href="' . $view->vars['value']['url'] . '" target="_new"><img src="' . $view->vars['value']['url'] . '" class="preview-image">';
353
354
            if (   $data['value']['size_x']
355
                && $data['value']['size_y'])
356
            {
357
                $size = "{$data['value']['size_x']}&times;{$data['value']['size_y']}";
358
            }
359
            else
360
            {
361
                $size = $this->renderer->humanize('unknown');
362
            }
363
            $string .= "<br><span title=\"{$data['value']['guid']}\">{$size}, {$data['value']['formattedsize']}</span></a>";
364
        }
365
366
        $string .= '</td><td>';
367
368
        foreach ($view->children as $child)
369
        {
370
            $string .= $this->renderer->row($child);
371
        }
372
373
        return $string . '</td></tr></table></div>';
374
    }
375
376
    public function photo_widget(FormView $view, array $data)
377
    {
378
        $string = '<div' . $this->renderer->block($view, 'widget_container_attributes') . '>';
379
        $string .= '<table class="midcom_datamanager_table_photo"><tr><td>';
380
        $preview_url = null;
381
        foreach ($data['value'] as $identifier => $info)
382
        {
383
            $preview_url = $info['url'];
384
            if ($identifier == 'thumbnail')
385
            {
386
                break;
387
            }
388
        }
389
        if (!empty($preview_url))
390
        {
391
            $string .= '<img src="' . $preview_url . '" class="preview-image">';
392
        }
393
        $string .= '</td><td>';
394
        if (!empty($data['value']))
395
        {
396
            $string .= '<ul>';
397
            foreach ($data['value'] as $identifier => $info)
398
            {
399
                if (   $info['size_x']
400
                    && $info['size_y'])
401
                {
402
                    $size = "{$info['size_x']}x{$info['size_y']}";
403
                }
404
                else
405
                {
406
                    $size = 'unknown';
407
                }
408
                $string .= "<li title=\"{$info['guid']}\"><a href='{$info['url']}' target='_new'>{$info['filename']}:</a>
409
                {$size}, {$info['formattedsize']}</li>";
410
            }
411
            $string .= '</ul>';
412
        }
413
        $string .= $this->renderer->widget($data['form']['file']);
414
        if (array_key_exists('title', $view->children))
415
        {
416
            $string .= $this->renderer->widget($view->children['title']);
417
        }
418
        $string .= '<label class="midcom_datamanager_photo_lable">' . $this->renderer->humanize('delete photo') . ' ' . $this->renderer->widget($data['form']['delete']) . '</label>';
419
        $string .= '</td></tr></table></div>';
420
421
        return $string . $this->jsinit('init_photo_widget("' . $view->vars['id'] .'");');
422
    }
423
424
    public function subform_widget(FormView $view, array $data)
425
    {
426
        $view->vars['attr']['data-prototype'] = $this->escape($this->renderer->row($view->vars['prototype']));
427
        $view->vars['attr']['data-max-count'] = $view->vars['max_count'];
428
        $string = $this->renderer->widget($data['form'], $view->vars);
429
        return $string . $this->jsinit('init_subform("' . $view->vars['id'] . '", ' . $view->vars['sortable'] . ');');
430
    }
431
432
    public function submit_widget(FormView $view, array $data)
433
    {
434
        return $this->renderer->block($view, 'button_widget', array('type' => isset($data['type']) ? $data['type'] : 'submit'));
435
    }
436
437
    public function delete_widget(FormView $view, array $data)
438
    {
439
        return $this->renderer->block($view, 'button_widget', array('type' => isset($data['type']) ? $data['type'] : 'delete'));
440
    }
441
442
    public function textarea_widget(FormView $view, array $data)
443
    {
444
        $view->vars['attr'] = array
445
        (
446
            'class' => 'longtext',
447
            'cols' => 50
448
        );
449
        return '<textarea' . $this->renderer->block($view, 'widget_attributes') . '>' . $data['value'] . '</textarea>';
450
    }
451
452
    public function tinymce_widget(FormView $view, array $data)
453
    {
454
        //we set required to false, because tinymce doesn't play well with html5 validation..
455
        $string = '<textarea' . $this->renderer->block($view, 'widget_attributes', array('required' => false)) . '>' . $data['value'] . '</textarea>';
456
        return $string . $this->jsinit($data['tinymce_snippet']);
457
    }
458
459
    public function form_label(FormView $view, array $data)
460
    {
461
        if ($data['label'] === false)
462
        {
463
            return '';
464
        }
465
        if (!$data['label'])
466
        {
467
            $data['label'] = $data['name'];
468
        }
469
        $data['label'] = $this->renderer->humanize($data['label']);
470
471
        $label_attr = $data['label_attr'];
472
        if ($data['required'])
473
        {
474
            $label_attr['class'] = trim((isset($label_attr['class']) ? $label_attr['class'] : '') . ' required');
475
            $data['label'] .= ' <span class="field_required_start">*</span>';
476
        }
477
        if (!$data['compound'])
478
        {
479
            $label_attr['for'] = $data['id'];
480
        }
481
        return '<label' . $this->attributes($label_attr) . '><span class="field_text">' . $data['label'] . '</span></label>';
482
    }
483
}
484