Passed
Push — master ( f44a03...ba8ea7 )
by Andreas
08:58
created

midcom_services_toolbars::bind_toolbar_to_object()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 16
ccs 8
cts 10
cp 0.8
crap 3.072
rs 9.9666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A midcom_services_toolbars::render_node_toolbar() 0 3 1
1
<?php
2
/**
3
 * @package midcom.services
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
 * On-site toolbar service.
11
 *
12
 * This service manages the toolbars used for the on-site administration system.
13
 * For each context, it provides the following set of toolbars:
14
 *
15
 * 1. The <i>Node</i> toolbar is applicable to the current
16
 *    node, which is usually a topic. MidCOM places the topic management operations
17
 *    into this toolbar, where applicable.
18
 *
19
 * 2. The <i>View</i> toolbar is applicable to the specific view ("url"). Usually
20
 *    this maps to a single displayed object (see also the bind_to() member
21
 *    function). MidCOM places the object-specific management operations (like
22
 *    Metadata controls) into this toolbar, if it is bound to an object. Otherwise,
23
 *    this toolbar is not touched by MidCOM.
24
 *
25
 * It is important to understand that the default toolbars made available through this
26
 * service are completely specific to a given request context. If you have a dynamic_load
27
 * running on a given site, it will have its own set of toolbars for each instance.
28
 *
29
 * In addition, components may retrieve a third kind of toolbars, which are not under
30
 * the general control of MidCOM, the <i>Object</i> toolbars. They apply to a single
31
 * database object (like a bound <i>View</i> toolbar). The usage of this kind of
32
 * toolbars is completely component-specific.
33
 *
34
 * <b>Implementation notes</b>
35
 *
36
 * It has yet to prove if the toolbar system is needed for a dynamic_load environments.
37
 * The main reason for this is that dl'ed stuff is often quite tight in space and thus cannot
38
 * display any toolbars in a sane way. Usually, the administrative tasks will be bound to the
39
 * main request.
40
 *
41
 * This could be different for portal applications, which display several components on the
42
 * welcome page, each with its own management options.
43
 *
44
 * <b>Configuration</b>
45
 * See midcom_config for configuration options.
46
 *
47
 * @package midcom.services
48
 */
49
class midcom_services_toolbars
50
{
51
    /**
52
     * The toolbars currently available.
53
     *
54
     * This array is indexed by context id; each value consists of a flat array
55
     * of two toolbars, the first object being the Node toolbar, the second
56
     * View toolbar. The toolbars are created on-demand.
57
     *
58
     * @var array
59
     */
60
    private $_toolbars = [];
61
62
    /**
63
     * Whether we're in centralized mode, i.e. centralized toolbar has been shown
64
     *
65
     * @var boolean
66
     */
67
    private $_centralized_mode = false;
68
69
    public function get_class_magic_default_privileges()
70
    {
71
        return [
72
            'EVERYONE' => [],
73
            'ANONYMOUS' => [],
74
            'USERS' => []
75
        ];
76
    }
77
78
    /**
79
     * Returns the host toolbar of the current context.
80
     * The toolbar will be created if this is the first request.
81
     */
82
    function get_host_toolbar() : midcom_helper_toolbar_host
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
    {
84
        return $this->_get_toolbar(MIDCOM_TOOLBAR_HOST);
85
    }
86
87
    /**
88
     * Returns the node toolbar of the current context.
89
     * The toolbar will be created if this is the first request.
90
     */
91 348
    public function get_node_toolbar() : midcom_helper_toolbar_node
92
    {
93 348
        return $this->_get_toolbar(MIDCOM_TOOLBAR_NODE);
94
    }
95
96
    /**
97
     * Returns the view toolbar of the current context.
98
     * The toolbar will be created if this is the first request.
99
     */
100 348
    public function get_view_toolbar() : midcom_helper_toolbar_view
101
    {
102 348
        return $this->_get_toolbar(MIDCOM_TOOLBAR_VIEW);
103
    }
104
105
    /**
106
     * Returns the help toolbar of the current context.
107
     * The toolbar will be created if this is the first request.
108
     */
109
    public function get_help_toolbar() : midcom_helper_toolbar_help
110
    {
111
        return $this->_get_toolbar(MIDCOM_TOOLBAR_HELP);
112
    }
113
114 348
    private function _get_toolbar(string $identifier) : midcom_helper_toolbar
115
    {
116 348
        $context = midcom_core_context::get();
117
118 348
        if (!array_key_exists($context->id, $this->_toolbars)) {
119 348
            $this->_toolbars[$context->id] = [
120
                MIDCOM_TOOLBAR_HELP => null,
121
                MIDCOM_TOOLBAR_HOST => null,
122
                MIDCOM_TOOLBAR_NODE => null,
123
                MIDCOM_TOOLBAR_VIEW => null
124
            ];
125
        }
126 348
        if (!isset($this->_toolbars[$context->id][$identifier])) {
127 348
            switch ($identifier) {
128
                case MIDCOM_TOOLBAR_HELP:
129 7
                    $component = $context->get_key(MIDCOM_CONTEXT_COMPONENT);
130 7
                    $toolbar = new midcom_helper_toolbar_help($component);
0 ignored issues
show
Bug introduced by
It seems like $component can also be of type false; however, parameter $component of midcom_helper_toolbar_help::__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

130
                    $toolbar = new midcom_helper_toolbar_help(/** @scrutinizer ignore-type */ $component);
Loading history...
131 7
                    break;
132
                case MIDCOM_TOOLBAR_HOST:
133 7
                    $toolbar = new midcom_helper_toolbar_host;
134 7
                    break;
135
                case MIDCOM_TOOLBAR_NODE:
136 348
                    $topic = $context->get_key(MIDCOM_CONTEXT_CONTENTTOPIC);
137 348
                    $toolbar = new midcom_helper_toolbar_node($topic);
0 ignored issues
show
Bug introduced by
It seems like $topic can also be of type false; however, parameter $topic of midcom_helper_toolbar_node::__construct() does only seem to accept midcom_db_topic, 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

137
                    $toolbar = new midcom_helper_toolbar_node(/** @scrutinizer ignore-type */ $topic);
Loading history...
138 348
                    break;
139
                case MIDCOM_TOOLBAR_VIEW:
140 348
                    $toolbar = new midcom_helper_toolbar_view;
141 348
                    break;
142
                default:
143 7
                    $toolbar = new midcom_helper_toolbar;
144 7
                    break;
145
            }
146 348
            $this->_toolbars[$context->id][$identifier] = $toolbar;
147
        }
148 348
        return $this->_toolbars[$context->id][$identifier];
149
    }
150
151
    /**
152
     * Add a toolbar
153
     *
154
     * @param string $identifier
155
     */
156 7
    function add_toolbar($identifier, midcom_helper_toolbar $toolbar)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
157
    {
158 7
        $context_id = midcom_core_context::get()->id;
159 7
        $this->_toolbars[$context_id][$identifier] = $toolbar;
160
    }
161
162
    /**
163
     * Renders the specified toolbar for the current context.
164
     *
165
     * If the toolbar is undefined, an empty string is returned.
166
     *
167
     * @param int $toolbar_identifier The toolbar identifier constant (one of
168
     *     MIDCOM_TOOLBAR_NODE or MIDCOM_TOOLBAR_VIEW etc.)
169
     * @see midcom_helper_toolbar::render()
170
     */
171 13
    function _render_toolbar($toolbar_identifier) : string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
172
    {
173 13
        $this->add_head_elements();
174 13
        return $this->_get_toolbar($toolbar_identifier)->render();
175
    }
176
177
    /**
178
     * Renders the node toolbar for the current context. If the toolbar is undefined,
179
     * an empty string is returned. If you want to show the toolbar directly, look for
180
     * the show_xxx_toolbar methods.
181
     *
182
     * @see midcom_helper_toolbar::render()
183
     */
184
    public function render_node_toolbar() : string
185
    {
186
        return $this->_render_toolbar(MIDCOM_TOOLBAR_NODE);
187
    }
188
189
    /**
190
     * Renders the view toolbar for the current context. If the toolbar is undefined,
191
     * an empty string is returned. If you want to show the toolbar directly, look for
192
     * the show_xxx_toolbar methods.
193
     *
194
     * @see midcom_helper_toolbar::render()
195
     */
196 13
    public function render_view_toolbar() : string
197
    {
198 13
        return $this->_render_toolbar(MIDCOM_TOOLBAR_VIEW);
199
    }
200
201
    /**
202
     * Renders the host toolbar for the current context. If the toolbar is undefined,
203
     * an empty string is returned. If you want to show the toolbar directly, look for
204
     * the show_xxx_toolbar methods.
205
     *
206
     * @see midcom_helper_toolbar::render()
207
     */
208
    public function render_host_toolbar() : string
209
    {
210
        return $this->_render_toolbar(MIDCOM_TOOLBAR_HOST);
211
    }
212
213
    /**
214
     * Renders the help toolbar for the current context. If the toolbar is undefined,
215
     * an empty string is returned. If you want to show the toolbar directly, look for
216
     * the show_xxx_toolbar methods.
217
     *
218
     * @see midcom_helper_toolbar::render()
219
     */
220
    public function render_help_toolbar() : string
221
    {
222
        return $this->_render_toolbar(MIDCOM_TOOLBAR_HELP);
223
    }
224
225
    /**
226
     * Displays the node toolbar for the current context. If the toolbar is undefined,
227
     * an empty string is returned.
228
     *
229
     * @see midcom_helper_toolbar::render()
230
     */
231
    function show_node_toolbar()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
232
    {
233
        if (!$this->_centralized_mode) {
234
            echo $this->render_node_toolbar();
235
        }
236
    }
237
238
    /**
239
     * Displays the host toolbar for the current context. If the toolbar is undefined,
240
     * an empty string is returned.
241
     *
242
     * @see midcom_helper_toolbar::render()
243
     */
244
    function show_host_toolbar()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
245
    {
246
        if (!$this->_centralized_mode) {
247
            echo $this->render_host_toolbar();
248
        }
249
    }
250
251
    /**
252
     * Displays the view toolbar for the current context. If the toolbar is undefined,
253
     * an empty string is returned.
254
     *
255
     * @see midcom_helper_toolbar::render()
256
     */
257 10
    public function show_view_toolbar()
258
    {
259 10
        if (!$this->_centralized_mode) {
260 6
            echo $this->render_view_toolbar();
261
        }
262
    }
263
264
    /**
265
     * Displays the help toolbar for the current context. If the toolbar is undefined,
266
     * an empty string is returned.
267
     *
268
     * @see midcom_helper_toolbar::render()
269
     */
270
    function show_help_toolbar()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
271
    {
272
        if (!$this->_centralized_mode) {
273
            echo $this->render_help_toolbar();
274
        }
275
    }
276
277 13
    private function add_head_elements(bool $centralized = false) : bool
278
    {
279 13
        if (   !midcom::get()->auth->user
280 8
            || !midcom::get()->config->get('toolbars_enable_centralized')
281 13
            || !midcom::get()->auth->can_user_do('midcom:centralized_toolbar', null, __CLASS__)) {
282 5
            return false;
283
        }
284 8
        if ($centralized && midcom::get()->auth->can_user_do('midcom:ajax', null, $this)) {
0 ignored issues
show
Bug introduced by
$this of type midcom_services_toolbars is incompatible with the type string expected by parameter $class of midcom_services_auth::can_user_do(). ( Ignorable by Annotation )

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

284
        if ($centralized && midcom::get()->auth->can_user_do('midcom:ajax', null, /** @scrutinizer ignore-type */ $this)) {
Loading history...
285 7
            $this->_centralized_mode = true;
286 7
            midcom::get()->head->enable_jquery_ui(['mouse', 'draggable']);
287 7
            midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.services.toolbars/jquery.midcom_services_toolbars.js');
288 7
            midcom::get()->head->prepend_stylesheet(MIDCOM_STATIC_URL . '/midcom.services.toolbars/fancy.css', 'screen');
289
        } else {
290 8
            $path = midcom::get()->config->get('toolbars_simple_css_path', MIDCOM_STATIC_URL . "/midcom.services.toolbars/simple.css");
291 8
            midcom::get()->head->prepend_stylesheet($path, 'screen');
292
        }
293 8
        midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css');
294 8
        return true;
295
    }
296
297
    /**
298
     * Displays the combined MidCOM toolbar system for the current context.
299
     *
300
     * @see midcom_helper_toolbar::render()
301
     */
302 7
    public function show()
303
    {
304 7
        $context_id = midcom_core_context::get()->id;
305
306 7
        if (empty($this->_toolbars[$context_id]) || !$this->add_head_elements(true)) {
307
            return;
308
        }
309
310 7
        $enable_drag = false;
311 7
        $toolbar_style = "";
312 7
        $toolbar_class = "midcom_services_toolbars_simple";
313
314 7
        if (midcom::get()->auth->can_user_do('midcom:ajax', null, __CLASS__)) {
315 7
            $enable_drag = true;
316 7
            $toolbar_class = "midcom_services_toolbars_fancy";
317 7
            $toolbar_style = "display: none;";
318
        }
319
320 7
        echo "<div class=\"{$toolbar_class}\" style=\"{$toolbar_style}\">\n";
321 7
        echo "    <div class=\"minimizer\">\n";
322 7
        echo "    </div>\n";
323 7
        echo "    <div class=\"items\">\n";
324
325 7
        foreach (array_reverse($this->_toolbars[$context_id], true) as $identifier => $toolbar) {
326 7
            if ($toolbar === null) {
327 7
                $toolbar = $this->_get_toolbar($identifier);
328
            }
329 7
            if (!$toolbar->is_rendered()) {
330 7
                $this->show_toolbar($identifier, $toolbar);
331
            }
332
        }
333 7
        echo "</div>\n";
334
335 7
        if ($enable_drag) {
336 7
            echo "     <div class=\"dragbar\"></div>\n";
337
        }
338 7
        echo "</div>\n";
339 7
        echo '<script type="text/javascript">';
340 7
        echo "jQuery('body div.midcom_services_toolbars_fancy').midcom_services_toolbar();";
341 7
        echo '</script>';
342
    }
343
344 7
    private function show_toolbar($identifier, midcom_helper_toolbar $toolbar)
345
    {
346 7
        if (empty($toolbar->items)) {
347
            return;
348
        }
349 7
        switch ($identifier) {
350
            case MIDCOM_TOOLBAR_VIEW:
351
                $id = $class = 'page';
352
                break;
353
            case MIDCOM_TOOLBAR_NODE:
354 7
                $id = $class = 'folder';
355 7
                break;
356
            case MIDCOM_TOOLBAR_HOST:
357 7
                $id = $class = 'host';
358 7
                break;
359
            case MIDCOM_TOOLBAR_HELP:
360 7
                $id = $class = 'help';
361 7
                break;
362
            default:
363
                $id = 'custom-' . $identifier;
364
                $class = 'custom';
365
                break;
366
        }
367 7
        echo "        <div id=\"midcom_services_toolbars_topic-{$id}\" class=\"item\">\n";
368 7
        echo "            <span class=\"midcom_services_toolbars_topic_title {$class}\">" . $toolbar->get_label() . "</span>\n";
369 7
        echo $toolbar->render();
370 7
        echo "        </div>\n";
371
    }
372
}
373