Skip to content

User: Implement optional CSV header output to export #6571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions public/main/admin/user_export.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@
$form->addElement('radio', 'file_type', get_lang('Output file type'), 'XML', 'xml');
$form->addElement('radio', 'file_type', null, 'CSV', 'csv');
$form->addElement('radio', 'file_type', null, 'XLS', 'xls');
$form->addCheckBox(
'addcsvheader',
[
get_lang('Include header line?'),
get_lang(
'This will put the fields names on the first line. It is necessary when you want to import the file later on in a Chamilo portal.'
),
],
get_lang('Yes, add the headers')
$form->addElement('checkbox', 'addcsvheader', get_lang('Include header line?'));
$form->addElement(
'static',
null,
null,
'<div class="form_help">'
. get_lang('This will put the field names on the first line. It is necessary when you want to import the file later on in a Chamilo portal.')
. '</div>'
);
$form->addSelect('course_code', get_lang('Only users from the course'), $courses);
$form->addSelect('course_session', get_lang('Only users from the courseSession'), $coursesSessions);
Expand Down Expand Up @@ -199,7 +198,14 @@
exit;
break;
case 'csv':
Export::arrayToCsv($data, $filename);
$includeHeader = !empty($export['addcsvheader']);
Export::arrayToCsv(
$data,
$filename,
false,
'"',
false
);
exit;
case 'xls':
Export::arrayToXls($data, $filename);
Expand Down
31 changes: 24 additions & 7 deletions public/main/inc/lib/export.lib.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,36 @@ public function __construct()
}

/**
* Export tabular data to CSV-file.
* Export tabular data to a CSV file.
*
* @return mixed csv raw data | false if no data to export | string file path if success in $writeOnly mode
* @param array $data Data rows as an array of associative arrays
* @param string $filename Base filename without extension
* @param bool $writeOnly If true returns the file path instead of forcing download
* @param string $enclosure Field enclosure character (default: ")
* @param bool $includeHeader Whether to output a header row (keys of the first array)
*
* @return mixed Raw CSV data | false if $data is empty | string file path if $writeOnly = true
*/
public static function arrayToCsv(array $data, string $filename = 'export', bool $writeOnly = false, string $enclosure = '"')
{
public static function arrayToCsv(
array $data,
string $filename = 'export',
bool $writeOnly = false,
string $enclosure = '"',
bool $includeHeader = true
) {
if (empty($data)) {
return false;
}

$enclosure = !empty($enclosure) ? $enclosure : '"';
$filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv';
$writer = new CsvWriter($filePath, ',', $enclosure, true);
$enclosure = $enclosure !== '' ? $enclosure : '"';
$filePath = api_get_path(SYS_ARCHIVE_PATH) . uniqid() . '.csv';

// define a single-character escape
$escapeChar = '\\';

// CsvWriter signature: __construct($fileName, $delimiter, $enclosure, $escape, $withBOM, $withHeader)
// we pass false for BOM, and $includeHeader for header toggle
$writer = new CsvWriter($filePath, ',', $enclosure, $escapeChar, false, $includeHeader);

$source = new ArraySourceIterator($data);
$handler = Handler::create($source, $writer);
Expand Down
Loading