Merge pull request #15964 from mlutfy/financial109
[civicrm-core.git] / CRM / Core / Report / Excel.php
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 */
15 class 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 $add_character = "\015\012";
44
45 if ($outputHeader) {
46 self::outputHeaderRow($header);
47 }
48
49 $fields_cnt = count($header);
50 foreach ($rows as $row) {
51 $schema_insert = '';
52 $colNo = 0;
53
54 foreach ($row as $j => $value) {
55 if (!isset($value) || is_null($value) || $value === '') {
56 $schema_insert .= '';
57 }
58 else {
59 // loic1 : always enclose fields
60 //$value = ereg_replace("\015(\012)?", "\012", $value);
61 // Convert carriage return to line feed.
62 $value = preg_replace("/\015(\012)?/", "\012", $value);
63 if ((substr($value, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) &&
64 (substr($value, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)
65 ) {
66
67 $strArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
68 // Filter out empty value separated strings.
69 foreach ($strArray as $key => $val) {
70 if (trim($val) == '') {
71 unset($strArray[$key]);
72 }
73 }
74
75 $str = implode($seperator, $strArray);
76 $value = &$str;
77 }
78
79 $schema_insert .= '"' . str_replace('"', '""', $value) . '"';
80 }
81
82 if ($colNo < $fields_cnt - 1) {
83 $schema_insert .= $seperator;
84 }
85 $colNo++;
86 }
87 // end for
88
89 $out = $schema_insert . $add_character;
90 echo $out;
91 }
92 }
93
94 /**
95 * Output the header row for a csv file.
96 *
97 * @param array $header
98 * Array of field names.
99 */
100 public static function outputHeaderRow($header) {
101 $schema_insert = '';
102 $separator = Civi::settings()->get('fieldSeparator');
103 foreach ($header as $field) {
104 $schema_insert .= '"' . str_replace('"', '""', stripslashes($field)) . '"';
105 $schema_insert .= $separator;
106 }
107 // end while
108 // need to add PMA_exportOutputHandler functionality out here, rather than
109 // doing it the moronic way of assembling a buffer
110 // We append a hex newline at the end.
111 echo trim(substr($schema_insert, 0, -1)) . "\015\012";
112 }
113
114 /**
115 * @param string $fileName
116 * @param $header
117 * @param $rows
118 * @param null $titleHeader
119 * @param bool $outputHeader
120 */
121 public function writeHTMLFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
122 if ($outputHeader) {
123 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
124 'application/vnd.ms-excel',
125 CRM_Core_DAO::$_nullObject,
126 'xls',
127 FALSE
128 );
129 }
130
131 echo "<table><thead><tr>";
132 foreach ($header as $field) {
133 echo "<th>$field</th>";
134 }
135 echo "</tr></thead><tbody>";
136
137 foreach ($rows as $row) {
138 $schema_insert = '';
139 $colNo = 0;
140 echo "<tr>";
141 foreach ($row as $j => $value) {
142 echo "<td>" . htmlentities($value, ENT_COMPAT, 'UTF-8') . "</td>";
143 }
144 echo "</tr>";
145 }
146
147 echo "</tbody></table>";
148 }
149
150 /**
151 * Write a CSV file to the browser output.
152 *
153 * @param string $fileName
154 * The name of the file that will be downloaded (this is sent to the browser).
155 * @param array $header
156 * An array of the headers.
157 * @param array $rows
158 * An array of arrays of the table contents.
159 * @param string $titleHeader
160 * If set this will be the title in the CSV.
161 * @param bool $outputHeader
162 * Should we output the header row.
163 *
164 * @return void
165 */
166 public static function writeCSVFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
167 if ($outputHeader) {
168 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
169 'text/x-csv',
170 CRM_Core_DAO::$_nullObject,
171 'csv',
172 FALSE
173 );
174 }
175
176 if (!empty($rows)) {
177 return self::makeCSVTable($header, $rows, $titleHeader, $outputHeader);
178 }
179 }
180
181 }