b4aed5ee2acc07021213db6c251806a0ebcfe817
[civicrm-core.git] / CRM / Core / Report / Excel.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27 class CRM_Core_Report_Excel {
28
29 /**
30 * Code copied from phpMyAdmin (v2.6.1-pl3)
31 * File: PHPMYADMIN/libraries/export/csv.php
32 * Function: PMA_exportData
33 *
34 * Outputs a result set with a given header
35 * in the string buffer result
36 *
37 * @param string $header (reference ) column headers
38 * @param string $rows (reference ) result set rows
39 * @param null $titleHeader
40 * @param boolean $print should the output be printed
41 *
42 * @param bool $outputHeader
43 *
44 * @return mixed empty if output is printed, else output
45 *
46 * @access public
47 * @static
48 */
49 static function makeCSVTable(&$header, &$rows, $titleHeader = NULL, $print = TRUE, $outputHeader = TRUE) {
50 if ($titleHeader) {
51 echo $titleHeader;
52 }
53
54 $result = '';
55
56 $config = CRM_Core_Config::singleton();
57 $seperator = $config->fieldSeparator;
58 $enclosed = '"';
59 $escaped = $enclosed;
60 $add_character = "\015\012";
61
62 $schema_insert = '';
63 foreach ($header as $field) {
64 if ($enclosed == '') {
65 $schema_insert .= stripslashes($field);
66 }
67 else {
68 $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, stripslashes($field)) . $enclosed;
69 }
70 $schema_insert .= $seperator;
71 }
72 // end while
73
74 if ($outputHeader) {
75 // need to add PMA_exportOutputHandler functionality out here, rather than
76 // doing it the moronic way of assembling a buffer
77 $out = trim(substr($schema_insert, 0, -1)) . $add_character;
78 if ($print) {
79 echo $out;
80 }
81 else {
82 $result .= $out;
83 }
84 }
85
86 $fields_cnt = count($header);
87 foreach ($rows as $row) {
88 $schema_insert = '';
89 $colNo = 0;
90
91 foreach ($row as $j => $value) {
92 if (!isset($value) || is_null($value)) {
93 $schema_insert .= '';
94 }
95 elseif ($value == '0' || $value != '') {
96 // loic1 : always enclose fields
97 //$value = ereg_replace("\015(\012)?", "\012", $value);
98 $value = preg_replace("/\015(\012)?/", "\012", $value);
99 if ($enclosed == '') {
100 $schema_insert .= $value;
101 }
102 else {
103 if ((substr($value, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) &&
104 (substr($value, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)
105 ) {
106
107 $strArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
108
109 foreach ($strArray as $key => $val) {
110 if (trim($val) == '') {
111 unset($strArray[$key]);
112 }
113 }
114
115 $str = implode($seperator, $strArray);
116 $value = &$str;
117 }
118
119 $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, $value) . $enclosed;
120 }
121 }
122 else {
123 $schema_insert .= '';
124 }
125
126 if ($colNo < $fields_cnt - 1) {
127 $schema_insert .= $seperator;
128 }
129 $colNo++;
130 }
131 // end for
132
133 $out = $schema_insert . $add_character;
134 if ($print) {
135 echo $out;
136 }
137 else {
138 $result .= $out;
139 }
140 }
141
142 if ($print) {
143 return;
144 }
145 else {
146 return $result;
147 }
148 }
149
150 function writeHTMLFile($fileName, &$header, &$rows, $titleHeader = NULL, $outputHeader = TRUE) {
151 if ($outputHeader) {
152 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
153 'application/vnd.ms-excel',
154 CRM_Core_DAO::$_nullObject,
155 'xls',
156 FALSE
157 );
158 }
159
160 echo "<table><thead><tr>";
161 foreach ($header as $field) {
162 echo "<th>$field</th>";
163 }
164 echo "</tr></thead><tbody>";
165
166 foreach ($rows as $row) {
167 $schema_insert = '';
168 $colNo = 0;
169 echo "<tr>";
170 foreach ($row as $j => $value) {
171 echo "<td>" . htmlentities($value, ENT_COMPAT, 'UTF-8') . "</td>";
172 }
173 echo "</tr>";
174 }
175
176 echo "</tbody></table>";
177 }
178
179 /**
180 * Write a CSV file to the browser output
181 *
182 * @param string $fileName - the name of the file that will be downloaded (this is sent to the browser)
183 * @param array $header - an array of the headers
184 * @param array $rows - an array of arrays of the table contents
185 * @param string $titleHeader - if set this will be the title in the CSV
186 * @param boolean $outputHeader - should we output the header row
187 * @param boolean $saveFile -
188 *
189 * @return void
190 * @public
191 * @static
192 */
193 static function writeCSVFile($fileName, &$header, &$rows, $titleHeader = NULL, $outputHeader = TRUE, $saveFile = NULL) {
194 if ($outputHeader && !$saveFile) {
195 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
196 'text/x-csv',
197 CRM_Core_DAO::$_nullObject,
198 'csv',
199 FALSE
200 );
201 }
202
203 if (!empty($rows)) {
204 $print = TRUE;
205 if ($saveFile) {
206 $print = FALSE;
207 }
208 return self::makeCSVTable( $header, $rows, $titleHeader, $print, $outputHeader );
209 }
210 }
211
212 }
213