Total Complexity | 43 |
Total Lines | 255 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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 = '"') |
||
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, array $header = []) |
||
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 (!empty($header)) { |
||
69 | fputcsv($handle, $header, ';'); |
||
70 | } |
||
71 | |||
72 | foreach ($data as $row) { |
||
73 | fputcsv($handle, (array)$row, ';'); |
||
74 | } |
||
75 | |||
76 | fclose($handle); |
||
77 | |||
78 | if (!$writeOnly) { |
||
79 | DocumentManager::file_send_for_download($file, true, $filename . '.csv'); |
||
80 | unlink($file); |
||
81 | exit; |
||
82 | } |
||
83 | |||
84 | return $file; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Export tabular data to XLS-file. |
||
89 | */ |
||
90 | public static function arrayToXls(array $data, string $filename = 'export') |
||
91 | { |
||
92 | if (empty($data)) { |
||
93 | return false; |
||
94 | } |
||
95 | |||
96 | $spreadsheet = new Spreadsheet(); |
||
97 | $sheet = $spreadsheet->getActiveSheet(); |
||
98 | $rowNumber = 1; |
||
99 | foreach ($data as $row) { |
||
100 | $colNumber = 'A'; |
||
101 | foreach ($row as $cell) { |
||
102 | $sheet->setCellValue($colNumber . $rowNumber, $cell); |
||
103 | $colNumber++; |
||
104 | } |
||
105 | $rowNumber++; |
||
106 | } |
||
107 | |||
108 | $filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xlsx'; |
||
109 | $writer = new Xlsx($spreadsheet); |
||
110 | $writer->save($filePath); |
||
111 | |||
112 | DocumentManager::file_send_for_download($filePath, true, $filename.'.xlsx'); |
||
113 | exit; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Export tabular data to XLS-file (as html table). |
||
118 | * |
||
119 | * @param array $data |
||
120 | * @param string $filename |
||
121 | */ |
||
122 | public static function export_table_xls_html($data, $filename = 'export', $encoding = 'utf-8') |
||
123 | { |
||
124 | $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls'; |
||
125 | $handle = fopen($file, 'a+'); |
||
126 | $systemEncoding = api_get_system_encoding(); |
||
127 | fwrite($handle, '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html" charset="'.$encoding.'" /><body><table>'); |
||
128 | foreach ($data as $id => $row) { |
||
129 | foreach ($row as $id2 => $row2) { |
||
130 | $data[$id][$id2] = api_htmlentities($row2); |
||
131 | } |
||
132 | } |
||
133 | foreach ($data as $row) { |
||
134 | $string = implode("</td><td>", $row); |
||
135 | $string = '<tr><td>'.$string.'</td></tr>'; |
||
136 | if ('utf-8' != $encoding) { |
||
137 | $string = api_convert_encoding($string, $encoding, $systemEncoding); |
||
138 | } |
||
139 | fwrite($handle, $string."\n"); |
||
140 | } |
||
141 | fwrite($handle, '</table></body></html>'); |
||
142 | fclose($handle); |
||
143 | DocumentManager::file_send_for_download($file, true, $filename.'.xls'); |
||
144 | exit; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Export tabular data to XML-file. |
||
149 | * |
||
150 | * @param array Simple array of data to put in XML |
||
151 | * @param string Name of file to be given to the user |
||
152 | * @param string Name of common tag to place each line in |
||
153 | * @param string Name of the root element. A root element should always be given. |
||
154 | * @param string Encoding in which the data is provided |
||
155 | */ |
||
156 | public static function arrayToXml( |
||
157 | $data, |
||
158 | $filename = 'export', |
||
159 | $item_tagname = 'item', |
||
160 | $wrapper_tagname = null, |
||
161 | $encoding = null |
||
162 | ) { |
||
163 | if (empty($encoding)) { |
||
164 | $encoding = api_get_system_encoding(); |
||
165 | } |
||
166 | $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml'; |
||
167 | $handle = fopen($file, 'a+'); |
||
168 | fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n"); |
||
169 | if (!is_null($wrapper_tagname)) { |
||
170 | fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n"); |
||
171 | } |
||
172 | foreach ($data as $row) { |
||
173 | fwrite($handle, '<'.$item_tagname.'>'."\n"); |
||
174 | foreach ($row as $key => $value) { |
||
175 | fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n"); |
||
176 | } |
||
177 | fwrite($handle, "\t".'</'.$item_tagname.'>'."\n"); |
||
178 | } |
||
179 | if (!is_null($wrapper_tagname)) { |
||
180 | fwrite($handle, '</'.$wrapper_tagname.'>'."\n"); |
||
181 | } |
||
182 | fclose($handle); |
||
183 | DocumentManager::file_send_for_download($file, true, $filename.'.xml'); |
||
184 | exit; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param array $data table to be read with the HTML_table class |
||
189 | */ |
||
190 | public static function export_table_pdf($data, $params = []) |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param string $html |
||
202 | * @param array $params |
||
203 | */ |
||
204 | public static function export_html_to_pdf($html, $params = []) |
||
205 | { |
||
206 | $params['format'] = isset($params['format']) ? $params['format'] : 'A4'; |
||
207 | $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P'; |
||
208 | |||
209 | $pdf = new PDF($params['format'], $params['orientation'], $params); |
||
210 | $pdf->html_to_pdf_with_template($html); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param array $data |
||
215 | * @param array $params |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | public static function convert_array_to_html($data, $params = []) |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Export HTML content in a ODF document. |
||
256 | * |
||
257 | * @param string $html |
||
258 | * @param string $name |
||
259 | * @param string $format |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public static function htmlToOdt($html, $name, $format = 'odt') |
||
272 | /*$fs = new Filesystem(); |
||
273 | $paths = [ |
||
274 | 'root_sys' => api_get_path(SYS_PATH), |
||
275 | 'path.temp' => api_get_path(SYS_ARCHIVE_PATH), |
||
319 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.