Passed
Push — master ( daf040...4ffbc4 )
by Andreas
21:32
created

midgard_admin_asgard_handler_welcome   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 65.76%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 110
c 1
b 0
f 0
dl 0
loc 197
ccs 73
cts 111
cp 0.6576
rs 9.92
wmc 31

7 Methods

Rating   Name   Duplication   Size   Complexity  
B _list_revised() 0 29 7
A _handler_welcome() 0 41 5
A _mass_delete() 0 11 4
A _populate_toolbar() 0 41 2
A _mass_approve() 0 13 5
A _show_welcome() 0 4 2
B _prepare_tabledata() 0 32 6
1
<?php
2
/**
3
 * @package midgard.admin.asgard
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
use midcom\grid\provider;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * Welcome interface
14
 *
15
 * @package midgard.admin.asgard
16
 */
17
class midgard_admin_asgard_handler_welcome extends midcom_baseclasses_components_handler
18
{
19
    use midgard_admin_asgard_handler;
20
21 1
    private function _list_revised(string $since, ?string $type = null, bool $only_mine = false) : array
22
    {
23 1
        $types = ($type !== null) ? [$type] : $this->_request_data['schema_types'];
24 1
        $revised = [];
25
26
        // List installed MgdSchema types and convert to DBA classes
27 1
        foreach ($types as $schema_type) {
28 1
            $midcom_dba_classname = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object(new $schema_type);
29 1
            if (!empty($midcom_dba_classname)) {
30
                // List all revised objects
31 1
                $qb = new midcom_core_querybuilder($midcom_dba_classname);
32 1
                $qb->add_constraint('metadata.revised', '>=', $since);
33
34 1
                if (   $only_mine
35 1
                    && midcom::get()->auth->user) {
36
                    $qb->add_constraint('metadata.authors', 'LIKE', '|' . midcom::get()->auth->user->guid . '|');
37
                }
38
39 1
                foreach ($qb->execute() as $object) {
40 1
                    $revised["{$object->metadata->revised}_{$object->guid}_{$object->metadata->revision}"] = [
41 1
                        'object' => $object,
42 1
                        'revisor' => midcom::get()->auth->get_user($object->metadata->revisor)
43
                    ];
44
                }
45
            }
46
        }
47
48 1
        krsort($revised);
49 1
        return $revised;
50
    }
51
52
    /**
53
     * Object editing view
54
     */
55 1
    public function _handler_welcome(Request $request, array &$data)
56
    {
57 1
        $data['schema_types'] = array_diff(midcom_connection::get_schema_types(), $this->_config->get_array('skip_in_filter'));
58
59 1
        $data['view_title'] = $this->_l10n->get('asgard');
60
61 1
        if ($request->request->has('action') && $request->request->has('entries')) {
62
            $method_name = '_mass_' . $request->request->get('action');
63
            $this->$method_name($request->request->get('entries'));
64
        }
65
66 1
        if ($request->query->has('revised_after')) {
67
            $data['revised_after'] = date('Y-m-d', $request->query->get('revised_after'));
68
69
            $data['type_filter'] = null;
70
            if ($request->query->get('type_filter', 'any') != 'any') {
71
                $data['type_filter'] = $request->query->get('type_filter');
72
            }
73
74
            $data['only_mine'] = $request->query->getBoolean('only_mine');
75
76
            $objects = $this->_list_revised($data['revised_after'], $data['type_filter'], $data['only_mine']);
77
        } else {
78 1
            $data['revised_after'] = date('Y-m-d', strtotime('yesterday'));
79 1
            $objects = $this->_list_revised($data['revised_after']);
80
        }
81
82 1
        $this->_prepare_tabledata($objects);
83
84 1
        $data['action_options'] = [
85 1
            'none' => ['label' => $this->_l10n->get('apply to selected')],
86
            'delete' => [
87 1
                'label' => $this->_l10n_midcom->get('delete'),
88
            ],
89
            'approve' => [
90 1
                'label' => $this->_l10n_midcom->get('approve'),
91
            ]
92
        ];
93
94 1
        $this->_populate_toolbar();
95 1
        return $this->get_response();
96
    }
97
98 1
    private function _prepare_tabledata(array $objects)
99
    {
100 1
        $rows = [];
101 1
        foreach ($objects as $data) {
102 1
            $object = $data['object'];
103 1
            $reflector = midcom_helper_reflector::get($object);
104
105
            $row = [
106 1
                'revision' => $object->metadata->revision,
107 1
                'revised' => $object->metadata->revised,
108 1
                'id' => $object->guid,
109 1
                'class' => get_class($object),
110
            ];
111
112 1
            $row['approved'] = ($object->is_approved()) ? strftime('%x %X', $object->metadata->approved) : $this->_l10n->get('not approved');
113
            try {
114 1
                $row['index_title'] = $reflector->get_object_label($object) ?: '[' . $this->_l10n->get('no title') . ']';
115
            } catch (Exception $e) {
116
                $row['index_title'] = 'ERROR: ' . $e->getMessage();
117
            }
118 1
            $link = $this->router->generate('object_' . $this->_request_data['default_mode'], ['guid' => $object->guid]);
119 1
            $row['title'] = '<a href="' . $link . '" title="' . $row['class'] . '">' . $reflector->get_object_icon($object) . ' ' . $row['index_title'] . '</a>';
120
121 1
            if (empty($data['revisor'])) {
122
                $row['revisor'] = $this->_l10n_midcom->get('unknown');
123
            } else {
124 1
                $row['revisor'] = $data['revisor']->name;
125
            }
126 1
            $rows[] = $row;
127
        }
128 1
        $provider = new provider($rows, 'local');
129 1
        $this->_request_data['grid'] = $provider->get_grid('revised');
130 1
    }
131
132 1
    private function _populate_toolbar()
133
    {
134 1
        $buttons = [];
135 1
        $buttons[] = [
136 1
            MIDCOM_TOOLBAR_URL => $this->router->generate('preferences'),
137 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('user preferences'),
138 1
            MIDCOM_TOOLBAR_GLYPHICON => 'sliders',
139
        ];
140
141 1
        if (midcom::get()->auth->admin) {
142
            $buttons[] = [
143
                MIDCOM_TOOLBAR_URL => $this->router->generate('shell'),
144
                MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('shell'),
145
                MIDCOM_TOOLBAR_GLYPHICON => 'terminal',
146
            ];
147
            $buttons[] = [
148
                MIDCOM_TOOLBAR_URL => $this->router->generate('trash'),
149
                MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('trash'),
150
                MIDCOM_TOOLBAR_GLYPHICON => 'trash',
151
            ];
152
        }
153
154 1
        $buttons[] = [
155 1
            MIDCOM_TOOLBAR_URL => $this->router->generate('components'),
156 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('components'),
157 1
            MIDCOM_TOOLBAR_GLYPHICON => 'puzzle-piece',
158
        ];
159
160
        // Add link to site
161 1
        $buttons[] = [
162 1
            MIDCOM_TOOLBAR_URL => midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX),
163 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('back to site'),
164 1
            MIDCOM_TOOLBAR_GLYPHICON => 'home',
165
        ];
166
167 1
        $buttons[] = [
168 1
            MIDCOM_TOOLBAR_URL => midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . "midcom-logout-",
0 ignored issues
show
Bug introduced by
Are you sure midcom_core_context::get...M_CONTEXT_ANCHORPREFIX) of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

168
            MIDCOM_TOOLBAR_URL => /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . "midcom-logout-",
Loading history...
169 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('logout'),
170 1
            MIDCOM_TOOLBAR_GLYPHICON => 'sign-out',
171
        ];
172 1
        $this->_request_data['asgard_toolbar']->add_items($buttons);
173 1
    }
174
175
    private function _mass_delete(array $guids)
176
    {
177
        foreach ($guids as $guid) {
178
            try {
179
                $object = midcom::get()->dbfactory->get_object_by_guid($guid);
180
            } catch (midcom_error $e) {
181
                continue;
182
            }
183
184
            if ($object->delete()) {
185
                midcom::get()->uimessages->add($this->_l10n->get('midgard.admin.asgard'), sprintf($this->_l10n->get('object %s removed'), $object->guid));
186
            }
187
        }
188
    }
189
190
    private function _mass_approve(array $guids)
191
    {
192
        foreach ($guids as $guid) {
193
            try {
194
                $object = midcom::get()->dbfactory->get_object_by_guid($guid);
195
            } catch (midcom_error $e) {
196
                continue;
197
            }
198
199
            if (   $object->can_do('midgard:update')
200
                && $object->can_do('midcom:approve')) {
201
                $object->metadata->approve();
202
                midcom::get()->uimessages->add($this->_l10n->get('midgard.admin.asgard'), sprintf($this->_l10n->get('object %s approved'), $object->guid));
203
            }
204
        }
205
    }
206
207
    /**
208
     * Shows the loaded object in editor.
209
     */
210 1
    public function _show_welcome(string $handler_id, array &$data)
211
    {
212 1
        if (midcom::get()->auth->can_user_do('midgard.admin.asgard:manage_objects', null, 'midgard_admin_asgard_plugin')) {
213 1
            midcom_show_style('midgard_admin_asgard_welcome');
214
        }
215 1
    }
216
}
217