Convert Excel column name correctly
[phpmyadmin/arisferyanto.git] / libraries / export / htmlword.php
blob0d895fa2a65bd1b1b19af7c5a67ba5bdeb638650
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 * @package phpMyAdmin-Export-HTMLWord
7 * @version $Id$
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['htmlword'] = array(
18 'text' => 'strHTMLWord',
19 'extension' => 'doc',
20 'mime_type' => 'application/vnd.ms-word',
21 'force_file' => true,
22 'options' => array(
23 array('type' => 'bool', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data'),
24 array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure'),
25 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
26 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
27 array('type' => 'egroup'),
29 'options_text' => 'strOptions',
31 } else {
33 /**
34 * Outputs comment
36 * @param string Text of comment
38 * @return bool Whether it suceeded
40 function PMA_exportComment($text) {
41 return TRUE;
44 /**
45 * Outputs export footer
47 * @return bool Whether it suceeded
49 * @access public
51 function PMA_exportFooter() {
52 return PMA_exportOutputHandler('</body></html>');
55 /**
56 * Outputs export header
58 * @return bool Whether it suceeded
60 * @access public
62 function PMA_exportHeader() {
63 global $charset, $charset_of_file;
64 return PMA_exportOutputHandler('<html xmlns:o="urn:schemas-microsoft-com:office:office"
65 xmlns:x="urn:schemas-microsoft-com:office:word"
66 xmlns="http://www.w3.org/TR/REC-html40">
68 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
69 <html>
70 <head>
71 <meta http-equiv="Content-type" content="text/html;charset=' . (isset($charset_of_file) ? $charset_of_file : $charset) . '" />
72 </head>
73 <body>');
76 /**
77 * Outputs database header
79 * @param string Database name
81 * @return bool Whether it suceeded
83 * @access public
85 function PMA_exportDBHeader($db) {
86 return PMA_exportOutputHandler('<h1>' . $GLOBALS['strDatabase'] . ' ' . $db . '</h1>');
89 /**
90 * Outputs database footer
92 * @param string Database name
94 * @return bool Whether it suceeded
96 * @access public
98 function PMA_exportDBFooter($db) {
99 return TRUE;
103 * Outputs create database database
105 * @param string Database name
107 * @return bool Whether it suceeded
109 * @access public
111 function PMA_exportDBCreate($db) {
112 return TRUE;
116 * Outputs the content of a table in CSV format
118 * @param string the database name
119 * @param string the table name
120 * @param string the end of line sequence
121 * @param string the url to go back in case of error
122 * @param string SQL query for obtaining data
124 * @return bool Whether it suceeded
126 * @access public
128 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
130 global $what;
132 if (! PMA_exportOutputHandler('<h2>' . $GLOBALS['strDumpingData'] . ' ' . $table . '</h2>')) {
133 return FALSE;
135 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
136 return FALSE;
139 // Gets the data from the database
140 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
141 $fields_cnt = PMA_DBI_num_fields($result);
143 // If required, get fields name at the first line
144 if (isset($GLOBALS['htmlword_columns'])) {
145 $schema_insert = '<tr class="print-category">';
146 for ($i = 0; $i < $fields_cnt; $i++) {
147 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</b></td>';
148 } // end for
149 $schema_insert .= '</tr>';
150 if (! PMA_exportOutputHandler($schema_insert)) {
151 return FALSE;
153 } // end if
155 // Format the data
156 while ($row = PMA_DBI_fetch_row($result)) {
157 $schema_insert = '<tr class="print-category">';
158 for ($j = 0; $j < $fields_cnt; $j++) {
159 if (! isset($row[$j]) || is_null($row[$j])) {
160 $value = $GLOBALS[$what . '_null'];
161 } elseif ($row[$j] == '0' || $row[$j] != '') {
162 $value = $row[$j];
163 } else {
164 $value = '';
166 $schema_insert .= '<td class="print">' . htmlspecialchars($value) . '</td>';
167 } // end for
168 $schema_insert .= '</tr>';
169 if (! PMA_exportOutputHandler($schema_insert)) {
170 return FALSE;
172 } // end while
173 PMA_DBI_free_result($result);
174 if (! PMA_exportOutputHandler('</table>')) {
175 return FALSE;
178 return TRUE;
181 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
183 global $cfgRelation;
185 if (! PMA_exportOutputHandler('<h2>' . $GLOBALS['strTableStructure'] . ' ' .$table . '</h2>')) {
186 return FALSE;
190 * Get the unique keys in the table
192 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
193 $keys_result = PMA_DBI_query($keys_query);
194 $unique_keys = array();
195 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
196 if ($key['Non_unique'] == 0) {
197 $unique_keys[] = $key['Column_name'];
200 PMA_DBI_free_result($keys_result);
203 * Gets fields properties
205 PMA_DBI_select_db($db);
206 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
207 $result = PMA_DBI_query($local_query);
208 $fields_cnt = PMA_DBI_num_rows($result);
210 // Check if we can use Relations (Mike Beck)
211 if ($do_relation && ! empty($cfgRelation['relation'])) {
212 // Find which tables are related with the current one and write it in
213 // an array
214 $res_rel = PMA_getForeigners($db, $table);
216 if ($res_rel && count($res_rel) > 0) {
217 $have_rel = TRUE;
218 } else {
219 $have_rel = FALSE;
221 } else {
222 $have_rel = FALSE;
223 } // end if
226 * Displays the table structure
228 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
229 return FALSE;
232 $columns_cnt = 4;
233 if ($do_relation && $have_rel) {
234 $columns_cnt++;
236 if ($do_comments && $cfgRelation['commwork']) {
237 $columns_cnt++;
239 if ($do_mime && $cfgRelation['mimework']) {
240 $columns_cnt++;
243 $schema_insert = '<tr class="print-category">';
244 $schema_insert .= '<th class="print">' . htmlspecialchars($GLOBALS['strField']) . '</th>';
245 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strType']) . '</b></td>';
246 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strNull']) . '</b></td>';
247 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strDefault']) . '</b></td>';
248 if ($do_relation && $have_rel) {
249 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strLinksTo']) . '</b></td>';
251 if ($do_comments) {
252 $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strComments']) . '</b></td>';
253 $comments = PMA_getComments($db, $table);
255 if ($do_mime && $cfgRelation['mimework']) {
256 $schema_insert .= '<td class="print"><b>' . htmlspecialchars('MIME') . '</b></td>';
257 $mime_map = PMA_getMIME($db, $table, true);
259 $schema_insert .= '</tr>';
261 if (! PMA_exportOutputHandler($schema_insert)) {
262 return FALSE;
265 while ($row = PMA_DBI_fetch_assoc($result)) {
267 $schema_insert = '<tr class="print-category">';
268 $type = $row['Type'];
269 // reformat mysql query output - staybyte - 9. June 2001
270 // loic1: set or enum types: slashes single quotes inside options
271 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
272 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
273 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
274 $type_nowrap = '';
276 $binary = 0;
277 $unsigned = 0;
278 $zerofill = 0;
279 } else {
280 $type_nowrap = ' nowrap="nowrap"';
281 $type = preg_replace('/BINARY/i', '', $type);
282 $type = preg_replace('/ZEROFILL/i', '', $type);
283 $type = preg_replace('/UNSIGNED/i', '', $type);
284 if (empty($type)) {
285 $type = '&nbsp;';
288 $binary = preg_match('/BINARY/i', $row['Type']);
289 $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
290 $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
292 $strAttribute = '&nbsp;';
293 if ($binary) {
294 $strAttribute = 'BINARY';
296 if ($unsigned) {
297 $strAttribute = 'UNSIGNED';
299 if ($zerofill) {
300 $strAttribute = 'UNSIGNED ZEROFILL';
302 if (! isset($row['Default'])) {
303 if ($row['Null'] != 'NO') {
304 $row['Default'] = 'NULL';
306 } else {
307 $row['Default'] = $row['Default'];
310 $fmt_pre = '';
311 $fmt_post = '';
312 if (in_array($row['Field'], $unique_keys)) {
313 $fmt_pre = '<b>' . $fmt_pre;
314 $fmt_post = $fmt_post . '</b>';
316 if ($row['Key'] == 'PRI') {
317 $fmt_pre = '<i>' . $fmt_pre;
318 $fmt_post = $fmt_post . '</i>';
320 $schema_insert .= '<td class="print">' . $fmt_pre . htmlspecialchars($row['Field']) . $fmt_post . '</td>';
321 $schema_insert .= '<td class="print">' . htmlspecialchars($type) . '</td>';
322 $schema_insert .= '<td class="print">' . htmlspecialchars(($row['Null'] == '' || $row['Null'] == 'NO') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . '</td>';
323 $schema_insert .= '<td class="print">' . htmlspecialchars(isset($row['Default']) ? $row['Default'] : '') . '</td>';
325 $field_name = $row['Field'];
327 if ($do_relation && $have_rel) {
328 $schema_insert .= '<td class="print">' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '') . '</td>';
330 if ($do_comments && $cfgRelation['commwork']) {
331 $schema_insert .= '<td class="print">' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '') . '</td>';
333 if ($do_mime && $cfgRelation['mimework']) {
334 $schema_insert .= '<td class="print">' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '') . '</td>';
337 $schema_insert .= '</tr>';
339 if (! PMA_exportOutputHandler($schema_insert)) {
340 return FALSE;
342 } // end while
343 PMA_DBI_free_result($result);
345 return PMA_exportOutputHandler('</table>');