1<?php
2
3namespace dokuwiki\plugin\structodt\meta;
4
5use dokuwiki\plugin\struct\meta\SearchConfig;
6use dokuwiki\plugin\struct\meta\AggregationEditorTable;
7
8class AggregationEditorTableOdt extends AggregationEditorTable {
9
10    /** @var  string odt file used as export template */
11    protected $template;
12
13    /** @var bool download rendered files as PDFs */
14    protected $pdf;
15
16    /** @var bool hide the form for global and local structs */
17    protected $hideform;
18
19    /**
20     * @var \helper_plugin_structodt
21     */
22    protected $helper_structodt;
23
24    /**
25     * Initialize the Aggregation renderer and executes the search
26     *
27     * You need to call @see render() on the resulting object.
28     *
29     * @param string $id
30     * @param string $mode
31     * @param \Doku_Renderer $renderer
32     * @param SearchConfig $searchConfig
33     */
34    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig) {
35        parent::__construct($id, $mode, $renderer, $searchConfig);
36        $conf = $searchConfig->getConf();
37        $this->template = $conf['template'];
38        $this->pdf = $conf['pdf'] ?? false;
39        $this->hideform = $conf['hideform'] ?? false;
40        $this->filename = $conf['filename'] ?? '';
41        $this->helper_structodt = plugin_load('helper', 'structodt');
42    }
43
44    /**
45     * Adds additional info to document and renderer in XHTML mode
46     *
47     * We add the schema name as data attribute
48     *
49     * @see finishScope()
50     */
51    public function startScope()
52    {
53        // unique identifier for this aggregation
54        $this->renderer->info['struct_table_hash'] = md5(var_export($this->data, true));
55
56        if ($this->mode != 'xhtml') return;
57
58        $table = hsc($this->columns[0]->getTable());
59
60        $config = $this->searchConfig->getConf();
61        if (isset($config['filter'])) unset($config['filter']);
62        $config = hsc(json_encode($config));
63
64        $filetype = $this->pdf ? 'pdf' : 'odt';
65        $template = hsc(json_encode($this->template));
66
67        $hideform = '';
68        if ($this->hideform) {
69            $hideform = 'hideform';
70        }
71
72        $filename = hsc($this->filename);
73        // wrapping div
74        $this->renderer->doc .= "<div class=\"structaggregation structaggregationeditor structodt $hideform\"
75                                    data-schema=\"$table\" data-searchconf=\"$config\"
76                                    data-template=\"$template\" data-filetype=\"$filetype\"
77                                    data-filename=\"$filename\"'>";
78
79        // unique identifier for this aggregation
80        $this->renderer->info['struct_table_hash'] = md5(var_export($this->data, true));
81    }
82
83    /**
84     * Adds PDF export controls
85     */
86    protected function renderExportControls() {
87        global $ID;
88
89        parent::renderExportControls();
90
91        if($this->mode != 'xhtml') return;
92        if(!$this->resultCount) return;
93
94        // FIXME apply dynamic filters
95        $urlParameters = array(
96            'do' => 'structodt',
97            'action' => 'renderAll',
98            'template_string' => hsc(json_encode($this->template)),
99            'filename' => $this->filename
100        );
101
102        foreach($this->data['schemas'] as $key => $schema) {
103            $urlParameters['schema[' . $key . '][0]'] = $schema[0];
104            $urlParameters['schema[' . $key . '][1]'] = $schema[1];
105        }
106
107        foreach($this->data['filter'] as $i => $filter) {
108            foreach ($filter as $j => $value) {
109                $urlParameters["filter[$i][$j]"] = $value;
110            }
111        }
112
113
114
115        $margin=0;
116        if (!empty($this->data['csv'])) {
117            $margin = 10;
118        }
119
120        $href = wl($ID, $urlParameters);
121        $this->renderer->doc .= '<a href="' . $href . '" class="export mediafile mf_zip" style="margin-left: ' . $margin . 'em;">' .
122            $this->helper_structodt->getLang('btn_downloadAll') .
123        '</a>';
124
125        if ($this->pdf) {
126            $urlParameters['format'] = 'pdf';
127            $href = wl($ID, $urlParameters);
128            $this->renderer->doc .= '<a href="' . $href . '" class="export mediafile mf_pdf" style="margin-left: ' . $margin + 11 . 'em;">' .
129                $this->helper_structodt->getLang('btn_downloadAll') .
130                '</a>';
131        }
132    }
133}