xref: /plugin/struct/action/output.php (revision a2672b004a6b66665da7e9702be5f9d728ba9694)
1<?php
2
3/**
4 * DokuWiki Plugin struct (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10/**
11 * Class action_plugin_struct_output
12 *
13 * This action component handles the automatic output of all schema data that has been assigned
14 * to the current page by appending the appropriate instruction to the handler calls.
15 *
16 * The real output creation is done within the syntax component
17 * @see syntax_plugin_struct_output
18 */
19class action_plugin_struct_output extends DokuWiki_Action_Plugin
20{
21
22    const DW2PDF_PLACEHOLDER_PREFIX = 'PLUGIN_STRUCT';
23
24    /**
25     * Registers a callback function for a given event
26     *
27     * @param Doku_Event_Handler $controller DokuWiki's event controller object
28     * @return void
29     */
30    public function register(Doku_Event_Handler $controller)
31    {
32        $controller->register_hook('PARSER_HANDLER_DONE', 'AFTER', $this, 'handleOutput');
33        $controller->register_hook('PLUGIN_DW2PDF_REPLACE', 'BEFORE', $this, 'replaceDw2pdf');
34        $controller->register_hook('PLUGIN_DW2PDF_REPLACE', 'AFTER', $this, 'cleanupDw2pdf');
35    }
36
37    /**
38     * Appends the instruction to render our syntax output component to each page
39     * after the first found headline or the very begining if no headline was found
40     *
41     * @param Doku_Event $event
42     * @param $param
43     */
44    public function handleOutput(Doku_Event $event, $param)
45    {
46        global $ID;
47        if (!page_exists($ID)) return;
48
49        $pos = 0;
50        $ins = -1;
51
52        // display struct data at the bottom?
53        if ($this->getConf('bottomoutput')) {
54            $ins = count($event->data->calls);
55        } else if (!$this->getConf('topoutput')) {
56            foreach ($event->data->calls as $num => $call) {
57                // try to find the first header
58                if ($call[0] == 'header') {
59                    $pos = $call[2];
60                    $ins = $num;
61                    break;
62                }
63
64                // abort when after we looked at the first 150 bytes
65                if (isset($call[3]) && $call[3] > 150) {
66                    break;
67                }
68            }
69        }
70
71        // insert our own call after the found position
72        array_splice(
73            $event->data->calls,
74            $ins + 1,
75            0,
76            array(
77                array(
78                    'plugin',
79                    array(
80                        'struct_output', array('pos' => $pos), DOKU_LEXER_SPECIAL, ''
81                    ),
82                    $pos
83                )
84            )
85        );
86    }
87
88    /**
89     * If the page has a schema assigned, add its struct data
90     * to dw2pdf's template replacements
91     *
92     * @param Doku_Event $event
93     * @param $param
94     */
95    public function replaceDw2pdf(Doku_Event $event, $param)
96    {
97        if (!$event->data['id'] || !page_exists($event->data['id'])) return;
98
99        global $REV;
100        $rev = $REV ?: time();
101
102        /** @var helper_plugin_struct $helper */
103        $helper = plugin_load('helper', 'struct');
104        $data = $helper->getData($event->data['id'], null, $rev);
105
106        if (!$data) return;
107
108        foreach ($data as $schema => $fields) {
109            foreach ($fields as $field => $value) {
110                $placeholder = sprintf('@%s_%s_%s@', self::DW2PDF_PLACEHOLDER_PREFIX, $schema, $field);
111                $event->data['replace'][$placeholder] = is_array($value) ? implode(', ', $value) : $value;
112            }
113        }
114    }
115
116    /**
117     * Remove struct placeholders still present after replacement.
118     * Requested data was not found.
119     *
120     * @param Doku_Event $event
121     * @param $param
122     */
123    public function cleanupDw2pdf(Doku_Event $event, $param)
124    {
125        $pattern = '~@' . self::DW2PDF_PLACEHOLDER_PREFIX . '_[^@]+?@~';
126        $event->data['content'] = preg_replace($pattern, '', $event->data['content']);
127    }
128}
129
130// vim:ts=4:sw=4:et:
131