Skip to content

Commit

Permalink
Internal: Fix CSV export formatting and score calculation issues - re…
Browse files Browse the repository at this point in the history
…fs BT#22096
  • Loading branch information
christianbeeznest committed Dec 3, 2024
1 parent e260348 commit 9b1f1e6
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 199 deletions.
45 changes: 39 additions & 6 deletions public/main/inc/lib/export.lib.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@ public function __construct()
/**
* Export tabular data to CSV-file.
*
* @param array $data
* @param string $filename
* @param bool $writeOnly Whether to only write on disk or also send for download
* @param string $enclosure
*
* @return mixed csv raw data | false if no data to export | string file path if success in $writeOnly mode
*/
public static function arrayToCsv($data, $filename = 'export', $writeOnly = false, $enclosure = '"')
public static function arrayToCsv(array $data, string $filename = 'export', bool $writeOnly = false, string $enclosure = '"')
{
if (empty($data)) {
return false;
Expand All @@ -55,6 +50,44 @@ public static function arrayToCsv($data, $filename = 'export', $writeOnly = fals
return $filePath;
}

/**
* Converts an array of data into a CSV file and optionally sends it for download.
*
* @return string|void Returns the file path if $writeOnly is true, otherwise sends the file for download and exits.
*/
public static function arrayToCsvSimple(array $data, string $filename = 'export', bool $writeOnly = false)
{
$file = api_get_path(SYS_ARCHIVE_PATH) . uniqid('') . '.csv';

$handle = fopen($file, 'w');

if ($handle === false) {
throw new \RuntimeException("Unable to create or open the file: $file");
}

if (is_array($data)) {
foreach ($data as $row) {
$line = '';
if (is_array($row)) {
foreach ($row as $value) {
$line .= '"' . str_replace('"', '""', (string)$value) . '";';
}
}
fwrite($handle, rtrim($line, ';') . "\n");
}
}

fclose($handle);

if (!$writeOnly) {
DocumentManager::file_send_for_download($file, true, $filename . '.csv');
unlink($file);
exit;
}

return $file;
}

/**
* Export tabular data to XLS-file.
*/
Expand Down
Loading

0 comments on commit 9b1f1e6

Please sign in to comment.