Completed
Push — master ( 2134f6...01c567 )
by Andreas
22:35
created

_handler_welcome()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.2017

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 6
nop 2
dl 0
loc 41
ccs 14
cts 22
cp 0.6364
crap 6.2017
rs 9.2248
c 0
b 0
f 0
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($since, $type = null, $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
            $mgdschema_class = midcom_helper_reflector::class_rewrite($schema_type);
29 1
            $dummy_object = new $mgdschema_class();
30 1
            $midcom_dba_classname = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($dummy_object);
31 1
            if (!empty($midcom_dba_classname)) {
32
                // List all revised objects
33 1
                $qb = new midcom_core_querybuilder($midcom_dba_classname);
34 1
                $qb->add_constraint('metadata.revised', '>=', $since);
35
36 1
                if (   $only_mine
37 1
                    && midcom::get()->auth->user) {
38
                    $qb->add_constraint('metadata.authors', 'LIKE', '|' . midcom::get()->auth->user->guid . '|');
39
                }
40
41 1
                $qb->add_order('metadata.revision', 'DESC');
42
43 1
                foreach ($qb->execute() as $object) {
44 1
                    $revised["{$object->metadata->revised}_{$object->guid}_{$object->metadata->revision}"] = [
45 1
                        'object' => $object,
46 1
                        'revisor' => midcom::get()->auth->get_user($object->metadata->revisor)
47
                    ];
48
                }
49
            }
50
        }
51
52 1
        krsort($revised);
53 1
        return $revised;
54
    }
55
56
    /**
57
     * Object editing view
58
     */
59 1
    public function _handler_welcome(Request $request, array &$data)
60
    {
61 1
        $data['schema_types'] = array_diff(midcom_connection::get_schema_types(), $this->_config->get('skip_in_filter'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('skip_in_filter') can also be of type false; however, parameter $array2 of array_diff() does only seem to accept array, 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

61
        $data['schema_types'] = array_diff(midcom_connection::get_schema_types(), /** @scrutinizer ignore-type */ $this->_config->get('skip_in_filter'));
Loading history...
62
63 1
        $data['view_title'] = $this->_l10n->get('asgard');
64
65 1
        if ($request->request->has('action') && $request->request->has('entries')) {
66
            $method_name = '_mass_' . $request->request->get('action');
67
            $this->$method_name($request->request->get('entries'));
68
        }
69
70 1
        if ($request->query->has('revised_after')) {
71
            $data['revised_after'] = date('Y-m-d', $request->query->get('revised_after'));
72
73
            $data['type_filter'] = null;
74
            if ($request->query->get('type_filter', 'any') != 'any') {
75
                $data['type_filter'] = $request->query->get('type_filter');
76
            }
77
78
            $data['only_mine'] = $request->query->getBoolean('only_mine');
79
80
            $objects = $this->_list_revised($data['revised_after'], $data['type_filter'], $data['only_mine']);
81
        } else {
82 1
            $data['revised_after'] = date('Y-m-d', strtotime('yesterday'));
83 1
            $objects = $this->_list_revised($data['revised_after']);
84
        }
85
86 1
        $this->_prepare_tabledata($objects);
87
88 1
        $data['action_options'] = [
89 1
            'none' => ['label' => $this->_l10n->get('apply to selected')],
90
            'delete' => [
91 1
                'label' => $this->_l10n_midcom->get('delete'),
92
            ],
93
            'approve' => [
94 1
                'label' => $this->_l10n_midcom->get('approve'),
95
            ]
96
        ];
97
98 1
        $this->_populate_toolbar();
99 1
        return $this->get_response();
100
    }
101
102 1
    private function _prepare_tabledata(array $objects)
103
    {
104 1
        $rows = [];
105 1
        foreach ($objects as $data) {
106 1
            $object = $data['object'];
107 1
            $reflector = midcom_helper_reflector::get($object);
108
109
            $row = [
110 1
                'revision' => $object->metadata->revision,
111 1
                'revised' => $object->metadata->revised,
112 1
                'id' => $object->guid,
113 1
                'class' => get_class($object),
114
            ];
115
116 1
            $row['approved'] = ($object->is_approved()) ? strftime('%x %X', $object->metadata->approved) : $this->_l10n->get('not approved');
117
118 1
            $row['index_title'] = $reflector->get_object_label($object) ?: '[' . $this->_l10n->get('no title') . ']';
119 1
            $link = $this->router->generate('object_' . $this->_request_data['default_mode'], ['guid' => $object->guid]);
120 1
            $row['title'] = '<a href="' . $link . '" title="' . $row['class'] . '">' . $reflector->get_object_icon($object) . ' ' . $row['index_title'] . '</a>';
121
122 1
            if (empty($data['revisor'])) {
123
                $row['revisor'] = $this->_l10n_midcom->get('unknown');
124
            } else {
125 1
                $row['revisor'] = $data['revisor']->name;
126
            }
127 1
            $rows[] = $row;
128
        }
129 1
        $provider = new provider($rows, 'local');
130 1
        $this->_request_data['grid'] = $provider->get_grid('revised');
131 1
    }
132
133 1
    private function _populate_toolbar()
134
    {
135 1
        $buttons = [];
136 1
        $buttons[] = [
137 1
            MIDCOM_TOOLBAR_URL => $this->router->generate('preferences'),
138 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('user preferences'),
139 1
            MIDCOM_TOOLBAR_GLYPHICON => 'sliders',
140
        ];
141
142 1
        if (midcom::get()->auth->admin) {
143
            $buttons[] = [
144
                MIDCOM_TOOLBAR_URL => $this->router->generate('shell'),
145
                MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('shell'),
146
                MIDCOM_TOOLBAR_GLYPHICON => 'terminal',
147
            ];
148
            $buttons[] = [
149
                MIDCOM_TOOLBAR_URL => $this->router->generate('trash'),
150
                MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('trash'),
151
                MIDCOM_TOOLBAR_GLYPHICON => 'trash',
152
            ];
153
        }
154
155 1
        $buttons[] = [
156 1
            MIDCOM_TOOLBAR_URL => $this->router->generate('components'),
157 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('components'),
158 1
            MIDCOM_TOOLBAR_GLYPHICON => 'puzzle-piece',
159
        ];
160
161
        // Add link to site
162 1
        $buttons[] = [
163 1
            MIDCOM_TOOLBAR_URL => midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX),
164 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('back to site'),
165 1
            MIDCOM_TOOLBAR_GLYPHICON => 'home',
166
        ];
167
168 1
        $buttons[] = [
169 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

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