Passed
Push — master ( 6e87e0...ff8c9b )
by Andreas
28:36 queued 02:39
created

midgard_admin_asgard_schemadb::_add_copy_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

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