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