Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportCsv.class.php
blob98558e9255b5220fafd59d0e281535a522dc3575
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * CSV export code
6 * @package PhpMyAdmin-Export
7 * @subpackage CSV
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 /**
17 * Handles the export for the CSV format
19 * @package PhpMyAdmin-Export
20 * @subpackage CSV
22 class ExportCsv extends ExportPlugin
24 /**
25 * The string used to end lines
27 * @var string
29 private $_csvTerminated;
31 /**
32 * The string used to separate columns
34 * @var string
36 private $_csvSeparator;
38 /**
39 * The string used to enclose columns
41 * @var string
43 private $_csvEnclosed;
45 /**
46 * The string used to escape columns
48 * @var string
50 private $_csvEscaped;
52 /**
53 * Constructor
55 public function __construct()
57 $this->setProperties();
60 /**
61 * Sets the export CSV properties
63 * @return void
65 protected function setProperties()
67 $props = 'libraries/properties/';
68 include_once "$props/plugins/ExportPluginProperties.class.php";
69 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
70 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
71 include_once "$props/options/items/TextPropertyItem.class.php";
72 include_once "$props/options/items/BoolPropertyItem.class.php";
73 include_once "$props/options/items/HiddenPropertyItem.class.php";
75 $exportPluginProperties = new ExportPluginProperties();
76 $exportPluginProperties->setText('CSV');
77 $exportPluginProperties->setExtension('csv');
78 $exportPluginProperties->setMimeType('text/comma-separated-values');
79 $exportPluginProperties->setOptionsText(__('Options'));
81 // create the root group that will be the options field for
82 // $exportPluginProperties
83 // this will be shown as "Format specific options"
84 $exportSpecificOptions = new OptionsPropertyRootGroup();
85 $exportSpecificOptions->setName("Format Specific Options");
87 // general options main group
88 $generalOptions = new OptionsPropertyMainGroup();
89 $generalOptions->setName("general_opts");
90 // create leaf items and add them to the group
91 $leaf = new TextPropertyItem();
92 $leaf->setName("separator");
93 $leaf->setText(__('Columns separated with:'));
94 $generalOptions->addProperty($leaf);
95 $leaf = new TextPropertyItem();
96 $leaf->setName("enclosed");
97 $leaf->setText(__('Columns enclosed with:'));
98 $generalOptions->addProperty($leaf);
99 $leaf = new TextPropertyItem();
100 $leaf->setName("escaped");
101 $leaf->setText(__('Columns escaped with:'));
102 $generalOptions->addProperty($leaf);
103 $leaf = new TextPropertyItem();
104 $leaf->setName("terminated");
105 $leaf->setText(__('Lines terminated with:'));
106 $generalOptions->addProperty($leaf);
107 $leaf = new TextPropertyItem();
108 $leaf->setName('null');
109 $leaf->setText(__('Replace NULL with:'));
110 $generalOptions->addProperty($leaf);
111 $leaf = new BoolPropertyItem();
112 $leaf->setName('removeCRLF');
113 $leaf->setText(
114 __('Remove carriage return/line feed characters within columns')
116 $generalOptions->addProperty($leaf);
117 $leaf = new BoolPropertyItem();
118 $leaf->setName('columns');
119 $leaf->setText(__('Put columns names in the first row'));
120 $generalOptions->addProperty($leaf);
121 $leaf = new HiddenPropertyItem();
122 $leaf->setName('structure_or_data');
123 $generalOptions->addProperty($leaf);
124 // add the main group to the root group
125 $exportSpecificOptions->addProperty($generalOptions);
127 // set the options for the export plugin property item
128 $exportPluginProperties->setOptions($exportSpecificOptions);
129 $this->properties = $exportPluginProperties;
133 * This method is called when any PluginManager to which the observer
134 * is attached calls PluginManager::notify()
136 * @param SplSubject $subject The PluginManager notifying the observer
137 * of an update.
139 * @return void
141 public function update (SplSubject $subject)
146 * Outputs export header
148 * @return bool Whether it succeeded
150 public function exportHeader ()
152 global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
154 // Here we just prepare some values for export
155 if ($what == 'excel') {
156 $csv_terminated = "\015\012";
157 switch($GLOBALS['excel_edition']) {
158 case 'win':
159 // as tested on Windows with Excel 2002 and Excel 2007
160 $csv_separator = ';';
161 break;
162 case 'mac_excel2003':
163 $csv_separator = ';';
164 break;
165 case 'mac_excel2008':
166 $csv_separator = ',';
167 break;
169 $csv_enclosed = '"';
170 $csv_escaped = '"';
171 if (isset($GLOBALS['excel_columns'])) {
172 $GLOBALS['csv_columns'] = 'yes';
174 } else {
175 if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
176 $csv_terminated = $GLOBALS['crlf'];
177 } else {
178 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
179 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
180 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
181 } // end if
182 $csv_separator = str_replace('\\t', "\011", $csv_separator);
185 return true;
189 * Outputs export footer
191 * @return bool Whether it succeeded
193 public function exportFooter ()
195 return true;
199 * Outputs database header
201 * @param string $db Database name
203 * @return bool Whether it succeeded
205 public function exportDBHeader ($db)
207 return true;
211 * Outputs database footer
213 * @param string $db Database name
215 * @return bool Whether it succeeded
217 public function exportDBFooter ($db)
219 return true;
223 * Outputs CREATE DATABASE statement
225 * @param string $db Database name
227 * @return bool Whether it succeeded
229 public function exportDBCreate($db)
231 return true;
235 * Outputs the content of a table in CSV format
237 * @param string $db database name
238 * @param string $table table name
239 * @param string $crlf the end of line sequence
240 * @param string $error_url the url to go back in case of error
241 * @param string $sql_query SQL query for obtaining data
243 * @return bool Whether it succeeded
245 public function exportData($db, $table, $crlf, $error_url, $sql_query)
247 global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
249 // Gets the data from the database
250 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
251 $fields_cnt = PMA_DBI_num_fields($result);
253 // If required, get fields name at the first line
254 if (isset($GLOBALS['csv_columns'])) {
255 $schema_insert = '';
256 for ($i = 0; $i < $fields_cnt; $i++) {
257 if ($csv_enclosed == '') {
258 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
259 } else {
260 $schema_insert .= $csv_enclosed
261 . str_replace(
262 $csv_enclosed,
263 $csv_escaped . $csv_enclosed,
264 stripslashes(PMA_DBI_field_name($result, $i))
266 . $csv_enclosed;
268 $schema_insert .= $csv_separator;
269 } // end for
270 $schema_insert = trim(substr($schema_insert, 0, -1));
271 if (! PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
272 return false;
274 } // end if
276 // Format the data
277 while ($row = PMA_DBI_fetch_row($result)) {
278 $schema_insert = '';
279 for ($j = 0; $j < $fields_cnt; $j++) {
280 if (! isset($row[$j]) || is_null($row[$j])) {
281 $schema_insert .= $GLOBALS[$what . '_null'];
282 } elseif ($row[$j] == '0' || $row[$j] != '') {
283 // always enclose fields
284 if ($what == 'excel') {
285 $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
287 // remove CRLF characters within field
288 if (isset($GLOBALS[$what . '_removeCRLF'])
289 && $GLOBALS[$what . '_removeCRLF']
291 $row[$j] = str_replace(
292 "\n",
294 str_replace(
295 "\r",
297 $row[$j]
301 if ($csv_enclosed == '') {
302 $schema_insert .= $row[$j];
303 } else {
304 // also double the escape string if found in the data
305 if ($csv_escaped != $csv_enclosed) {
306 $schema_insert .= $csv_enclosed
307 . str_replace(
308 $csv_enclosed,
309 $csv_escaped . $csv_enclosed,
310 str_replace(
311 $csv_escaped,
312 $csv_escaped . $csv_escaped,
313 $row[$j]
316 . $csv_enclosed;
317 } else {
318 // avoid a problem when escape string equals enclose
319 $schema_insert .= $csv_enclosed
320 . str_replace(
321 $csv_enclosed,
322 $csv_escaped . $csv_enclosed,
323 $row[$j]
325 . $csv_enclosed;
328 } else {
329 $schema_insert .= '';
331 if ($j < $fields_cnt-1) {
332 $schema_insert .= $csv_separator;
334 } // end for
336 if (! PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
337 return false;
339 } // end while
340 PMA_DBI_free_result($result);
342 return true;