1<?php
2
3namespace dokuwiki\plugin\extension;
4
5use dokuwiki\Form\Form;
6
7class GuiAdmin extends Gui
8{
9    public function render()
10    {
11        $html = '<div id="extension__manager">';
12
13        $html .= $this->tabNavigation();
14
15        switch ($this->currentTab()) {
16            case 'search':
17                $html .= $this->tabSearch();
18                break;
19            case 'templates':
20                $html .= $this->tabTemplates();
21                break;
22            case 'install':
23                $html .= $this->tabInstall();
24                break;
25            case 'plugins':
26            default:
27                $html .= $this->tabPlugins();
28        }
29
30        $html .= '</div>';
31        return $html;
32    }
33
34    /**
35     * Print the tab navigation
36     *
37     */
38    public function tabNavigation()
39    {
40        $html = '<ul class="tabs">';
41        foreach ($this->tabs as $tab) {
42            $url = $this->tabURL($tab);
43            if ($this->currentTab() == $tab) {
44                $class = ' active';
45            } else {
46                $class = '';
47            }
48            $html .= '<li class="' . $tab . $class . '"><a href="' . $url . '">' .
49                $this->getLang('tab_' . $tab) . '</a></li>';
50        }
51        $html .= '</ul>';
52        return $html;
53    }
54
55    /**
56     * Return the HTML for the list of installed plugins
57     *
58     * @return string
59     */
60    public function tabPlugins()
61    {
62        $html = '<div class="panelHeader">';
63        $html .= $this->helper->locale_xhtml('intro_plugins');
64        $html .= '</div>';
65
66        $plugins = (new Local())->getPlugins();
67        try {
68            // initialize remote data in one go
69            Repository::getInstance()->initExtensions(array_keys($plugins));
70        } catch (Exception $e) {
71            msg($e->getMessage(), -1); // this should not happen
72        }
73
74        $html .= '<div id="extension__list">';
75        $html .= '<form action="' . $this->tabURL('plugins') . '" method="post">';
76        $html .= '<input type="hidden" name="overwrite" value="1">';
77        $html .= formSecurityToken(false);
78        foreach ($plugins as $ext) {
79            $gui = new GuiExtension($ext);
80            $html .= $gui->render();
81        }
82        $html .= '</form>';
83        $html .= '</div>';
84
85        return $html;
86    }
87
88    /**
89     * Return the HTML for the list of installed templates
90     *
91     * @return string
92     */
93    public function tabTemplates()
94    {
95        $html = '<div class="panelHeader">';
96        $html .= $this->helper->locale_xhtml('intro_templates');
97        $html .= '</div>';
98
99        $templates = (new Local())->getTemplates();
100        try {
101            // initialize remote data in one go
102            Repository::getInstance()->initExtensions(array_keys($templates));
103        } catch (Exception $e) {
104            msg($e->getMessage(), -1); // this should not happen
105        }
106
107        $html .= '<div id="extension__list">';
108        $html .= '<form action="' . $this->tabURL('templates') . '" method="post">';
109        $html .= '<input type="hidden" name="overwrite" value="1">';
110        $html .= formSecurityToken(false);
111        foreach ($templates as $ext) {
112            $gui = new GuiExtension($ext);
113            $html .= $gui->render();
114        }
115        $html .= '</form>';
116        $html .= '</div>';
117
118        return $html;
119    }
120
121    /**
122     * Return the HTML for the search tab
123     *
124     * @return string
125     */
126    public function tabSearch()
127    {
128        global $INPUT;
129
130        $html = '<div class="panelHeader">';
131        $html .= $this->helper->locale_xhtml('intro_search');
132        $html .= '</div>';
133
134        $form = new Form([
135            'action' => $this->tabURL('search'),
136            'class' => 'search',
137        ]);
138        $form->addTagOpen('div')->addClass('no');
139        $form->addTextInput('q', $this->getLang('search_for'))
140            ->addClass('edit')
141            ->val($INPUT->str('q'));
142        $form->addButton('submit', $this->getLang('search'))
143            ->attrs(['type' => 'submit', 'title' => $this->getLang('search')]);
144        $form->addTagClose('div');
145        $html .= $form->toHTML();
146
147        if ($INPUT->str('q')) $html .= $this->searchResults($INPUT->str('q'));
148
149        return $html;
150    }
151
152    /**
153     * Return the HTML for the install tab
154     *
155     * @return string
156     */
157    public function tabInstall()
158    {
159        global $lang;
160
161        $html = '<div class="panelHeader">';
162        $html .= $this->helper->locale_xhtml('intro_install');
163        $html .= '</div>';
164
165        $form = new Form([
166            'action' => $this->tabURL('install'),
167            'enctype' => 'multipart/form-data',
168            'class' => 'install',
169        ]);
170        $form->addTagOpen('div')->addClass('no');
171        $form->addTextInput('installurl', $this->getLang('install_url'))
172            ->addClass('block')
173            ->attrs(['type' => 'url']);
174        $form->addTag('br');
175        $form->addTextInput('installfile', $this->getLang('install_upload'))
176            ->addClass('block')
177            ->attrs(['type' => 'file']);
178        $form->addTag('br');
179        $form->addCheckbox('overwrite', $lang['js']['media_overwrt'])
180            ->addClass('block');
181        $form->addTag('br');
182        $form->addButton('', $this->getLang('btn_install'))
183            ->attrs(['type' => 'submit', 'title' => $this->getLang('btn_install')]);
184        $form->addTagClose('div');
185        $html .= $form->toHTML();
186
187        return $html;
188    }
189
190    /**
191     * Execute the given search query and return the results
192     *
193     * @param string $q the query
194     * @return string
195     */
196    protected function searchResults($q)
197    {
198        $repo = Repository::getInstance();
199
200        $html = '<div id="extension__list">';
201        $html .= '<form action="' . $this->tabURL('search') . '" method="post">';
202        $html .= formSecurityToken(false);
203
204        try {
205            $extensions = $repo->searchExtensions($q);
206            $html .= '<div id="extension__results">';
207            foreach ($extensions as $ext) {
208                $gui = new GuiExtension($ext);
209                $html .= $gui->render();
210            }
211            $html .= '</div>';
212        } catch (Exception $e) {
213            msg($e->getMessage(), -1);
214        }
215
216        $html .= '</form>';
217        $html .= '</div>';
218
219        return $html;
220    }
221}
222