updated adodb package to work with php 7.1
[openemr.git] / vendor / adodb / adodb-php / toexport.inc.php
blob94c394b8eed93e559c1656f0b22509f1a25a7e1c
1 <?php
3 /**
4 * @version v5.20.9 21-Dec-2016
5 * @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
6 * @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
7 * Released under both BSD license and Lesser GPL library license.
8 * Whenever there is any discrepancy between the two licenses,
9 * the BSD license will take precedence.
11 * Code to export recordsets in several formats:
13 * AS VARIABLE
14 * $s = rs2csv($rs); # comma-separated values
15 * $s = rs2tab($rs); # tab delimited
17 * TO A FILE
18 * $f = fopen($path,'w');
19 * rs2csvfile($rs,$f);
20 * fclose($f);
22 * TO STDOUT
23 * rs2csvout($rs);
26 // returns a recordset as a csv string
27 function rs2csv(&$rs,$addtitles=true)
29 return _adodb_export($rs,',',',',false,$addtitles);
32 // writes recordset to csv file
33 function rs2csvfile(&$rs,$fp,$addtitles=true)
35 _adodb_export($rs,',',',',$fp,$addtitles);
38 // write recordset as csv string to stdout
39 function rs2csvout(&$rs,$addtitles=true)
41 $fp = fopen('php://stdout','wb');
42 _adodb_export($rs,',',',',true,$addtitles);
43 fclose($fp);
46 function rs2tab(&$rs,$addtitles=true)
48 return _adodb_export($rs,"\t",',',false,$addtitles);
51 // to file pointer
52 function rs2tabfile(&$rs,$fp,$addtitles=true)
54 _adodb_export($rs,"\t",',',$fp,$addtitles);
57 // to stdout
58 function rs2tabout(&$rs,$addtitles=true)
60 $fp = fopen('php://stdout','wb');
61 _adodb_export($rs,"\t",' ',true,$addtitles);
62 if ($fp) fclose($fp);
65 function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
67 if (!$rs) return '';
68 //----------
69 // CONSTANTS
70 $NEWLINE = "\r\n";
71 $BUFLINES = 100;
72 $escquotequote = $escquote.$quote;
73 $s = '';
75 if ($addtitles) {
76 $fieldTypes = $rs->FieldTypesArray();
77 reset($fieldTypes);
78 $i = 0;
79 $elements = array();
80 while(list(,$o) = each($fieldTypes)) {
82 $v = ($o) ? $o->name : 'Field'.($i++);
83 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
84 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
85 $elements[] = $v;
88 $s .= implode($sep, $elements).$NEWLINE;
90 $hasNumIndex = isset($rs->fields[0]);
92 $line = 0;
93 $max = $rs->FieldCount();
95 while (!$rs->EOF) {
96 $elements = array();
97 $i = 0;
99 if ($hasNumIndex) {
100 for ($j=0; $j < $max; $j++) {
101 $v = $rs->fields[$j];
102 if (!is_object($v)) $v = trim($v);
103 else $v = 'Object';
104 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
105 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
107 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
108 else $elements[] = $v;
110 } else { // ASSOCIATIVE ARRAY
111 foreach($rs->fields as $v) {
112 if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
113 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
115 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
116 else $elements[] = $v;
119 $s .= implode($sep, $elements).$NEWLINE;
120 $rs->MoveNext();
121 $line += 1;
122 if ($fp && ($line % $BUFLINES) == 0) {
123 if ($fp === true) echo $s;
124 else fwrite($fp,$s);
125 $s = '';
129 if ($fp) {
130 if ($fp === true) echo $s;
131 else fwrite($fp,$s);
132 $s = '';
135 return $s;