Add OK and Cancel button for grid editing
[phpmyadmin/arisferyanto.git] / libraries / export / odt.php
blob94a01fc1230cc9053389d0121edafec62e0c4656
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build OpenDocument Text dumps of tables
6 * @package PhpMyAdmin-Export
7 * @subpackage ODT
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $hide_structure = false;
18 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
19 $hide_structure = true;
21 $plugin_list['odt'] = array(
22 'text' => __('Open Document Text'),
23 'extension' => 'odt',
24 'mime_type' => 'application/vnd.oasis.opendocument.text',
25 'force_file' => true,
26 'options' => array(), /* Filled later */
27 'options_text' => __('Options'),
30 /* what to dump (structure/data/both) */
31 $plugin_list['odt']['options'][] =
32 array('type' => 'begin_group', 'text' => __('Dump table') , 'name' => 'general_opts');
33 $plugin_list['odt']['options'][] =
34 array('type' => 'radio', 'name' => 'structure_or_data', 'values' => array('structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data')));
35 $plugin_list['odt']['options'][] = array('type' => 'end_group');
37 /* Structure options */
38 if (!$hide_structure) {
39 $plugin_list['odt']['options'][] =
40 array('type' => 'begin_group', 'name' => 'structure', 'text' => __('Object creation options'), 'force' => 'data');
41 if (!empty($GLOBALS['cfgRelation']['relation'])) {
42 $plugin_list['odt']['options'][] =
43 array('type' => 'bool', 'name' => 'relation', 'text' => __('Display foreign key relationships'));
45 $plugin_list['odt']['options'][] =
46 array('type' => 'bool', 'name' => 'comments', 'text' => __('Display comments'));
47 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
48 $plugin_list['odt']['options'][] =
49 array('type' => 'bool', 'name' => 'mime', 'text' => __('Display MIME types'));
51 $plugin_list['odt']['options'][] =
52 array('type' => 'end_group');
54 /* Data */
55 $plugin_list['odt']['options'][] =
56 array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'), 'force' => 'structure');
57 $plugin_list['odt']['options'][] =
58 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row'));
59 $plugin_list['odt']['options'][] =
60 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:'));
61 $plugin_list['odt']['options'][] =
62 array('type' => 'end_group');
63 } else {
65 $GLOBALS['odt_buffer'] = '';
66 include_once './libraries/opendocument.lib.php';
68 /**
69 * Outputs export footer
71 * @return bool Whether it suceeded
73 * @access public
75 function PMA_exportFooter() {
76 $GLOBALS['odt_buffer'] .= '</office:text>'
77 . '</office:body>'
78 . '</office:document-content>';
79 if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) {
80 return false;
82 return true;
85 /**
86 * Outputs export header
88 * @return bool Whether it suceeded
90 * @access public
92 function PMA_exportHeader() {
93 $GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="utf-8"?' . '>'
94 . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
95 . '<office:body>'
96 . '<office:text>';
97 return true;
101 * Outputs database header
103 * @param string $db Database name
104 * @return bool Whether it suceeded
106 * @access public
108 function PMA_exportDBHeader($db) {
109 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="1" text:style-name="Heading_1" text:is-list-header="true">' . __('Database') . ' ' . htmlspecialchars($db) . '</text:h>';
110 return true;
114 * Outputs database footer
116 * @param string $db Database name
117 * @return bool Whether it suceeded
119 * @access public
121 function PMA_exportDBFooter($db) {
122 return true;
126 * Outputs CREATE DATABASE statement
128 * @param string $db Database name
129 * @return bool Whether it suceeded
131 * @access public
133 function PMA_exportDBCreate($db) {
134 return true;
138 * Outputs the content of a table in ODT format
140 * @param string $db database name
141 * @param string $table table name
142 * @param string $crlf the end of line sequence
143 * @param string $error_url the url to go back in case of error
144 * @param string $sql_query SQL query for obtaining data
145 * @return bool Whether it suceeded
147 * @access public
149 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
150 global $what;
152 // Gets the data from the database
153 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
154 $fields_cnt = PMA_DBI_num_fields($result);
155 $fields_meta = PMA_DBI_get_fields_meta($result);
156 $field_flags = array();
157 for ($j = 0; $j < $fields_cnt; $j++) {
158 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
161 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . __('Dumping data for table') . ' ' . htmlspecialchars($table) . '</text:h>';
162 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_structure">';
163 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $fields_cnt . '"/>';
165 // If required, get fields name at the first line
166 if (isset($GLOBALS[$what . '_columns'])) {
167 $GLOBALS['odt_buffer'] .= '<table:table-row>';
168 for ($i = 0; $i < $fields_cnt; $i++) {
169 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
170 . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
171 . '</table:table-cell>';
172 } // end for
173 $GLOBALS['odt_buffer'] .= '</table:table-row>';
174 } // end if
176 // Format the data
177 while ($row = PMA_DBI_fetch_row($result)) {
178 $GLOBALS['odt_buffer'] .= '<table:table-row>';
179 for ($j = 0; $j < $fields_cnt; $j++) {
180 if (!isset($row[$j]) || is_null($row[$j])) {
181 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
182 . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
183 . '</table:table-cell>';
184 // ignore BLOB
185 } elseif (stristr($field_flags[$j], 'BINARY')
186 && $fields_meta[$j]->blob) {
187 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
188 . '<text:p></text:p>'
189 . '</table:table-cell>';
190 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
191 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
192 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
193 . '</table:table-cell>';
194 } else {
195 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
196 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
197 . '</table:table-cell>';
199 } // end for
200 $GLOBALS['odt_buffer'] .= '</table:table-row>';
201 } // end while
202 PMA_DBI_free_result($result);
204 $GLOBALS['odt_buffer'] .= '</table:table>';
206 return true;
210 * Outputs table's structure
212 * @param string $db database name
213 * @param string $table table name
214 * @param string $crlf the end of line sequence
215 * @param string $error_url the url to go back in case of error
216 * @param bool $do_relation whether to include relation comments
217 * @param bool $do_comments whether to include the pmadb-style column comments
218 * as comments in the structure; this is deprecated
219 * but the parameter is left here because export.php
220 * calls PMA_exportStructure() also for other export
221 * types which use this parameter
222 * @param bool $do_mime whether to include mime comments
223 * @param bool $dates whether to include creation/update/check dates
224 * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in'
225 * @param string $export_type 'server', 'database', 'table'
226 * @return bool Whether it suceeded
228 * @access public
230 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
232 global $cfgRelation;
234 /* Heading */
235 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . __('Table structure for table') . ' ' . htmlspecialchars($table) . '</text:h>';
238 * Get the unique keys in the table
240 $unique_keys = array();
241 $keys = PMA_DBI_get_table_indexes($db, $table);
242 foreach ($keys as $key) {
243 if ($key['Non_unique'] == 0) {
244 $unique_keys[] = $key['Column_name'];
249 * Gets fields properties
251 PMA_DBI_select_db($db);
253 // Check if we can use Relations
254 if ($do_relation && !empty($cfgRelation['relation'])) {
255 // Find which tables are related with the current one and write it in
256 // an array
257 $res_rel = PMA_getForeigners($db, $table);
259 if ($res_rel && count($res_rel) > 0) {
260 $have_rel = true;
261 } else {
262 $have_rel = false;
264 } else {
265 $have_rel = false;
266 } // end if
269 * Displays the table structure
271 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
272 $columns_cnt = 4;
273 if ($do_relation && $have_rel) {
274 $columns_cnt++;
276 if ($do_comments) {
277 $columns_cnt++;
279 if ($do_mime && $cfgRelation['mimework']) {
280 $columns_cnt++;
282 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
283 /* Header */
284 $GLOBALS['odt_buffer'] .= '<table:table-row>';
285 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
286 . '<text:p>' . __('Column') . '</text:p>'
287 . '</table:table-cell>';
288 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
289 . '<text:p>' . __('Type') . '</text:p>'
290 . '</table:table-cell>';
291 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
292 . '<text:p>' . __('Null') . '</text:p>'
293 . '</table:table-cell>';
294 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
295 . '<text:p>' . __('Default') . '</text:p>'
296 . '</table:table-cell>';
297 if ($do_relation && $have_rel) {
298 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
299 . '<text:p>' . __('Links to') . '</text:p>'
300 . '</table:table-cell>';
302 if ($do_comments) {
303 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
304 . '<text:p>' . __('Comments') . '</text:p>'
305 . '</table:table-cell>';
306 $comments = PMA_getComments($db, $table);
308 if ($do_mime && $cfgRelation['mimework']) {
309 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
310 . '<text:p>' . __('MIME type') . '</text:p>'
311 . '</table:table-cell>';
312 $mime_map = PMA_getMIME($db, $table, true);
314 $GLOBALS['odt_buffer'] .= '</table:table-row>';
316 $columns = PMA_DBI_get_columns($db, $table);
317 foreach ($columns as $column) {
318 $field_name = $column['Field'];
319 $GLOBALS['odt_buffer'] .= '<table:table-row>';
320 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
321 . '<text:p>' . htmlspecialchars($field_name) . '</text:p>'
322 . '</table:table-cell>';
324 $extracted_fieldspec = PMA_extractFieldSpec($column['Type']);
325 $type = htmlspecialchars($extracted_fieldspec['print_type']);
326 if (empty($type)) {
327 $type = '&nbsp;';
330 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
331 . '<text:p>' . htmlspecialchars($type) . '</text:p>'
332 . '</table:table-cell>';
333 if (!isset($column['Default'])) {
334 if ($column['Null'] != 'NO') {
335 $column['Default'] = 'NULL';
336 } else {
337 $column['Default'] = '';
339 } else {
340 $column['Default'] = $column['Default'];
342 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
343 . '<text:p>' . (($column['Null'] == '' || $column['Null'] == 'NO') ? __('No') : __('Yes')) . '</text:p>'
344 . '</table:table-cell>';
345 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
346 . '<text:p>' . htmlspecialchars($column['Default']) . '</text:p>'
347 . '</table:table-cell>';
349 if ($do_relation && $have_rel) {
350 if (isset($res_rel[$field_name])) {
351 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
352 . '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>'
353 . '</table:table-cell>';
356 if ($do_comments) {
357 if (isset($comments[$field_name])) {
358 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
359 . '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>'
360 . '</table:table-cell>';
361 } else {
362 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
363 . '<text:p></text:p>'
364 . '</table:table-cell>';
367 if ($do_mime && $cfgRelation['mimework']) {
368 if (isset($mime_map[$field_name])) {
369 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
370 . '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>'
371 . '</table:table-cell>';
372 } else {
373 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
374 . '<text:p></text:p>'
375 . '</table:table-cell>';
378 $GLOBALS['odt_buffer'] .= '</table:table-row>';
379 } // end while
381 $GLOBALS['odt_buffer'] .= '</table:table>';
382 return true;
383 } // end of the 'PMA_exportStructure' function
385 } // end else