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