3.3.2-rc1
[phpmyadmin/madhuracj.git] / libraries / export / ods.php
blob50d7393a10b41d5cb893726fa09b4699ef9c1a16
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build OpenDocument Spreadsheet dumps of tables
6 * @package phpMyAdmin-Export-ODS
7 * @version $Id$
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['ods'] = array(
18 'text' => 'strOpenDocumentSpreadsheet',
19 'extension' => 'ods',
20 'mime_type' => 'application/vnd.oasis.opendocument.spreadsheet',
21 'force_file' => true,
22 'options' => array(
23 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
24 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
25 array('type' => 'hidden', 'name' => 'data'),
27 'options_text' => 'strOptions',
29 } else {
31 $GLOBALS['ods_buffer'] = '';
32 require_once './libraries/opendocument.lib.php';
34 /**
35 * Outputs comment
37 * @param string Text of comment
39 * @return bool Whether it suceeded
41 function PMA_exportComment($text) {
42 return TRUE;
45 /**
46 * Outputs export footer
48 * @return bool Whether it suceeded
50 * @access public
52 function PMA_exportFooter() {
53 $GLOBALS['ods_buffer'] .= '</office:spreadsheet>'
54 . '</office:body>'
55 . '</office:document-content>';
56 if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.spreadsheet', $GLOBALS['ods_buffer']))) {
57 return FALSE;
59 return TRUE;
62 /**
63 * Outputs export header
65 * @return bool Whether it suceeded
67 * @access public
69 function PMA_exportHeader() {
70 $GLOBALS['ods_buffer'] .= '<?xml version="1.0" encoding="' . $GLOBALS['charset'] . '"?' . '>'
71 . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
72 . '<office:automatic-styles>'
73 . '<number:date-style style:name="N37" number:automatic-order="true">'
74 . '<number:month number:style="long"/>'
75 . '<number:text>/</number:text>'
76 . '<number:day number:style="long"/>'
77 . '<number:text>/</number:text>'
78 . '<number:year/>'
79 . '</number:date-style>'
80 . '<number:time-style style:name="N43">'
81 . '<number:hours number:style="long"/>'
82 . '<number:text>:</number:text>'
83 . '<number:minutes number:style="long"/>'
84 . '<number:text>:</number:text>'
85 . '<number:seconds number:style="long"/>'
86 . '<number:text> </number:text>'
87 . '<number:am-pm/>'
88 . '</number:time-style>'
89 . '<number:date-style style:name="N50" number:automatic-order="true" number:format-source="language">'
90 . '<number:month/>'
91 . '<number:text>/</number:text>'
92 . '<number:day/>'
93 . '<number:text>/</number:text>'
94 . '<number:year/>'
95 . '<number:text> </number:text>'
96 . '<number:hours number:style="long"/>'
97 . '<number:text>:</number:text>'
98 . '<number:minutes number:style="long"/>'
99 . '<number:text> </number:text>'
100 . '<number:am-pm/>'
101 . '</number:date-style>'
102 . '<style:style style:name="DateCell" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N37"/>'
103 . '<style:style style:name="TimeCell" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N43"/>'
104 . '<style:style style:name="DateTimeCell" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N50"/>'
105 . '</office:automatic-styles>'
106 . '<office:body>'
107 . '<office:spreadsheet>';
108 return TRUE;
112 * Outputs database header
114 * @param string Database name
116 * @return bool Whether it suceeded
118 * @access public
120 function PMA_exportDBHeader($db) {
121 return TRUE;
125 * Outputs database footer
127 * @param string Database name
129 * @return bool Whether it suceeded
131 * @access public
133 function PMA_exportDBFooter($db) {
134 return TRUE;
138 * Outputs create database database
140 * @param string Database name
142 * @return bool Whether it suceeded
144 * @access public
146 function PMA_exportDBCreate($db) {
147 return TRUE;
151 * Outputs the content of a table in CSV format
153 * @param string the database name
154 * @param string the table name
155 * @param string the end of line sequence
156 * @param string the url to go back in case of error
157 * @param string SQL query for obtaining data
159 * @return bool Whether it suceeded
161 * @access public
163 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
164 global $what;
166 // Gets the data from the database
167 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
168 $fields_cnt = PMA_DBI_num_fields($result);
169 $fields_meta = PMA_DBI_get_fields_meta($result);
170 $field_flags = array();
171 for ($j = 0; $j < $fields_cnt; $j++) {
172 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
175 $GLOBALS['ods_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '">';
177 // If required, get fields name at the first line
178 if (isset($GLOBALS[$what . '_columns'])) {
179 $GLOBALS['ods_buffer'] .= '<table:table-row>';
180 for ($i = 0; $i < $fields_cnt; $i++) {
181 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
182 . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
183 . '</table:table-cell>';
184 } // end for
185 $GLOBALS['ods_buffer'] .= '</table:table-row>';
186 } // end if
188 // Format the data
189 while ($row = PMA_DBI_fetch_row($result)) {
190 $GLOBALS['ods_buffer'] .= '<table:table-row>';
191 for ($j = 0; $j < $fields_cnt; $j++) {
192 if (!isset($row[$j]) || is_null($row[$j])) {
193 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
194 . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
195 . '</table:table-cell>';
196 // ignore BLOB
197 } elseif (stristr($field_flags[$j], 'BINARY')
198 && $fields_meta[$j]->blob) {
199 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
200 . '<text:p></text:p>'
201 . '</table:table-cell>';
202 } elseif ($fields_meta[$j]->type == "date") {
203 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="date" office:date-value="' . date("Y-m-d", strtotime($row[$j])) . '" table:style-name="DateCell">'
204 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
205 . '</table:table-cell>';
206 } elseif ($fields_meta[$j]->type == "time") {
207 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="time" office:time-value="' . date("\P\TH\Hi\Ms\S", strtotime($row[$j])) . '" table:style-name="TimeCell">'
208 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
209 . '</table:table-cell>';
210 } elseif ($fields_meta[$j]->type == "datetime") {
211 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="date" office:date-value="' . date("Y-m-d\TH:i:s", strtotime($row[$j])) . '" table:style-name="DateTimeCell">'
212 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
213 . '</table:table-cell>';
214 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
215 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
216 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
217 . '</table:table-cell>';
218 } else {
219 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
220 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
221 . '</table:table-cell>';
223 } // end for
224 $GLOBALS['ods_buffer'] .= '</table:table-row>';
225 } // end while
226 PMA_DBI_free_result($result);
228 $GLOBALS['ods_buffer'] .= '</table:table>';
230 return TRUE;