Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportHtmlword.class.php
blobcdd5ec6837a354c9d37f02e5e7fa3fad09930992
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * HTML-Word export code
6 * @package PhpMyAdmin-Export
7 * @subpackage HTML-Word
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 /**
17 * Handles the export for the HTML-Word format
19 * @package PhpMyAdmin-Export
20 * @subpackage HTML-Word
22 class ExportHtmlword extends ExportPlugin
24 /**
25 * Constructor
27 public function __construct()
29 $this->setProperties();
32 /**
33 * Sets the export HTML-Word properties
35 * @return void
37 protected function setProperties()
39 $props = 'libraries/properties/';
40 include_once "$props/plugins/ExportPluginProperties.class.php";
41 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
42 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
43 include_once "$props/options/items/RadioPropertyItem.class.php";
44 include_once "$props/options/items/TextPropertyItem.class.php";
45 include_once "$props/options/items/BoolPropertyItem.class.php";
47 $exportPluginProperties = new ExportPluginProperties();
48 $exportPluginProperties->setText('Microsoft Word 2000');
49 $exportPluginProperties->setExtension('doc');
50 $exportPluginProperties->setMimeType('application/vnd.ms-word');
51 $exportPluginProperties->setForceFile(true);
52 $exportPluginProperties->setOptionsText(__('Options'));
54 // create the root group that will be the options field for
55 // $exportPluginProperties
56 // this will be shown as "Format specific options"
57 $exportSpecificOptions = new OptionsPropertyRootGroup();
58 $exportSpecificOptions->setName("Format Specific Options");
60 // what to dump (structure/data/both)
61 $dumpWhat = new OptionsPropertyMainGroup();
62 $dumpWhat->setName("dump_what");
63 $dumpWhat->setText(__('Dump table'));
64 // create primary items and add them to the group
65 $leaf = new RadioPropertyItem();
66 $leaf->setName("structure_or_data");
67 $leaf->setValues(
68 array(
69 'structure' => __('structure'),
70 'data' => __('data'),
71 'structure_and_data' => __('structure and data')
74 $dumpWhat->addProperty($leaf);
75 // add the main group to the root group
76 $exportSpecificOptions->addProperty($dumpWhat);
78 // data options main group
79 $dataOptions = new OptionsPropertyMainGroup();
80 $dataOptions->setName("dump_what");
81 $dataOptions->setText(__('Data dump options'));
82 $dataOptions->setForce('structure');
83 // create primary items and add them to the group
84 $leaf = new TextPropertyItem();
85 $leaf->setName("null");
86 $leaf->setText(__('Replace NULL with:'));
87 $dataOptions->addProperty($leaf);
88 $leaf = new BoolPropertyItem();
89 $leaf->setName("columns");
90 $leaf->setText(__('Put columns names in the first row'));
91 $dataOptions->addProperty($leaf);
92 // add the main group to the root group
93 $exportSpecificOptions->addProperty($dataOptions);
95 // set the options for the export plugin property item
96 $exportPluginProperties->setOptions($exportSpecificOptions);
97 $this->properties = $exportPluginProperties;
101 * This method is called when any PluginManager to which the observer
102 * is attached calls PluginManager::notify()
104 * @param SplSubject $subject The PluginManager notifying the observer
105 * of an update.
107 * @return void
109 public function update (SplSubject $subject)
114 * Outputs export header
116 * @return bool Whether it succeeded
118 public function exportHeader ()
120 global $charset_of_file;
122 return PMA_exportOutputHandler(
123 '<html xmlns:o="urn:schemas-microsoft-com:office:office"
124 xmlns:x="urn:schemas-microsoft-com:office:word"
125 xmlns="http://www.w3.org/TR/REC-html40">
127 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
128 . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
129 <html>
130 <head>
131 <meta http-equiv="Content-type" content="text/html;charset='
132 . (isset($charset_of_file) ? $charset_of_file : 'utf-8') . '" />
133 </head>
134 <body>'
139 * Outputs export footer
141 * @return bool Whether it succeeded
143 public function exportFooter ()
145 return PMA_exportOutputHandler('</body></html>');
149 * Outputs database header
151 * @param string $db Database name
153 * @return bool Whether it succeeded
155 public function exportDBHeader ($db)
157 return PMA_exportOutputHandler(
158 '<h1>' . __('Database') . ' ' . htmlspecialchars($db) . '</h1>'
163 * Outputs database footer
165 * @param string $db Database name
167 * @return bool Whether it succeeded
169 public function exportDBFooter ($db)
171 return true;
175 * Outputs CREATE DATABASE statement
177 * @param string $db Database name
179 * @return bool Whether it succeeded
181 public function exportDBCreate($db)
183 return true;
187 * Outputs the content of a table in HTML-Word format
189 * @param string $db database name
190 * @param string $table table name
191 * @param string $crlf the end of line sequence
192 * @param string $error_url the url to go back in case of error
193 * @param string $sql_query SQL query for obtaining data
195 * @return bool Whether it succeeded
197 public function exportData($db, $table, $crlf, $error_url, $sql_query)
199 global $what;
201 if (! PMA_exportOutputHandler(
202 '<h2>'
203 . __('Dumping data for table') . ' ' . htmlspecialchars($table)
204 . '</h2>'
205 )) {
206 return false;
208 if (! PMA_exportOutputHandler(
209 '<table class="width100" cellspacing="1">'
210 )) {
211 return false;
214 // Gets the data from the database
215 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
216 $fields_cnt = PMA_DBI_num_fields($result);
218 // If required, get fields name at the first line
219 if (isset($GLOBALS['htmlword_columns'])) {
220 $schema_insert = '<tr class="print-category">';
221 for ($i = 0; $i < $fields_cnt; $i++) {
222 $schema_insert .= '<td class="print"><strong>'
223 . htmlspecialchars(
224 stripslashes(PMA_DBI_field_name($result, $i))
226 . '</strong></td>';
227 } // end for
228 $schema_insert .= '</tr>';
229 if (! PMA_exportOutputHandler($schema_insert)) {
230 return false;
232 } // end if
234 // Format the data
235 while ($row = PMA_DBI_fetch_row($result)) {
236 $schema_insert = '<tr class="print-category">';
237 for ($j = 0; $j < $fields_cnt; $j++) {
238 if (! isset($row[$j]) || is_null($row[$j])) {
239 $value = $GLOBALS[$what . '_null'];
240 } elseif ($row[$j] == '0' || $row[$j] != '') {
241 $value = $row[$j];
242 } else {
243 $value = '';
245 $schema_insert .= '<td class="print">'
246 . htmlspecialchars($value)
247 . '</td>';
248 } // end for
249 $schema_insert .= '</tr>';
250 if (! PMA_exportOutputHandler($schema_insert)) {
251 return false;
253 } // end while
254 PMA_DBI_free_result($result);
255 if (! PMA_exportOutputHandler('</table>')) {
256 return false;
259 return true;
263 * Returns a stand-in CREATE definition to resolve view dependencies
265 * @param string $db the database name
266 * @param string $view the view name
267 * @param string $crlf the end of line sequence
269 * @return string resulting definition
271 public function getTableDefStandIn($db, $view, $crlf)
273 $schema_insert = '<table class="width100" cellspacing="1">'
274 . '<tr class="print-category">'
275 . '<th class="print">'
276 . __('Column')
277 . '</th>'
278 . '<td class="print"><strong>'
279 . __('Type')
280 . '</strong></td>'
281 . '<td class="print"><strong>'
282 . __('Null')
283 . '</strong></td>'
284 . '<td class="print"><strong>'
285 . __('Default')
286 . '</strong></td>'
287 . '</tr>';
290 * Get the unique keys in the view
292 $unique_keys = array();
293 $keys = PMA_DBI_get_table_indexes($db, $view);
294 foreach ($keys as $key) {
295 if ($key['Non_unique'] == 0) {
296 $unique_keys[] = $key['Column_name'];
300 $columns = PMA_DBI_get_columns($db, $view);
301 foreach ($columns as $column) {
302 $schema_insert .= $this->formatOneColumnDefinition(
303 $column,
304 $unique_keys
306 $schema_insert .= '</tr>';
309 $schema_insert .= '</table>';
310 return $schema_insert;
314 * Returns $table's CREATE definition
316 * @param string $db the database name
317 * @param string $table the table name
318 * @param string $crlf the end of line sequence
319 * @param string $error_url the url to go back in case of error
320 * @param bool $do_relation whether to include relation comments
321 * @param bool $do_comments whether to include the pmadb-style column
322 * comments as comments in the structure;
323 * this is deprecated but the parameter is
324 * left here because export.php calls
325 * PMA_exportStructure() also for other
326 * export types which use this parameter
327 * @param bool $do_mime whether to include mime comments
328 * @param bool $show_dates whether to include creation/update/check dates
329 * @param bool $add_semicolon whether to add semicolon and end-of-line
330 * at the end
331 * @param bool $view whether we're handling a view
333 * @return string resulting schema
335 public function getTableDef(
336 $db,
337 $table,
338 $crlf,
339 $error_url,
340 $do_relation,
341 $do_comments,
342 $do_mime,
343 $show_dates = false,
344 $add_semicolon = true,
345 $view = false
347 // set $cfgRelation here, because there is a chance that it's modified
348 // since the class initialization
349 global $cfgRelation;
351 $schema_insert = '';
354 * Gets fields properties
356 PMA_DBI_select_db($db);
358 // Check if we can use Relations
359 if ($do_relation && ! empty($cfgRelation['relation'])) {
360 // Find which tables are related with the current one and write it in
361 // an array
362 $res_rel = PMA_getForeigners($db, $table);
364 if ($res_rel && count($res_rel) > 0) {
365 $have_rel = true;
366 } else {
367 $have_rel = false;
369 } else {
370 $have_rel = false;
371 } // end if
374 * Displays the table structure
376 $schema_insert .= '<table class="width100" cellspacing="1">';
378 $columns_cnt = 4;
379 if ($do_relation && $have_rel) {
380 $columns_cnt++;
382 if ($do_comments && $cfgRelation['commwork']) {
383 $columns_cnt++;
385 if ($do_mime && $cfgRelation['mimework']) {
386 $columns_cnt++;
389 $schema_insert .= '<tr class="print-category">';
390 $schema_insert .= '<th class="print">'
391 . __('Column')
392 . '</th>';
393 $schema_insert .= '<td class="print"><strong>'
394 . __('Type')
395 . '</strong></td>';
396 $schema_insert .= '<td class="print"><strong>'
397 . __('Null')
398 . '</strong></td>';
399 $schema_insert .= '<td class="print"><strong>'
400 . __('Default')
401 . '</strong></td>';
402 if ($do_relation && $have_rel) {
403 $schema_insert .= '<td class="print"><strong>'
404 . __('Links to')
405 . '</strong></td>';
407 if ($do_comments) {
408 $schema_insert .= '<td class="print"><strong>'
409 . __('Comments')
410 . '</strong></td>';
411 $comments = PMA_getComments($db, $table);
413 if ($do_mime && $cfgRelation['mimework']) {
414 $schema_insert .= '<td class="print"><strong>'
415 . htmlspecialchars('MIME')
416 . '</strong></td>';
417 $mime_map = PMA_getMIME($db, $table, true);
419 $schema_insert .= '</tr>';
421 $columns = PMA_DBI_get_columns($db, $table);
423 * Get the unique keys in the table
425 $unique_keys = array();
426 $keys = PMA_DBI_get_table_indexes($db, $table);
427 foreach ($keys as $key) {
428 if ($key['Non_unique'] == 0) {
429 $unique_keys[] = $key['Column_name'];
432 foreach ($columns as $column) {
433 $schema_insert .= $this->formatOneColumnDefinition(
434 $column,
435 $unique_keys
437 $field_name = $column['Field'];
439 if ($do_relation && $have_rel) {
440 $schema_insert .= '<td class="print">'
441 . (isset($res_rel[$field_name])
442 ? htmlspecialchars(
443 $res_rel[$field_name]['foreign_table']
444 . ' (' . $res_rel[$field_name]['foreign_field']
445 . ')'
447 : '') . '</td>';
449 if ($do_comments && $cfgRelation['commwork']) {
450 $schema_insert .= '<td class="print">'
451 . (isset($comments[$field_name])
452 ? htmlspecialchars($comments[$field_name])
453 : '') . '</td>';
455 if ($do_mime && $cfgRelation['mimework']) {
456 $schema_insert .= '<td class="print">'
457 . (isset($mime_map[$field_name]) ?
458 htmlspecialchars(
459 str_replace('_', '/', $mime_map[$field_name]['mimetype'])
461 : '') . '</td>';
464 $schema_insert .= '</tr>';
465 } // end foreach
467 $schema_insert .= '</table>';
468 return $schema_insert;
472 * Outputs triggers
474 * @param string $db database name
475 * @param string $table table name
477 * @return string Formatted triggers list
479 protected function getTriggers($db, $table)
481 $dump = '<table class="width100" cellspacing="1">';
482 $dump .= '<tr class="print-category">';
483 $dump .= '<th class="print">' . __('Name') . '</th>';
484 $dump .= '<td class="print"><strong>' . __('Time') . '</strong></td>';
485 $dump .= '<td class="print"><strong>' . __('Event') . '</strong></td>';
486 $dump .= '<td class="print"><strong>' . __('Definition') . '</strong></td>';
487 $dump .= '</tr>';
489 $triggers = PMA_DBI_get_triggers($db, $table);
491 foreach ($triggers as $trigger) {
492 $dump .= '<tr class="print-category">';
493 $dump .= '<td class="print">'
494 . htmlspecialchars($trigger['name'])
495 . '</td>'
496 . '<td class="print">'
497 . htmlspecialchars($trigger['action_timing'])
498 . '</td>'
499 . '<td class="print">'
500 . htmlspecialchars($trigger['event_manipulation'])
501 . '</td>'
502 . '<td class="print">'
503 . htmlspecialchars($trigger['definition'])
504 . '</td>'
505 . '</tr>';
508 $dump .= '</table>';
509 return $dump;
513 * Outputs table's structure
515 * @param string $db database name
516 * @param string $table table name
517 * @param string $crlf the end of line sequence
518 * @param string $error_url the url to go back in case of error
519 * @param string $export_mode 'create_table', 'triggers', 'create_view',
520 * 'stand_in'
521 * @param string $export_type 'server', 'database', 'table'
522 * @param bool $do_relation whether to include relation comments
523 * @param bool $do_comments whether to include the pmadb-style column
524 * comments as comments in the structure;
525 * this is deprecated but the parameter is
526 * left here because export.php calls
527 * PMA_exportStructure() also for other
528 * export types which use this parameter
529 * @param bool $do_mime whether to include mime comments
530 * @param bool $dates whether to include creation/update/check dates
532 * @return bool Whether it succeeded
534 public function exportStructure(
535 $db,
536 $table,
537 $crlf,
538 $error_url,
539 $export_mode,
540 $export_type,
541 $do_relation = false,
542 $do_comments = false,
543 $do_mime = false,
544 $dates = false
546 $dump = '';
548 switch($export_mode) {
549 case 'create_table':
550 $dump .= '<h2>'
551 . __('Table structure for table') . ' ' . htmlspecialchars($table)
552 . '</h2>';
553 $dump .= $this->getTableDef(
554 $db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime,
555 $dates
557 break;
558 case 'triggers':
559 $dump = '';
560 $triggers = PMA_DBI_get_triggers($db, $table);
561 if ($triggers) {
562 $dump .= '<h2>'
563 . __('Triggers') . ' ' . htmlspecialchars($table)
564 . '</h2>';
565 $dump .= $this->getTriggers($db, $table);
567 break;
568 case 'create_view':
569 $dump .= '<h2>'
570 . __('Structure for view') . ' ' . htmlspecialchars($table)
571 . '</h2>';
572 $dump .= $this->getTableDef(
573 $db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime,
574 $dates, true, true
576 break;
577 case 'stand_in':
578 $dump .= '<h2>'
579 . __('Stand-in structure for view') . ' ' . htmlspecialchars($table)
580 . '</h2>';
581 // export a stand-in definition to resolve view dependencies
582 $dump .= $this->getTableDefStandIn($db, $table, $crlf);
583 } // end switch
585 return PMA_exportOutputHandler($dump);
589 * Formats the definition for one column
591 * @param array $column info about this column
592 * @param array $unique_keys unique keys of the table
594 * @return string Formatted column definition
596 protected function formatOneColumnDefinition(
597 $column, $unique_keys
599 $definition = '<tr class="print-category">';
601 $extracted_columnspec
602 = PMA_Util::extractColumnSpec($column['Type']);
604 $type = htmlspecialchars($extracted_columnspec['print_type']);
605 if (empty($type)) {
606 $type = '&nbsp;';
609 if (! isset($column['Default'])) {
610 if ($column['Null'] != 'NO') {
611 $column['Default'] = 'NULL';
615 $fmt_pre = '';
616 $fmt_post = '';
617 if (in_array($column['Field'], $unique_keys)) {
618 $fmt_pre = '<strong>' . $fmt_pre;
619 $fmt_post = $fmt_post . '</strong>';
621 if ($column['Key'] == 'PRI') {
622 $fmt_pre = '<em>' . $fmt_pre;
623 $fmt_post = $fmt_post . '</em>';
625 $definition .= '<td class="print">' . $fmt_pre
626 . htmlspecialchars($column['Field']) . $fmt_post . '</td>';
627 $definition .= '<td class="print">' . htmlspecialchars($type)
628 . '</td>';
629 $definition .= '<td class="print">'
630 . (($column['Null'] == '' || $column['Null'] == 'NO')
631 ? __('No')
632 : __('Yes'))
633 . '</td>';
634 $definition .= '<td class="print">'
635 . htmlspecialchars(
636 isset($column['Default'])
637 ? $column['Default']
638 : ''
640 . '</td>';
642 return $definition;