2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build OpenDocument Spreadsheet dumps of tables
6 * @package phpMyAdmin-Export-ODS
8 if (! defined('PHPMYADMIN')) {
15 if (isset($plugin_list)) {
16 $plugin_list['ods'] = array(
17 'text' => __('Open Document Spreadsheet'),
19 'mime_type' => 'application/vnd.oasis.opendocument.spreadsheet',
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'),
32 $GLOBALS['ods_buffer'] = '';
33 require_once './libraries/opendocument.lib.php';
38 * @param string Text of comment
40 * @return bool Whether it suceeded
42 function PMA_exportComment($text) {
47 * Outputs export footer
49 * @return bool Whether it suceeded
53 function PMA_exportFooter() {
54 $GLOBALS['ods_buffer'] .= '</office:spreadsheet>'
56 . '</office:document-content>';
57 if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.spreadsheet', $GLOBALS['ods_buffer']))) {
64 * Outputs export header
66 * @return bool Whether it suceeded
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>'
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>'
89 . '</number:time-style>'
90 . '<number:date-style style:name="N50" number:automatic-order="true" number:format-source="language">'
92 . '<number:text>/</number:text>'
94 . '<number:text>/</number:text>'
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>'
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>'
108 . '<office:spreadsheet>';
113 * Outputs database header
115 * @param string Database name
117 * @return bool Whether it suceeded
121 function PMA_exportDBHeader($db) {
126 * Outputs database footer
128 * @param string Database name
130 * @return bool Whether it suceeded
134 function PMA_exportDBFooter($db) {
139 * Outputs create database database
141 * @param string Database name
143 * @return bool Whether it suceeded
147 function PMA_exportDBCreate($db) {
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
164 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
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>';
186 $GLOBALS['ods_buffer'] .= '</table:table-row>';
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>';
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>';
220 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
221 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
222 . '</table:table-cell>';
225 $GLOBALS['ods_buffer'] .= '</table:table-row>';
227 PMA_DBI_free_result($result);
229 $GLOBALS['ods_buffer'] .= '</table:table>';