xref: /plugin/pagetemplater/action.php (revision 264a5e4ef69cb202016f41781d6a3a4aeec051c7)
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(Doku_Event_Handler $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			$new = str_replace(urlencode('@@') . strtoupper(trim($rkey)) . urlencode('@@'), $replace[$key], $new);
70		}
71
72		if ( $new != $event->data ) {
73			$event->data = $new;
74		}
75
76		return true;
77    }
78
79    function _getActiveNamespace() {
80    	global $ID;
81    	global $INFO;
82
83		if (!$INFO['exists'])
84			return false;
85
86    	$pattern = $this->getConf('excluded_pages');
87		if (strlen($pattern) > 0 && preg_match($pattern, $ID)) {
88			return false;
89		}
90
91        $namespaces = explode(',', $this->getConf('enabled_namespaces'));
92        foreach ($namespaces as $namespace) {
93			$namespace = cleanID($namespace);
94            if (trim($namespace) && (strpos($ID, $namespace . ':') === 0)) {
95                return $namespace;
96            }
97        }
98
99        return false;
100    }
101
102}
103
104//Setup VIM: ex: et ts=4 enc=utf-8 :
105