Completed
Push — master ( f34725...886fb9 )
by Andreas
15:32
created

org_openpsa_widgets_ui::add_navigation_toolbar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 7
    public static function get_config_value($value)
19
    {
20 7
        $config = midcom_baseclasses_components_configuration::get('org.openpsa.widgets', 'config');
21 7
        return $config->get($value);
22
    }
23
24 2
    public static function get_search_providers()
25
    {
26 2
        $defaults = ['autocomplete' => false];
27 2
        $providers = [];
28 2
        $siteconfig = org_openpsa_core_siteconfig::get_instance();
29 2
        $configured_providers = self::get_config_value('search_providers');
30 2
        $user_id = false;
31
32 2
        if (!midcom::get()->auth->admin) {
33 2
            $user_id = midcom::get()->auth->acl->get_user_id();
34
        }
35 2
        foreach ($configured_providers as $component => $config) {
36 2
            if (!is_array($config)) {
37 2
                $config = ['route' => $config];
38
            }
39 2
            $config = array_merge($defaults, $config);
40
41 2
            $node_url = $siteconfig->get_node_full_url($component);
42 2
            if (   $node_url
43 2
                && (   !$user_id
44 2
                    || 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 2
                $providers[] = [
46 2
                    'placeholder' => midcom::get()->i18n->get_string('search title', $component),
47 2
                    'url' => $node_url . $config['route'],
48 2
                    'identifier' => $component,
49 2
                    'autocomplete' => $config['autocomplete'],
50
                ];
51
            }
52
        }
53 2
        return $providers;
54
    }
55
56
    public static function initialize_search()
57
    {
58
        $providers = self::get_search_providers();
59
        foreach ($providers as $config) {
60
            if ($config['autocomplete'] === true) {
61
                autocomplete::add_head_elements();
62
            }
63
        }
64
65
        midcom::get()->head->add_jquery_state_script('org_openpsa_layout.initialize_search
66
        (
67
            ' . json_encode($providers) . ',
68
            "' . midgard_admin_asgard_plugin::get_preference('openpsa2_search_provider') . '"
69
        );');
70
    }
71
72 1
    public static function add_head_elements()
73
    {
74 1
        $head = midcom::get()->head;
75 1
        $head->enable_jquery();
76
77 1
        $head->add_jsfile(MIDCOM_STATIC_URL . '/org.openpsa.widgets/ui.js');
78 1
    }
79
80
    /**
81
     * Function to load the necessary javascript & css files for ui_tab
82
     */
83 10
    public static function enable_ui_tab()
84
    {
85 10
        $head = midcom::get()->head;
86 10
        $head->enable_jquery_ui(['tabs']);
87
88
        //functions needed for ui-tab to work here
89 10
        $head->add_jsfile(MIDCOM_STATIC_URL . '/org.openpsa.widgets/tab_functions.js');
90 10
    }
91
92
    /**
93
     * Render jquery.ui tab controls. Relatedto tabs are automatically added if a GUID is found
94
     *
95
     * @param string $guid The GUID, if any
96
     * @param array $tabdata Any custom tabs the handler wants to add
97
     */
98 6
    public static function render_tabs($guid, array $tabdata)
99
    {
100 6
        $uipage = self::get_config_value('ui_page');
101 6
        $prefix = midcom_connection::get_url('self') . $uipage . '/';
102
103 6
        if (!empty($guid)) {
104
            //pass the urls & titles for the tabs
105 6
            $tabdata[] = [
106 6
               'url' => '__mfa/org.openpsa.relatedto/journalentry/' . $guid . '/',
107 6
               'title' => midcom::get()->i18n->get_string('journal entries', 'org.openpsa.relatedto'),
108
            ];
109 6
            $tabdata[] = [
110 6
               'url' => '__mfa/org.openpsa.relatedto/render/' . $guid . '/both/',
111 6
               'title' => midcom::get()->i18n->get_string('related objects', 'org.openpsa.relatedto'),
112
            ];
113
        }
114
115 6
        echo '<div id="tabs">';
116 6
        echo "\n<ul>\n";
117 6
        foreach ($tabdata as $key => $tab) {
118 6
            echo "<li><a id='key_" . $key ."' class='tabs_link' href='" . $prefix . $tab['url'] . "' ><span> " . $tab['title'] . "</span></a></li>";
119
        }
120 6
        echo "\n</ul>\n";
121 6
        echo "</div>\n";
122
123
        echo <<<JSINIT
124
<script type="text/javascript">
125
$(document).ready(
126
    function()
127
    {
128 6
        org_openpsa_widgets_tabs.initialize('{$uipage}');
129
    }
130
);
131 6
</script>
132
JSINIT;
133 6
    }
134
135
    /**
136
     * @param array $items
137
     */
138 7
    public static function add_navigation_toolbar(array $items)
139
    {
140 7
        $toolbar = new midcom_helper_toolbar('midcom_toolbar navigation_toolbar');
141 7
        $toolbar->add_items($items);
142 7
        midcom::get()->toolbars->add_toolbar('navigation', $toolbar);
143 7
    }
144
}
145