Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
4c6ce474 EM |
11 | |
12 | /** | |
13 | * Class CRM_Core_Report_Excel | |
14 | */ | |
6a488035 TO |
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 | * | |
e39a0000 | 25 | * @param array $header |
26 | * column headers. | |
27 | * @param array $rows | |
28 | * result set rows. | |
29 | * @param string $titleHeader | |
77b97be7 | 30 | * @param bool $outputHeader |
6a488035 | 31 | * |
72b3a70c CW |
32 | * @return mixed |
33 | * empty if output is printed, else output | |
6a488035 | 34 | * |
6a488035 | 35 | */ |
8a188f37 | 36 | public static function makeCSVTable($header, $rows, $titleHeader = NULL, $outputHeader = TRUE) { |
6a488035 TO |
37 | if ($titleHeader) { |
38 | echo $titleHeader; | |
39 | } | |
40 | ||
353ffa53 TO |
41 | $config = CRM_Core_Config::singleton(); |
42 | $seperator = $config->fieldSeparator; | |
43 | $enclosed = '"'; | |
44 | $escaped = $enclosed; | |
6a488035 TO |
45 | $add_character = "\015\012"; |
46 | ||
47 | $schema_insert = ''; | |
48 | foreach ($header as $field) { | |
8c5f84b6 | 49 | $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, stripslashes($field)) . $enclosed; |
6a488035 TO |
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; | |
8a188f37 | 58 | echo $out; |
6a488035 TO |
59 | } |
60 | ||
6a488035 | 61 | $fields_cnt = count($header); |
6a488035 TO |
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); | |
8c5f84b6 | 74 | if ((substr($value, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) && |
75 | (substr($value, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) | |
76 | ) { | |
6a488035 | 77 | |
8c5f84b6 | 78 | $strArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); |
6a488035 | 79 | |
8c5f84b6 | 80 | foreach ($strArray as $key => $val) { |
81 | if (trim($val) == '') { | |
82 | unset($strArray[$key]); | |
6a488035 | 83 | } |
6a488035 TO |
84 | } |
85 | ||
8c5f84b6 | 86 | $str = implode($seperator, $strArray); |
87 | $value = &$str; | |
6a488035 | 88 | } |
8c5f84b6 | 89 | |
90 | $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, $value) . $enclosed; | |
6a488035 TO |
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; | |
8a188f37 | 104 | echo $out; |
6a488035 TO |
105 | } |
106 | } | |
51da6b03 | 107 | |
a0ee3941 | 108 | /** |
100fef9d | 109 | * @param string $fileName |
a0ee3941 EM |
110 | * @param $header |
111 | * @param $rows | |
112 | * @param null $titleHeader | |
113 | * @param bool $outputHeader | |
114 | */ | |
e39a0000 | 115 | public function writeHTMLFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) { |
6a488035 TO |
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 | } | |
51da6b03 | 124 | |
6a488035 TO |
125 | echo "<table><thead><tr>"; |
126 | foreach ($header as $field) { | |
127 | echo "<th>$field</th>"; | |
128 | } | |
6a488035 | 129 | echo "</tr></thead><tbody>"; |
6a488035 TO |
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 | } | |
6a488035 TO |
138 | echo "</tr>"; |
139 | } | |
51da6b03 | 140 | |
6a488035 TO |
141 | echo "</tbody></table>"; |
142 | } | |
51da6b03 DL |
143 | |
144 | /** | |
fe482240 | 145 | * Write a CSV file to the browser output. |
51da6b03 | 146 | * |
6a0b768e TO |
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. | |
51da6b03 DL |
157 | * |
158 | * @return void | |
51da6b03 | 159 | */ |
5ae1c8dc | 160 | public static function writeCSVFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) { |
161 | if ($outputHeader) { | |
6a488035 TO |
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)) { | |
8a188f37 | 171 | return self::makeCSVTable($header, $rows, $titleHeader, $outputHeader); |
6a488035 TO |
172 | } |
173 | } | |
51da6b03 | 174 | |
6a488035 | 175 | } |