1<?php
2
3/**
4 * DokuWiki Plugin extension (Helper Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr <andi@splitbrain.org>
8 */
9
10use dokuwiki\Extension\Plugin;
11use dokuwiki\Form\Form;
12
13/**
14 * Class helper_plugin_extension_list takes care of the overall GUI
15 */
16class helper_plugin_extension_gui extends Plugin
17{
18    protected $tabs = ['plugins', 'templates', 'search', 'install'];
19
20    /** @var string the extension that should have an open info window FIXME currently broken */
21    protected $infoFor = '';
22
23    /**
24     * Constructor
25     *
26     * initializes requested info window
27     */
28    public function __construct()
29    {
30        global $INPUT;
31        $this->infoFor = $INPUT->str('info');
32    }
33
34    /**
35     * display the plugin tab
36     */
37    public function tabPlugins()
38    {
39        echo '<div class="panelHeader">';
40        echo $this->locale_xhtml('intro_plugins');
41        echo '</div>';
42
43        $pluginlist = plugin_list('', true);
44        /* @var helper_plugin_extension_extension $extension */
45        $extension = $this->loadHelper('extension_extension');
46        /* @var helper_plugin_extension_list $list */
47        $list = $this->loadHelper('extension_list');
48
49        $form = new Form([
50            'action' => $this->tabURL('', [], '&'),
51            'id' => 'extension__list',
52        ]);
53        $list->startForm();
54        foreach ($pluginlist as $name) {
55            $extension->setExtension($name);
56            $list->addRow($extension, $extension->getID() == $this->infoFor);
57        }
58        $list->endForm();
59        $form->addHTML($list->render(true));
60        echo $form->toHTML();
61    }
62
63    /**
64     * Display the template tab
65     */
66    public function tabTemplates()
67    {
68        echo '<div class="panelHeader">';
69        echo $this->locale_xhtml('intro_templates');
70        echo '</div>';
71
72        // FIXME do we have a real way?
73        $tpllist = glob(DOKU_INC . 'lib/tpl/*', GLOB_ONLYDIR);
74        $tpllist = array_map('basename', $tpllist);
75        sort($tpllist);
76
77        /* @var helper_plugin_extension_extension $extension */
78        $extension = $this->loadHelper('extension_extension');
79        /* @var helper_plugin_extension_list $list */
80        $list = $this->loadHelper('extension_list');
81
82        $form = new Form([
83            'action' => $this->tabURL('', [], '&'),
84            'id' => 'extension__list',
85        ]);
86        $list->startForm();
87        foreach ($tpllist as $name) {
88            $extension->setExtension("template:$name");
89            $list->addRow($extension, $extension->getID() == $this->infoFor);
90        }
91        $list->endForm();
92        $form->addHTML($list->render(true));
93        echo $form->toHTML();
94    }
95
96    /**
97     * Display the search tab
98     */
99    public function tabSearch()
100    {
101        global $INPUT;
102        echo '<div class="panelHeader">';
103        echo $this->locale_xhtml('intro_search');
104        echo '</div>';
105
106        $form = new Form([
107            'action' => $this->tabURL('', [], '&'),
108            'class' => 'search',
109        ]);
110        $form->addTagOpen('div')->addClass('no');
111        $form->addTextInput('q', $this->getLang('search_for'))
112            ->addClass('edit')
113            ->val($INPUT->str('q'));
114        $form->addButton('submit', $this->getLang('search'))
115            ->attrs(['type' => 'submit', 'title' => $this->getLang('search')]);
116        $form->addTagClose('div');
117        echo $form->toHTML();
118
119        if (!$INPUT->bool('q')) return;
120
121        /* @var helper_plugin_extension_repository $repository FIXME should we use some gloabl instance? */
122        $repository = $this->loadHelper('extension_repository');
123        $result = $repository->search($INPUT->str('q'));
124
125        /* @var helper_plugin_extension_extension $extension */
126        $extension = $this->loadHelper('extension_extension');
127        /* @var helper_plugin_extension_list $list */
128        $list = $this->loadHelper('extension_list');
129
130        $form = new Form([
131            'action' => $this->tabURL('', [], '&'),
132            'id' => 'extension__list',
133        ]);
134        $list->startForm();
135        if ($result) {
136            foreach ($result as $name) {
137                $extension->setExtension($name);
138                $list->addRow($extension, $extension->getID() == $this->infoFor);
139            }
140        } else {
141            $list->nothingFound();
142        }
143        $list->endForm();
144        $form->addHTML($list->render(true));
145        echo $form->toHTML();
146    }
147
148    /**
149     * Display the template tab
150     */
151    public function tabInstall()
152    {
153        global $lang;
154        echo '<div class="panelHeader">';
155        echo $this->locale_xhtml('intro_install');
156        echo '</div>';
157
158        $form = new Form([
159            'action' => $this->tabURL('', [], '&'),
160            'enctype' => 'multipart/form-data',
161            'class' => 'install',
162        ]);
163        $form->addTagOpen('div')->addClass('no');
164        $form->addTextInput('installurl', $this->getLang('install_url'))
165            ->addClass('block')
166            ->attrs(['type' => 'url']);
167        $form->addTag('br');
168        $form->addTextInput('installfile', $this->getLang('install_upload'))
169            ->addClass('block')
170            ->attrs(['type' => 'file']);
171        $form->addTag('br');
172        $form->addCheckbox('overwrite', $lang['js']['media_overwrt'])
173            ->addClass('block');
174        $form->addTag('br');
175        $form->addButton('', $this->getLang('btn_install'))
176            ->attrs(['type' => 'submit', 'title' => $this->getLang('btn_install')]);
177        $form->addTagClose('div');
178        echo $form->toHTML();
179    }
180
181    /**
182     * Print the tab navigation
183     *
184     * @fixme style active one
185     */
186    public function tabNavigation()
187    {
188        echo '<ul class="tabs">';
189        foreach ($this->tabs as $tab) {
190            $url = $this->tabURL($tab);
191            if ($this->currentTab() == $tab) {
192                $class = ' active';
193            } else {
194                $class = '';
195            }
196            echo '<li class="' . $tab . $class . '"><a href="' . $url . '">' .
197                $this->getLang('tab_' . $tab) . '</a></li>';
198        }
199        echo '</ul>';
200    }
201
202    /**
203     * Return the currently selected tab
204     *
205     * @return string
206     */
207    public function currentTab()
208    {
209        global $INPUT;
210
211        $tab = $INPUT->str('tab', 'plugins', true);
212        if (!in_array($tab, $this->tabs)) $tab = 'plugins';
213        return $tab;
214    }
215
216    /**
217     * Create an URL inside the extension manager
218     *
219     * @param string $tab tab to load, empty for current tab
220     * @param array $params associative array of parameter to set
221     * @param string $sep seperator to build the URL
222     * @param bool $absolute create absolute URLs?
223     * @return string
224     */
225    public function tabURL($tab = '', $params = [], $sep = '&', $absolute = false)
226    {
227        global $ID;
228        global $INPUT;
229
230        if (!$tab) $tab = $this->currentTab();
231        $defaults = [
232            'do' => 'admin',
233            'page' => 'extension',
234            'tab' => $tab
235        ];
236        if ($tab == 'search') $defaults['q'] = $INPUT->str('q');
237
238        return wl($ID, array_merge($defaults, $params), $absolute, $sep);
239    }
240}
241