Correct missing instances of xl in various files.
[openemr.git] / interface / main / calendar / pnadodb / toexport.inc.php
blob312f0ebe2926d3d612d30c2d19e0438009100df5
1 <?php
3 /**
4 * @version V3.60 16 June 2003 (c) 2000-2003 John Lim (jlim@natsoft.com.my). All rights reserved.
5 * Released under both BSD license and Lesser GPL library license.
6 * Whenever there is any discrepancy between the two licenses,
7 * the BSD license will take precedence.
9 * Code to export recordsets in several formats:
11 * AS VARIABLE
12 * $s = rs2csv($rs); # comma-separated values
13 * $s = rs2tab($rs); # tab delimited
15 * TO A FILE
16 * $f = fopen($path,'w');
17 * rs2csvfile($rs,$f);
18 * fclose($f);
20 * TO STDOUT
21 * rs2csvout($rs);
24 // returns a recordset as a csv string
25 function rs2csv(&$rs,$addtitles=true)
27 return _adodb_export($rs,',',',',false,$addtitles);
30 // writes recordset to csv file
31 function rs2csvfile(&$rs,$fp,$addtitles=true)
33 _adodb_export($rs,',',',',$fp,$addtitles);
36 // write recordset as csv string to stdout
37 function rs2csvout(&$rs,$addtitles=true)
39 $fp = fopen('php://stdout','wb');
40 _adodb_export($rs,',',',',true,$addtitles);
41 fclose($fp);
44 function rs2tab(&$rs,$addtitles=true)
46 return _adodb_export($rs,"\t",',',false,$addtitles);
49 // to file pointer
50 function rs2tabfile(&$rs,$fp,$addtitles=true)
52 _adodb_export($rs,"\t",',',$fp,$addtitles);
55 // to stdout
56 function rs2tabout(&$rs,$addtitles=true)
58 $fp = fopen('php://stdout','wb');
59 _adodb_export($rs,"\t",' ',true,$addtitles);
60 fclose($fp);
63 function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
65 if (!$rs) return '';
66 //----------
67 // CONSTANTS
68 $NEWLINE = "\r\n";
69 $BUFLINES = 100;
70 $escquotequote = $escquote.$quote;
71 $s = '';
73 if ($addtitles) {
74 $fieldTypes = $rs->FieldTypesArray();
75 foreach($fieldTypes as $o) {
77 $v = $o->name;
78 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
79 $v = strip_tags(str_replace("\n",$replaceNewLine,str_replace($sep,$sepreplace,$v)));
80 $elements[] = $v;
83 $s .= implode($sep, $elements).$NEWLINE;
85 $hasNumIndex = isset($rs->fields[0]);
87 $line = 0;
88 $max = $rs->FieldCount();
90 while (!$rs->EOF) {
91 $elements = array();
92 $i = 0;
94 if ($hasNumIndex) {
95 for ($j=0; $j < $max; $j++) {
96 $v = trim($rs->fields[$j]);
97 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
98 $v = strip_tags(str_replace("\n",$replaceNewLine,str_replace($sep,$sepreplace,$v)));
100 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
101 else $elements[] = $v;
103 } else { // ASSOCIATIVE ARRAY
104 foreach($rs->fields as $v) {
105 if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
106 $v = strip_tags(str_replace("\n",$replaceNewLine,str_replace($sep,$sepreplace,$v)));
108 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
109 else $elements[] = $v;
112 $s .= implode($sep, $elements).$NEWLINE;
113 $rs->MoveNext();
114 $line += 1;
115 if ($fp && ($line % $BUFLINES) == 0) {
116 if ($fp === true) echo $s;
117 else fwrite($fp,$s);
118 $s = '';
122 if ($fp) {
123 if ($fp === true) echo $s;
124 else fwrite($fp,$s);
125 $s = '';
128 return $s;