Fix for the Open in New Window in Patient/Client->Patients search gui, take 2.
[openemr.git] / phpmyadmin / libraries / export / latex.php
blob3711d37ebcdb3402305cdee354365f39b044f6c6
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build dumps of tables
6 * @version $Id$
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if (isset($plugin_list)) {
16 $hide_structure = false;
17 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
18 $hide_structure = true;
20 $plugin_list['latex'] = array(
21 'text' => 'strLaTeX',
22 'extension' => 'tex',
23 'mime_type' => 'application/x-tex',
24 'options' => array(
25 array('type' => 'bool', 'name' => 'caption', 'text' => 'strLatexIncludeCaption'),
27 'options_text' => 'strOptions',
29 /* Structure options */
30 if (!$hide_structure) {
31 $plugin_list['latex']['options'][] =
32 array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
33 $plugin_list['latex']['options'][] =
34 array('type' => 'text', 'name' => 'structure_caption', 'text' => 'strLatexCaption');
35 $plugin_list['latex']['options'][] =
36 array('type' => 'text', 'name' => 'structure_continued_caption', 'text' => 'strLatexContinuedCaption');
37 $plugin_list['latex']['options'][] =
38 array('type' => 'text', 'name' => 'structure_label', 'text' => 'strLatexLabel');
39 if (!empty($GLOBALS['cfgRelation']['relation'])) {
40 $plugin_list['latex']['options'][] =
41 array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
43 if (!empty($GLOBALS['cfgRelation']['commwork']) || PMA_MYSQL_INT_VERSION >= 40100) {
44 $plugin_list['latex']['options'][] =
45 array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
47 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
48 $plugin_list['latex']['options'][] =
49 array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
51 $plugin_list['latex']['options'][] =
52 array('type' => 'egroup');
54 /* Data */
55 $plugin_list['latex']['options'][] =
56 array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
57 $plugin_list['latex']['options'][] =
58 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames');
59 $plugin_list['latex']['options'][] =
60 array('type' => 'text', 'name' => 'data_caption', 'text' => 'strLatexCaption');
61 $plugin_list['latex']['options'][] =
62 array('type' => 'text', 'name' => 'data_continued_caption', 'text' => 'strLatexContinuedCaption');
63 $plugin_list['latex']['options'][] =
64 array('type' => 'text', 'name' => 'data_label', 'text' => 'strLatexLabel');
65 $plugin_list['latex']['options'][] =
66 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy');
67 $plugin_list['latex']['options'][] =
68 array('type' => 'egroup');
69 } else {
71 /**
72 * Escapes some special characters for use in TeX/LaTeX
74 * @param string the string to convert
76 * @return string the converted string with escape codes
78 * @access private
80 function PMA_texEscape($string) {
81 $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
82 $cnt_escape = count($escape);
83 for ($k=0; $k < $cnt_escape; $k++) {
84 $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
86 return $string;
89 /**
90 * Outputs comment
92 * @param string Text of comment
94 * @return bool Whether it suceeded
96 function PMA_exportComment($text) {
97 return PMA_exportOutputHandler('% ' . $text . $GLOBALS['crlf']);
101 * Outputs export footer
103 * @return bool Whether it suceeded
105 * @access public
107 function PMA_exportFooter() {
108 return TRUE;
112 * Outputs export header
114 * @return bool Whether it suceeded
116 * @access public
118 function PMA_exportHeader() {
119 global $crlf;
120 global $cfg;
122 $head = '% phpMyAdmin LaTeX Dump' . $crlf
123 . '% version ' . PMA_VERSION . $crlf
124 . '% http://www.phpmyadmin.net' . $crlf
125 . '%' . $crlf
126 . '% ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
127 if (!empty($cfg['Server']['port'])) {
128 $head .= ':' . $cfg['Server']['port'];
130 $head .= $crlf
131 . '% ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
132 . '% ' . $GLOBALS['strServerVersion'] . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
133 . '% ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
134 return PMA_exportOutputHandler($head);
138 * Outputs database header
140 * @param string Database name
142 * @return bool Whether it suceeded
144 * @access public
146 function PMA_exportDBHeader($db) {
147 global $crlf;
148 $head = '% ' . $crlf
149 . '% ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
150 . '% ' . $crlf;
151 return PMA_exportOutputHandler($head);
155 * Outputs database footer
157 * @param string Database name
159 * @return bool Whether it suceeded
161 * @access public
163 function PMA_exportDBFooter($db) {
164 return TRUE;
168 * Outputs create database database
170 * @param string Database name
172 * @return bool Whether it suceeded
174 * @access public
176 function PMA_exportDBCreate($db) {
177 return TRUE;
181 * Outputs the content of a table in LaTeX table/sideways table environment
183 * @param string the database name
184 * @param string the table name
185 * @param string the end of line sequence
186 * @param string the url to go back in case of error
187 * @param string SQL query for obtaining data
189 * @return bool Whether it suceeded
191 * @access public
193 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
194 $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
196 $columns_cnt = PMA_DBI_num_fields($result);
197 for ($i = 0; $i < $columns_cnt; $i++) {
198 $columns[$i] = PMA_DBI_field_name($result, $i);
200 unset($i);
202 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . ': ' . $table . $crlf . '%' . $crlf
203 . ' \\begin{longtable}{|';
205 for ($index=0;$index<$columns_cnt;$index++) {
206 $buffer .= 'l|';
208 $buffer .= '} ' . $crlf ;
210 $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
211 if (isset($GLOBALS['latex_caption'])) {
212 $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_caption'])
213 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_data_label']) . '} \\\\';
215 if (!PMA_exportOutputHandler($buffer)) {
216 return FALSE;
219 // show column names
220 if (isset($GLOBALS['latex_columns'])) {
221 $buffer = '\\hline ';
222 for ($i = 0; $i < $columns_cnt; $i++) {
223 $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' . PMA_texEscape(stripslashes($columns[$i])) . '}} & ';
226 $buffer = substr($buffer, 0, -2) . '\\\\ \\hline \hline ';
227 if (!PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) {
228 return FALSE;
230 if (isset($GLOBALS['latex_caption'])) {
231 if (!PMA_exportOutputHandler('\\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_continued_caption']) . '} \\\\ ')) return FALSE;
233 if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
234 return FALSE;
236 } else {
237 if (!PMA_exportOutputHandler('\\\\ \hline')) {
238 return FALSE;
242 // print the whole table
243 while ($record = PMA_DBI_fetch_assoc($result)) {
245 $buffer = '';
246 // print each row
247 for ($i = 0; $i < $columns_cnt; $i++) {
248 if (isset($record[$columns[$i]])
249 && (! function_exists('is_null') || !is_null($record[$columns[$i]]))) {
250 $column_value = PMA_texEscape(stripslashes($record[$columns[$i]]));
251 } else {
252 $column_value = $GLOBALS['latex_null'];
255 // last column ... no need for & character
256 if ($i == ($columns_cnt - 1)) {
257 $buffer .= $column_value;
258 } else {
259 $buffer .= $column_value . " & ";
262 $buffer .= ' \\\\ \\hline ' . $crlf;
263 if (!PMA_exportOutputHandler($buffer)) {
264 return FALSE;
268 $buffer = ' \\end{longtable}' . $crlf;
269 if (!PMA_exportOutputHandler($buffer)) {
270 return FALSE;
273 PMA_DBI_free_result($result);
274 return TRUE;
276 } // end getTableLaTeX
279 * Returns $table's structure as LaTeX
281 * @param string the database name
282 * @param string the table name
283 * @param string the end of line sequence
284 * @param string the url to go back in case of error
285 * @param boolean whether to include relation comments
286 * @param boolean whether to include column comments
287 * @param boolean whether to include mime comments
288 * @param string future feature: support view dependencies
290 * @return bool Whether it suceeded
292 * @access public
294 // @@@ $strTableStructure
295 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
297 global $cfgRelation;
300 * Get the unique keys in the table
302 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
303 $keys_result = PMA_DBI_query($keys_query);
304 $unique_keys = array();
305 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
306 if ($key['Non_unique'] == 0) {
307 $unique_keys[] = $key['Column_name'];
310 PMA_DBI_free_result($keys_result);
313 * Gets fields properties
315 PMA_DBI_select_db($db);
316 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
317 $result = PMA_DBI_query($local_query);
318 $fields_cnt = PMA_DBI_num_rows($result);
320 // Check if we can use Relations (Mike Beck)
321 if ($do_relation && !empty($cfgRelation['relation'])) {
322 // Find which tables are related with the current one and write it in
323 // an array
324 $res_rel = PMA_getForeigners($db, $table);
326 if ($res_rel && count($res_rel) > 0) {
327 $have_rel = TRUE;
328 } else {
329 $have_rel = FALSE;
331 } else {
332 $have_rel = FALSE;
333 } // end if
336 * Displays the table structure
338 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf
339 . ' \\begin{longtable}{';
340 if (!PMA_exportOutputHandler($buffer)) {
341 return FALSE;
344 $columns_cnt = 4;
345 $alignment = '|l|c|c|c|';
346 if ($do_relation && $have_rel) {
347 $columns_cnt++;
348 $alignment .= 'l|';
350 if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
351 $columns_cnt++;
352 $alignment .= 'l|';
354 if ($do_mime && $cfgRelation['mimework']) {
355 $columns_cnt++;
356 $alignment .='l|';
358 $buffer = $alignment . '} ' . $crlf ;
360 $header = ' \\hline ';
361 $header .= '\\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strField'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strType'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strNull'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strDefault'] . '}}';
362 if ($do_relation && $have_rel) {
363 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}';
365 if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
366 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}';
367 $comments = PMA_getComments($db, $table);
369 if ($do_mime && $cfgRelation['mimework']) {
370 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
371 $mime_map = PMA_getMIME($db, $table, true);
374 $local_buffer = PMA_texEscape($table);
376 // Table caption for first page and label
377 if (isset($GLOBALS['latex_caption'])) {
378 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption'])
379 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label'])
380 . '} \\\\' . $crlf;
382 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
383 // Table caption on next pages
384 if (isset($GLOBALS['latex_caption'])) {
385 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption'])
386 . '} \\\\ ' . $crlf;
388 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
390 if (!PMA_exportOutputHandler($buffer)) {
391 return FALSE;
394 while ($row = PMA_DBI_fetch_assoc($result)) {
396 $type = $row['Type'];
397 // reformat mysql query output - staybyte - 9. June 2001
398 // loic1: set or enum types: slashes single quotes inside options
399 if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
400 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
401 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
402 $type_nowrap = '';
404 $binary = 0;
405 $unsigned = 0;
406 $zerofill = 0;
407 } else {
408 $type_nowrap = ' nowrap="nowrap"';
409 $type = eregi_replace('BINARY', '', $type);
410 $type = eregi_replace('ZEROFILL', '', $type);
411 $type = eregi_replace('UNSIGNED', '', $type);
412 if (empty($type)) {
413 $type = '&nbsp;';
416 $binary = eregi('BINARY', $row['Type']);
417 $unsigned = eregi('UNSIGNED', $row['Type']);
418 $zerofill = eregi('ZEROFILL', $row['Type']);
420 if (!isset($row['Default'])) {
421 if ($row['Null'] != '') {
422 $row['Default'] = 'NULL';
424 } else {
425 $row['Default'] = $row['Default'];
428 $field_name = $row['Field'];
430 $local_buffer = $field_name . "\000" . $type . "\000" . (($row['Null'] == '') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . "\000" . (isset($row['Default']) ? $row['Default'] : '');
432 if ($do_relation && $have_rel) {
433 $local_buffer .= "\000";
434 if (isset($res_rel[$field_name])) {
435 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
438 if ($do_comments && $cfgRelation['commwork']) {
439 $local_buffer .= "\000";
440 if (isset($comments[$field_name])) {
441 $local_buffer .= $comments[$field_name];
444 if ($do_mime && $cfgRelation['mimework']) {
445 $local_buffer .= "\000";
446 if (isset($mime_map[$field_name])) {
447 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
450 $local_buffer = PMA_texEscape($local_buffer);
451 if ($row['Key']=='PRI') {
452 $pos=strpos($local_buffer, "\000");
453 $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
455 if (in_array($field_name, $unique_keys)) {
456 $pos=strpos($local_buffer, "\000");
457 $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
459 $buffer = str_replace("\000", ' & ', $local_buffer);
460 $buffer .= ' \\\\ \\hline ' . $crlf;
462 if (!PMA_exportOutputHandler($buffer)) {
463 return FALSE;
465 } // end while
466 PMA_DBI_free_result($result);
468 $buffer = ' \\end{longtable}' . $crlf;
469 return PMA_exportOutputHandler($buffer);
470 } // end of the 'PMA_exportStructure' function
472 } // end else