1<?php
2/**
3 * DokuWiki Plugin templatepagename (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Martin <martin@sound4.biz>
7 */
8
9// must be run within Dokuwiki
10use dokuwiki\Extension\ActionPlugin;
11use dokuwiki\Extension\Event;
12use dokuwiki\Extension\EventHandler;
13
14/**
15 * Class action_plugin_templatepagename_TemplatePageName
16 */
17class action_plugin_templatepagename_TemplatePageName extends ActionPlugin {
18
19    /**
20     * Registers a callback function for a given event
21     *
22     * @param EventHandler $controller
23     */
24    public function register(EventHandler $controller) {
25
26        $controller->register_hook('COMMON_PAGETPL_LOAD', 'BEFORE', $this, 'handleCommonPagetplLoad');
27
28    }
29
30    /**
31     * Adjust the pagetemplate names
32     *
33     * @param Event $event
34     */
35    public function handleCommonPagetplLoad(Event $event) {
36        global $conf;
37
38        // from here is it almost the same code as inc/common.php pageTemplate
39        // function (core dokuwiki) but vars name are adjusted to be used
40        // within the plugin.
41
42        $c_pagename = $this->getConf('current_pagename_tpl');
43        $i_pagename = $this->getConf('inherited_pagename_tpl');
44
45        $path = dirname(wikiFN($event->data['id']));
46
47        if(@file_exists($path . '/' . $c_pagename . '.txt')) {
48            $event->data['tplfile'] = $path . '/' . $c_pagename . '.txt';
49        } else {
50            // search upper namespaces for templates
51            $len = strlen(rtrim($conf['datadir'], '/'));
52            while(strlen($path) >= $len) {
53                if(@file_exists($path . '/' . $i_pagename . '.txt')) {
54                    $event->data['tplfile'] = $path . '/' . $i_pagename . '.txt';
55                    break;
56                }
57                $path = substr($path, 0, strrpos($path, '/'));
58            }
59        }
60    }
61
62}
63