Passed
Push — master ( f8c985...ada476 )
by Michael
02:25
created

admin_plugin_issuelinks_repoadmin::getMenuIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * DokuWiki Plugin IssueLinks (Admin Component)
4
 *
5
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6
 * @author  Andreas Gohr, Michael Große <[email protected]>
7
 */
8
9
// must be run within Dokuwiki
10
use dokuwiki\plugin\issuelinks\classes\ServiceProvider;
11
use dokuwiki\plugin\issuelinks\services\ServiceInterface;
12
13
if (!defined('DOKU_INC')) {
14
    die();
15
}
16
17
class admin_plugin_issuelinks_repoadmin extends DokuWiki_Admin_Plugin
18
{
19
20
    private $orgs = [];
21
    private $configNeeded = [];
22
23
    /**
24
     * @return int sort number in admin menu
25
     */
26
    public function getMenuSort()
27
    {
28
        return 500;
29
    }
30
31
    /**
32
     * Return the text that is displayed at the main admin menu
33
     * (Default localized language string 'menu' is returned, override this function for setting another name)
34
     *
35
     * @param string $language language code
36
     *
37
     * @return string menu string
38
     */
39
    public function getMenuText($language)
40
    {
41
        return $this->getLang('menu:repo-admin');
42
    }
43
44
    public function getMenuIcon()
45
    {
46
        $plugin = $this->getPluginName();
47
        return DOKU_PLUGIN . $plugin . '/images/issue-opened.svg';
48
    }
49
50
    /**
51
     * @return bool true if only access for superuser, false is for superusers and moderators
52
     */
53
    public function forAdminOnly()
54
    {
55
        return true;
56
    }
57
58
    /**
59
     * Should carry out any processing required by the plugin.
60
     */
61
    public function handle()
62
    {
63
        global $INPUT;
64
65
        $serviceProvider = ServiceProvider::getInstance();
66
        /** @var ServiceInterface[] $services */
67
        $services = $serviceProvider->getServices();
68
69
        if ($INPUT->has('authorize')) {
70
            $serviceID = $INPUT->str('authorize');
71
            $service = $services[$serviceID]::getInstance();
72
            $service->handleAuthorization();
73
        }
74
75
        foreach ($services as $serviceID => $serviceClass) {
76
            $service = $serviceClass::getInstance();
77
            $this->orgs[$serviceID] = [];
78
            if ($INPUT->str('reconfigureService') === $serviceID || !$service->isConfigured()) {
79
                $this->configNeeded[] = $serviceID;
80
                continue;
81
            }
82
83
            $this->orgs[$serviceID] = $service->getListOfAllUserOrganisations();
84
            sort($this->orgs[$serviceID]);
85
        }
86
    }
87
88
    /**
89
     * Render HTML output, e.g. helpful text and a form
90
     */
91
    public function html()
92
    {
93
        $activeServices = array_keys($this->orgs);
94
        $html = "<div id='plugin__issuelinks_repoadmin'>";
95
        $html .= "<div><ul class='tabs'>";
96
        $html = array_reduce($activeServices, [$this, 'appendServiceTab'], $html);
97
        $html .= '</ul></div>';
98
        $html = array_reduce($activeServices, [$this, 'appendServicePage'], $html);
99
100
        $html .= '</div>'; // <div id='plugin__issuelinks_repoadmin'>
101
102
        echo $html;
103
    }
104
105
    /**
106
     * Callback to append a `<li>`-tab for services like GitLab or GitHub to a tabbar
107
     *
108
     * @param string $html the html to which we append the tab
109
     * @param string $serviceID
110
     *
111
     * @return string
112
     */
113
    protected function appendServiceTab($html, $serviceID)
114
    {
115
        $serviceProvider = ServiceProvider::getInstance();
116
        $services = $serviceProvider->getServices();
117
        $service = $services[$serviceID];
118
        $serviceName = $service::DISPLAY_NAME;
119
        return $html . "<li><a data-service='$serviceID'>" . $serviceName . '</a></li>';
120
    }
121
122
    /**
123
     * Callback creating and appending a service's page for adjusting its webhooks
124
     *
125
     * @param string $html the html to which we append the page
126
     * @param string $serviceID
127
     *
128
     * @return string
129
     */
130
    protected function appendServicePage($html, $serviceID)
131
    {
132
        $serviceProvider = ServiceProvider::getInstance();
133
        $services = $serviceProvider->getServices();
134
        $service = $services[$serviceID]::getInstance();
135
        $serviceName = $service::DISPLAY_NAME;
136
137
        $html .= "<div data-service='$serviceID' class='service_wrapper'>";
138
139
        if (in_array($serviceID, $this->configNeeded)) {
140
            $configForm = new \dokuwiki\Form\Form();
141
            $configForm->addClass('plugin__repoadmin_serviceConfig');
142
            $configForm->setHiddenField('authorize', $serviceID);
143
            $configForm->addFieldsetOpen();
144
            $service->hydrateConfigForm($configForm);
145
            $configForm->addButton('', 'Submit FIXME')->attr('type', 'submit');
146
            $configForm->addFieldsetClose();
147
            $html .= $configForm->toHTML();
148
        } elseif (count($this->orgs[$serviceID]) === 0) {
149
            $html .= '<p>No organisations available for ' . $serviceName . '</p>';
150
        } else {
151
            global $INPUT;
152
            $reconfigureURL = $INPUT->server->str('REQUEST_URI') . '&reconfigureService=' . $serviceID;
153
            $reconfigureLink = "<a href=\"$reconfigureURL\">{$this->getLang('label: reconfigure service')}</a>";
154
            $authorizedUserLabel = sprintf($this->getLang('label: authorized with user'),$service->getUserString());
155
            $form = new \dokuwiki\Form\Form(['data-service' => $serviceID]);
156
            $form->addFieldsetOpen($this->getLang('legend:user'));
157
            $form->addTagOpen('p');
158
            $form->addHTML($authorizedUserLabel . ' ' . $reconfigureLink);
159
            $form->addTagClose('p');
160
            $form->addFieldsetClose();
161
            $form->addFieldsetOpen($this->getLang("legend:group $serviceID"));
162
            $form->addDropdown('mm_organisation', array_merge([''], $this->orgs[$serviceID]),
163
                $this->getLang("label $serviceID:choose organisation"));
164
            $form->addFieldsetClose();
165
            $html .= $form->toHTML();
166
            $html .= "<div data-service='$serviceID' class='repo_area'></div>";
167
        }
168
        $html .= '</div>'; // <div data-service='$servicename' class='service_area'>
169
        return $html;
170
    }
171
172
}
173
174
// vim:ts=4:sw=4:et:
175