Merge branch 'master' of ssh://repo.or.cz/srv/git/phpmyadmin/madhuracj into OpenGIS
[phpmyadmin/madhuracj.git] / libraries / export / ods.php
blob868edf3ef49d07f21585aa77c207f58bca2355d5
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
7 * @subpackage ODS
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['ods'] = array(
18 'text' => __('Open Document Spreadsheet'),
19 'extension' => 'ods',
20 'mime_type' => 'application/vnd.oasis.opendocument.spreadsheet',
21 'force_file' => true,
22 'options' => array(
23 array('type' => 'begin_group', 'name' => 'general_opts'),
24 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:')),
25 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
26 array('type' => 'hidden', 'name' => 'structure_or_data'),
27 array('type' => 'end_group'),
29 'options_text' => __('Options'),
31 } else {
33 $GLOBALS['ods_buffer'] = '';
34 include_once './libraries/opendocument.lib.php';
36 /**
37 * Outputs export footer
39 * @return bool Whether it suceeded
41 * @access public
43 function PMA_exportFooter() {
44 $GLOBALS['ods_buffer'] .= '</office:spreadsheet>'
45 . '</office:body>'
46 . '</office:document-content>';
47 if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.spreadsheet', $GLOBALS['ods_buffer']))) {
48 return false;
50 return true;
53 /**
54 * Outputs export header
56 * @return bool Whether it suceeded
58 * @access public
60 function PMA_exportHeader() {
61 $GLOBALS['ods_buffer'] .= '<?xml version="1.0" encoding="utf-8"?' . '>'
62 . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
63 . '<office:automatic-styles>'
64 . '<number:date-style style:name="N37" number:automatic-order="true">'
65 . '<number:month number:style="long"/>'
66 . '<number:text>/</number:text>'
67 . '<number:day number:style="long"/>'
68 . '<number:text>/</number:text>'
69 . '<number:year/>'
70 . '</number:date-style>'
71 . '<number:time-style style:name="N43">'
72 . '<number:hours number:style="long"/>'
73 . '<number:text>:</number:text>'
74 . '<number:minutes number:style="long"/>'
75 . '<number:text>:</number:text>'
76 . '<number:seconds number:style="long"/>'
77 . '<number:text> </number:text>'
78 . '<number:am-pm/>'
79 . '</number:time-style>'
80 . '<number:date-style style:name="N50" number:automatic-order="true" number:format-source="language">'
81 . '<number:month/>'
82 . '<number:text>/</number:text>'
83 . '<number:day/>'
84 . '<number:text>/</number:text>'
85 . '<number:year/>'
86 . '<number:text> </number:text>'
87 . '<number:hours number:style="long"/>'
88 . '<number:text>:</number:text>'
89 . '<number:minutes number:style="long"/>'
90 . '<number:text> </number:text>'
91 . '<number:am-pm/>'
92 . '</number:date-style>'
93 . '<style:style style:name="DateCell" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N37"/>'
94 . '<style:style style:name="TimeCell" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N43"/>'
95 . '<style:style style:name="DateTimeCell" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N50"/>'
96 . '</office:automatic-styles>'
97 . '<office:body>'
98 . '<office:spreadsheet>';
99 return true;
103 * Outputs database header
105 * @param string $db Database name
106 * @return bool Whether it suceeded
108 * @access public
110 function PMA_exportDBHeader($db) {
111 return true;
115 * Outputs database footer
117 * @param string $db Database name
118 * @return bool Whether it suceeded
120 * @access public
122 function PMA_exportDBFooter($db) {
123 return true;
127 * Outputs CREATE DATABASE statement
129 * @param string $db Database name
130 * @return bool Whether it suceeded
132 * @access public
134 function PMA_exportDBCreate($db) {
135 return true;
139 * Outputs the content of a table in ODS format
141 * @param string $db database name
142 * @param string $table table name
143 * @param string $crlf the end of line sequence
144 * @param string $error_url the url to go back in case of error
145 * @param string $sql_query SQL query for obtaining data
146 * @return bool Whether it suceeded
148 * @access public
150 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
151 global $what;
153 // Gets the data from the database
154 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
155 $fields_cnt = PMA_DBI_num_fields($result);
156 $fields_meta = PMA_DBI_get_fields_meta($result);
157 $field_flags = array();
158 for ($j = 0; $j < $fields_cnt; $j++) {
159 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
162 $GLOBALS['ods_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '">';
164 // If required, get fields name at the first line
165 if (isset($GLOBALS[$what . '_columns'])) {
166 $GLOBALS['ods_buffer'] .= '<table:table-row>';
167 for ($i = 0; $i < $fields_cnt; $i++) {
168 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
169 . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
170 . '</table:table-cell>';
171 } // end for
172 $GLOBALS['ods_buffer'] .= '</table:table-row>';
173 } // end if
175 // Format the data
176 while ($row = PMA_DBI_fetch_row($result)) {
177 $GLOBALS['ods_buffer'] .= '<table:table-row>';
178 for ($j = 0; $j < $fields_cnt; $j++) {
179 if (!isset($row[$j]) || is_null($row[$j])) {
180 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
181 . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
182 . '</table:table-cell>';
183 // ignore BLOB
184 } elseif (stristr($field_flags[$j], 'BINARY')
185 && $fields_meta[$j]->blob) {
186 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
187 . '<text:p></text:p>'
188 . '</table:table-cell>';
189 } elseif ($fields_meta[$j]->type == "date") {
190 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="date" office:date-value="' . date("Y-m-d", strtotime($row[$j])) . '" table:style-name="DateCell">'
191 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
192 . '</table:table-cell>';
193 } elseif ($fields_meta[$j]->type == "time") {
194 $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">'
195 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
196 . '</table:table-cell>';
197 } elseif ($fields_meta[$j]->type == "datetime") {
198 $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">'
199 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
200 . '</table:table-cell>';
201 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
202 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
203 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
204 . '</table:table-cell>';
205 } else {
206 $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
207 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
208 . '</table:table-cell>';
210 } // end for
211 $GLOBALS['ods_buffer'] .= '</table:table-row>';
212 } // end while
213 PMA_DBI_free_result($result);
215 $GLOBALS['ods_buffer'] .= '</table:table>';
217 return true;