1<?php
2/**
3 * Fields Plugin: Re-usable user fields
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Aurelien Bompard <aurelien@bompard.org>, LarsDW223
7 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14class syntax_plugin_fields extends DokuWiki_Syntax_Plugin {
15    /**
16     * Constructor. Loads helper plugin.
17     */
18    public function __construct() {
19        $this->helper = $this->loadHelper('fields');
20    }
21
22    /**
23     * What kind of syntax are we?
24     */
25    function getType(){
26        return 'substition';
27    }
28
29    /**
30     * Where to sort in?
31     */
32    function getSort(){
33        return 319; // Before image detection, which uses {{...}} and is 320
34    }
35
36
37    /**
38     * Connect pattern to lexer
39     */
40    function connectTo($mode) {
41        $this->Lexer->addSpecialPattern('{{fields>.+?}}',$mode,'plugin_fields');
42    }
43
44    /**
45     * Handle the match
46     */
47    public function handle($match, $state, $pos, Doku_Handler $handler) {
48        $match = substr($match,9,-2); //strip markup
49        $extinfo = explode('=',$match);
50        $field_name = $extinfo[0];
51        if (count($extinfo) < 2) { // no value
52            $field_value = '';
53        } elseif (count($extinfo) == 2) {
54            $field_value = $extinfo[1];
55        } else { // value may contain equal signs
56            $field_value = implode(array_slice($extinfo,1), '=');
57        }
58        return array($field_name, $field_value);
59    }
60
61    /**
62     * Create output
63     */
64    public function render($format, Doku_Renderer $renderer, $data) {
65        global $ID;
66        list($field_name, $field_value) = $data;
67        if ($field_value == '') { // no value -> get the field
68            if ($format == 'xhtml' && isset($renderer->fields)
69                    && array_key_exists($field_name, $renderer->fields)) {
70                $renderer->doc .= $renderer->fields[$field_name];
71                return true;
72            } elseif ($format == 'odt') {
73                $renderer->doc .= $this->helper->ODTDisplayUserField($renderer, $field_name);
74                return true;
75            }
76        } else {
77            // set field
78            if ($format == 'xhtml') {
79                if (!isset($renderer->fields)) {
80                    $renderer->fields = array();
81                }
82                $renderer->fields[$field_name] = htmlentities($field_value);
83                return true;
84            } elseif ($format == 'odt') {
85                $this->helper->ODTSetUserField($renderer, $field_name,
86                    $renderer->_xmlEntities($field_value));
87                return true;
88            }
89        }
90        return false;
91    }
92
93    function _fieldsODTFilterUserFieldName($name) {
94        // keep only allowed chars in the name
95        return preg_replace('/[^a-zA-Z0-9_.]/', '', $name);
96    }
97}
98
99//Setup VIM: ex: et ts=4 fileencoding=utf-8 :
100