Passed
Push — master ( 2874e4...bdfe16 )
by Andreas
11:31
created

midcom_helper_toolbar_view   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 64.1%

Importance

Changes 0
Metric Value
eloc 69
c 0
b 0
f 0
dl 0
loc 113
ccs 50
cts 78
cp 0.641
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
B get_approval_controls() 0 37 6
B bind_to() 0 52 8
1
<?php
2
/**
3
 * @package midcom.helper
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * This class is a view toolbar class.
11
 *
12
 * @package midcom.helper
13
 */
14
class midcom_helper_toolbar_view extends midcom_helper_toolbar
15
{
16
    /**
17
     * @param string $class_style The class style tag for the UL.
18
     * @param string $id_style The id style tag for the UL.
19
     */
20 350
    public function __construct(string $class_style = null, string $id_style = null)
21
    {
22 350
        $config = midcom::get()->config;
23 350
        $class_style = $class_style ?: $config->get('toolbars_view_style_class');
24 350
        $id_style = $id_style ?: $config->get('toolbars_view_style_id');
25 350
        parent::__construct($class_style, $id_style);
0 ignored issues
show
Bug introduced by
It seems like $class_style can also be of type null; however, parameter $class_style of midcom_helper_toolbar::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        parent::__construct(/** @scrutinizer ignore-type */ $class_style, $id_style);
Loading history...
26 350
        $this->label = midcom::get()->i18n->get_string('page', 'midcom');
27
    }
28
29
    /**
30
     * Binds the a toolbar to a DBA object. This will append a number of globally available
31
     * toolbar options. For example, expect Metadata- and Version Control-related options
32
     * to be added.
33
     *
34
     * Repeated bind calls are intercepted, you can only bind a toolbar to a single object.
35
     */
36 52
    public function bind_to(midcom_core_dbaobject $object)
37
    {
38 52
        if (!midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX)) {
39
            debug_add("Toolbar for object {$object->guid} was called before topic prefix was available, skipping global items.", MIDCOM_LOG_WARN);
40
            return;
41
        }
42 52
        if (array_key_exists('midcom_services_toolbars_bound_to_object', $this->customdata)) {
43
            // We already processed this toolbar, skipping further adds.
44 1
            return;
45
        }
46 52
        $this->customdata['midcom_services_toolbars_bound_to_object'] = true;
47
48 52
        $reflector = new midcom_helper_reflector($object);
0 ignored issues
show
Bug introduced by
$object of type midcom_core_dbaobject is incompatible with the type midgard\portable\api\mgdobject|string expected by parameter $src of midcom_helper_reflector::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $reflector = new midcom_helper_reflector(/** @scrutinizer ignore-type */ $object);
Loading history...
49 52
        $this->set_label($reflector->get_class_label());
50
51 52
        $buttons = $this->get_approval_controls($object);
52
53 52
        if ($object->can_do('midgard:update')) {
54 49
            $workflow = new midcom\workflow\datamanager;
55 49
            $buttons[] = $workflow->get_button("__ais/folder/metadata/{$object->guid}/", [
56 49
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit metadata', 'midcom.admin.folder'),
57 49
                MIDCOM_TOOLBAR_GLYPHICON => 'database',
58 49
                MIDCOM_TOOLBAR_ACCESSKEY => 'm',
59 49
            ]);
60 49
            $viewer = new midcom\workflow\viewer;
61 49
            $buttons = array_merge($buttons, [
62 49
                $viewer->get_button("__ais/folder/move/{$object->guid}/", [
63 49
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('move', 'midcom.admin.folder'),
64 49
                    MIDCOM_TOOLBAR_GLYPHICON => 'arrows',
65 49
                    MIDCOM_TOOLBAR_ENABLED => $object instanceof midcom_db_article
66 49
                ]),
67 49
                [
68 49
                    MIDCOM_TOOLBAR_URL => midcom_connection::get_url('self') . "__mfa/asgard/object/open/{$object->guid}/",
69 49
                    MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('manage object', 'midgard.admin.asgard'),
70 49
                    MIDCOM_TOOLBAR_GLYPHICON => 'cog',
71 49
                    MIDCOM_TOOLBAR_ENABLED =>    midcom::get()->auth->can_user_do('midgard.admin.asgard:access', class: 'midgard_admin_asgard_plugin')
72 49
                                              && midcom::get()->auth->can_user_do('midgard.admin.asgard:manage_objects', class: 'midgard_admin_asgard_plugin'),
73 49
                ]
74 49
            ]);
75
        }
76
77 52
        if (   midcom::get()->config->get('midcom_services_rcs_enable')
78 52
            && $object->can_do('midgard:update')
79 52
            && $object->_use_rcs) {
80 48
            $buttons[] = [
81 48
                MIDCOM_TOOLBAR_URL => "__ais/rcs/{$object->guid}/",
82 48
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('show history', 'midcom.admin.rcs'),
83 48
                MIDCOM_TOOLBAR_GLYPHICON => 'history',
84 48
                MIDCOM_TOOLBAR_ACCESSKEY => 'v',
85 48
            ];
86
        }
87 52
        $this->add_items($buttons);
88
    }
89
90 139
    public function get_approval_controls(midcom_core_dbaobject $object, bool $add_accesskey = false) : array
91
    {
92 139
        $buttons = [];
93 139
        if (midcom::get()->config->get('metadata_approval')) {
94
            if ($object->metadata->is_approved()) {
95
                $action = 'unapprove';
96
                $helptext = 'approved';
97
                $icontype = 'approved';
98
                $accesskey = 'u';
99
            } else {
100
                $action = 'approve';
101
                $helptext = 'unapproved';
102
                $icontype = 'notapproved';
103
                $accesskey = 'a';
104
            }
105
106
            $icon = 'stock-icons/16x16/page-' . $icontype . '.png';
107
            if (   !midcom::get()->config->get('show_hidden_objects')
108
                && !$object->metadata->is_visible()) {
109
                // Take scheduling into account
110
                $icon = 'stock-icons/16x16/page-' . $icontype . '-notpublished.png';
111
            }
112
            $buttons[] = [
113
                MIDCOM_TOOLBAR_URL => "__ais/folder/" . $action . "/",
114
                MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string($action, 'midcom'),
115
                MIDCOM_TOOLBAR_HELPTEXT => midcom::get()->i18n->get_string($helptext, 'midcom'),
116
                MIDCOM_TOOLBAR_ICON => $icon,
117
                MIDCOM_TOOLBAR_POST => true,
118
                MIDCOM_TOOLBAR_POST_HIDDENARGS => [
119
                    'guid' => $object->guid,
120
                    'return_to' => $_SERVER['REQUEST_URI'],
121
                ],
122
                MIDCOM_TOOLBAR_ACCESSKEY => ($add_accesskey) ? $accesskey : null,
123
                MIDCOM_TOOLBAR_ENABLED => $object->can_do('midcom:approve'),
124
            ];
125
        }
126 139
        return $buttons;
127
    }
128
}
129