xref: /plugin/struct/renderer/csv.php (revision b22abfe9262a7f884fa8c3f24095863ade84cc13)
1eafc109fSAndreas Gohr<?php
2eafc109fSAndreas Gohr
3eafc109fSAndreas Gohr/**
4*b22abfe9SAndreas Gohr * CSV export of tabular data
5eafc109fSAndreas Gohr *
6*b22abfe9SAndreas Gohr * @link https://tools.ietf.org/html/rfc4180
7*b22abfe9SAndreas Gohr * @link http://csvlint.io/
8eafc109fSAndreas Gohr */
9eafc109fSAndreas Gohrclass renderer_plugin_struct_csv extends Doku_Renderer {
10eafc109fSAndreas Gohr
11eafc109fSAndreas Gohr    protected $first = false;
12eafc109fSAndreas Gohr
13eafc109fSAndreas Gohr    /**
14eafc109fSAndreas Gohr     * Determine if out put is wanted right now
15eafc109fSAndreas Gohr     *
16eafc109fSAndreas Gohr     * @return bool
17eafc109fSAndreas Gohr     */
18eafc109fSAndreas Gohr    function _doOutput() {
19eafc109fSAndreas Gohr        global $INPUT;
20eafc109fSAndreas Gohr
21eafc109fSAndreas Gohr        if(
22eafc109fSAndreas Gohr            !isset($this->info['struct_table_hash']) or
23eafc109fSAndreas Gohr            $this->info['struct_table_hash'] != $INPUT->str('hash')
24eafc109fSAndreas Gohr        ) {
25eafc109fSAndreas Gohr            return false;
26eafc109fSAndreas Gohr        }
27eafc109fSAndreas Gohr
28eafc109fSAndreas Gohr        if(!empty($this->info['struct_table_meta'])) {
29eafc109fSAndreas Gohr            return false;
30eafc109fSAndreas Gohr        }
31eafc109fSAndreas Gohr
32eafc109fSAndreas Gohr        return true;
33eafc109fSAndreas Gohr    }
34eafc109fSAndreas Gohr
35eafc109fSAndreas Gohr    /**
36eafc109fSAndreas Gohr     * Our own format
37eafc109fSAndreas Gohr     *
38eafc109fSAndreas Gohr     * @return string
39eafc109fSAndreas Gohr     */
40eafc109fSAndreas Gohr    function getFormat() {
41eafc109fSAndreas Gohr        return 'struct_csv';
42eafc109fSAndreas Gohr    }
43eafc109fSAndreas Gohr
44eafc109fSAndreas Gohr    /**
45eafc109fSAndreas Gohr     * Set proper headers
46eafc109fSAndreas Gohr     */
47eafc109fSAndreas Gohr    function document_start() {
48eafc109fSAndreas Gohr        global $ID;
49*b22abfe9SAndreas Gohr        $filename = noNS($ID) . '.csv';
50eafc109fSAndreas Gohr        $headers = array(
51*b22abfe9SAndreas Gohr            'Content-Type' => 'text/csv',
52eafc109fSAndreas Gohr            'Content-Disposition' => 'attachment; filename="' . $filename . '";'
53eafc109fSAndreas Gohr        );
54eafc109fSAndreas Gohr        p_set_metadata($ID, array('format' => array('struct' => $headers)));
55eafc109fSAndreas Gohr        // don't cache
56eafc109fSAndreas Gohr        $this->nocache();
57eafc109fSAndreas Gohr    }
58eafc109fSAndreas Gohr
59eafc109fSAndreas Gohr    /**
60eafc109fSAndreas Gohr     * Opening a table row prevents the separator for the first following cell
61eafc109fSAndreas Gohr     */
62eafc109fSAndreas Gohr    function tablerow_open() {
63eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
64eafc109fSAndreas Gohr        $this->first = true;
65eafc109fSAndreas Gohr    }
66eafc109fSAndreas Gohr
67eafc109fSAndreas Gohr    /**
68*b22abfe9SAndreas Gohr     * Output the delimiter (unless it's the first cell of this row) and the text wrapper
69eafc109fSAndreas Gohr     *
70eafc109fSAndreas Gohr     * @param int $colspan ignored
71eafc109fSAndreas Gohr     * @param null $align ignored
72eafc109fSAndreas Gohr     * @param int $rowspan ignored
73eafc109fSAndreas Gohr     */
74eafc109fSAndreas Gohr    function tablecell_open($colspan = 1, $align = null, $rowspan = 1) {
75eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
76eafc109fSAndreas Gohr        if(!$this->first) {
77*b22abfe9SAndreas Gohr            $this->doc .= ",";
78eafc109fSAndreas Gohr        }
79eafc109fSAndreas Gohr        $this->first = false;
80*b22abfe9SAndreas Gohr
81*b22abfe9SAndreas Gohr        $this->doc .= '"';
82*b22abfe9SAndreas Gohr    }
83*b22abfe9SAndreas Gohr
84*b22abfe9SAndreas Gohr    /**
85*b22abfe9SAndreas Gohr     * Close the text wrapper
86*b22abfe9SAndreas Gohr     */
87*b22abfe9SAndreas Gohr    function tablecell_close() {
88*b22abfe9SAndreas Gohr        $this->doc .= '"';
89eafc109fSAndreas Gohr    }
90eafc109fSAndreas Gohr
91eafc109fSAndreas Gohr    /**
92eafc109fSAndreas Gohr     * Alias for tablecell_open
93eafc109fSAndreas Gohr     *
94eafc109fSAndreas Gohr     * @param int $colspan ignored
95eafc109fSAndreas Gohr     * @param null $align ignored
96eafc109fSAndreas Gohr     * @param int $rowspan ignored
97eafc109fSAndreas Gohr     */
98eafc109fSAndreas Gohr    function tableheader_open($colspan = 1, $align = null, $rowspan = 1) {
99eafc109fSAndreas Gohr        $this->tablecell_open($colspan, $align, $rowspan);
100eafc109fSAndreas Gohr    }
101eafc109fSAndreas Gohr
102eafc109fSAndreas Gohr    /**
103*b22abfe9SAndreas Gohr     * Alias for tablecell_close
104*b22abfe9SAndreas Gohr     */
105*b22abfe9SAndreas Gohr    function tableheader_close() {
106*b22abfe9SAndreas Gohr        $this->tablecell_close();
107*b22abfe9SAndreas Gohr    }
108*b22abfe9SAndreas Gohr
109*b22abfe9SAndreas Gohr    /**
110*b22abfe9SAndreas Gohr     * Add CRLF newline at the end of one line
111eafc109fSAndreas Gohr     */
112eafc109fSAndreas Gohr    function tablerow_close() {
113eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
114*b22abfe9SAndreas Gohr        $this->doc .= "\r\n";
115eafc109fSAndreas Gohr    }
116eafc109fSAndreas Gohr
117eafc109fSAndreas Gohr    /**
118eafc109fSAndreas Gohr     * Outputs cell content
119eafc109fSAndreas Gohr     *
120eafc109fSAndreas Gohr     * @param string $text
121eafc109fSAndreas Gohr     */
122eafc109fSAndreas Gohr    function cdata($text) {
123eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
124*b22abfe9SAndreas Gohr        if($text === '') return;
125*b22abfe9SAndreas Gohr
126*b22abfe9SAndreas Gohr        $this->doc .= str_replace('"', '""', $text);
127eafc109fSAndreas Gohr    }
128eafc109fSAndreas Gohr
129*b22abfe9SAndreas Gohr    /**
130*b22abfe9SAndreas Gohr     * Uses cdata to output the title
131*b22abfe9SAndreas Gohr     *
132*b22abfe9SAndreas Gohr     * @param string $link
133*b22abfe9SAndreas Gohr     * @param null $title
134*b22abfe9SAndreas Gohr     */
135eafc109fSAndreas Gohr    function internallink($link, $title = null) {
136eafc109fSAndreas Gohr        $this->cdata($title);
137eafc109fSAndreas Gohr    }
138eafc109fSAndreas Gohr
139eafc109fSAndreas Gohr}
140