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

midgard_admin_asgard_toolbar::bind_to_object()   F

Complexity

Conditions 33
Paths 864

Size

Total Lines 168
Code Lines 94

Duplication

Lines 29
Ratio 17.26 %

Importance

Changes 0
Metric Value
cc 33
eloc 94
nc 864
nop 3
dl 29
loc 168
rs 2.1014
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
/**
10
 * Wrapper for Asgard's toolbar
11
 *
12
 * @package midgard.admin.asgard
13
 */
14
class midgard_admin_asgard_toolbar extends midcom_helper_toolbar_view
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
{
16
    private function _generate_url($action, $object)
17
    {
18
        return '__mfa/asgard/object/' . $action . '/' . $object->guid . '/';
19
    }
20
21
    /**
22
     * Populate the object toolbar
23
     *
24
     * @param mixed $object        MgdSchema object for which the toolbar will be created
25
     * @param String $handler_id   Initialized handler id
26
     * @param array $data          Local request data
27
     */
28
    public function bind_to_object($object, $handler_id, $data)
29
    {
30
        $buttons = array();
31
        // Show view toolbar button, if the user hasn't configured to use straight the edit mode
32
        if ($data['default_mode'] === 'view')
33
        {
34
            $buttons[] = array
35
            (
36
                MIDCOM_TOOLBAR_URL => $this->_generate_url('view', $object),
37
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('view', 'midcom'),
38
                MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/view.png',
39
                MIDCOM_TOOLBAR_ACCESSKEY => 'v',
40
            );
41
        }
42
43
        if (   !is_a($object, 'midcom_db_style')
44
            && !is_a($object, 'midcom_db_element')
45
            && !is_a($object, 'midcom_db_snippetdir')
46
            && !is_a($object, 'midcom_db_snippet')
47
            && !is_a($object, 'midcom_db_page')
48
            && !is_a($object, 'midcom_db_pageelement')
49
            && !is_a($object, 'midcom_db_parameter')
50
            && substr($object->__mgdschema_class_name__, 0, 23) != 'org_routamc_positioning'
51
            && substr($object->__mgdschema_class_name__, 0, 14) != 'net_nemein_tag')
52
        {
53
            $link = midcom::get()->permalinks->resolve_permalink($object->guid);
54 View Code Duplication
            if ($link)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
            {
56
                $buttons[] = array
57
                (
58
                    MIDCOM_TOOLBAR_URL => $link,
59
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('view on site', 'midgard.admin.asgard'),
60
                    MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_internet.png',
61
                );
62
            }
63
        }
64
65 View Code Duplication
        if ($object->can_do('midgard:update'))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            $buttons[] = array
68
            (
69
                MIDCOM_TOOLBAR_URL => $this->_generate_url('edit', $object),
70
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit', 'midcom'),
71
                MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/edit.png',
72
                MIDCOM_TOOLBAR_ACCESSKEY => 'e',
73
            );
74
        }
75
76
        if ($object->can_do('midgard:create'))
77
        {
78
            $url = (midcom_helper_reflector_tree::get_child_objects($object)) ? 'copy/tree' : 'copy';
79
            $buttons[] = array
80
            (
81
                MIDCOM_TOOLBAR_URL => $this->_generate_url($url, $object),
82
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('copy', 'midcom'),
83
                MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/editcopy.png',
84
            );
85
        }
86
87
        if ($object->can_do('midgard:update'))
88
        {
89
            $buttons = array_merge($buttons, $this->get_toolbar_update_items($object));
90
        }
91
92
        if ($object->can_do('midgard:create'))
93
        {
94
            // Find out what types of children the object can have and show create buttons for them
95
            $child_types = $data['tree_reflector']->get_child_classes();
96
            if (!is_array($child_types))
97
            {
98
                debug_add("\$data['tree_reflector']->get_child_classes() failed critically, recasting \$child_types as array", MIDCOM_LOG_WARN);
99
                $child_types = array();
100
            }
101
            foreach ($child_types as $type)
102
            {
103
                $display_button = true;
104
                if (is_a($object, 'midcom_db_topic'))
105
                {
106
                    // With topics we should check for component before populating create buttons as so many types can be children of topics
107
                    switch ($type)
108
                    {
109
                        case 'midgard_topic':
110
                        case 'midgard_article':
111
                            // Articles and topics can always be created
112
                            break;
113
                        default:
114
                            $midcom_dba_classname = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($type);
115
                            if (!$midcom_dba_classname)
116
                            {
117
                                $display_button = false;
118
                                break;
119
                            }
120
                            $component = midcom::get()->dbclassloader->get_component_for_class($type);
121
                            if ($component != $object->component)
122
                            {
123
                                $display_button = false;
124
                            }
125
                            break;
126
                    }
127
                }
128
                else if (is_a($object, 'midcom_db_article'))
129
                {
130
                    try
131
                    {
132
                        $topic = new midcom_db_topic($object->topic);
133
                        // With articles we should check for topic component before populating create buttons as so many types can be children of topics
134
                        switch ($type)
135
                        {
136
                            case 'midgard_article':
137
                                // Articles can always be created
138
                                break;
139
                            default:
140
                                $component = midcom::get()->dbclassloader->get_component_for_class($type);
141
                                if ($component != $topic->component)
142
                                {
143
                                    $display_button = false;
144
                                }
145
                                break;
146
                        }
147
                    }
148
                    catch (midcom_error $e)
149
                    {
150
                        $e->log();
151
                    }
152
                }
153
154
                if (!$display_button)
155
                {
156
                    // Skip this type
157
                    continue;
158
                }
159
160
                $buttons[] = array
161
                (
162
                    MIDCOM_TOOLBAR_URL => $this->_generate_url('create/' . $type, $object),
163
                    MIDCOM_TOOLBAR_LABEL => sprintf(midcom::get()->i18n->get_string('create %s', 'midcom'), midgard_admin_asgard_plugin::get_type_label($type)),
164
                    MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/' . $data['tree_reflector']->get_create_icon($type),
165
                );
166
            }
167
        }
168
169 View Code Duplication
        if ($object->can_do('midgard:delete'))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
170
        {
171
            $buttons[] = array
172
            (
173
                MIDCOM_TOOLBAR_URL => $this->_generate_url('delete', $object),
174
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('delete', 'midcom'),
175
                MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash.png',
176
                MIDCOM_TOOLBAR_ACCESSKEY => 'd',
177
            );
178
        }
179
180
        if (   midcom::get()->config->get('midcom_services_rcs_enable')
181
            && $object->can_do('midgard:update')
182
            && $object->_use_rcs)
183
        {
184
            $buttons[] = array
185
            (
186
                MIDCOM_TOOLBAR_URL => $this->_generate_url('rcs', $object),
187
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('show history', 'midgard.admin.asgard'),
188
                MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/history.png',
189
                MIDCOM_TOOLBAR_ENABLED => (substr($handler_id, 0, 25) !== '____mfa-asgard-object_rcs'),
190
                MIDCOM_TOOLBAR_ACCESSKEY => 'h',
191
            );
192
        }
193
        $this->add_items($buttons);
194
        $this->_disable_active_item($handler_id, $object, $data);
195
    }
196
197
    private function _disable_active_item($handler_id, $object, array $data)
198
    {
199
        switch ($handler_id)
200
        {
201
            case '____mfa-asgard-object_view':
202
                $this->disable_item($this->_generate_url('view', $object));
203
                break;
204
            case '____mfa-asgard-object_edit':
205
                $this->disable_item($this->_generate_url('edit', $object));
206
                break;
207
            case '____mfa-asgard-object_copy':
208
                $this->disable_item($this->_generate_url('copy', $object));
209
                break;
210
            case '____mfa-asgard-object_copy_tree':
211
                $this->disable_item($this->_generate_url('copy/tree', $object));
212
                break;
213
            case '____mfa-asgard-components_configuration_edit_folder':
214
                $this->disable_item("__mfa/asgard/components/configuration/edit/{$object->component}/{$object->guid}/");
215
                break;
216
            case '____mfa-asgard-object_metadata':
217
                $this->disable_item($this->_generate_url('metadata', $object));
218
                break;
219
            case '____mfa-asgard-object_attachments':
220
            case '____mfa-asgard-object_attachments_edit':
221
                $this->disable_item($this->_generate_url('attachments', $object));
222
                break;
223
            case '____mfa-asgard-object_parameters':
224
                $this->disable_item($this->_generate_url('parameters', $object));
225
                break;
226
            case '____mfa-asgard-object_permissions':
227
                $this->disable_item($this->_generate_url('permissions', $object));
228
                break;
229
            case '____mfa-asgard-object_create':
230
                $this->disable_item($this->_generate_url('create/' . $data['current_type'], $object));
231
                break;
232
            case '____mfa-asgard-object_delete':
233
                $this->disable_item($this->_generate_url('delete', $object));
234
                break;
235
            case '____mfa-asgard_midcom.helper.replicator-object':
236
                $this->disable_item("__mfa/asgard_midcom.helper.replicator/object/{$object->guid}/");
237
                break;
238
        }
239
    }
240
241
    private function get_toolbar_update_items($object)
242
    {
243
        $buttons = array();
244
        if (   is_a($object, 'midcom_db_topic')
245
            && $object->component
246
            && $object->can_do('midcom:component_config'))
247
        {
248
            $buttons[] = array
249
            (
250
                MIDCOM_TOOLBAR_URL => "__mfa/asgard/components/configuration/edit/{$object->component}/{$object->guid}/",
251
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('component configuration', 'midcom'),
252
                MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_folder-properties.png',
253
            );
254
        }
255
256
        $buttons[] = array
257
        (
258
            MIDCOM_TOOLBAR_URL => $this->_generate_url('metadata', $object),
259
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('metadata', 'midcom'),
260
            MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/metadata.png',
261
            MIDCOM_TOOLBAR_ACCESSKEY => 'm',
262
        );
263
        $buttons = array_merge($buttons, $this->get_approval_controls($object));
264
265
        $buttons[] = array
266
        (
267
            MIDCOM_TOOLBAR_URL => $this->_generate_url('attachments', $object),
268
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('attachments', 'midgard.admin.asgard'),
269
            MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/attach.png',
270
        );
271
272
        $buttons[] = array
273
        (
274
            MIDCOM_TOOLBAR_URL => $this->_generate_url('parameters', $object),
275
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('parameters', 'midcom'),
276
            MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/properties.png',
277
            MIDCOM_TOOLBAR_ENABLED => $object->can_do('midgard:parameters'),
278
        );
279
280
        $buttons[] = array
281
        (
282
            MIDCOM_TOOLBAR_URL => $this->_generate_url('permissions', $object),
283
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('privileges', 'midcom'),
284
            MIDCOM_TOOLBAR_ICON => 'midgard.admin.asgard/permissions-16.png',
285
            MIDCOM_TOOLBAR_ENABLED => $object->can_do('midgard:privileges'),
286
        );
287
288 View Code Duplication
        if (   midcom::get()->componentloader->is_installed('midcom.helper.replicator')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
289
            && midcom::get()->auth->admin)
290
        {
291
            $buttons[] = array
292
            (
293
                MIDCOM_TOOLBAR_URL => "__mfa/asgard_midcom.helper.replicator/object/{$object->guid}/",
294
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('replication information', 'midcom.helper.replicator'),
295
                MIDCOM_TOOLBAR_ICON => 'midcom.helper.replicator/replicate-server-16.png',
296
                MIDCOM_TOOLBAR_ACCESSKEY => 'r',
297
            );
298
        }
299
        return $buttons;
300
    }
301
}
302