Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: xls.php,v 2.1 2006/01/17 17:03:02 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 require_once('Spreadsheet/Excel/Writer.php');
6  
7 /**
8 * Set of functions used to build MS Excel dumps of tables
9 */
10  
11 /**
12 * Outputs comment
13 *
14 * @param string Text of comment
15 *
16 * @return bool Whether it suceeded
17 */
18 function PMA_exportComment($text)
19 {
20 return TRUE;
21 }
22  
23 /**
24 * Outputs export footer
25 *
26 * @return bool Whether it suceeded
27 *
28 * @access public
29 */
30 function PMA_exportFooter()
31 {
32 global $workbook;
33 global $tmp_filename;
34  
35 $res = $workbook->close();
36 if (PEAR::isError($res)) {
37 echo $res->getMessage();
38 return FALSE;
39 }
40 if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) {
41 return FALSE;
42 }
43 unlink($tmp_filename);
44  
45 return TRUE;
46 }
47  
48 /**
49 * Outputs export header
50 *
51 * @return bool Whether it suceeded
52 *
53 * @access public
54 */
55 function PMA_exportHeader()
56 {
57 global $workbook;
58 global $tmp_filename;
59  
60 if (empty($GLOBALS['cfg']['TempDir'])) {
61 return FALSE;
62 }
63 $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xls_');
64 $workbook = new Spreadsheet_Excel_Writer($tmp_filename);
65  
66 return TRUE;
67 }
68  
69 /**
70 * Outputs database header
71 *
72 * @param string Database name
73 *
74 * @return bool Whether it suceeded
75 *
76 * @access public
77 */
78 function PMA_exportDBHeader($db)
79 {
80 return TRUE;
81 }
82  
83 /**
84 * Outputs database footer
85 *
86 * @param string Database name
87 *
88 * @return bool Whether it suceeded
89 *
90 * @access public
91 */
92 function PMA_exportDBFooter($db)
93 {
94 return TRUE;
95 }
96  
97 /**
98 * Outputs create database database
99 *
100 * @param string Database name
101 *
102 * @return bool Whether it suceeded
103 *
104 * @access public
105 */
106 function PMA_exportDBCreate($db)
107 {
108 return TRUE;
109 }
110  
111 /**
112 * Outputs the content of a table in CSV format
113 *
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
119 *
120 * @return bool Whether it suceeded
121 *
122 * @access public
123 */
124 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
125 {
126 global $what;
127 global $workbook;
128  
129 $worksheet =& $workbook->addWorksheet($table);
130 $workbook->setTempDir(realpath($GLOBALS['cfg']['TempDir']));
131  
132 // Gets the data from the database
133 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
134 $fields_cnt = PMA_DBI_num_fields($result);
135 $col = 0;
136  
137 // If required, get fields name at the first line
138 if (isset($GLOBALS['xls_shownames']) && $GLOBALS['xls_shownames'] == 'yes') {
139 $schema_insert = '';
140 for ($i = 0; $i < $fields_cnt; $i++) {
141 $worksheet->write(0, $i, stripslashes(PMA_DBI_field_name($result, $i)));
142 } // end for
143 $col++;
144 } // end if
145  
146 // Format the data
147 while ($row = PMA_DBI_fetch_row($result)) {
148 $schema_insert = '';
149 for ($j = 0; $j < $fields_cnt; $j++) {
150 if (!isset($row[$j]) || is_null($row[$j])) {
151 $worksheet->write($col, $j, $GLOBALS['xls_replace_null']);
152 } elseif ($row[$j] == '0' || $row[$j] != '') {
153 // FIXME: we should somehow handle character set here!
154 $worksheet->write($col, $j, $row[$j]);
155 } else {
156 $worksheet->write($col, $j, '');
157 }
158 } // end for
159 $col++;
160 } // end while
161 PMA_DBI_free_result($result);
162  
163 return TRUE;
164 }
165 ?>