1<?php
2/**
3 * DokuWiki Plugin templatebyname (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Jan Van Opstal <jvanopstal@mznl.be>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class action_plugin_templatebyname_findtemplate extends DokuWiki_Action_Plugin {
13
14    public function register(Doku_Event_Handler &$controller) {
15
16       $controller->register_hook('COMMON_PAGETPL_LOAD', 'BEFORE', $this, 'handle_common_pagetpl_load');
17
18    }
19
20    public function handle_common_pagetpl_load(Doku_Event &$event, $param) {
21		global $conf;
22
23	    if(empty($event->data['tplfile'])){
24
25            $path = dirname(wikiFN($event->data['id']));
26            $len = strlen(rtrim($conf['datadir'],'/'));
27            $dir = substr($path, strrpos($path, '/')+1);
28            $blnFirst = true;
29            $blnFirstDir = true;
30            while (strLen($path) >= $len){
31                if($blnFirst == true && @file_exists($path.'/_'.noNS($event->data['id']).'.txt')){
32                    $event->data['tplfile'] = $path.'/_'.noNS($event->data['id'].'.txt');
33                    break;
34                }
35                elseif(@file_exists($path.'/__'.noNS($event->data['id']).'.txt')){
36                    $event->data['tplfile'] = $path.'/__'.noNS($event->data['id'].'.txt');
37                    break;
38                }
39                elseif($blnFirst == true && @file_exists($path.'/_template.txt')){
40                    $event->data['tplfile'] = $path.'/_template.txt';
41                    break;
42                }
43                elseif($blnFirst == false && $blnFirstDir == true && @file_exists($path.'/~_'.$dir.'.txt') && noNS($event->data['id']) == 'start'){
44                    $event->data['tplfile'] = $path.'/~_'.$dir.'.txt';
45                    break;
46                }
47                elseif($blnFirst == false && $blnFirstDir == true && @file_exists($path.'/~'.$dir.'.txt')){
48                    $event->data['tplfile'] = $path.'/~'.$dir.'.txt';
49                    break;
50                }
51                elseif($blnFirst == false && @file_exists($path.'/~~_'.$dir.'.txt') && noNS($event->data['id']) == 'start'){
52                    $event->data['tplfile'] = $path.'/~~_'.$dir.'.txt';
53                    break;
54                }
55                elseif($blnFirst == false && @file_exists($path.'/~~'.$dir.'.txt')){
56                    $event->data['tplfile'] = $path.'/~~'.$dir.'.txt';
57                    break;
58                }
59                elseif(@file_exists($path.'/__template.txt')){
60                    $event->data['tplfile'] = $path.'/__template.txt';
61                    break;
62                }
63                $path = substr($path, 0, strrpos($path, '/'));
64                if($blnFirst == false){
65                    $blnFirstDir = false;
66                }
67                $blnFirst = false;
68            }
69        }
70
71    }
72
73}
74
75// vim:ts=4:sw=4:et:
76