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