Completed
Push — master ( a15f92...5650a5 )
by Andreas
18:52
created

org_openpsa_widgets_ui   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 99
ccs 56
cts 56
cp 1
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_config_value() 0 4 1
B get_search_providers() 0 30 7
A add_head_elements() 0 6 1
A add_navigation_toolbar() 0 5 1
A render_tabs() 0 32 3
1
<?php
2
/**
3
 * @package org.openpsa.widgets
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
use midcom\datamanager\helper\autocomplete;
10
11
/**
12
 * Helper class to load parts of the ui
13
 *
14
 * @package org.openpsa.widgets
15
 */
16
class org_openpsa_widgets_ui extends midcom_baseclasses_components_purecode
17
{
18 19
    public static function get_config_value($value)
19
    {
20 19
        $config = midcom_baseclasses_components_configuration::get('org.openpsa.widgets', 'config');
21 19
        return $config->get($value);
22
    }
23
24 11
    public static function get_search_providers()
25
    {
26 11
        $defaults = ['autocomplete' => false];
27 11
        $providers = [];
28 11
        $siteconfig = org_openpsa_core_siteconfig::get_instance();
29 11
        $configured_providers = self::get_config_value('search_providers');
30 11
        $user_id = false;
31
32 11
        if (!midcom::get()->auth->admin) {
33 11
            $user_id = midcom::get()->auth->acl->get_user_id();
34
        }
35 11
        foreach ($configured_providers as $component => $config) {
36 11
            if (!is_array($config)) {
37 11
                $config = ['route' => $config];
38
            }
39 11
            $config = array_merge($defaults, $config);
40
41 11
            $node_url = $siteconfig->get_node_full_url($component);
42 11
            if (   $node_url
43 11
                && (   !$user_id
44 11
                    || midcom::get()->auth->acl->can_do_byguid('midgard:read', $siteconfig->get_node_guid($component), midcom_db_topic::class, $user_id))) {
0 ignored issues
show
Bug introduced by
It seems like $siteconfig->get_node_guid($component) can also be of type false; however, parameter $object_guid of midcom_services_auth_acl::can_do_byguid() 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

44
                    || midcom::get()->auth->acl->can_do_byguid('midgard:read', /** @scrutinizer ignore-type */ $siteconfig->get_node_guid($component), midcom_db_topic::class, $user_id))) {
Loading history...
45 11
                $providers[] = [
46 11
                    'placeholder' => midcom::get()->i18n->get_string('search title', $component),
47 11
                    'url' => $node_url . $config['route'],
48 11
                    'identifier' => $component,
49 11
                    'autocomplete' => $config['autocomplete'],
50
                ];
51
            }
52
        }
53 11
        return $providers;
54
    }
55
56 10
    public static function add_head_elements()
57
    {
58 10
        $head = midcom::get()->head;
59 10
        $head->enable_jquery();
60
61 10
        $head->add_jsfile(MIDCOM_STATIC_URL . '/org.openpsa.widgets/ui.js');
62 10
    }
63
64
    /**
65
     * Render jquery.ui tab controls. Relatedto tabs are automatically added if a GUID is found
66
     * Also adds the necessary javascript & css files for ui_tab
67
     *
68
     * @param string $guid The GUID, if any
69
     * @param array $tabdata Any custom tabs the handler wants to add
70
     */
71 10
    public static function render_tabs($guid, array $tabdata)
72
    {
73 10
        $head = midcom::get()->head;
74 10
        $head->enable_jquery_ui(['tabs']);
75 10
        $head->add_jsfile(MIDCOM_STATIC_URL . '/org.openpsa.widgets/tab_functions.js');
76
77 10
        $uipage = self::get_config_value('ui_page');
78 10
        $prefix = midcom_connection::get_url('self') . $uipage . '/';
79
80 10
        if (!empty($guid)) {
81
            //pass the urls & titles for the tabs
82 8
            $tabdata[] = [
83 8
               'url' => '__mfa/org.openpsa.relatedto/journalentry/' . $guid . '/',
84 8
               'title' => midcom::get()->i18n->get_string('journal entries', 'org.openpsa.relatedto'),
85
            ];
86 8
            $tabdata[] = [
87 8
               'url' => '__mfa/org.openpsa.relatedto/render/' . $guid . '/both/',
88 8
               'title' => midcom::get()->i18n->get_string('related objects', 'org.openpsa.relatedto'),
89
            ];
90
        }
91
92 10
        echo '<div id="tabs">';
93 10
        echo "\n<ul>\n";
94 10
        foreach ($tabdata as $key => $tab) {
95 10
            echo "<li><a id='key_" . $key ."' class='tabs_link' href='" . $prefix . $tab['url'] . "' ><span> " . $tab['title'] . "</span></a></li>";
96
        }
97 10
        echo "\n</ul>\n";
98 10
        echo "</div>\n";
99
100
        echo <<<JSINIT
101
<script type="text/javascript">
102 10
    org_openpsa_widgets_tabs.initialize('{$uipage}');
103 10
</script>
104
JSINIT;
105 10
    }
106
107
    /**
108
     * @param array $items
109
     */
110 7
    public static function add_navigation_toolbar(array $items)
111
    {
112 7
        $toolbar = new midcom_helper_toolbar('midcom_toolbar navigation_toolbar');
113 7
        $toolbar->add_items($items);
114 7
        midcom::get()->toolbars->add_toolbar('navigation', $toolbar);
115 7
    }
116
}
117