Total Complexity | 45 |
Total Lines | 259 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Export often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Export, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Export |
||
18 | { |
||
19 | /** |
||
20 | * Constructor. |
||
21 | */ |
||
22 | public function __construct() |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Export tabular data to CSV-file. |
||
28 | * |
||
29 | * @return mixed csv raw data | false if no data to export | string file path if success in $writeOnly mode |
||
30 | */ |
||
31 | public static function arrayToCsv(array $data, string $filename = 'export', bool $writeOnly = false, string $enclosure = '"') |
||
32 | { |
||
33 | if (empty($data)) { |
||
34 | return false; |
||
35 | } |
||
36 | |||
37 | $enclosure = !empty($enclosure) ? $enclosure : '"'; |
||
38 | $filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv'; |
||
39 | $writer = new CsvWriter($filePath, ';', $enclosure, true); |
||
40 | |||
41 | $source = new ArraySourceIterator($data); |
||
42 | $handler = Handler::create($source, $writer); |
||
43 | $handler->export(); |
||
44 | |||
45 | if (!$writeOnly) { |
||
46 | DocumentManager::file_send_for_download($filePath, true, $filename.'.csv'); |
||
47 | exit; |
||
|
|||
48 | } |
||
49 | |||
50 | return $filePath; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Converts an array of data into a CSV file and optionally sends it for download. |
||
55 | * |
||
56 | * @return string|void Returns the file path if $writeOnly is true, otherwise sends the file for download and exits. |
||
57 | */ |
||
58 | public static function arrayToCsvSimple(array $data, string $filename = 'export', bool $writeOnly = false) |
||
59 | { |
||
60 | $file = api_get_path(SYS_ARCHIVE_PATH) . uniqid('') . '.csv'; |
||
61 | |||
62 | $handle = fopen($file, 'w'); |
||
63 | |||
64 | if ($handle === false) { |
||
65 | throw new \RuntimeException("Unable to create or open the file: $file"); |
||
66 | } |
||
67 | |||
68 | if (is_array($data)) { |
||
69 | foreach ($data as $row) { |
||
70 | $line = ''; |
||
71 | if (is_array($row)) { |
||
72 | foreach ($row as $value) { |
||
73 | $line .= '"' . str_replace('"', '""', (string)$value) . '";'; |
||
74 | } |
||
75 | } |
||
76 | fwrite($handle, rtrim($line, ';') . "\n"); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | fclose($handle); |
||
81 | |||
82 | if (!$writeOnly) { |
||
83 | DocumentManager::file_send_for_download($file, true, $filename . '.csv'); |
||
84 | unlink($file); |
||
85 | exit; |
||
86 | } |
||
87 | |||
88 | return $file; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Export tabular data to XLS-file. |
||
93 | */ |
||
94 | public static function arrayToXls(array $data, string $filename = 'export') |
||
95 | { |
||
96 | if (empty($data)) { |
||
97 | return false; |
||
98 | } |
||
99 | |||
100 | $spreadsheet = new Spreadsheet(); |
||
101 | $sheet = $spreadsheet->getActiveSheet(); |
||
102 | $rowNumber = 1; |
||
103 | foreach ($data as $row) { |
||
104 | $colNumber = 'A'; |
||
105 | foreach ($row as $cell) { |
||
106 | $sheet->setCellValue($colNumber . $rowNumber, $cell); |
||
107 | $colNumber++; |
||
108 | } |
||
109 | $rowNumber++; |
||
110 | } |
||
111 | |||
112 | $filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xlsx'; |
||
113 | $writer = new Xlsx($spreadsheet); |
||
114 | $writer->save($filePath); |
||
115 | |||
116 | DocumentManager::file_send_for_download($filePath, true, $filename.'.xlsx'); |
||
117 | exit; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Export tabular data to XLS-file (as html table). |
||
122 | * |
||
123 | * @param array $data |
||
124 | * @param string $filename |
||
125 | */ |
||
126 | public static function export_table_xls_html($data, $filename = 'export', $encoding = 'utf-8') |
||
127 | { |
||
128 | $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls'; |
||
129 | $handle = fopen($file, 'a+'); |
||
130 | $systemEncoding = api_get_system_encoding(); |
||
131 | fwrite($handle, '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html" charset="'.$encoding.'" /><body><table>'); |
||
132 | foreach ($data as $id => $row) { |
||
133 | foreach ($row as $id2 => $row2) { |
||
134 | $data[$id][$id2] = api_htmlentities($row2); |
||
135 | } |
||
136 | } |
||
137 | foreach ($data as $row) { |
||
138 | $string = implode("</td><td>", $row); |
||
139 | $string = '<tr><td>'.$string.'</td></tr>'; |
||
140 | if ('utf-8' != $encoding) { |
||
141 | $string = api_convert_encoding($string, $encoding, $systemEncoding); |
||
142 | } |
||
143 | fwrite($handle, $string."\n"); |
||
144 | } |
||
145 | fwrite($handle, '</table></body></html>'); |
||
146 | fclose($handle); |
||
147 | DocumentManager::file_send_for_download($file, true, $filename.'.xls'); |
||
148 | exit; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Export tabular data to XML-file. |
||
153 | * |
||
154 | * @param array Simple array of data to put in XML |
||
155 | * @param string Name of file to be given to the user |
||
156 | * @param string Name of common tag to place each line in |
||
157 | * @param string Name of the root element. A root element should always be given. |
||
158 | * @param string Encoding in which the data is provided |
||
159 | */ |
||
160 | public static function arrayToXml( |
||
161 | $data, |
||
162 | $filename = 'export', |
||
163 | $item_tagname = 'item', |
||
164 | $wrapper_tagname = null, |
||
165 | $encoding = null |
||
166 | ) { |
||
167 | if (empty($encoding)) { |
||
168 | $encoding = api_get_system_encoding(); |
||
169 | } |
||
170 | $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml'; |
||
171 | $handle = fopen($file, 'a+'); |
||
172 | fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n"); |
||
173 | if (!is_null($wrapper_tagname)) { |
||
174 | fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n"); |
||
175 | } |
||
176 | foreach ($data as $row) { |
||
177 | fwrite($handle, '<'.$item_tagname.'>'."\n"); |
||
178 | foreach ($row as $key => $value) { |
||
179 | fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n"); |
||
180 | } |
||
181 | fwrite($handle, "\t".'</'.$item_tagname.'>'."\n"); |
||
182 | } |
||
183 | if (!is_null($wrapper_tagname)) { |
||
184 | fwrite($handle, '</'.$wrapper_tagname.'>'."\n"); |
||
185 | } |
||
186 | fclose($handle); |
||
187 | DocumentManager::file_send_for_download($file, true, $filename.'.xml'); |
||
188 | exit; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param array $data table to be read with the HTML_table class |
||
193 | */ |
||
194 | public static function export_table_pdf($data, $params = []) |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param string $html |
||
206 | * @param array $params |
||
207 | */ |
||
208 | public static function export_html_to_pdf($html, $params = []) |
||
209 | { |
||
210 | $params['format'] = isset($params['format']) ? $params['format'] : 'A4'; |
||
211 | $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P'; |
||
212 | |||
213 | $pdf = new PDF($params['format'], $params['orientation'], $params); |
||
214 | $pdf->html_to_pdf_with_template($html); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param array $data |
||
219 | * @param array $params |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public static function convert_array_to_html($data, $params = []) |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Export HTML content in a ODF document. |
||
260 | * |
||
261 | * @param string $html |
||
262 | * @param string $name |
||
263 | * @param string $format |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public static function htmlToOdt($html, $name, $format = 'odt') |
||
276 | /*$fs = new Filesystem(); |
||
277 | $paths = [ |
||
278 | 'root_sys' => api_get_path(SYS_PATH), |
||
279 | 'path.temp' => api_get_path(SYS_ARCHIVE_PATH), |
||
323 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.