Skip to content

Add support for downloading CSV file #7

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
51 changes: 50 additions & 1 deletion syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function handle($match, $state, $pos, Doku_Handler &$handler) {
'hdr_cols' => 0,
'span_empty_cols' => 0,
'file' => '',
'export' => '',
'linkname' => 'Download CSV file',
'delim' => ',',
'enclosure' => '"',
'escape' => '"',
Expand All @@ -71,9 +73,29 @@ function handle($match, $state, $pos, Doku_Handler &$handler) {

// parse options
$optsin = explode(' ', $optstr);
$populate = '';
foreach($optsin as $o) {
$o = trim($o);
if(preg_match('/(\w+)=(.*)/', $o, $matches)) {
if ( $populate != '' ) {
// handle closing quote
$opt[$populate] .= ' ';
if ( substr($o, -1) == '"' ) {
$o = substr($o, 0, -1);
$opt[$populate] .= $o;
$populate = '';
}
else
$opt[$populate] .= $o;
}
elseif (preg_match('/(\w+)=(.*)/', $o, $matches)) {
// strip leading quote
if ( substr($matches[2], 0, 1) == '"' ) {
$matches[2] = substr($matches[2], 1, -1);
if ( substr($matches[2], -1) == '"' )
$matches[2] = substr($matches[2], 0, -1);
else
$populate = $matches[1];
}
$opt[$matches[1]] = $matches[2];
} elseif($o) {
if(preg_match('/^https?:\/\//i', $o)) {
Expand Down Expand Up @@ -128,6 +150,30 @@ function render($mode, Doku_Renderer &$renderer, $opt) {
return true;
}

// Export the csv file
$targetfile = '';
$export = $opt['export'];
if ( $export != '' ) {
$targetfile = htmlspecialchars(trim($export));
if ( auth_quickaclcheck(getNS($targetfile.':*')) < AUTH_EDIT) {
$renderer->cdata('Access denied: Could not create download link.');
$targetfile = '';
return true;
} else {
$file = mediaFN($targetfile);
if ( file_put_contents ($file, $content, LOCK_EX) > 0 ) {
$linkname = $opt['linkname'];
if ( $linkname == '' )
$linkname = 'Download CSV file';
}
else {
$targetfile = '';
$renderer->cdata('Failed to write '.$file.': Could not create download link.');
return true;
}
}
}

// get the first row - it will define the structure
$row = $this->csv_explode_row($content, $opt['delim'], $opt['enclosure'], $opt['escape']);
$maxcol = count($row);
Expand Down Expand Up @@ -186,6 +232,9 @@ function render($mode, Doku_Renderer &$renderer, $opt) {
}
$renderer->table_close();

if ( $targetfile != '' )
$renderer->internalmedia($targetfile, $linkname);

return true;
}

Expand Down