tiki-view_sheets.php 8.77 KB
Newer Older
lphuberdeau's avatar
lphuberdeau committed
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
6
//
lphuberdeau's avatar
lphuberdeau committed
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.
9
// $Id$
changi67's avatar
changi67 committed
10

11
$section = 'sheet';
12
$tiki_sheet_div_style = '';
lphuberdeau's avatar
lphuberdeau committed
13
require_once ('tiki-setup.php');
RobertPlummer's avatar
RobertPlummer committed
14

15
$sheetlib = TikiLib::lib('sheet');
RobertPlummer's avatar
RobertPlummer committed
16

changi67's avatar
changi67 committed
17
$auto_query_args = array(
18 19
	'sheetId',
	'readdate',
20 21
	'parse',
	'simple',
22
	'height',
23
	'file',
RobertPlummer's avatar
RobertPlummer committed
24 25
	'fileId',
	'page',
changi67's avatar
changi67 committed
26
);
27

28
$access->check_feature('feature_sheet');
29
$access->check_feature('feature_jquery_ui');
30

31
$info = $sheetlib->get_sheet_info($_REQUEST['sheetId']);
32

33
if (empty($info) && !isset( $_REQUEST['file']) && !isset($_REQUEST['fileId'])) {
sylvieg's avatar
sylvieg committed
34 35
	$smarty->assign('Incorrect parameter');
	$smarty->display('error.tpl');
36
	die;
lphuberdeau's avatar
lphuberdeau committed
37
}
38

39
$objectperms = Perms::get('sheet', $_REQUEST['sheetId']);
sylvieg's avatar
sylvieg committed
40 41 42 43 44 45
if ($user && $user == $info['author']) {
	$objectperms->view_sheet = 1;
	$objectperms->edit_sheet = 1;
	$objectperms->tiki_p_view_sheet = 1;
	$objectperms->tiki_p_edit_sheet = 1;
}
46

47
if (!$sheetlib->user_can_view($_REQUEST['sheetId'])) {
sylvieg's avatar
sylvieg committed
48
	$smarty->assign('msg', tra('Permission denied'));
49
	$smarty->display('error.tpl');
50
	die;
51
}
52

53
$smarty->assign('page', isset($_REQUEST['page']) ? $_REQUEST['page'] : '');
54 55
$smarty->assign('objectperms', $objectperms);

56
if (isset($_REQUEST['height'])) {
57
	 $tiki_sheet_div_style .= 'height:'.$_REQUEST['height'].';';
58 59
}

60
if ( $tiki_sheet_div_style) {
61
	$smarty->assign('tiki_sheet_div_style', $tiki_sheet_div_style);
62 63
}

64
//here we make sure parse is set so we don't have to keep checking using isset
65 66 67
if (!isset($_REQUEST['parse'])) {
	$_REQUEST['parse'] = 'y';
}
68

69
$smarty->assign('parse', $_REQUEST['parse']);
70

71
$smarty->assign('sheetId', $_REQUEST['sheetId']);
72
$smarty->assign('chart_enabled', (function_exists('imagepng') || function_exists('pdf_new')) ? 'y' : 'n');
lphuberdeau's avatar
lphuberdeau committed
73 74
$smarty->assign('title', $info['title']);
$smarty->assign('description', $info['description']);
75

76
// Start permissions
77 78 79
if ( $_REQUEST['parse'] == 'edit' && !$sheetlib->user_can_edit($_REQUEST['sheetId']) ) {
	$smarty->assign('msg', tra('Permission denied') . ': feature_sheet');
	$smarty->display('error.tpl');
80
	die;
sylvieg's avatar
sylvieg committed
81
}
82

83 84
//check to see if we are to do something other than view a file, which is not allowed
if ( $_REQUEST['parse'] != 'y' && ( isset($_REQUEST['file']) || isset($_REQUEST['fileId']) ) ) {
85 86
	$smarty->assign('msg', tra('Files are read only at this time'));
	$smarty->display('error.tpl');
87 88 89
	die;
}

90 91 92 93 94 95 96
if ($prefs['feature_contribution'] == 'y') {
	$contributionItemId = $_REQUEST['sheetId'];
	include_once ('contribution.php');
}
//End permissions

//Save
97 98
if (isset($_REQUEST['s']) && !empty($_REQUEST['s']) ) { //save
	if ( $_REQUEST['sheetId'] ) {
99
		$result = $sheetlib->save_sheet($_REQUEST['s'], $_REQUEST['sheetId'], $_REQUEST);
100
	}
101
	die($result);
102

103
//Clone
104 105 106
} elseif ( $_REQUEST['parse'] == 'clone' ) {
	if ( !$sheetlib->user_can_edit($_REQUEST['sheetId']) ) {
		$smarty->assign('msg', tra('Permission denied'));
107 108
		die;
	}
109 110
	$access->check_authenticity(tra('Are you sure you want to clone this spreadsheet?'));
	$id = $sheetlib->clone_sheet($_REQUEST['sheetId'], $_REQUEST['readdate']);
111
	if ($id) {
112
		header('Location: tiki-view_sheets.php?sheetId=' . $id);
113
	} else {
114
		$smarty->assign('msg', tra('Clone Error'));
115
	}
116 117

//Rollback
118
} elseif ($_REQUEST['parse'] == 'rollback' && !empty($_REQUEST['readdate'])) {
119 120
	if ( !$sheetlib->user_can_edit($_REQUEST['sheetId']) ) {
		$smarty->assign('msg', tra('Permission denied'));
121 122
		die;
	}
123
	$access->check_authenticity(tra('Are you sure you want to roll back this spreadsheet?'));
124
	$id = $sheetlib->rollback_sheet($_REQUEST['sheetId'], $_REQUEST['readdate']);
125
	if ($id) {
126
		header('Location: tiki-view_sheets.php?sheetId=' . $id);
127
	} else {
128
		$smarty->assign('msg', tra('Rollback Error'));
129
	}
130 131 132
}

//Edit & View
RobertPlummer's avatar
RobertPlummer committed
133 134
if ( isset($_REQUEST['relate']) && isset($_REQUEST['trackerId']) ) {
	if ( $_REQUEST['relate'] == 'add' ) {
135 136
		$sheetlib->add_related_tracker($_REQUEST['sheetId'], $_REQUEST['trackerId']);
		$smarty->assign('msg', tra('Tracker Added To Spreadsheet'));
137
	} elseif ( $_REQUEST['relate'] == 'remove' ) {
138 139
		$sheetlib->remove_related_tracker($_REQUEST['sheetId'], $_REQUEST['trackerId']);
		$smarty->assign('msg', tra('Tracker Removed From Spreadsheet'));
RobertPlummer's avatar
RobertPlummer committed
140 141 142
	}
} elseif ( isset($_REQUEST['relate']) && isset($_REQUEST['childSheetId']) ) {
	if ( $_REQUEST['relate'] == 'add' ) {
143 144
		$sheetlib->add_related_sheet($_REQUEST['sheetId'], $_REQUEST['childSheetId']);
		$smarty->assign('msg', tra('Spreadsheet added'));
145
	} elseif ( $_REQUEST['relate'] == 'remove' ) {
146 147
		$sheetlib->remove_related_sheet($_REQUEST['sheetId'], $_REQUEST['childSheetId']);
		$smarty->assign('msg', tra('Spreadsheet removed'));
RobertPlummer's avatar
RobertPlummer committed
148 149 150
	}
} elseif ( isset($_REQUEST['relate']) && isset($_REQUEST['fileId']) ) {
	if ( $_REQUEST['relate'] == 'add' ) {
151 152
		$sheetlib->add_related_file($_REQUEST['sheetId'], $_REQUEST['fileId']);
		$smarty->assign('msg', tra('File added'));
153
	} elseif ( $_REQUEST['relate'] == 'remove' ) {
154 155
		$sheetlib->remove_related_file($_REQUEST['sheetId'], $_REQUEST['fileId']);
		$smarty->assign('msg', tra('File removed'));
RobertPlummer's avatar
RobertPlummer committed
156
	}
157
} elseif ( isset($_REQUEST['fileId']) ) {
158
	$filegallib = TikiLib::lib('filegal');
159
	$access->check_feature('feature_file_galleries');
160
	$fileInfo = $filegallib->get_file_info($_REQUEST['fileId']);
161
	$handler = new TikiSheetCSVHandler($fileInfo);
162
	$grid = new TikiSheet();
163
	$grid->import($handler);
164
	$tableHtml[0] = $grid->getTableHtml();
165
	$smarty->assign('notEditable', 'true');
166

167
	if ($handler->truncated) $smarty->assign('msg', tra('Spreadsheet truncated'));
168

changi67's avatar
changi67 committed
169
} else {
170
	//Database sheet
171
	$handler = new TikiSheetDatabaseHandler($_REQUEST['sheetId']);
172 173 174 175 176
	//We make sheet able to look at other date save
	if (isset($_REQUEST['readdate']) && !empty($_REQUEST['readdate'])) {
		$smarty->assign('read_date', $_REQUEST['readdate']);
		$handler->setReadDate($_REQUEST['readdate']);
	}
177

RobertPlummer's avatar
RobertPlummer committed
178
	$grid = new TikiSheet();
179 180
	$grid->import($handler);

181
	//ensure that sheet isn't being edited, then parse values if needed
182
	if ( $_REQUEST['parse'] != 'edit' ) {
183 184 185 186
		$grid->parseValues = true;
	} else {
		$grid->parseValues = false;
	}
187

188
	$smarty->assign('parseValues', $grid->parseValues);
189

190
	$tableHtml[0] = $grid->getTableHtml(true, isset($_REQUEST['readdate']) ? $_REQUEST['readdate'] : null);
191

192 193
	if (!empty($relatedTrackersAsHtml)) {
		$tableHtml[0] .= $relatedTrackersAsHtml;
194
	}
195
}
196

197
if (isset($_REQUEST['sheetonly']) && $_REQUEST['sheetonly'] == 'y') {
198
	foreach ( $tableHtml as $table ) {
199 200 201
		echo $table;
	}
	die;
lphuberdeau's avatar
lphuberdeau committed
202
}
203

204
$smarty->assign('grid_content', $tableHtml);
205
$smarty->assign('menu', $smarty->fetch('tiki-view_sheets_menu.tpl'));
206

207
$sheetlib->setup_jquery_sheet();
RobertPlummer's avatar
RobertPlummer committed
208
if (!empty($_REQUEST['parse']) && $_REQUEST['parse'] == 'edit') {
209
	$headerlib->add_jq_onready('$.sheet.tikiOptions.editable = true;');
210
} else {
211
	$headerlib->add_jq_onready('$.sheet.tikiOptions.editable = false;');
RobertPlummer's avatar
RobertPlummer committed
212 213
}

214
$headerlib->add_jq_onready(
215
	'$.sheet.tikiOptions = $.extend($.sheet.tikiOptions, {
216
					menuLeft: $("#sheetMenu").clone().html()
changi67's avatar
changi67 committed
217
				});
218

changi67's avatar
changi67 committed
219
				jST = $("div.tiki_sheet")
220
				    .height(window.innerHeight * 0.8)
changi67's avatar
changi67 committed
221
					.sheet($.sheet.tikiOptions);
222

223 224
				jST.id = "' . ($_REQUEST['sheetId'] * 1) . '";
				jST.file = "' . ( isset($fileInfo) ? 'true' : 'false' ) .'";
225

changi67's avatar
changi67 committed
226 227
				$.sheet.link.setupUI();
				$.sheet.readyState();
228

changi67's avatar
changi67 committed
229 230 231 232 233 234 235
				$(window).bind("beforeunload", function() {
					$($.sheet.instance).each(function() {
						if (this.isDirty) {
							return true;
						}
					});
				});
236

changi67's avatar
changi67 committed
237 238 239 240 241
				$("#edit_button a")
					.click(function() {
						$.sheet.manageState(true, "edit");
						return false;
					});
242

changi67's avatar
changi67 committed
243 244 245 246 247
				$("#save_button a")
					.click( function () {
						$.sheet.saveSheet(function() {
							$.sheet.manageState(true);
						});
248

changi67's avatar
changi67 committed
249 250
						return false;
					});
251

changi67's avatar
changi67 committed
252 253 254 255 256
				$("#cancel_button")
					.click(function() {
						$.sheet.manageState(true);
						return false;
					});'
257
);
258

259
$smarty->assign('semUser', '');
260
if ($prefs['feature_warn_on_edit'] == 'y') {
261 262 263
	if ($tikilib->semaphore_is_set($_REQUEST['sheetId'], $prefs['warn_on_edit_time'] * 60, 'sheet')
			&& ($semUser = $tikilib->get_semaphore_user($_REQUEST['sheetId'], 'sheet')) != $user
	) {
264 265 266 267 268
		$editconflict = 'y';
		$smarty->assign('semUser', $semUser);
	} else {
		$editconflict = 'n';
	}
269
	if ($_REQUEST['parse'] == 'edit') {
270 271 272 273 274
		$_SESSION['edit_lock_sheet' . $_REQUEST['sheetId']] = $tikilib->semaphore_set($_REQUEST['sheetId'], 'sheet');
	} elseif (isset($_SESSION['edit_lock_sheet' . $_REQUEST['sheetId']])) {
		$tikilib->semaphore_unset($_REQUEST['sheetId'], $_SESSION['edit_lock_sheet' . $_REQUEST['sheetId']]);
		unset($_SESSION['edit_lock_sheet' . $_REQUEST['sheetId']]);
	}
275 276
} else {
		$editconflict = 'n';
277
}
278
$smarty->assign('editconflict', $editconflict);
279 280

$cat_type = 'sheet';
281 282
$cat_objid = $_REQUEST['sheetId'];
include_once ('categorize_list.php');
lphuberdeau's avatar
lphuberdeau committed
283 284
include_once ('tiki-section_options.php');
ask_ticket('sheet');
285

lphuberdeau's avatar
lphuberdeau committed
286
// Display the template
287
$smarty->assign('mid', 'tiki-view_sheets.tpl');
288
$smarty->display('tiki.tpl');