tiki-graph_sheet.php 5.87 KB
Newer Older
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
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

require_once ('tiki-setup.php');
12

13
$sheetlib = TikiLib::lib('sheet');
14

15 16
require_once ('lib/graph-engine/gd.php');
require_once ('lib/graph-engine/pdflib.php');
17
require_once ('lib/graph-engine/ps.php');
18 19 20 21
require_once ('lib/graph-engine/graph.pie.php');
require_once ('lib/graph-engine/graph.bar.php');
require_once ('lib/graph-engine/graph.multiline.php');

22 23 24 25 26
/**
 * @param $serie
 * @param $sheet
 * @return array
 */
27 28
function handle_series( $serie, &$sheet )
{
29 30
	if ( !$range = $sheet->getRange($serie) )
		$range = array_map('trim', explode(',', $serie));
31

32
	if ( !is_array($range) )
33 34 35 36 37 38 39
		return array();

	return $range;
}

// Various validations {{{1

40
$access->check_feature('feature_sheet');
41
$access->check_feature('feature_jquery_ui');
42

43
$info = $sheetlib->get_sheet_info($_REQUEST['sheetId']);
44 45 46 47 48
if (empty($info)) {
	$smarty->assign('Incorrect parameter');
	$smarty->display('error.tpl');
	die;
}
49

50
$objectperms = Perms::get('sheet', $_REQUEST['sheetId']);
51 52 53
if ($tiki_p_admin != 'y' && !$objectperms->view_sheet && !($user && $info['author'] == $user)) {
	$smarty->assign('msg', tra('Permission denied'));
	$smarty->display('error.tpl');
54 55 56 57
	die;
}

// This condition will be removed when a php-based renderer will be written
58 59
if ( !function_exists('pdf_new') && !function_exists('imagepng') ) {
	$smarty->assign('msg', tra('No valid renderer found. GD or PDFLib required.'));
60

61
	$smarty->display('error.tpl');
62 63 64
	die;
}

65 66
if (!isset( $_REQUEST['sheetId'] )) {
	$smarty->assign('msg', tra('No sheet specified.'));
67

68
	$smarty->display('error.tpl');
69 70 71 72 73 74 75 76 77 78 79
	die;
}
// }}}1

$valid_graphs = array( 'PieChartGraphic', 'MultilineGraphic', 'MultibarGraphic', 'BarStackGraphic' );

$smarty->assign('sheetId', $_REQUEST["sheetId"]);

$smarty->assign('title', $info['title']);
$smarty->assign('description', $info['description']);

80
$smarty->assign('page_mode', 'form');
81 82 83

// Process the insertion or modification of a gallery here

84
$sheetId = $_REQUEST['sheetId'];
85

86 87 88
if ( isset($_REQUEST['title']) ) {
	if ( !in_array($_REQUEST['graphic'], $valid_graphs) )
		die( 'Unknown Graphic.' );
89

90
	$cache_file = 'temp/cache/tsge_' . md5($_SERVER['REQUEST_URI']);
91

92
	if ( !isset($_REQUEST['renderer']) )
93 94 95
		$_REQUEST['renderer'] = null;
	switch( $_REQUEST['renderer'] )
	{
96 97 98
		case 'PNG':
			$renderer = new GD_GRenderer($_REQUEST['width'], $_REQUEST['height'], 'png');
			$ext = 'png';
99
			break;
100 101 102 103

		case 'JPEG':
			$renderer = new GD_GRenderer($_REQUEST['width'], $_REQUEST['height'], 'jpg');
			$ext = 'jpg';
104
			break;
105 106 107 108

		case 'PDF':
			$renderer = new PDFLib_GRenderer($_REQUEST['format'], $_REQUEST['orientation']);
			$ext = 'pdf';
109
			break;
110 111 112 113

		case 'PS':
			$renderer = new PS_GRenderer($_REQUEST['format'], $_REQUEST['orientation']);
			$ext = 'ps';
114
			break;
115 116 117 118 119
		default:
			$smarty->assign('msg', tra('You must select a renderer.'));

			$smarty->display('error.tpl');
			die;
120 121
	}

122 123 124
	if ( file_exists($cache_file) && time() - filemtime($cache_file) < 3600 ) {
		$renderer->httpHeaders("graph.$ext");
		readfile($cache_file);
125 126 127
		exit;
	}

128 129 130
	$handler = new TikiSheetDatabaseHandler($sheetId);
	$grid = new TikiSheet($_REQUEST['sheetId']);
	$grid->import($handler);
131

132 133 134 135 136
	$graph = $_REQUEST['graphic'];
	$graph = new $graph;

	// Create Output
	$series = array();
137
	foreach ( $_REQUEST['series'] as $key => $value )
138 139 140
		if (!empty( $value) ) {
			$s = handle_series($value, $grid);
			if ( count($s) > 0 )
141 142 143
				$series[$key] = $s;
		}

144 145
	if ( !$graph->setData($series) ) {
		$smarty->assign('msg', tra('Invalid Series for current graphic.'));
146

147
		$smarty->display('error.tpl');
148 149 150
		die;
	}

151 152
	if ( !empty($_REQUEST['title']) )
		$graph->setTitle($_REQUEST['title']);
153

154 155 156 157
	if ( isset($_REQUEST['independant']) ) {
		$graph->setParam('grid-independant-location', $_REQUEST['independant']);
		$graph->setParam('grid-vertical-position', $_REQUEST['vertical']);
		$graph->setParam('grid-horizontal-position', $_REQUEST['horizontal']);
158 159
	}

160
	$graph->draw($renderer);
161

162
	ob_start();
163
	$renderer->httpOutput("graph.$ext");
164 165 166
	$content = ob_get_contents();
	ob_end_flush();

167
	file_put_contents($cache_file, $content);
168 169

	exit;
170 171
} else {
	if ( isset($_GET['graphic']) && in_array($_GET['graphic'], $valid_graphs) ) {
172 173 174
		$graph = $_GET['graphic'];
		$g = new $graph;
		$series = array();
175 176
		foreach ( array_keys($g->getRequiredSeries()) as $s )
			if ( $s == 'y0' ) {
177 178 179 180 181
				$series[] = 'y0';
				$series[] = 'y1';
				$series[] = 'y2';
				$series[] = 'y3';
				$series[] = 'y4';
182
			} else
183 184
				$series[] = $s;

185 186 187 188
		$smarty->assign('mode', 'param');
		$smarty->assign('series', $series);
		$smarty->assign('graph', $graph);
		$smarty->assign('renderer', $_GET['renderer']);
189

190 191 192 193 194 195
		$handler = new TikiSheetDatabaseHandler($sheetId);
		$grid = new TikiSheet($_REQUEST['sheetId']);
		$grid->import($handler);

		$dataGrid = $grid->getTableHtml(true);

196 197
		require_once ('lib/sheet/grid.php');
		$sheetlib->setup_jquery_sheet();
198
		$headerlib->add_jq_onready(
199
			'$("div.tiki_sheet").sheet($.extend($.sheet.tikiOptions, {editable: false}));'
200 201
		);

202
		$smarty->assign('dataGrid', $dataGrid);
203

204
		if ( function_exists('pdf_new') ) {
205 206
			$smarty->assign('format', $_GET['format']);
			$smarty->assign('orientation', $_GET['orientation']);
207
		}
208 209

		if ( function_exists('imagepng') ) {
210 211
			$smarty->assign('im_width', $_GET['width']);
			$smarty->assign('im_height', $_GET['height']);
212 213
		}

214
		if ( is_a($g, 'GridBasedGraphic') )
215
			$smarty->assign('showgridparam', true);
216
	} else {
217 218 219 220
		$smarty->assign('mode', 'graph');
		$smarty->assign('hasgd', function_exists('imagepng') && function_exists('imagejpeg'));
		$smarty->assign('haspdflib', function_exists('pdf_new'));
		$smarty->assign('hasps', function_exists('ps_new'));
221 222 223 224 225 226
	}
}

// Display the template
$smarty->assign('mid', 'tiki-graph-sheets.tpl');
$smarty->display("tiki.tpl");