Commit 8821efa8 authored by sylvieg's avatar sylvieg

[ENH]tracker: possibility to export the attached files of items

parent d7697ec7
......@@ -3934,6 +3934,22 @@ class TrackerLib extends TikiLib
$this->categorized_item($trackerId, $to, "item $to", $cats);
}
}
function export_attachment($itemId, $archive) {
global $prefs;
$files = $this->list_item_attachments( $itemId, 0, -1, 'attId_asc' );
if (!empty($files['cant'])) {
$archive->addEmptyDir($itemId);
}
foreach( $files['data'] as $file ) {
$localZip = "item_$itemId/".$file['filename'];
$complete = $this->get_item_attachment( $file['attId'] );
if (!empty($complete['path']) && file_exists($prefs['t_use_dir'].$complete['path'])) {
$archive->addFile($prefs['t_use_dir'].$complete['path'], $localZip);
} else {
$archive->addFromString($localZip, $complete['data']);
}
}
}
}
......
......@@ -5,7 +5,6 @@
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
@ini_set('max_execution_time', 0); //will not work in safe_mode is on
require_once('tiki-setup.php');
$access->check_feature('feature_trackers');
if (!isset($_REQUEST['trackerId'])) {
......@@ -14,22 +13,23 @@ if (!isset($_REQUEST['trackerId'])) {
die;
}
include_once('lib/trackers/trackerlib.php');
@ini_set('max_execution_time', 0); //will not work in safe_mode is on
$tracker_info = $trklib->get_tracker($_REQUEST['trackerId']);
if (empty($tracker_info)) {
$smarty->assign('msg', tra('No tracker indicated'));
$smarty->assign('msg', tra('No tracker indicated'));
$smarty->display('error.tpl');
die;
}
if ($t = $trklib->get_tracker_options($_REQUEST['trackerId'])) {
$tracker_info = array_merge($tracker_info,$t);
}
$smarty->assign_by_ref('trackerId', $_REQUEST['trackerId']);
$smarty->assign_by_ref('tracker_info', $tracker_info);
$tikilib->get_perm_object($_REQUEST['trackerId'], 'tracker', $tracker_info);
$access->check_permission('tiki_p_export_tracker');
$smarty->assign_by_ref('trackerId', $_REQUEST['trackerId']);
$smarty->assign_by_ref('tracker_info', $tracker_info);
if (isset($_REQUEST['dump_tracker'])) {
$access->check_permission('tiki_p_tracker_dump');
......@@ -148,15 +148,44 @@ $smarty->assign_by_ref('CR', $_REQUEST['CR']);
if (!empty($_REQUEST['debug'])) {
$fp = fopen($prefs['tmpDir'].'/'.tra('tracker')."_".$_REQUEST['trackerId'].".csv", 'w');
} else {
header("Content-type: text/comma-separated-values; charset:".$_REQUEST['encoding']);
// Compression of the stream may corrupt files on windows
ob_end_clean();
ini_set('zlib.output_compression','Off');
$extension = empty($_REQUEST['zip'])?'.csv':'.zip';
if (!empty($_REQUEST['file'])) {
if (preg_match('/.csv$/', $_REQUEST['file'])) {
if (preg_match('/'.$extension.'$/', $_REQUEST['file'])) {
$file = $_REQUEST['file'];
} else {
$file = $_REQUEST['file'].'.csv';
$file = $_REQUEST['file'].$extension;
}
} else {
$file = tra('tracker').'_'.$_REQUEST['trackerId'].'.csv';
$file = tra('tracker').'_'.$_REQUEST['trackerId'].$extension;
}
if (!empty($_REQUEST['zip'])) {
$tmpCsv = tempnam( $prefs['tmpDir'], 'tracker_'.$_REQUEST['trackerId'] ) . '.csv';
/*debug*/$tmpCsv = $prefs['tmpDir'].'/'.'tracker_'.$_REQUEST['trackerId']. '.csv';
if (!($fp = fopen( $tmpCsv, 'w' ))) {
$smarty->assign('msg', tra('Can not open the file'). ' '.$tmpCsv);
$smarty->display('error.tpl');
die;
}
if ( !($archive = new ZipArchive()) ) {
$smarty->assign('msg', tra('Problem zip initialisation'));
$smarty->display('error.tpl');
die;
}
$tmpZip = $prefs['tmpDir'].'/'.$file;
if ( !($archive->open( $tmpZip, ZIPARCHIVE::OVERWRITE )) ) {
$smarty->assign('msg', tra('Can not open the file'). ' '.$prefs['tmpDir'].'/'.$file);
$smarty->display('error.tpl');
die;
}
header('Content-Type: application/zip');
header('Content-Transfer-Encoding: binary');
} else {
header("Content-type: text/comma-separated-values; charset:".$_REQUEST['encoding']);
}
header("Content-Disposition: attachment; filename=$file");
header("Expires: 0");
......@@ -199,12 +228,24 @@ while (($items = $trklib->list_items($_REQUEST['trackerId'], $offset, $maxRecord
$offset += $maxRecords;
$heading = 'n';
if (!empty($fp)) {
echo $offset.' ';
fwrite($fp, $data);
} else
} else {
echo $data;
}
if ($tracker_info['useAttachments'] == 'y' && !empty($_REQUEST['zip'])) {
foreach ($items['data'] as $v) {
$trklib->export_attachment($v['itemId'], $archive);
}
}
}
if (!empty($fp)) {
fclose($fp);
}
if (!empty($_REQUEST['zip'])) {
$archive->addFile($tmpCsv, str_replace('.zip', '.csv', $file));
$archive->close();
readfile( $tmpZip );
unlink( $tmpZip );
unlink( $tmpCsv );
}
die;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment