1<?php
2/**
3 * DokuWiki Plugin jsontable (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Janez Paternoster <janez.paternoster@siol.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class helper_plugin_jsontable extends helper_plugin_json {
15
16    /**
17     * Handle extension to syntax_plugin_json_define
18     *
19     * @param array $data Reference to $data, which can be further manipulated
20     */
21    public function handle(&$data) {
22        $json_o = $this->loadHelper('json');
23
24        //options for the handsontable
25        $options = $data['keys']['options'] ?? null;
26        $options = is_string($options) ? $options : '{}';
27
28        $data['options'] = $options;
29        //replace %$ ... % snippets with data - prepare
30        $data['options_extractors'] = $json_o->extractors_handle($options);
31
32        //If 'save' attribute is set to 'all', then complete json object
33        //from handsontable will be saved into <json> element.
34        //Othervise only difference between original data and inline
35        //data will be saved (default).
36        $save = $data['keys']['save'] ?? null;
37        if(is_string($save) && strtolower($save) === 'all') {
38            $data['saveall'] = 'true';
39        }
40        else {
41            $data['saveall'] = 'false';
42            $data['display'] .= ',orig-hidden';
43        }
44
45        //make sure, necessary json data will be there
46        $data['display'] .= ',inl-hidden,comb-hidden';
47    }
48
49
50    /**
51     * Render extension to syntax_plugin_json_define
52     *
53     * For parameters see render::syntax_plugin_json_define
54     *
55     * @param array $data from handler
56     * @param array $class definitions for parent div
57     * @param array $data_attr html data attributes
58     * @param array $tabs definitions for jQuery UI tabs widget
59     * @param array $body definitions for jQuery UI tabs widget
60     * @param array $log for reporting errors
61     * @param array $tab_no must incerment for each tab
62     * @param array $tab_number for id attribute
63     */
64    public function render(Doku_Renderer $renderer, &$data, &$class, &$data_attr, &$tabs, &$body, &$log, &$tab_no, $tab_number) {
65        //replace %$ ... % snippets with data
66        if(count($data['options_extractors']) > 0) {
67            $json_o = $this->loadHelper('json');
68            $options = $json_o->extractors_replace($data['options'], $data['options_extractors']);
69        }
70        else {
71            $options = $data['options'];
72        }
73
74        $data_attr['active'] = $tab_no++;   //make this tab active
75        $data_attr['options'] = htmlspecialchars($options);
76        $data_attr['saveall'] = $data['saveall'];
77
78        $class[] = 'jsontable-plugin';
79        $tabs[] = '<li><a href="#json-tab-'.$tab_number.'-table">Table</a></li>';
80        $body[] =      '<div id="json-tab-'.$tab_number.'-table"><div class="json-table"></div></div>';
81    }
82}
83