Merge branch 'integration'
[phpmyadmin-regexreplace.git] / libraries / export / ods.php
blob28a5dfd4cb244a91934ed8013502d6c3763e7b71
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 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if (isset($plugin_list)) {
16 $plugin_list['ods'] = array(
17 'text' => __('Open Document Spreadsheet'),
18 'extension' => 'ods',
19 'mime_type' => 'application/vnd.oasis.opendocument.spreadsheet',
20 'force_file' => true,
21 'options' => array(
22 array('type' => 'begin_group', 'name' => 'general_opts'),
23 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:')),
24 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
25 array('type' => 'hidden', 'name' => 'structure_or_data'),
26 array('type' => 'end_group'),
28 'options_text' => __('Options'),
30 } else {
32 $GLOBALS['ods_buffer'] = '';
33 require_once './libraries/opendocument.lib.php';
35 /**
36 * Outputs comment
38 * @param string Text of comment
40 * @return bool Whether it suceeded
42 function PMA_exportComment($text) {
43 return TRUE;
46 /**
47 * Outputs export footer
49 * @return bool Whether it suceeded
51 * @access public
53 function PMA_exportFooter() {
54 $GLOBALS['ods_buffer'] .= '</office:spreadsheet>'
55 . '</office:body>'
56 . '</office:document-content>';
57 if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.spreadsheet', $GLOBALS['ods_buffer']))) {
58 return FALSE;
60 return TRUE;
63 /**
64 * Outputs export header
66 * @return bool Whether it suceeded
68 * @access public
70 function PMA_exportHeader() {
71 $GLOBALS['ods_buffer'] .= '<?xml version="1.0" encoding="' . $GLOBALS['charset'] . '"?' . '>'
72 . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
73 . '<office:automatic-styles>'
74 . '<number:date-style style:name="N37" number:automatic-order="true">'
75 . '<number:month number:style="long"/>'
76 . '<number:text>/</number:text>'
77 . '<number:day number:style="long"/>'
78 . '<number:text>/</number:text>'
79 . '<number:year/>'
80 . '</number:date-style>'
81 . '<number:time-style style:name="N43">'
82 . '<number:hours number:style="long"/>'
83 . '<number:text>:</number:text>'
84 . '<number:minutes number:style="long"/>'
85 . '<number:text>:</number:text>'
86 . '<number:seconds number:style="long"/>'
87 . '<number:text> </number:text>'
88 . '<number:am-pm/>'
89 . '</number:time-style>'
90 . '<number:date-style style:name="N50" number:automatic-order="true" number:format-source="language">'
91 . '<number:month/>'
92 . '<number:text>/</number:text>'
93 . '<number:day/>'
94 . '<number:text>/</number:text>'
95 . '<number:year/>'
96 . '<number:text> </number:text>'
97 . '<number:hours number:style="long"/>'
98 . '<number:text>:</number:text>'
99 . '<number:minutes number:style="long"/>'
100 . '<number:text> </number:text>'
101 . '<number:am-pm/>'
102 . '</number:date-style>'
103 . '<style:style style:name="DateCell" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N37"/>'
104 . '<style:style style:name="TimeCell" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N43"/>'
105 . '<style:style style:name="DateTimeCell" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N50"/>'
106 . '</office:automatic-styles>'
107 . '<office:body>'
108 . '<office:spreadsheet>';
109 return TRUE;
113 * Outputs database header
115 * @param string Database name
117 * @return bool Whether it suceeded
119 * @access public
121 function PMA_exportDBHeader($db) {
122 return TRUE;
126 * Outputs database footer
128 * @param string Database name
130 * @return bool Whether it suceeded
132 * @access public
134 function PMA_exportDBFooter($db) {
135 return TRUE;
139 * Outputs create database database
141 * @param string Database name
143 * @return bool Whether it suceeded
145 * @access public
147 function PMA_exportDBCreate($db) {
148 return TRUE;
152 * Outputs the content of a table in CSV format
154 * @param string the database name
155 * @param string the table name
156 * @param string the end of line sequence
157 * @param string the url to go back in case of error
158 * @param string SQL query for obtaining data
160 * @return bool Whether it suceeded
162 * @access public
164 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
165 global $what;
167 // Gets the data from the database
168 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
169 $fields_cnt = PMA_DBI_num_fields($result);
170 $fields_meta = PMA_DBI_get_fields_meta($result);
171 $field_flags = array();
172 for ($j = 0; $j < $fields_cnt; $j++) {
173 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
176 $GLOBALS['ods_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '">';
178 // If required, get fields name at the first line
179 if (isset($GLOBALS[$what . '_columns'])) {
180 $GLOBALS['ods_buffer'] .= '<table:table-row>';
181 for ($i = 0; $i < $fields_cnt; $i++) {
182 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
183 . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
184 . '</table:table-cell>';
185 } // end for
186 $GLOBALS['ods_buffer'] .= '</table:table-row>';
187 } // end if
189 // Format the data
190 while ($row = PMA_DBI_fetch_row($result)) {
191 $GLOBALS['ods_buffer'] .= '<table:table-row>';
192 for ($j = 0; $j < $fields_cnt; $j++) {
193 if (!isset($row[$j]) || is_null($row[$j])) {
194 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
195 . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
196 . '</table:table-cell>';
197 // ignore BLOB
198 } elseif (stristr($field_flags[$j], 'BINARY')
199 && $fields_meta[$j]->blob) {
200 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
201 . '<text:p></text:p>'
202 . '</table:table-cell>';
203 } elseif ($fields_meta[$j]->type == "date") {
204 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="date" office:date-value="' . date("Y-m-d", strtotime($row[$j])) . '" table:style-name="DateCell">'
205 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
206 . '</table:table-cell>';
207 } elseif ($fields_meta[$j]->type == "time") {
208 $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">'
209 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
210 . '</table:table-cell>';
211 } elseif ($fields_meta[$j]->type == "datetime") {
212 $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">'
213 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
214 . '</table:table-cell>';
215 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
216 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
217 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
218 . '</table:table-cell>';
219 } else {
220 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
221 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
222 . '</table:table-cell>';
224 } // end for
225 $GLOBALS['ods_buffer'] .= '</table:table-row>';
226 } // end while
227 PMA_DBI_free_result($result);
229 $GLOBALS['ods_buffer'] .= '</table:table>';
231 return TRUE;