1<?php
2
3namespace dokuwiki\plugin\structodt\meta;
4
5use dokuwiki\plugin\struct\meta\SearchConfig;
6use dokuwiki\plugin\struct\meta\AggregationTable;
7
8class AggregationTableOdt extends AggregationTable {
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    /**
17     * @var \helper_plugin_structodt
18     */
19    protected $helper_structodt;
20
21    /**
22     * Initialize the Aggregation renderer and executes the search
23     *
24     * You need to call @see render() on the resulting object.
25     *
26     * @param string $id
27     * @param string $mode
28     * @param \Doku_Renderer $renderer
29     * @param SearchConfig $searchConfig
30     */
31    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig) {
32        parent::__construct($id, $mode, $renderer, $searchConfig);
33        $conf = $searchConfig->getConf();
34        $this->template = $conf['template'];
35        $this->pdf = $conf['pdf'];
36        $this->helper_structodt = plugin_load('helper', 'structodt');
37    }
38
39    /**
40     * Adds additional info to document and renderer in XHTML mode
41     *
42     * @see finishScope()
43     */
44    protected function startScope()
45    {
46        // unique identifier for this aggregation
47        $this->renderer->info['struct_table_hash'] = md5(var_export($this->data, true));
48
49        if ($this->mode != 'xhtml') return;
50
51        $table = $this->columns[0]->getTable();
52
53        $filetype = $this->pdf ? 'pdf' : 'odt';
54
55        // wrapping div
56        $this->renderer->doc .= '<div class="structaggregation structodt" data-schema="' . $table . '"
57                                    data-template="' . $this->template . '" data-filetype="' . $filetype . '">';
58    }
59
60    /**
61     * Adds PDF export controls
62     */
63    protected function renderExportControls() {
64        global $ID;
65
66        parent::renderExportControls();
67
68        if($this->mode != 'xhtml') return;
69        if(!$this->data['pdf']) return;
70        if(!$this->resultCount) return;
71
72        // FIXME apply dynamic filters
73        $urlParameters = array(
74            'do' => 'structodt',
75            'action' => 'renderAll',
76            'template_string' => $this->template
77        );
78
79        foreach($this->data['schemas'] as $key => $schema) {
80            $urlParameters['schema[' . $key . '][0]'] = $schema[0];
81            $urlParameters['schema[' . $key . '][1]'] = $schema[1];
82        }
83
84        foreach($this->data['filter'] as $i => $filter) {
85            foreach ($filter as $j => $value) {
86                $urlParameters["filter[$i][$j]"] = $value;
87            }
88        }
89
90        $href = wl($ID, $urlParameters);
91
92        $style='';
93        if (!empty($this->data['csv'])) {
94            $style='style="margin-left: 10em;"';
95        }
96
97        $this->renderer->doc .= '<a href="' . $href . '" class="export mediafile mf_pdf" ' . $style . '>'.$this->helper_structodt->getLang('btn_downloadAll').'</a>';
98    }
99}