xref: /plugin/struct/renderer/csv.php (revision 3889beb291634325b9f12489c9da379fa7c5164d)
1eafc109fSAndreas Gohr<?php
2eafc109fSAndreas Gohr
3eafc109fSAndreas Gohr/**
4b22abfe9SAndreas Gohr * CSV export of tabular data
5eafc109fSAndreas Gohr *
6b22abfe9SAndreas Gohr * @link https://tools.ietf.org/html/rfc4180
7b22abfe9SAndreas 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;
49b22abfe9SAndreas Gohr        $filename = noNS($ID) . '.csv';
50eafc109fSAndreas Gohr        $headers = array(
51b22abfe9SAndreas 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    /**
68b22abfe9SAndreas 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) {
77b22abfe9SAndreas Gohr            $this->doc .= ",";
78eafc109fSAndreas Gohr        }
79eafc109fSAndreas Gohr        $this->first = false;
80b22abfe9SAndreas Gohr
81b22abfe9SAndreas Gohr        $this->doc .= '"';
82b22abfe9SAndreas Gohr    }
83b22abfe9SAndreas Gohr
84b22abfe9SAndreas Gohr    /**
85b22abfe9SAndreas Gohr     * Close the text wrapper
86b22abfe9SAndreas Gohr     */
87b22abfe9SAndreas Gohr    function tablecell_close() {
88*3889beb2SAndreas Gohr        if(!$this->_doOutput()) return;
89b22abfe9SAndreas Gohr        $this->doc .= '"';
90eafc109fSAndreas Gohr    }
91eafc109fSAndreas Gohr
92eafc109fSAndreas Gohr    /**
93eafc109fSAndreas Gohr     * Alias for tablecell_open
94eafc109fSAndreas Gohr     *
95eafc109fSAndreas Gohr     * @param int $colspan ignored
96eafc109fSAndreas Gohr     * @param null $align ignored
97eafc109fSAndreas Gohr     * @param int $rowspan ignored
98eafc109fSAndreas Gohr     */
99eafc109fSAndreas Gohr    function tableheader_open($colspan = 1, $align = null, $rowspan = 1) {
100eafc109fSAndreas Gohr        $this->tablecell_open($colspan, $align, $rowspan);
101eafc109fSAndreas Gohr    }
102eafc109fSAndreas Gohr
103eafc109fSAndreas Gohr    /**
104b22abfe9SAndreas Gohr     * Alias for tablecell_close
105b22abfe9SAndreas Gohr     */
106b22abfe9SAndreas Gohr    function tableheader_close() {
107b22abfe9SAndreas Gohr        $this->tablecell_close();
108b22abfe9SAndreas Gohr    }
109b22abfe9SAndreas Gohr
110b22abfe9SAndreas Gohr    /**
111b22abfe9SAndreas Gohr     * Add CRLF newline at the end of one line
112eafc109fSAndreas Gohr     */
113eafc109fSAndreas Gohr    function tablerow_close() {
114eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
115b22abfe9SAndreas Gohr        $this->doc .= "\r\n";
116eafc109fSAndreas Gohr    }
117eafc109fSAndreas Gohr
118eafc109fSAndreas Gohr    /**
119eafc109fSAndreas Gohr     * Outputs cell content
120eafc109fSAndreas Gohr     *
121eafc109fSAndreas Gohr     * @param string $text
122eafc109fSAndreas Gohr     */
123eafc109fSAndreas Gohr    function cdata($text) {
124eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
125b22abfe9SAndreas Gohr        if($text === '') return;
126b22abfe9SAndreas Gohr
127b22abfe9SAndreas Gohr        $this->doc .= str_replace('"', '""', $text);
128eafc109fSAndreas Gohr    }
129eafc109fSAndreas Gohr
130b22abfe9SAndreas Gohr    /**
131b22abfe9SAndreas Gohr     * Uses cdata to output the title
132b22abfe9SAndreas Gohr     *
133b22abfe9SAndreas Gohr     * @param string $link
134b22abfe9SAndreas Gohr     * @param null $title
135b22abfe9SAndreas Gohr     */
136eafc109fSAndreas Gohr    function internallink($link, $title = null) {
137eafc109fSAndreas Gohr        $this->cdata($title);
138eafc109fSAndreas Gohr    }
139eafc109fSAndreas Gohr
14049fe301bSAndreas Gohr    /**
14149fe301bSAndreas Gohr     * Uses cdata to output the email address
14249fe301bSAndreas Gohr     *
14349fe301bSAndreas Gohr     * @param string $address
14449fe301bSAndreas Gohr     * @param null $name
14549fe301bSAndreas Gohr     */
14649fe301bSAndreas Gohr    function emaillink($address, $name = null) {
14749fe301bSAndreas Gohr        $this->cdata($address);
14849fe301bSAndreas Gohr    }
149eafc109fSAndreas Gohr}
150