1<?php
2/**
3 * DokuWiki Plugin Heatmap (Syntax Component: heatmap)
4 *
5 * Render Heatmap for post counts.
6 *
7 * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
8 * @author  H.-H. PENG (Hsins) <hsinspeng@gmail.com>
9 */
10
11// must be run within Dokuwiki
12if ( !defined( 'DOKU_INC' ) ) die();
13
14/**
15 * Class syntax_plugin_heatmap
16 */
17class syntax_plugin_heatmap extends DokuWiki_Syntax_Plugin {
18
19    public function getType() {
20        return 'substition';
21    }
22
23    public function getPType() {
24        return 'block';
25    }
26
27    public function getSort() {
28        return 32;
29    }
30
31    public function connectTo( $mode ) {
32        $this->Lexer->addSpecialPattern( '<heatmap\b[^>]*>', $mode, 'plugin_heatmap' );
33    }
34
35    public function handle( $match, $state, $pos, Doku_Handler $handler ) {
36        $data = [];
37        preg_match_all('/\b(\w+)=([\w-]+)/', $match, $matches, PREG_SET_ORDER);
38        foreach ( $matches as $m ) {
39            $data[strtolower($m[1])] = $m[2];
40        }
41
42        // Set default values
43        $data['ns'] = $data['ns'] ?? '';
44        $data['year'] = $data['year'] ?? date('Y');
45        dbglog($data['ns']);
46
47        // Generate unique ID
48        $data['id'] = 'heatmap-' . uniqid();
49
50        return $data;
51    }
52
53    public function render($mode, Doku_Renderer $renderer, $data) {
54        if ( $mode === 'xhtml' ) {
55            $helper = plugin_load( 'helper', 'heatmap' );
56
57            $pages = $helper->search_pages( $data['ns'], $data['year']);
58            $json_data = $helper->parse_data( $data['year'], $pages );
59
60            // Render the div with the unique ID
61            $renderer->doc .= '<script type="application/json" id="data-' . $data['id'] . '">' . $json_data . '</script>';
62            $renderer->doc .= '<div id="' . $data['id'] . '" style="max-width: 800px; height: 200px; margin: 0 auto;"></div>';
63        }
64        return true;
65    }
66}
67?>
68