summaryrefslogtreecommitdiff
path: root/action.php
blob: e9b71581d1283f4f6107c52d852e2d724b41c0cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
 * Simple Tree Navigation Plugin
 * @author     Yves Fischer <yvesf-git@xapek.org>
 */ 
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'action.php';

/**
 * search_namespaces1
 * 
 * like search_namespaces but with limited depth to 1
 */
function treenav_search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
    $opts['listdirs'] = true;
    $opts['depth'] = 1;
    return search_universal($data,$base,$file,$type,$lvl,$opts);
}

/**
 * Helper class to list Pages and Namespaces
 */
class treenav_NamespaceNode {
    public $name;
    public $level;
    public function __construct($name, $level) {
        $this->name = $name;
        $this->level = $level;
    }

    public function getPages() {
        global $conf;
        $data = array();
        search($data,$conf['datadir'], 'search_list', array(), str_replace(':','/', $this->name));
        return $data;
    }

    public function getChildren() {
        global $conf;
        $data = array();
        $childs = array();
        search($data,$conf['datadir'], 'treenav_search_namespaces', array(), str_replace(':', '/', $this->name));
        foreach ($data as $ns) {
            $childs[] = new treenav_NamespaceNode($ns['id'], $this->level+1);
        }
        return $childs;
    }
}


/**
 * DokuWiki Action Plugin. Inserts Tree Navigation in div "page" and wraps
 * actual page in div "inner__page"
 */
class action_plugin_treenav extends DokuWiki_Action_Plugin {
    function register(&$controller) {
        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this,
                                   'hook_beforePage');
        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this,
                                   'hook_afterPage');
    }


    function hook_beforePage(&$event, $param) {
        global $ID;
        global $INFO;
        global $config_cascade;
        
        $deps = p_get_metadata($id, 'treenav_deps', true);
        $cache = new cache(cleanID($ID) . $INFO['userinfo']['name'], 'plugin_treenav');

        if (count($deps)>0 && $cache->useCache($deps)) {
            $doc = $cache->retrieveCache(false);
        } else {
            $deps = array('files' => array($config_cascade['acl']['default']));
            $deps['files'] = array_merge($deps['files'], getConfigFiles('main'));

            $root = new treenav_NamespaceNode('',0);

            $renderer =& p_get_renderer('xhtml');
            $renderer->listu_open();
            $this->_print($root, $renderer, $deps);
            $renderer->listu_close();
            $doc = $renderer->doc;

            $cache->storeCache($doc);
            p_set_metadata($id, array('treenav_deps' => $deps));
        }

        echo '<div class="treenav">' . DOKU_LF . $doc . DOKU_LF . '</div>';
        echo '<div class="page__inner">' . DOKU_LF;

        tpl_flush();
    }

    function hook_afterPage(&$event, $param) {
        echo '</div>' . DOKU_LF;
    }

    function _print($node, &$renderer, &$deps) {
        global $ID;
        foreach ($node->getChildren() as $child) {
            foreach ($child->getPages() as $page) {
                if (noNS($page['id']) === 'start' &&
                    auth_quickaclcheck($page['id']) >= AUTH_READ) {
                    $renderer->listitem_open();
                    $deps['files'][] = wikiFN($page['id']); // Add page file to dependencies
                    $this->_renderPagelink($page['id'], $renderer);
                    if (strpos(getNS($ID), $child->name) === 0) {
                        // Recursive Call with Sub-Namespace that contains
                        // current Page ($ID)
                        $renderer->listu_open();
                        $this->_print($child, $renderer, $deps);
                        $renderer->listu_close();
                    }
                    $renderer->listitem_close();
                }
            }
        }

        foreach ($node->getPages() as $page) {
            if (noNS($page['id']) == 'start' && getNS($page['id']) != '')
                continue;
            if (auth_quickaclcheck($page['id']) >= AUTH_READ) {
                $renderer->listitem_open();
                $deps['files'][] = wikiFN($page['id']); // Add page file to dependencies
                $this->_renderPagelink($page['id'], $renderer);
                $renderer->listitem_close();
            }
        }
    }

    function _renderPagelink($id, &$renderer) {
        global $conf;

        $id = ':' . cleanID($id);
        $options = p_get_metadata($id, 'treenav');

        // Icon
        $iconID = $options['icon'];
        if ($iconID) {
            resolve_mediaid(getNS($id), $iconID, $exists);
        }
        if ($iconID && $exists) {
            $link = ml($iconID, array('h'=>16, 'cache'=>NULL));
        } else {
            if (noNS($id) === $conf['start'] && getNS($id) !== '') {
                $link = DOKU_REL . 'lib/plugins/treenav/images/zoom-in-symbolic.png';
            } else {
                $link = DOKU_REL . 'lib/plugins/treenav/images/go-next-symbolic.png';
            }
        }
        $renderer->doc .= '<img height="16" src="'.$link.'" class="media" />' . DOKU_LF;

        if ($options['title']) {
            $title = $options['title'];
        } else {
            $title = p_get_first_heading($id);
        }

        $renderer->internallink($id, $title);
    }
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
/* Local Variables:       */
/* tab-width: 4           */
/* indent-tabs-mode: nil  */
/* c-basic-offset: 4      */
/* End:                   */