INFRA-132 - CRM/Contribute - Convert single-line @param to multi-line
[civicrm-core.git] / CRM / Core / Report / Excel.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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*/
4c6ce474
EM
27
28/**
29 * Class CRM_Core_Report_Excel
30 */
6a488035
TO
31class CRM_Core_Report_Excel {
32
33 /**
34 * Code copied from phpMyAdmin (v2.6.1-pl3)
35 * File: PHPMYADMIN/libraries/export/csv.php
36 * Function: PMA_exportData
37 *
38 * Outputs a result set with a given header
39 * in the string buffer result
40 *
77b97be7
EM
41 * @param string $header (reference ) column headers
42 * @param string $rows (reference ) result set rows
43 * @param null $titleHeader
44 * @param boolean $print should the output be printed
45 *
46 * @param bool $outputHeader
6a488035
TO
47 *
48 * @return mixed empty if output is printed, else output
49 *
25acfd6b 50 * @static
6a488035 51 */
00be9182 52 public static function makeCSVTable(&$header, &$rows, $titleHeader = NULL, $print = TRUE, $outputHeader = TRUE) {
6a488035
TO
53 if ($titleHeader) {
54 echo $titleHeader;
55 }
56
57 $result = '';
58
59 $config = CRM_Core_Config::singleton();
60 $seperator = $config->fieldSeparator;
61 $enclosed = '"';
62 $escaped = $enclosed;
63 $add_character = "\015\012";
64
65 $schema_insert = '';
66 foreach ($header as $field) {
67 if ($enclosed == '') {
68 $schema_insert .= stripslashes($field);
69 }
70 else {
71 $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, stripslashes($field)) . $enclosed;
72 }
73 $schema_insert .= $seperator;
74 }
75 // end while
76
77 if ($outputHeader) {
78 // need to add PMA_exportOutputHandler functionality out here, rather than
79 // doing it the moronic way of assembling a buffer
80 $out = trim(substr($schema_insert, 0, -1)) . $add_character;
81 if ($print) {
51da6b03
DL
82 echo $out;
83 }
6a488035
TO
84 else {
85 $result .= $out;
86 }
87 }
88
6a488035 89 $fields_cnt = count($header);
6a488035
TO
90 foreach ($rows as $row) {
91 $schema_insert = '';
92 $colNo = 0;
93
94 foreach ($row as $j => $value) {
95 if (!isset($value) || is_null($value)) {
96 $schema_insert .= '';
97 }
98 elseif ($value == '0' || $value != '') {
99 // loic1 : always enclose fields
100 //$value = ereg_replace("\015(\012)?", "\012", $value);
101 $value = preg_replace("/\015(\012)?/", "\012", $value);
102 if ($enclosed == '') {
103 $schema_insert .= $value;
104 }
105 else {
106 if ((substr($value, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) &&
107 (substr($value, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)
108 ) {
109
110 $strArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
111
112 foreach ($strArray as $key => $val) {
113 if (trim($val) == '') {
114 unset($strArray[$key]);
115 }
116 }
117
118 $str = implode($seperator, $strArray);
119 $value = &$str;
120 }
121
122 $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, $value) . $enclosed;
123 }
124 }
125 else {
126 $schema_insert .= '';
127 }
128
129 if ($colNo < $fields_cnt - 1) {
130 $schema_insert .= $seperator;
131 }
132 $colNo++;
133 }
134 // end for
135
136 $out = $schema_insert . $add_character;
137 if ($print) {
51da6b03
DL
138 echo $out;
139 }
6a488035
TO
140 else {
141 $result .= $out;
142 }
6a488035 143 }
51da6b03 144
6a488035
TO
145 if ($print) {
146 return;
147 }
148 else {
149 return $result;
150 }
151 }
51da6b03 152
a0ee3941 153 /**
100fef9d 154 * @param string $fileName
a0ee3941
EM
155 * @param $header
156 * @param $rows
157 * @param null $titleHeader
158 * @param bool $outputHeader
159 */
00be9182 160 public function writeHTMLFile($fileName, &$header, &$rows, $titleHeader = NULL, $outputHeader = TRUE) {
6a488035
TO
161 if ($outputHeader) {
162 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
163 'application/vnd.ms-excel',
164 CRM_Core_DAO::$_nullObject,
165 'xls',
166 FALSE
167 );
168 }
51da6b03 169
6a488035
TO
170 echo "<table><thead><tr>";
171 foreach ($header as $field) {
172 echo "<th>$field</th>";
173 }
6a488035 174 echo "</tr></thead><tbody>";
6a488035
TO
175
176 foreach ($rows as $row) {
177 $schema_insert = '';
178 $colNo = 0;
179 echo "<tr>";
180 foreach ($row as $j => $value) {
181 echo "<td>" . htmlentities($value, ENT_COMPAT, 'UTF-8') . "</td>";
182 }
6a488035
TO
183 echo "</tr>";
184 }
51da6b03 185
6a488035
TO
186 echo "</tbody></table>";
187 }
51da6b03
DL
188
189 /**
190 * Write a CSV file to the browser output
191 *
192 * @param string $fileName - the name of the file that will be downloaded (this is sent to the browser)
193 * @param array $header - an array of the headers
194 * @param array $rows - an array of arrays of the table contents
195 * @param string $titleHeader - if set this will be the title in the CSV
196 * @param boolean $outputHeader - should we output the header row
197 * @param boolean $saveFile -
198 *
199 * @return void
51da6b03
DL
200 * @static
201 */
00be9182 202 public static function writeCSVFile($fileName, &$header, &$rows, $titleHeader = NULL, $outputHeader = TRUE, $saveFile = NULL) {
51da6b03 203 if ($outputHeader && !$saveFile) {
6a488035
TO
204 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
205 'text/x-csv',
206 CRM_Core_DAO::$_nullObject,
207 'csv',
208 FALSE
209 );
210 }
211
212 if (!empty($rows)) {
51da6b03
DL
213 $print = TRUE;
214 if ($saveFile) {
215 $print = FALSE;
216 }
217 return self::makeCSVTable( $header, $rows, $titleHeader, $print, $outputHeader );
6a488035
TO
218 }
219 }
51da6b03 220
6a488035 221}