Remove unused parameter
[civicrm-core.git] / CRM / Core / Report / Excel.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * Class CRM_Core_Report_Excel
14 */
15class CRM_Core_Report_Excel {
16
17 /**
18 * Code copied from phpMyAdmin (v2.6.1-pl3)
19 * File: PHPMYADMIN/libraries/export/csv.php
20 * Function: PMA_exportData
21 *
22 * Outputs a result set with a given header
23 * in the string buffer result
24 *
25 * @param array $header
26 * column headers.
27 * @param array $rows
28 * result set rows.
29 * @param string $titleHeader
30 * @param bool $outputHeader
31 *
32 * @return mixed
33 * empty if output is printed, else output
34 *
35 */
36 public static function makeCSVTable($header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
37 if ($titleHeader) {
38 echo $titleHeader;
39 }
40
41 $config = CRM_Core_Config::singleton();
42 $seperator = $config->fieldSeparator;
43 $enclosed = '"';
44 $escaped = $enclosed;
45 $add_character = "\015\012";
46
47 $schema_insert = '';
48 foreach ($header as $field) {
49 $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, stripslashes($field)) . $enclosed;
50 $schema_insert .= $seperator;
51 }
52 // end while
53
54 if ($outputHeader) {
55 // need to add PMA_exportOutputHandler functionality out here, rather than
56 // doing it the moronic way of assembling a buffer
57 $out = trim(substr($schema_insert, 0, -1)) . $add_character;
58 echo $out;
59 }
60
61 $fields_cnt = count($header);
62 foreach ($rows as $row) {
63 $schema_insert = '';
64 $colNo = 0;
65
66 foreach ($row as $j => $value) {
67 if (!isset($value) || is_null($value)) {
68 $schema_insert .= '';
69 }
70 elseif ($value == '0' || $value != '') {
71 // loic1 : always enclose fields
72 //$value = ereg_replace("\015(\012)?", "\012", $value);
73 $value = preg_replace("/\015(\012)?/", "\012", $value);
74 if ((substr($value, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) &&
75 (substr($value, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)
76 ) {
77
78 $strArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
79
80 foreach ($strArray as $key => $val) {
81 if (trim($val) == '') {
82 unset($strArray[$key]);
83 }
84 }
85
86 $str = implode($seperator, $strArray);
87 $value = &$str;
88 }
89
90 $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, $value) . $enclosed;
91 }
92 else {
93 $schema_insert .= '';
94 }
95
96 if ($colNo < $fields_cnt - 1) {
97 $schema_insert .= $seperator;
98 }
99 $colNo++;
100 }
101 // end for
102
103 $out = $schema_insert . $add_character;
104 echo $out;
105 }
106 }
107
108 /**
109 * @param string $fileName
110 * @param $header
111 * @param $rows
112 * @param null $titleHeader
113 * @param bool $outputHeader
114 */
115 public function writeHTMLFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
116 if ($outputHeader) {
117 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
118 'application/vnd.ms-excel',
119 CRM_Core_DAO::$_nullObject,
120 'xls',
121 FALSE
122 );
123 }
124
125 echo "<table><thead><tr>";
126 foreach ($header as $field) {
127 echo "<th>$field</th>";
128 }
129 echo "</tr></thead><tbody>";
130
131 foreach ($rows as $row) {
132 $schema_insert = '';
133 $colNo = 0;
134 echo "<tr>";
135 foreach ($row as $j => $value) {
136 echo "<td>" . htmlentities($value, ENT_COMPAT, 'UTF-8') . "</td>";
137 }
138 echo "</tr>";
139 }
140
141 echo "</tbody></table>";
142 }
143
144 /**
145 * Write a CSV file to the browser output.
146 *
147 * @param string $fileName
148 * The name of the file that will be downloaded (this is sent to the browser).
149 * @param array $header
150 * An array of the headers.
151 * @param array $rows
152 * An array of arrays of the table contents.
153 * @param string $titleHeader
154 * If set this will be the title in the CSV.
155 * @param bool $outputHeader
156 * Should we output the header row.
157 *
158 * @return void
159 */
160 public static function writeCSVFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
161 if ($outputHeader) {
162 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
163 'text/x-csv',
164 CRM_Core_DAO::$_nullObject,
165 'csv',
166 FALSE
167 );
168 }
169
170 if (!empty($rows)) {
171 return self::makeCSVTable($header, $rows, $titleHeader, $outputHeader);
172 }
173 }
174
175}