2.11.1.2 release
[phpmyadmin/arisferyanto.git] / libraries / export / htmlword.php
blob94d0add9cbea3bf61eae10b31a70dccf5000d9e3
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build CSV dumps of tables
6 * @version $Id$
7 */
9 /**
12 if (isset($plugin_list)) {
13 $plugin_list['htmlword'] = array(
14 'text' => 'strHTMLWord',
15 'extension' => 'doc',
16 'mime_type' => 'application/vnd.ms-word',
17 'force_file' => true,
18 'options' => array(
19 array('type' => 'bool', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data'),
20 array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure'),
21 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
22 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
23 array('type' => 'egroup'),
25 'options_text' => 'strOptions',
27 } else {
29 /**
30 * Outputs comment
32 * @param string Text of comment
34 * @return bool Whether it suceeded
36 function PMA_exportComment($text) {
37 return TRUE;
40 /**
41 * Outputs export footer
43 * @return bool Whether it suceeded
45 * @access public
47 function PMA_exportFooter() {
48 return PMA_exportOutputHandler('</body></html>');
51 /**
52 * Outputs export header
54 * @return bool Whether it suceeded
56 * @access public
58 function PMA_exportHeader() {
59 global $charset, $charset_of_file;
60 return PMA_exportOutputHandler('<html xmlns:o="urn:schemas-microsoft-com:office:office"
61 xmlns:x="urn:schemas-microsoft-com:office:word"
62 xmlns="http://www.w3.org/TR/REC-html40">
64 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
65 <html>
66 <head>
67 <meta http-equiv="Content-type" content="text/html;charset=' . (isset($charset_of_file) ? $charset_of_file : $charset) .'" />
68 </head>
69 <body>');
72 /**
73 * Outputs database header
75 * @param string Database name
77 * @return bool Whether it suceeded
79 * @access public
81 function PMA_exportDBHeader($db) {
82 return PMA_exportOutputHandler('<h1>' . $GLOBALS['strDatabase'] . ' ' . $db . '</h1>');
85 /**
86 * Outputs database footer
88 * @param string Database name
90 * @return bool Whether it suceeded
92 * @access public
94 function PMA_exportDBFooter($db) {
95 return TRUE;
98 /**
99 * Outputs create database database
101 * @param string Database name
103 * @return bool Whether it suceeded
105 * @access public
107 function PMA_exportDBCreate($db) {
108 return TRUE;
112 * Outputs the content of a table in CSV format
114 * @param string the database name
115 * @param string the table name
116 * @param string the end of line sequence
117 * @param string the url to go back in case of error
118 * @param string SQL query for obtaining data
120 * @return bool Whether it suceeded
122 * @access public
124 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
126 global $what;
128 if (!PMA_exportOutputHandler('<h2>' . $GLOBALS['strDumpingData'] . ' ' . $table . '</h2>')) {
129 return FALSE;
131 if (!PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
132 return FALSE;
135 // Gets the data from the database
136 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
137 $fields_cnt = PMA_DBI_num_fields($result);
139 // If required, get fields name at the first line
140 if (isset($GLOBALS['htmlword_columns'])) {
141 $schema_insert = '<tr class="print-category">';
142 for ($i = 0; $i < $fields_cnt; $i++) {
143 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</b></td>';
144 } // end for
145 $schema_insert .= '</tr>';
146 if (!PMA_exportOutputHandler($schema_insert)) {
147 return FALSE;
149 } // end if
151 // Format the data
152 while ($row = PMA_DBI_fetch_row($result)) {
153 $schema_insert = '<tr class="print-category">';
154 for ($j = 0; $j < $fields_cnt; $j++) {
155 if (!isset($row[$j]) || is_null($row[$j])) {
156 $value = $GLOBALS[$what . '_null'];
157 } elseif ($row[$j] == '0' || $row[$j] != '') {
158 $value = $row[$j];
159 } else {
160 $value = '';
162 $schema_insert .= '<td class="print">' . htmlspecialchars($value) . '</td>';
163 } // end for
164 $schema_insert .= '</tr>';
165 if (!PMA_exportOutputHandler($schema_insert)) {
166 return FALSE;
168 } // end while
169 PMA_DBI_free_result($result);
170 if (!PMA_exportOutputHandler('</table>')) {
171 return FALSE;
174 return TRUE;
177 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
179 global $cfgRelation;
181 if (!PMA_exportOutputHandler('<h2>' . $GLOBALS['strTableStructure'] . ' ' .$table . '</h2>')) {
182 return FALSE;
186 * Get the unique keys in the table
188 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
189 $keys_result = PMA_DBI_query($keys_query);
190 $unique_keys = array();
191 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
192 if ($key['Non_unique'] == 0) {
193 $unique_keys[] = $key['Column_name'];
196 PMA_DBI_free_result($keys_result);
199 * Gets fields properties
201 PMA_DBI_select_db($db);
202 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
203 $result = PMA_DBI_query($local_query);
204 $fields_cnt = PMA_DBI_num_rows($result);
206 // Check if we can use Relations (Mike Beck)
207 if ($do_relation && !empty($cfgRelation['relation'])) {
208 // Find which tables are related with the current one and write it in
209 // an array
210 $res_rel = PMA_getForeigners($db, $table);
212 if ($res_rel && count($res_rel) > 0) {
213 $have_rel = TRUE;
214 } else {
215 $have_rel = FALSE;
217 } else {
218 $have_rel = FALSE;
219 } // end if
222 * Displays the table structure
224 if (!PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
225 return FALSE;
228 $columns_cnt = 4;
229 if ($do_relation && $have_rel) {
230 $columns_cnt++;
232 if ($do_comments && $cfgRelation['commwork']) {
233 $columns_cnt++;
235 if ($do_mime && $cfgRelation['mimework']) {
236 $columns_cnt++;
239 $schema_insert = '<tr class="print-category">';
240 $schema_insert .= '<th class="print">' . htmlspecialchars($GLOBALS['strField']) . '</th>';
241 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strType']) . '</b></td>';
242 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strNull']) . '</b></td>';
243 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strDefault']) . '</b></td>';
244 if ($do_relation && $have_rel) {
245 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strLinksTo']) . '</b></td>';
247 if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
248 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strComments']) . '</b></td>';
249 $comments = PMA_getComments($db, $table);
251 if ($do_mime && $cfgRelation['mimework']) {
252 $schema_insert .= '<td class="print"><b>' . htmlspecialchars('MIME') . '</b></td>';
253 $mime_map = PMA_getMIME($db, $table, true);
255 $schema_insert .= '</tr>';
257 if (!PMA_exportOutputHandler($schema_insert)) {
258 return FALSE;
261 while ($row = PMA_DBI_fetch_assoc($result)) {
263 $schema_insert = '<tr class="print-category">';
264 $type = $row['Type'];
265 // reformat mysql query output - staybyte - 9. June 2001
266 // loic1: set or enum types: slashes single quotes inside options
267 if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
268 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
269 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
270 $type_nowrap = '';
272 $binary = 0;
273 $unsigned = 0;
274 $zerofill = 0;
275 } else {
276 $type_nowrap = ' nowrap="nowrap"';
277 $type = eregi_replace('BINARY', '', $type);
278 $type = eregi_replace('ZEROFILL', '', $type);
279 $type = eregi_replace('UNSIGNED', '', $type);
280 if (empty($type)) {
281 $type = '&nbsp;';
284 $binary = eregi('BINARY', $row['Type']);
285 $unsigned = eregi('UNSIGNED', $row['Type']);
286 $zerofill = eregi('ZEROFILL', $row['Type']);
288 $strAttribute = '&nbsp;';
289 if ($binary) {
290 $strAttribute = 'BINARY';
292 if ($unsigned) {
293 $strAttribute = 'UNSIGNED';
295 if ($zerofill) {
296 $strAttribute = 'UNSIGNED ZEROFILL';
298 if (!isset($row['Default'])) {
299 if ($row['Null'] != '') {
300 $row['Default'] = 'NULL';
302 } else {
303 $row['Default'] = $row['Default'];
306 $fmt_pre = '';
307 $fmt_post = '';
308 if (in_array($row['Field'], $unique_keys)) {
309 $fmt_pre = '<b>' . $fmt_pre;
310 $fmt_post = $fmt_post . '</b>';
312 if ($row['Key']=='PRI') {
313 $fmt_pre = '<i>' . $fmt_pre;
314 $fmt_post = $fmt_post . '</i>';
316 $schema_insert .= '<td class="print">' . $fmt_pre . htmlspecialchars($row['Field']) . $fmt_post . '</td>';
317 $schema_insert .= '<td class="print">' . htmlspecialchars($type) . '</td>';
318 $schema_insert .= '<td class="print">' . htmlspecialchars($row['Null'] == '' ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . '</td>';
319 $schema_insert .= '<td class="print">' . htmlspecialchars(isset($row['Default']) ? $row['Default'] : '') . '</td>';
321 $field_name = $row['Field'];
323 if ($do_relation && $have_rel) {
324 $schema_insert .= '<td class="print">' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '') . '</td>';
326 if ($do_comments && $cfgRelation['commwork']) {
327 $schema_insert .= '<td class="print">' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '') . '</td>';
329 if ($do_mime && $cfgRelation['mimework']) {
330 $schema_insert .= '<td class="print">' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '') . '</td>';
333 $schema_insert .= '</tr>';
335 if (!PMA_exportOutputHandler($schema_insert)) {
336 return FALSE;
338 } // end while
339 PMA_DBI_free_result($result);
341 return PMA_exportOutputHandler('</table>');