1<?php
2/**
3 * Sidebar Action Plugin
4 *
5 * @license    GPLv3 (http://www.gnu.org/licenses/gpl.html)
6 * @link       http://www.dokuwiki.org/plugin:sidebar
7 * @author     Markus Birth <markus@birth-online.de>
8 * @author     Christopher Smith <chris@jalakai.co.uk>
9 */
10
11if(!defined('DOKU_INC')) die();
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'action.php');
14
15class action_plugin_sidebar extends DokuWiki_Action_Plugin {
16    protected static $done = false;
17
18    /**
19     * return some info
20     */
21    function getInfo(){
22        return confToHash(dirname(__FILE__).'/INFO.txt');
23    }
24
25    /*
26     * plugin should use this method to register its handlers with the dokuwiki's event controller
27     */
28    function register(&$controller) {
29        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, '_output');
30    }
31
32    function _debug(&$event, $param) {
33        ptln($param);
34        ptln('<!--');
35        print_r($event);
36        ptln('-->');
37    }
38
39    function _output(&$event, $param) {
40        if (!$this->getConf('enable') || self::$done) return;
41        self::$done = true;   // prevent recursive calls when doing tpl_content() later on
42        $bodyClass = 'sidebar sidebar_' . $this->getConf('layout') . '_' . $this->getConf('orientation');
43        ptln('</div>', 2);   // close the main content area
44        ptln('<script type="text/javascript">', 2);
45        ptln('var body = document.getElementsByTagName(\'BODY\')[0].className = \'' . $bodyClass . '\';', 4);
46        ptln('</script>', 2);
47        ptln('<style type="text/css" media="print">', 2);
48        ptln('#sidebar { display: none; }', 4);
49        ptln('</style>', 2);
50        ptln('<div id="sidebar">', 2);
51        ptln('<div id="sidebartop">', 4); $this->tpl_sidebar_editbtn(); ptln('</div>', 4);
52        ptln('<div id="sidebar_content">', 4); $this->tpl_sidebar_content(); ptln('</div>', 4);
53        // the </div> for closing the "sidebar"-div will be provided by DokuWiki main template
54    }
55
56    // recursive function to establish best sidebar file to be used
57    function getSidebarFN($ns, $file) {
58        // check for wiki page = $ns:$file (or $file where no namespace)
59        $nsFile = ($ns) ? "$ns:$file" : $file;
60        if (file_exists(wikiFN($nsFile)) && auth_quickaclcheck($nsFile)) return $nsFile;
61
62        // no namespace left, exit with no file found
63        if (!$ns) return '';
64
65        // remove deepest namespace level and call function recursively
66        $i = strrpos($ns, ":");
67        $ns = ($i) ? substr($ns, 0, $i) : false;
68        return $this->getSidebarFN($ns, $file);
69    }
70
71    // print a sidebar edit button - if appropriate
72    function tpl_sidebar_editbtn() {
73        global $ID;
74
75        // check sidebar configuration
76        if (!$this->getConf('showeditbtn') || !$this->getConf('page')) return;
77
78        // check sidebar page exists
79        $fileSidebar = $this->getSidebarFN(getNS($ID), $this->getConf('page'));
80        if (!$fileSidebar) return;
81
82        // check user has edit permission for the sidebar page
83        if (auth_quickaclcheck($fileSidebar) < AUTH_EDIT) return;
84
85        ptln('<div class="secedit">', 6);
86        ptln('<form class="button" method="post" action="' . wl($fileSidebar, 'do=edit') . '" onsubmit="return svchk()">', 8);
87        ptln('<input type="hidden" name="do" value="edit" />', 10);
88        ptln('<input type="hidden" name="rev" value="" />', 10);
89        ptln('<input type="hidden" name="id" value="' . $fileSidebar . '" />', 10);
90        ptln('<input type="submit" value="' . $this->getConf('editbtntxt') . '" class="button" />', 10);
91        ptln('</form>', 8);
92        ptln('</div>', 6);
93    }
94
95    // display the sidebar
96    function tpl_sidebar_content() {
97        global $ID, $REV, $ACT, $conf;
98
99        // save globals
100        $saveID = $ID;
101        $saveREV = $REV;
102        $saveACT = $ACT;
103
104        // discover file to be displayed in navigation sidebar
105        $fileSidebar = '';
106
107        if ($this->getConf('page')) {
108                $fileSidebar = $this->getSidebarFN(getNS($ID), $this->getConf('page'));
109        }
110
111        // determine what to display
112        if ($fileSidebar) {
113            $ID = $fileSidebar;
114            $REV = '';
115            $ACT = 'show';
116            // ptln(p_wiki_xhtml($ID, $REV, false));
117            tpl_content();
118        } else {
119#            global $IDX;
120#            html_index($IDX);
121#            $ID = getNS($ID);
122            $REV = '';
123            $ACT = 'index';
124            tpl_content();
125        }
126
127        // restore globals
128        $ID = $saveID;
129        $REV = $saveREV;
130        $ACT = $saveACT;
131    }
132}