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

base::attributes()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 2
dl 0
loc 16
ccs 10
cts 11
cp 0.9091
crap 5.0187
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\template;
7
8
use Symfony\Component\Form\FormView;
9
use midcom\datamanager\renderer;
10
use midcom;
11
12
/**
13
 * Experimental template class
14
 */
15
abstract class base
16
{
17
    /**
18
     *
19
     * @var \midcom\datamanager\renderer
20
     */
21
    protected $renderer;
22
23 140
    public function __construct(renderer $renderer)
24
    {
25 140
        $this->renderer = $renderer;
26 140
    }
27
28 111
    public function form(FormView $view, array $data)
29
    {
30 111
        $string = $this->renderer->start($view, $data);
31 111
        $string .= $this->renderer->widget($view, $data);
32 111
        return $string . $this->renderer->end($view, $data);
33
    }
34
35 124
    public function form_widget(FormView $view, array $data)
36
    {
37 124
        if ($data['compound']) {
38 118
            return $this->renderer->block($view, 'form_widget_compound');
39
        }
40 102
        return $this->renderer->block($view, 'form_widget_simple');
41
    }
42
43 118
    public function form_widget_compound(FormView $view, array $data)
44
    {
45 118
        $string = '<div ' . $this->renderer->block($view, 'widget_container_attributes') . '>';
46 118
        if (!$view->parent && $data['errors']) {
47 111
            $string .= $this->renderer->errors($view);
48
        }
49 118
        $string .= $this->renderer->block($view, 'form_rows');
50 118
        $string .= $this->renderer->rest($view);
51 118
        return $string . '</div>';
52
    }
53
54 101
    public function form_rest(FormView $view, array $data)
55
    {
56 101
        $string = '';
57 101
        foreach ($view as $child) {
58 101
            if (!$child->isRendered()) {
59 101
                $string .= $this->renderer->row($child);
60
            }
61
        }
62 101
        return $string;
63
    }
64
65 103
    public function widget_attributes(FormView $view, array $data)
66
    {
67 103
        $attributes = $data['attr'];
68 103
        $attributes['id'] = $data['id'];
69 103
        $attributes['name'] = $data['full_name'];
70 103
        if (!empty($data['readonly'])) {
71 9
            $attributes['readonly'] = 'readonly';
72
        }
73 103
        if ($data['disabled']) {
74
            $attributes['disabled'] = 'disabled';
75
        }
76 103
        if ($data['required']) {
77 67
            $attributes['required'] = 'required';
78
        }
79 103
        return $this->attributes($attributes, true);
80
    }
81
82 118
    public function widget_container_attributes(FormView $view, array $data)
83
    {
84 118
        $attr = $data['attr'];
85 118
        if (!empty($data['id'])) {
86 118
            $attr['id'] = $data['id'];
87
        }
88 118
        if (!$view->parent) {
89 111
            unset($attr['id']);
90 111
            $attr['class'] = 'form';
91
        }
92
93 118
        return $this->attributes($attr);
94
    }
95
96 76
    public function choice_widget(FormView $view, array $data)
97
    {
98 76
        if ($data['expanded']) {
99 25
            return $this->renderer->block($view, 'choice_widget_expanded');
100
        }
101 66
        return $this->renderer->block($view, 'choice_widget_collapsed');
102
    }
103
104 101
    public function button_attributes(FormView $view, array $data)
105
    {
106 101
        $attributes = $data['attr'];
107 101
        $attributes['id'] = $data['id'];
108 101
        $attributes['name'] = $data['full_name'];
109 101
        if ($data['disabled']) {
110
            $attributes['disabled'] = 'disabled';
111
        }
112
113 101
        return $this->attributes($attributes);
114
    }
115
116 109
    public function escape($input)
117
    {
118 109
        return htmlentities($input, ENT_COMPAT, 'utf-8');
119
    }
120
121 119
    public function attributes(array $attributes, $autoescape = false)
122
    {
123 119
        $rendered = [];
124 119
        foreach ($attributes as $name => $value) {
125 119
            if ($value === false) {
126
                continue;
127
            }
128 119
            if ($value === true) {
129 98
                $value = $name;
130
            }
131 119
            if ($autoescape) {
132 103
                $value = $this->escape($value);
133
            }
134 119
            $rendered[] = sprintf('%s="%s"', $name, $value);
135
        }
136 119
        return implode(' ', $rendered);
137
    }
138
139 71
    public function jsinit($code)
140
    {
141 71
        return "<script>\$(document).ready(function() { $code });</script>";
142
    }
143
144 57
    protected function add_head_elements_for_autocomplete(bool $sortable, bool $creation_mode_enabled)
145
    {
146 57
        $head = midcom::get()->head;
147 57
        $head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css");
148 57
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.datamanager/autocomplete.css');
149
150 57
        $components = ['menu', 'autocomplete'];
151 57
        if ($sortable) {
152
            $components[] = 'mouse';
153
            $components[] = 'sortable';
154
        }
155 57
        if ($creation_mode_enabled) {
156 23
            $components = array_merge($components, ['mouse', 'draggable', 'resizable', 'button', 'dialog']);
157
        }
158 57
        $head->enable_jquery_ui($components);
159 57
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/autocomplete.js');
160 57
    }
161
162 6
    protected function add_head_elements_for_codemirror(array $modes)
163
    {
164 6
        $prefix = MIDCOM_STATIC_URL . '/midcom.datamanager/codemirror-5.46.0/';
165 6
        midcom::get()->head->enable_jquery();
166 6
        midcom::get()->head->add_stylesheet($prefix . 'lib/codemirror.css');
167 6
        midcom::get()->head->add_stylesheet($prefix . 'theme/eclipse.css');
168 6
        midcom::get()->head->add_jsfile($prefix . 'lib/codemirror.js');
169 6
        foreach ($modes as $mode) {
170 6
            midcom::get()->head->add_jsfile($prefix . 'mode/' . $mode . '/' . $mode . '.js');
171
        }
172 6
        midcom::get()->head->add_jsfile($prefix . 'addon/edit/matchbrackets.js');
173 6
        midcom::get()->head->add_jsfile($prefix . 'addon/dialog/dialog.js');
174 6
        midcom::get()->head->add_stylesheet($prefix . 'addon/dialog/dialog.css');
175 6
        midcom::get()->head->add_jsfile($prefix . 'addon/search/searchcursor.js');
176 6
        midcom::get()->head->add_jsfile($prefix . 'addon/search/match-highlighter.js');
177 6
        midcom::get()->head->add_jsfile($prefix . 'addon/search/search.js');
178 6
    }
179
}
180