Fix for the Open in New Window in Patient/Client->Patients search gui, take 2.
[openemr.git] / phpmyadmin / libraries / export / xls.php
blob7f7ef27e69b635418accbc00dfce15a65698f418
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build XLS dumps of tables
6 * @version $Id$
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 // Check if we have native MS Excel export using PEAR class Spreadsheet_Excel_Writer
16 if (!empty($GLOBALS['cfg']['TempDir'])) {
17 @include_once 'Spreadsheet/Excel/Writer.php';
18 if (class_exists('Spreadsheet_Excel_Writer')) {
19 $xls = TRUE;
20 } else {
21 $xls = FALSE;
23 } else {
24 $xls = FALSE;
27 if ($xls) {
29 if (isset($plugin_list)) {
30 $plugin_list['xls'] = array(
31 'text' => 'strStrucNativeExcel',
32 'extension' => 'xls',
33 'mime_type' => 'application/vnd.ms-excel',
34 'force_file' => true,
35 'options' => array(
36 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
37 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
38 array('type' => 'hidden', 'name' => 'data'),
40 'options_text' => 'strOptions',
42 } else {
44 /**
45 * Set of functions used to build MS Excel dumps of tables
48 /**
49 * Outputs comment
51 * @param string Text of comment
53 * @return bool Whether it suceeded
55 function PMA_exportComment($text)
57 return TRUE;
60 /**
61 * Outputs export footer
63 * @return bool Whether it suceeded
65 * @access public
67 function PMA_exportFooter()
69 global $workbook;
70 global $tmp_filename;
72 $res = $workbook->close();
73 if (PEAR::isError($res)) {
74 echo $res->getMessage();
75 return FALSE;
77 if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) {
78 return FALSE;
80 unlink($tmp_filename);
82 return TRUE;
85 /**
86 * Outputs export header
88 * @return bool Whether it suceeded
90 * @access public
92 function PMA_exportHeader()
94 global $workbook;
95 global $tmp_filename;
97 if (empty($GLOBALS['cfg']['TempDir'])) {
98 return FALSE;
100 $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xls_');
101 $workbook = new Spreadsheet_Excel_Writer($tmp_filename);
103 return TRUE;
107 * Outputs database header
109 * @param string Database name
111 * @return bool Whether it suceeded
113 * @access public
115 function PMA_exportDBHeader($db)
117 return TRUE;
121 * Outputs database footer
123 * @param string Database name
125 * @return bool Whether it suceeded
127 * @access public
129 function PMA_exportDBFooter($db)
131 return TRUE;
135 * Outputs create database database
137 * @param string Database name
139 * @return bool Whether it suceeded
141 * @access public
143 function PMA_exportDBCreate($db)
145 return TRUE;
149 * Outputs the content of a table in CSV format
151 * @param string the database name
152 * @param string the table name
153 * @param string the end of line sequence
154 * @param string the url to go back in case of error
155 * @param string SQL query for obtaining data
157 * @return bool Whether it suceeded
159 * @access public
161 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
163 global $what;
164 global $workbook;
166 $workbook->setTempDir(realpath($GLOBALS['cfg']['TempDir']));
168 // Gets the data from the database
169 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
170 $fields_cnt = PMA_DBI_num_fields($result);
172 $row = PMA_DBI_fetch_row($result);
173 for ($sheetIndex = 0; ; $sheetIndex++) {
174 // Maximum sheet name length is 31 chars - leave 2 for numeric index
175 $sheetName = substr($table, 0, 29) . ($sheetIndex > 0 ? $sheetIndex : '');
176 $worksheet =& $workbook->addWorksheet($sheetName);
177 $rowIndex = 0;
179 // If required, get fields name at the first line
180 if (isset($GLOBALS['xls_columns']) && $GLOBALS['xls_columns']) {
181 for ($i = 0; $i < $fields_cnt; $i++) {
182 $worksheet->write(0, $i, stripslashes(PMA_DBI_field_name($result, $i)));
183 } // end for
184 $worksheet->repeatRows($rowIndex);
185 $worksheet->freezePanes(array($rowIndex + 1, 0, $rowIndex + 1, 0));
186 $rowIndex++;
187 } // end if
189 // Format the data (max 65536 rows per worksheet)
190 while ($rowIndex < 65536 && $row) {
191 set_time_limit(0);
192 for ($j = 0; $j < $fields_cnt; $j++) {
193 if (!isset($row[$j]) || is_null($row[$j])) {
194 $worksheet->write($rowIndex, $j, $GLOBALS['xls_null']);
195 } elseif ($row[$j] == '0' || $row[$j] != '') {
197 * @todo we should somehow handle character set here!
199 $worksheet->write($rowIndex, $j, $row[$j]);
200 } else {
201 $worksheet->write($rowIndex, $j, '');
203 } // end for
204 $rowIndex++;
205 $row = PMA_DBI_fetch_row($result);
206 } // end while
207 if (!$row) {
208 break;
210 } // end for
211 PMA_DBI_free_result($result);
213 return TRUE;