Passed
Push — master ( 6feecf...d85f56 )
by Andreas
11:09
created

add_longtext_field()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.3906

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 1
dl 0
loc 13
ccs 6
cts 8
cp 0.75
crap 5.3906
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midgard.admin.asgard
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
use midcom\datamanager\schemabuilder;
10
11
/**
12
 * Helper class to create a DM schema from an object via reflection
13
 *
14
 * @package midgard.admin.asgard
15
 */
16
class midgard_admin_asgard_schemadb extends schemabuilder
17
{
18
    private midcom_helper_configuration $_config;
19
20
    private midcom_services_i18n_l10n $l10n;
21
22
    public bool $add_copy_fields = false;
23
24 31
    public function __construct(midcom_core_dbaobject $object, midcom_helper_configuration $config)
25
    {
26 31
        parent::__construct($object);
27 31
        $this->_config = $config;
28 31
        $this->l10n = midcom::get()->i18n->get_l10n('midgard.admin.asgard');
29
    }
30
31
    /**
32
     * Generates, loads and prepares the schema database.
33
     *
34
     * The operations are done on all available schemas within the DB.
35
     */
36 9
    protected function process_type(string $type, array $type_fields)
37
    {
38 9
        usort($type_fields, [$this, 'sort_schema_fields']);
39
40 9
        parent::process_type($type, $type_fields);
41
42 9
        $this->_add_rcs_field();
43
44 9
        if ($this->add_copy_fields) {
45 2
            $this->_add_copy_fields();
46
        }
47 9
        if (empty($this->schema['l10n_db'])) {
48
            $this->schema['l10n_db'] = 'midgard.admin.asgard';
49
        }
50
    }
51
52 7
    protected function add_string_field(string $key, string $type)
53
    {
54 7
        if (   $key == 'component'
55 7
            && $type == midcom_db_topic::class) {
56 5
            $this->_add_component_dropdown($key);
57 5
            return;
58
        }
59
60
        // Special name handling, start by checking if given type is same as $this->object and if not making a dummy copy (we're probably in creation mode then)
61 7
        if ($this->object instanceof $type) {
62 7
            $name_obj = $this->object;
63
        } else {
64
            $name_obj = new $type();
65
        }
66
67 7
        if ($key === midcom_helper_reflector::get_name_property($name_obj)) {
68 7
            $this->_add_name_field($key, $name_obj);
69 7
            return;
70
        }
71 7
        parent::add_string_field($key, $type);
72
    }
73
74 7
    private function _add_name_field(string $key, midcom_core_dbaobject $name_obj)
75
    {
76 7
        $type_urlname_config = [];
77 7
        $allow_unclean_name_types = $this->_config->get_array('allow_unclean_names_for');
78 7
        foreach ($allow_unclean_name_types as $allow_unclean_name_types_type) {
79 7
            if ($name_obj->__object instanceof $allow_unclean_name_types_type) {
80
                $type_urlname_config['allow_unclean'] = true;
81
                break;
82
            }
83
        }
84
85
        // Enable generating the name from the title property
86 7
        if ($title_field = midcom_helper_reflector::get_title_property($name_obj)) {
87 7
            $type_urlname_config['title_field'] = $title_field;
88
        }
89
90 7
        $this->schema['fields'][$key] = [
91 7
            'title'       => $key,
92 7
            'storage'     => $key,
93 7
            'type'        => 'urlname',
94 7
            'type_config' => $type_urlname_config,
95 7
            'widget'      => 'text',
96 7
        ];
97
    }
98
99 5
    private function _add_component_dropdown(string $key)
100
    {
101 5
        $components = ['' => ''];
102 5
        foreach (midcom::get()->componentloader->get_manifests() as $manifest) {
103
            // Skip purecode components
104 5
            if (!$manifest->purecode) {
105 5
                $components[$manifest->name] = $manifest->get_name_translated() . " ({$manifest->name})";
106
            }
107
        }
108 5
        asort($components);
109
110 5
        $this->schema['fields'][$key] = [
111 5
            'title'       => $key,
112 5
            'storage'     => $key,
113 5
            'type'        => 'select',
114 5
            'type_config' => [
115 5
                'options' => $components,
116 5
            ],
117 5
            'widget'      => 'select',
118 5
        ];
119
    }
120
121 7
    protected function add_longtext_field(string $key)
122
    {
123 7
        parent::add_longtext_field($key);
124
125
        // Check the user preference and configuration
126 7
        if (   in_array($key, ['content', 'description'])
127 7
            && midgard_admin_asgard_plugin::get_preference('tinymce_enabled')) {
128
            $this->schema['fields'][$key]['widget'] = 'tinymce';
129
        }
130
131 7
        if (   in_array($key, ['value', 'code'])
132 7
            && midgard_admin_asgard_plugin::get_preference('codemirror_enabled')) {
133
            $this->schema['fields'][$key]['widget'] = 'codemirror';
134
        }
135
    }
136
137 8
    protected function add_linked_field(string $key)
138
    {
139 8
        parent::add_linked_field($key);
140
141 8
        $linked_type = $this->reflector->get_link_name($key);
142 8
        $type_label = midcom_helper_reflector::get($linked_type)->get_class_label();
143
144 8
        if ($key == 'up') {
145 8
            $field_label = sprintf($this->l10n->get('under %s'), $type_label);
146
        } else {
147 2
            $field_label = sprintf($this->l10n->get('%s (%s)'), $this->schema['fields'][$key]['title'], $type_label);
148
        }
149 8
        $this->schema['fields'][$key]['title'] = $field_label;
150
151 8
        $this->schema['fields'][$key]['widget_config']['creation_mode_enabled'] = true;
152 8
        $this->schema['fields'][$key]['widget_config']['creation_handler'] = midcom_connection::get_url('self') . "__mfa/asgard/object/create/chooser/{$linked_type}/";
153 8
        $this->schema['fields'][$key]['widget_config']['creation_default_key'] = midcom_helper_reflector::get_title_property(new $linked_type);
154
    }
155
156 9
    private function _add_rcs_field()
157
    {
158 9
        $this->schema['fields']['_rcs_message'] = [
159 9
            'title'       => $this->l10n->get('revision comment'),
160 9
            'storage'     => null,
161 9
            'type'        => 'rcsmessage',
162 9
            'widget'      => 'text',
163 9
            'start_fieldset' => [
164 9
                'title' => $this->l10n->get('revision'),
165 9
                'css_group' => 'rcs',
166 9
            ],
167 9
            'end_fieldset' => '',
168 9
        ];
169
    }
170 2
    private function _add_copy_fields()
171
    {
172
        // Add switch for copying parameters
173 2
        $this->schema['fields']['parameters'] = [
174 2
            'title'       => $this->l10n->get('copy parameters'),
175 2
            'storage'     => null,
176 2
            'type'        => 'boolean',
177 2
            'widget'      => 'checkbox',
178 2
            'default'     => true,
179 2
        ];
180
181
        // Add switch for copying metadata
182 2
        $this->schema['fields']['metadata'] = [
183 2
            'title'       => $this->l10n->get('copy metadata'),
184 2
            'storage'     => null,
185 2
            'type'        => 'boolean',
186 2
            'widget'      => 'checkbox',
187 2
            'default'     => true,
188 2
        ];
189
190
        // Add switch for copying attachments
191 2
        $this->schema['fields']['attachments'] = [
192 2
            'title'       => $this->l10n->get('copy attachments'),
193 2
            'storage'     => null,
194 2
            'type'        => 'boolean',
195 2
            'widget'      => 'checkbox',
196 2
            'default'     => true,
197 2
        ];
198
199
        // Add switch for copying privileges
200 2
        $this->schema['fields']['privileges'] = [
201 2
            'title'       => $this->l10n->get('copy privileges'),
202 2
            'storage'     => null,
203 2
            'type'        => 'boolean',
204 2
            'widget'      => 'checkbox',
205 2
            'default'     => true,
206 2
        ];
207
    }
208
209 29
    private function _get_score(string $field) : int
210
    {
211 29
        $preferred_fields = $this->_config->get_array('object_preferred_fields');
212 29
        $timerange_fields = $this->_config->get_array('object_timerange_fields');
213 29
        $phone_fields = $this->_config->get_array('object_phone_fields');
214 29
        $address_fields = $this->_config->get_array('object_address_fields');
215 29
        $location_fields = $this->_config->get_array('object_location_fields');
216
217 29
        $score = 7;
218
219 29
        if ($this->reflector->get_midgard_type($field) == MGD_TYPE_LONGTEXT) {
220 14
            $score = 1;
221 28
        } elseif (in_array($field, $preferred_fields)) {
222 14
            $score = 0;
223 25
        } elseif ($this->reflector->is_link($field)) {
224 11
            $score = 2;
225 23
        } elseif (in_array($field, $timerange_fields)) {
226 5
            $score = 3;
227 19
        } elseif (in_array($field, $phone_fields)) {
228 5
            $score = 4;
229 16
        } elseif (in_array($field, $address_fields)) {
230 5
            $score = 5;
231 12
        } elseif (in_array($field, $location_fields)) {
232
            $score = 6;
233
        }
234
235 29
        return $score;
236
    }
237
238 29
    public function sort_schema_fields(string $first, string $second) : int
239
    {
240 29
        $score1 = $this->_get_score($first);
241 29
        $score2 = $this->_get_score($second);
242 29
        if ($score1 < $score2) {
243 13
            return -1;
244
        }
245 23
        if ($score1 > $score2) {
246 17
            return 1;
247
        }
248 13
        if (   $score1 < 3
249 13
            || $score1 > 6) {
250 10
            return strnatcmp($first, $second);
251
        }
252
        switch ($score1) {
253 3
            case 3:
254 1
                $type = 'timerange';
255 1
                break;
256 2
            case 4:
257 1
                $type = 'phone';
258 1
                break;
259 1
            case 5:
260 1
                $type = 'address';
261 1
                break;
262
            case 6:
263
                $type = 'location';
264
                break;
265
        }
266 3
        $fields = $this->_config->get_array('object_' . $type . '_fields');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.
Loading history...
267 3
        return array_search($first, $fields) <=> array_search($second, $fields);
268
    }
269
}
270