tiki-export_tracker.php 8.55 KB
Newer Older
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2015 by authors of the Tiki Wiki CMS Groupware Project
changi67's avatar
changi67 committed
6
// 
7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
changi67's avatar
changi67 committed
9
// $Id$
10

11 12
// still used for export from trackerfilter (only - Tiki 9)

13
require_once('tiki-setup.php');
14
$access->check_feature('feature_trackers');
15 16 17 18 19
if (!isset($_REQUEST['trackerId'])) {
	$smarty->assign('msg', tra('No tracker indicated'));
	$smarty->display('error.tpl');
	die;
}
lphuberdeau's avatar
lphuberdeau committed
20
$trklib = TikiLib::lib('trk');
21
@ini_set('max_execution_time', 0); //will not work in safe_mode is on
22 23 24 25

$tracker_info = $trklib->get_tracker($_REQUEST['trackerId']);
if (empty($tracker_info)) {
	$smarty->assign('msg', tra('No tracker indicated'));
26
	$smarty->display('error.tpl');
27 28 29
	die;
}
if ($t = $trklib->get_tracker_options($_REQUEST['trackerId'])) {
30
	$tracker_info = array_merge($tracker_info, $t);
31 32
}
$tikilib->get_perm_object($_REQUEST['trackerId'], 'tracker', $tracker_info);
33
$access->check_permission('tiki_p_export_tracker', tra('Export Tracker'), 'tracker', $_REQUEST['trackerId']);
34

35 36 37
$smarty->assign_by_ref('trackerId', $_REQUEST['trackerId']);
$smarty->assign_by_ref('tracker_info', $tracker_info);

38 39
$filters = array();
if (!empty($_REQUEST['listfields'])) {
40
	if (is_string($_REQUEST['listfields'])) {
41
		$filters['fieldId'] = preg_split('/[,:]/', $_REQUEST['listfields']);
42 43 44
	} elseif (is_array($_REQUEST['listfields'])) {
		$filters['fieldId'] = $_REQUEST['listfields'];
	}
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
} elseif (isset($_REQUEST['which']) && $_REQUEST['which'] == 'ls') {
	$filters['or'] = array('isSearchable'=>'y', 'isTblVisible'=>'y');
} elseif (isset($_REQUEST['which']) && $_REQUEST['which'] == 'list') {
	$filters['isTblVisible'] = 'y';
}
if ($tiki_p_admin_trackers != 'y') {
	$filters['isHidden'] = array('n', 'c');
}
if ($tiki_p_tracker_view_ratings != 'y') {
	$filters['not'] = array('type'=>'s');
}
$filters['not'] = array('type'=>'h');

$fields = $trklib->list_tracker_fields($_REQUEST['trackerId'], 0, -1, 'position_asc', '', true, $filters);
$listfields = array();
foreach ($fields['data'] as $field) {
	$listfields[$field['fieldId']] = $field;
}

if (!isset($_REQUEST['which'])) {
	$_REQUEST['which'] = 'all';
}
if (!isset($_REQUEST['status'])) {
	$_REQUEST['status'] = '';
}
if (!isset($_REQUEST['initial'])) {
	$_REQUEST['initial'] = '';
}
$filterFields = '';
$values = '';
$exactValues = '';
foreach ($_REQUEST as $key =>$val) {
	if (substr($key, 0, 2) == 'f_' && !empty($val) && (!is_array($val) || !empty($val[0]))) {
		$fieldId = substr($key, 2);
		$filterFields[] = $fieldId;
		if (isset($_REQUEST["x_$fieldId"]) && $_REQUEST["x_$fieldId"] == 't' ) {
			$exactValues[] = '';
82
			$values[] = urldecode($val);
83
		} else {
84
			$exactValues[] = urldecode($val);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
			$values[] = '';
		}
	}
}
$smarty->assign_by_ref('listfields', $listfields);

if (isset($_REQUEST['showStatus'])) {
	$showStatus = $_REQUEST['showStatus'] == 'on'?'y':'n';
} else {
	$showStatus = 'n';
}
$smarty->assign_by_ref('showStatus', $showStatus);

if (isset($_REQUEST['showItemId'])) {
	$showItemId = $_REQUEST['showItemId'] == 'on'?'y':'n';
} else {
	$showItemId = 'n';
}
$smarty->assign_by_ref('showItemId', $showItemId);

if (isset($_REQUEST['showCreated'])) {
	$showCreated = $_REQUEST['showCreated'] == 'on'?'y':'n';
} else {
	$showCreated = 'n';
}
$smarty->assign_by_ref('showCreated', $showCreated);

if (isset($_REQUEST['showLastModif'])) {
	$showLastModif = $_REQUEST['showLastModif'] == 'on'?'y':'n';
} else {
	$showLastModif = 'n';
}
$smarty->assign_by_ref('showLastModif', $showLastModif);

if (isset($_REQUEST['parse'])) {
	$parse = $_REQUEST['parse'] == 'on'?'y':'n';
} else {
	$parse = 'n';
}
$smarty->assign_by_ref('parse', $parse);

if (empty($_REQUEST['encoding'])) {
127
	$_REQUEST['encoding'] = 'ISO-8859-1';
128
}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
if (empty($_REQUEST['separator'])) {
	$_REQUEST['separator'] = ',';
}
$smarty->assign_by_ref('separator', $_REQUEST['separator']);
if (empty($_REQUEST['delimitorL'])) {
	$_REQUEST['delimitorL'] = '"';
}
$smarty->assign_by_ref('delimitorL', $_REQUEST['delimitorL']);
if (empty($_REQUEST['delimitorR'])) {
	$_REQUEST['delimitorR'] = '"';
}
$smarty->assign_by_ref('delimitorR', $_REQUEST['delimitorR']);
if (empty($_REQUEST['CR'])) {
	$_REQUEST['CR'] = '%%%';
}
$smarty->assign_by_ref('CR', $_REQUEST['CR']);
145

146 147
$fp = null;

148
if (!empty($_REQUEST['debug'])) {
149
	$fp = fopen($prefs['tmpDir'].'/'.tra('tracker')."_".$_REQUEST['trackerId'].".csv", 'w');
150
	echo 'ouput:'.$prefs['tmpDir'].'/'.tra('tracker')."_".$_REQUEST['trackerId'].".csv";
151
} else {
152
	// Compression of the stream may corrupt files on windows
153 154
	if ($prefs['feature_obzip'] != 'y')
		ob_end_clean();
155
	ini_set('zlib.output_compression', 'Off');
156 157

	$extension = empty($_REQUEST['zip'])?'.csv':'.zip';
158
	if (!empty($_REQUEST['file'])) {
159
		if (preg_match('/'.$extension.'$/', $_REQUEST['file'])) {
160 161
			$file = $_REQUEST['file'];
		} else {
162
			$file = $_REQUEST['file'].$extension;
163 164
		}
	} else {
165 166 167
		$file = tra('tracker').'_'.$_REQUEST['trackerId'].$extension;
	}
	if (!empty($_REQUEST['zip'])) {
168
		$tmpCsv = tempnam($prefs['tmpDir'], 'tracker_'.$_REQUEST['trackerId']) . '.csv';
169
		/*debug*/$tmpCsv = $prefs['tmpDir'].'/'.'tracker_'.$_REQUEST['trackerId']. '.csv';
170
		if (!($fp = fopen($tmpCsv, 'w'))) {
171 172 173 174 175 176 177 178 179 180
			$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;
181
		if ( !($archive->open($tmpZip, ZIPARCHIVE::OVERWRITE)) ) {
182 183 184 185 186 187 188 189 190
			$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']);
191 192
	}
	header("Content-Disposition: attachment; filename=$file");
193 194 195 196 197 198 199
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
	header("Pragma: public");
}

$offset = 0;
$maxRecords = 100;
200 201 202 203 204 205 206
if ($tracker_info['defaultOrderKey'] == -1)
	$sort_mode = 'lastModif';
elseif ($tracker_info['defaultOrderKey'] == -2)
	$sort_mode = 'created';
elseif ($tracker_info['defaultOrderKey'] == -3)
	$sort_mode = 'itemId';
elseif (isset($tracker_info['defaultOrderKey'])) {
207
	$sort_mode = 'f_'.$tracker_info['defaultOrderKey'];
208 209 210 211 212 213 214 215
} else {
	$sort_mode = 'itemId';
}
if (isset($tracker_info['defaultOrderDir'])) {
	$sort_mode.= "_".$tracker_info['defaultOrderDir'];
} else {
		$sort_mode.= "_asc";
}
216
$heading = 'y';
217
$smarty->assign_by_ref('heading', $heading);
218 219 220 221 222 223 224 225 226 227 228 229 230
if (empty($_REQUEST['itemId'])) {
	while (($items = $trklib->list_items($_REQUEST['trackerId'], $offset, $maxRecords, $sort_mode, $listfields, $filterFields, $values, $_REQUEST['status'], $_REQUEST['initial'], $exactValues)) && !empty($items['data'])) {
		// still need to filter the fields that are view only by the admin and the item creator
		if ($tracker_info['useRatings'] == 'y')
			foreach ($items['data'] as $f => $v) {
				$items['data'][$f]['my_rate'] = $tikilib->get_user_vote("tracker." . $_REQUEST['trackerId'] . '.' . $items['data'][$f]['itemId'], $user);
			}
		$smarty->assign_by_ref('items', $items["data"]);

		$data = $smarty->fetch('tiki-export_tracker_item.tpl');
		$data = preg_replace("/^\n/", "", $data);
		if (empty($_REQUEST['encoding']) || $_REQUEST['encoding'] == 'ISO-8859-1') {
			$data = utf8_decode($data);
231
		}
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

		$offset += $maxRecords;
		$heading = 'n';
		if (!empty($fp)) {
			fwrite($fp, $data);
		} else {
			echo $data;
		}
		if ($tracker_info['useAttachments'] == 'y' && !empty($_REQUEST['zip'])) {
			foreach ($items['data'] as $v) {
				if (!$trklib->export_attachment($v['itemId'], $archive)) {
					$smarty->assign('msg', tra('Problem zip'));
					$smarty->display('error.tpl');
					die;
				}
			}
		}
	}
} else {
	$items = array();
	$items[] = $trklib->get_tracker_item($_REQUEST['itemId']);
	$items[0]['field_values'] = $trklib->get_item_fields($_REQUEST['trackerId'], $_REQUEST['itemId'], $listfields);
	$smarty->assign_by_ref('items',$items);
255

sylvieg's avatar
sylvieg committed
256
	$data = $smarty->fetch('tiki-export_tracker_item.tpl');
sylvieg's avatar
sylvieg committed
257
	$data = preg_replace("/^\n/", "", $data);
258
	if (empty($_REQUEST['encoding']) || $_REQUEST['encoding'] == 'ISO-8859-1') {
259 260 261 262
		$data = utf8_decode($data);
	}
	if (!empty($fp)) {
		fwrite($fp, $data);
263
	} else {
264
		echo $data;
265
	}
266
}
267
if (!empty($fp)) {
268
	fclose($fp);
269
}
270 271 272
if (!empty($_REQUEST['zip'])) {
	$archive->addFile($tmpCsv, str_replace('.zip', '.csv', $file));
	$archive->close();
273 274 275
	readfile($tmpZip);
	unlink($tmpZip);
	unlink($tmpCsv);
276
}
277
die;