<?php
//maanigraph hack 06.09.25
//charts.inc.php 0.2 06.09.30
include_once "charts.php";
function plugin_charts_convert()
{
	global $script, $vars, $edit_auth, $edit_auth_pages;
	static $number = array();

	$opt = array(
		'table_num' => '',
		'graph_mod' => '',
		'list_mod' => '',
		'setting' => '',
		'change' => '0',
		'width' => '400',
		'height' => '250',
		'bg_color' => '666666',
	);

	$args = func_get_args();
	foreach ($args as $opt_key) {
		list($key, $val) = explode('=', strtolower($opt_key));
		$opt[$key] = htmlspecialchars($val);
	}

//	if ( isset($opt['refer']) ) {
//		$page = htmlspecialchars($opt['refer']);
//		if (! is_page($page) ) return "page name error";
//	} else {
		$page = isset($vars['page']) ? $vars['page'] : '';
//	}

	if (! isset($number[$page])) $number[$page] = 1;
	$count = $number[$page]++;
	$r_page = rawurlencode($page);

	$auth_chk = auth::check_auth();
	foreach($edit_auth_pages as $key=>$val){
		if (preg_match($key, $page) && $edit_auth){
			if(! empty($auth_chk)){
			} else {
				if($opt['setting'] != 'on'){
					$opt['setting'] = 'off';
				}
			}
		}
	}

	$head_button = '';

	// if (PKWK_READONLY) {
	if (auth::check_role('readonly') || is_freeze($page) ) {
		$opt['setting'] = 'off';
	}

	$change = $opt['change'];

	$swf				 = SKIN_DIR . 'charts.swf';
	$charts_library		 = SKIN_DIR . 'charts_library';
	$php_source			 = $scripe . '?plugin=charts&refer=' . $r_page . '&chart_type=' . $opt['graph_mod']
							 . '&table_num=' . $opt['table_num'] . '&change=' . $change;
//	$php_source			 = EXT_PLUGIN_DIR . 'chartsdata.php?type=' . $opt['graph_mod'] . '&table_num=' . $opt['table_num']
//							 . '&change=' . $change . '&file=' . get_filename($page);

	if (isset($opt['chart_transition'])){
		$php_source .= '&chart_transition';
	}
	if (isset($opt['chart_value'])){
		$php_source .= '&chart_value=' . $opt['chart_value'];
	}

	if ($opt['setting'] != 'off'){

		if ( $opt['list_mod'] == 'open' ){

			//graph list
			$graph_type = array('line', 'column', 'stacked column', 'floating column', 
						'3d column', 'stacked 3d column', 'parallel 3d column', 
						'pie', '3d pie', 'bar', 'stacked bar', 'floating bar', 
						'area', 'stacked area', 'candlestick', 'scatter', 'polar',
						);

			foreach($graph_type as $type) {
				if ($opt['graph_mod'] == $type){
					$select_list .= "<option value=\"$type\" selected>$type</option>\n";
				} else {
					$select_list .= "<option value=\"$type\">$type</option>\n";
				}
			}
			$radio_chk[$opt['change']] = "checked";
			$radio = "   column<input type=\"radio\" name=\"change\" value=\"0\"" . $radio_chk[0] . " />\n";
			$radio .= "   row<input type=\"radio\" name=\"change\" value=\"1\"" . $radio_chk[1] . " />\n";

			$head_button = <<<EOD
<div style="float:left;">
 <form action="$script" method="post">
  <div>
   <input type="hidden" name="cmd" value="charts" />
   <input type="hidden" name="refer" value="$page" />
   <input type="hidden" name="charts_num" value="$count" />
   <select name="graph_mod">
$select_list</select>
$radio
   <input type="submit" name="list" value="execution" />
  </div>
 </form>
</div>
EOD;

//		$head_button .= "\n<a href=\"$php_source\">check</a>\n";

		// open or close
			$image_png = 'plus/close.png" width="11" height="11" alt="" title="list close"';
			$list_mod = 'close';
		} else if ($opt['list_mod'] == 'close' || $opt['list_mod'] == ''){
			$image_png = 'list.png" width="20" height="20" alt="" title="list open"';
			$opt['list_mod'] = 'off';
			$list_mod = 'open';
		}
		$head_button .= '<div style="float:right;"><a name="charts_number' . $count . '"></a><a href="' . $script
			  . "?plugin=charts&amp;refer=$r_page&amp;list_mod=" . $list_mod . "&amp;charts_num=$count"
			  . '"><img src="' . IMAGE_URI . $image_png . '/></a></div><div style="float:both;"></div>';

	}

    return $head_button . InsertChart( $swf, $charts_library, $php_source, $opt['width'], $opt['height'], $opt['bg_color']); 
}



function plugin_charts_action()
{
	global $vars, $post, $script;
	$line_count = 1;
	$charts_num = 	$vars['charts_num'];
	$page = 		$vars['refer'];


	if ( isset($vars['chart_type']) ) {
		pkwk_common_headers();
		header('Content-type: text/xml');
		print '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
		print $xml = chart_xml($vars['chart_type'], $vars['table_num'], $vars['change'], $page, $vars);
		exit;
	}

	if (auth::check_role('readonly')) die_message('PKWK_READONLY prohibits editing');
	if (! path_check($script,$_SERVER['HTTP_REFERER'])) return;

	$args  = get_source($page);
	static $count = 0;
	$source_s = '';

	$notimestamp = (isset($vars['notimestamp'])) ? TRUE : FALSE;

	if ($vars['list_mod'] == 'close'){
		$search_r = array ('@list_mod=open@si');
		$replace_r = array ('list_mod=close');
	} else if ($vars['list_mod'] == 'open') {
		$search_r = array ('@list_mod=close@si');
		$replace_r = array ('list_mod=open');
	}

	foreach ($args as $args_line) {
//		if (preg_match('/^#charts([^\(\{]+)(?:\(([^\r]*)\))?(\{*)/', $args_line, $matches)){
		if(preg_match('/^#([^\(\{]+)(?:\(([^\r]*)\))?(\{*)/', $args_line, $matches) && $matches[1] == 'charts') {
			$charts_find = 1;
			$count++;
			if( $line_count === 1 && $count == $charts_num ){
				if(! preg_match('/(auth_check_off)/i',$matches[2])){
					check_editable($page, true, true);
				}
				if ( isset($vars['list_mod']) ){		//list_mod
					$notimestamp = TRUE;
					if(preg_match('/(list_mod)/',$matches[2])){
						$args_line = preg_replace($search_r, $replace_r, $args_line);
					} else {
						$args_line = "#charts(" . $matches[2] . ",list_mod=" . $vars['list_mod'] . ")" . $matches[3] . "\n";
					}
				}
				if ( isset($vars['list']) ){		//list_mod
					$notimestamp = TRUE;
					if( preg_match('/(graph_mod)/', $matches[2] ) ){
						$args_line = preg_replace( array ('@graph_mod=[^>]*?,@si'), array ('graph_mod=' . $vars['graph_mod'] . ','), $args_line);
					} else {
						$args_line = "#charts(" . "graph_mod=" . $vars['graph_mod'] . "," . $matches[2] . ")" . $matches[3] . "\n";
					}
					if( preg_match('/(change)/', $matches[2] ) ){
						$args_line = preg_replace( array ('@change=\d@si'), array ('change=' . $vars['change']), $args_line);
					} else {
						$args_line = "#charts(" . $matches[2] . ",change=" . $vars['change'] . ")" . $matches[3] . "\n";
					}
				}
			}
			$end_line = strlen($matches[3]);
		}
		if (preg_match('/^\}{' . $end_line . '}/', $args_line)) {
			$charts_find = 0;
		}
		$source_s .= $args_line;
	}

	if(isset($vars['list_mod']) || isset($vars['list'])){
		page_write($page, $source_s, $notimestamp);
	}

	$post['refer'] = $page;
	header('Location: ' . $script . '?' . rawurlencode($page) . '#' . "charts_number$charts_num");
	exit;
}

function chart_xml($chart_type, $table_num, $change, $page, $vars)
{
	$source = get_source($page);

	$table_count = 1;
	$table_chk = 0;
	$line_count = 0;

	foreach ($source as $s_line){

		//table search
		if ( preg_match('/^\|(.+)\|([hHfFcC]?)$/', $s_line, $match) ){
			if ( $table_count == $table_num ){
				if ( strtolower( $match[2] ) != 'c'){
					$cells = explode("|", $match[1]);
					$cell_count = 0;
					foreach ($cells as $cell){
						if( $line_count == 0 || $cell_count == 0 ){
							$cell = "\t\t\t<string>" . $cell . "</string>\r\n";
						} else {
							$cell = "\t\t\t<number>" . $cell . "</number>\r\n";
						}
						if( $change ){
							$chart[$cell_count][$line_count] = $cell;
						} else {
							$chart[$line_count][$cell_count] = $cell;
						}
						$cell_count++;
					}
					$line_count++;
				}
			}
			$table_chk = 1;
		} else {
			if ( $table_chk ) {
				$table_count++;
				$table_chk = 0;
			}
		}
	}

	if ( $change ) {
		$max_y = $line_count;
		$max_x = $cell_count;
	} else {
		$max_x = $line_count;
		$max_y = $cell_count;
	}
//	$max_x = count($chart);
//	$max_y = count($chart[1]);

	$xml =  "<chart>\r\n";
	$xml .= "\t<chart_type>" . $chart_type . "</chart_type>\r\n";
	if ( isset($vars['chart_transition'])){
		$xml .= "\t<chart_transition type=\"drop\" delay=\"1\" duration=\"2\" order=\"series\" />\r\n";
	}
	if ( isset($vars['chart_value'])){
		$xml .= "\t<chart_value prefix=\"\" suffix=\"" . $vars['chart_value'] . "\" decimals=\"0\" decimal_char=\".\" separator=\"\""
			 . " position=\"middle\" hide_zero=\"\" as_percentage=\"\" font=\"Arial\" bold=\"1\" size=\"10\" color=\"FFFFFF\" alpha=\"90\" />\r\n";
	}
	$xml .= "\t<chart_data>\r\n";

	for ( $x = 0; $x < $max_x;$x++){
		$xml .= "\t\t<row>\r\n";
		for ( $y = 0; $y < $max_y;$y++){
				$xml .= $chart[$x][$y];
		}
		$xml .= "\t\t</row>\r\n";
	}
	$xml .= "\t</chart_data>\r\n";
	$xml .=  "</chart>\r\n";

	return $xml;
}

?>
