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 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 = '';
rjsmelo's avatar
rjsmelo committed
13
require_once('tiki-setup.php');
RobertPlummer's avatar
RobertPlummer committed
14

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

rjsmelo's avatar
rjsmelo committed
17
$auto_query_args = [
18 19
	'sheetId',
	'readdate',
20 21
	'parse',
	'simple',
22
	'height',
23
	'file',
RobertPlummer's avatar
RobertPlummer committed
24 25
	'fileId',
	'page',
rjsmelo's avatar
rjsmelo 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

rjsmelo's avatar
rjsmelo committed
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

rjsmelo's avatar
rjsmelo committed
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'])) {
rjsmelo's avatar
rjsmelo committed
57
	 $tiki_sheet_div_style .= 'height:' . $_REQUEST['height'] . ';';
58 59
}

rjsmelo's avatar
rjsmelo committed
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
rjsmelo's avatar
rjsmelo committed
65
if (! isset($_REQUEST['parse'])) {
66 67
	$_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
rjsmelo's avatar
rjsmelo committed
77
if ($_REQUEST['parse'] == 'edit' && ! $sheetlib->user_can_edit($_REQUEST['sheetId'])) {
78 79
	$smarty->assign('msg', tra('Permission denied') . ': feature_sheet');
	$smarty->display('error.tpl');
80
	die;
sylvieg's avatar
sylvieg committed
81
}
82

83
//check to see if we are to do something other than view a file, which is not allowed
rjsmelo's avatar
rjsmelo committed
84
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
if ($prefs['feature_contribution'] == 'y') {
	$contributionItemId = $_REQUEST['sheetId'];
rjsmelo's avatar
rjsmelo committed
92
	include_once('contribution.php');
93 94 95 96
}
//End permissions

//Save
rjsmelo's avatar
rjsmelo committed
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
rjsmelo's avatar
rjsmelo committed
104 105
} elseif ($_REQUEST['parse'] == 'clone') {
	if (! $sheetlib->user_can_edit($_REQUEST['sheetId'])) {
106
		$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
rjsmelo's avatar
rjsmelo committed
118 119
} elseif ($_REQUEST['parse'] == 'rollback' && ! empty($_REQUEST['readdate'])) {
	if (! $sheetlib->user_can_edit($_REQUEST['sheetId'])) {
120
		$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
rjsmelo's avatar
rjsmelo 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'));
rjsmelo's avatar
rjsmelo committed
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
	}
rjsmelo's avatar
rjsmelo committed
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'));
rjsmelo's avatar
rjsmelo committed
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
	}
rjsmelo's avatar
rjsmelo committed
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'));
rjsmelo's avatar
rjsmelo committed
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
	}
rjsmelo's avatar
rjsmelo committed
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

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

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

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

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

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

rjsmelo's avatar
rjsmelo committed
193
	if (! empty($relatedTrackersAsHtml)) {
194
		$tableHtml[0] .= $relatedTrackersAsHtml;
195
	}
196
}
197

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

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

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

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

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

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

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

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

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

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

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

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

260
$serviceLib = TikiLib::lib('service');
261
$smarty->assign('semUser', '');
262
if ($prefs['feature_warn_on_edit'] == 'y') {
263 264
	if ($serviceLib->internal('semaphore', 'is_set', ['object_id' => $_REQUEST['sheetId'], 'object_type' => 'sheet']) &&
		($semUser = $serviceLib->internal('semaphore', 'get_user', ['object_id' => $_REQUEST['sheetId'], 'object_type' => 'sheet'])) !== $user
265
	) {
266 267 268 269 270
		$editconflict = 'y';
		$smarty->assign('semUser', $semUser);
	} else {
		$editconflict = 'n';
	}
271
	if ($_REQUEST['parse'] == 'edit' && $editconflict === 'n') {
272 273
		$serviceLib->internal('semaphore', 'set', ['object_id' => $_REQUEST['sheetId'], 'object_type' => 'sheet']);
	} else {
274 275 276 277
		$serviceLib->internal('semaphore', 'unset', [
			'object_id' => $_REQUEST['sheetId'],
			'object_type' => 'sheet',
		]);
278
	}
279 280
} else {
		$editconflict = 'n';
281
}
282
$smarty->assign('editconflict', $editconflict);
283 284

$cat_type = 'sheet';
285
$cat_objid = $_REQUEST['sheetId'];
rjsmelo's avatar
rjsmelo committed
286 287
include_once('categorize_list.php');
include_once('tiki-section_options.php');
lphuberdeau's avatar
lphuberdeau committed
288
ask_ticket('sheet');
289

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