xref: /plugin/pagetemplater/action.php (revision 4d302956c319ddd7cb789df2a514e64b398fdce7)
1<?php
2
3/**
4 * Select Template Pages for your Content
5 * The templates Pages have to have the entry @@CONTENT@@
6 * the template per page can be defined using the META plugin
7 *
8 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     i-net software <tools@inetsoftware.de>
10 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
11 */
12
13// must be run within Dokuwiki
14if (!defined('DOKU_INC'))
15    die();
16
17if (!defined('DOKU_LF'))
18    define('DOKU_LF', "\n");
19if (!defined('DOKU_TAB'))
20    define('DOKU_TAB', "\t");
21if (!defined('DOKU_PLUGIN'))
22    define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
23
24require_once (DOKU_PLUGIN . 'action.php');
25require_once(DOKU_INC . 'inc/pageutils.php');
26
27class action_plugin_pagetemplater extends DokuWiki_Action_Plugin {
28
29    function getInfo(){
30        return array_merge(confToHash(dirname(__FILE__).'/info.txt'), array(
31				'name' => 'Page Templater Action Component',
32		));
33    }
34
35    /**
36     * Register the eventhandlers.
37     */
38    function register(& $controller) {
39        $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'handle_content_display', array ());
40    }
41
42    function handle_content_display(& $event, $params) {
43		global $ACT, $INFO;
44
45		if ( $ACT != 'show' )
46			return;
47
48		// are we in an avtive Namespace?
49		$namespace = $this->_getActiveNamespace();
50		if (!$namespace && empty($INFO['meta']['templater']['page'])) { return; }
51
52		// check for the template
53		$template = p_wiki_xhtml(empty ($INFO['meta']['templater']['page']) ? resolve_id($namespace, $this->getConf('templater_page')) : $INFO['meta']['templater']['page'],'',false);
54		if ( !$template ) { return; }
55
56		// set the replacements
57		$replace = $INFO['meta']['templater'];
58		unset($replace['page']);
59		$replace['content'] = $event->data;
60
61		$new = $template;
62		foreach (array_keys($replace) as $key) {
63			if ( $new != $template ) { $template = $new; }
64			if ( $key != 'content' && substr($key, 0, 1) == '!' ) {
65				$rkey = substr($key, 1);
66				$replace[$key] = p_render('xhtml', p_get_instructions($replace[$key]),$info);
67			} else { $rkey = $key; }
68			$new = str_replace('@@' . strtoupper(trim($rkey)) . '@@', $replace[$key], $template);
69		}
70
71		if ( $new != $event->data ) {
72			$event->data = $new;
73		}
74
75		return true;
76    }
77
78    function _getActiveNamespace() {
79    	global $ID;
80    	global $INFO;
81
82		if (!$INFO['exists'])
83			return false;
84
85    	$pattern = $this->getConf('excluded_pages');
86		if (strlen($pattern) > 0 && preg_match($pattern, $ID)) {
87			return false;
88		}
89
90        $namespaces = explode(',', $this->getConf('enabled_namespaces'));
91        foreach ($namespaces as $namespace) {
92			$namespace = cleanID($namespace);
93            if (trim($namespace) && (strpos($ID, $namespace . ':') === 0)) {
94                return $namespace;
95            }
96        }
97
98        return false;
99    }
100
101}
102
103//Setup VIM: ex: et ts=4 enc=utf-8 :
104