1<?php
2/**
3 * DokuWiki Helper Plugin LoadSkin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Anika Henke <anika@selfthinker.org>
7 * @author     Michael Klier <chi@chimeric.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13class helper_plugin_loadskin extends DokuWiki_Plugin {
14
15    public $origTpl;
16
17    /**
18     * Returns an array of available templates to choose from
19     *
20     * @author Michael Klier <chi@chimeric.de>
21     */
22    public function getTemplates() {
23        $tpl_dir = DOKU_INC.'lib/tpl/';
24        if ($dh = @opendir($tpl_dir)) {
25            while (false !== ($entry = readdir($dh))) {
26                if ($entry == '.' || $entry == '..') continue;
27                if (!preg_match('/^[\w-]+$/', $entry)) continue;
28
29                $file = (is_link($tpl_dir.$entry)) ? readlink($tpl_dir.$entry) : $entry;
30                if (is_dir($tpl_dir.$file)) $list[] = $entry;
31            }
32            closedir($dh);
33            sort($list);
34        }
35        return $list;
36    }
37
38
39    /**
40     * Builds a select box with all available templates
41     *  (unless excluded in 'excludeTemplates')
42     *  or show only two templates for mobile switcher: standard plus mobile template
43     *
44     * @author Anika Henke <anika@selfthinker.org>
45     */
46    public function showTemplateSwitcher() {
47        global $conf;
48        global $ID;
49        global $ACT;
50
51        if ($ACT != 'show') return;
52
53        $mobileSwitch = $this->getConf('mobileSwitch');
54        $mobileTpl = $this->getConf('mobileTemplate');
55        if ($mobileSwitch && $mobileTpl) {
56            // templates for mobile switcher
57            $templates = array(
58                $mobileTpl     => $this->getLang('switchMobile'),
59                $this->origTpl => $this->getLang('switchFull')
60            );
61        } else {
62            // all templates (minus excluded templates)
63            $excludeTemplates = array_map('trim', explode(",", $this->getConf('excludeTemplates')));
64            $templates        = array_diff($this->getTemplates(),$excludeTemplates);
65        }
66
67        $form = new Doku_Form(array(
68            'id' => 'tpl__switcher',
69            'title' => $this->getLang('switchTpl'),
70            'action' => wl($ID)
71        ));
72        $form->addHidden('act','select');
73        $form->addElement(form_makeListboxField(
74            'tpl',
75            $templates,
76            $conf['template'],
77            $this->getLang('template'),
78            '',
79            '',
80            array('class' => 'quickselect')
81        ));
82        $form->addElement(form_makeButton(
83            'submit',
84            '',
85            $this->getLang('switch'),
86            array('name' => 'switch')
87        ));
88
89        $out  = '<div class="plugin_loadskin">';
90        $out .= $form->getForm();
91        $out .= '</div>';
92
93        return $out;
94    }
95
96}
97