<?php
	# Mantis - a php based bugtracking system
	# Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
	# Copyright (C) 2002 - 2004  Mantis Team   - mantisbt-dev@lists.sourceforge.net
	# This program is distributed under the terms and conditions of the GPL
	# See the README and LICENSE files for details

	# --------------------------------------------------------
	# $Id: $
	# --------------------------------------------------------

	# ======================================================================
	# Author: Marcello Scata' <marcelloscata at users.sourceforge.net> ITALY
	# ======================================================================

	require_once( 'core.php' );

	$t_core_path = config_get( 'core_path' );

	require_once( $t_core_path . 'graphviz_api.php' );

	compress_enable();

	$f_access_level = gpc_get_int( 'access', VIEWER );

	$t_enum_status = config_get( 'status_enum_string' );
	$t_enum_workflow = config_get( 'status_enum_workflow' );
	$t_reopen = config_get( 'bug_reopen_status' );
	$t_thresh_array = config_get( 'set_status_threshold' );
	$t_bug_submit_status = config_get( 'bug_submit_status' );
	$t_bug_readonly_status_threshold = config_get( 'bug_readonly_status_threshold' );
	$t_reopen_bug_threshold = config_get( 'reopen_bug_threshold' );
	$t_bug_reopen_status = config_get( 'bug_reopen_status' );

	$t_graph_fontname	= config_get( 'relationship_graph_fontname' );
	$t_graph_fontsize	= config_get( 'relationship_graph_fontsize' );
	$t_graph_fontpath	= config_get( 'relationship_graph_fontpath' );
	$t_dot_tool			= config_get( 'dot_tool' );

	$t_graph_attributes	= array ( );

	if ( !empty( $t_graph_fontpath ) )
		$t_graph_attributes['fontpath'] = $t_graph_fontpath;

	$t_graph = new Digraph( 'STATES', $t_graph_attributes, $t_dot_tool );

	$t_graph->set_default_node_attr( array (
		'fontname'	=> $t_graph_fontname,
		'fontsize'	=> $t_graph_fontsize,
		'shape'		=> 'record',
		'style'		=> 'filled',
		'height'	=> '0.2',
		'width'		=> '0.4'
	) );

	$t_graph->set_default_edge_attr( array (
		'fontname'	=> $t_graph_fontname,
		'fontsize'	=> $t_graph_fontsize,
		'style'		=> 'solid',
		'color'		=> '#000000',
		'dir'		=> 'forward'
	) );

	$t_status_arr  = explode_enum_string( $t_enum_status );

	foreach ( $t_status_arr as $t_status ) {
		list( $t_status_id, $t_status_label ) = explode_enum_arr( $t_status );
		$t_status_label = get_enum_element( 'status', $t_status_id );

		$t_node_attributes = array ( );
		$t_node_attributes['label'] = $t_status_label;
		if( $t_bug_submit_status == $t_status_id || $t_bug_readonly_status_threshold <= $t_status_id ) {
			# submit status with bold border
			$t_node_attributes['style'] = 'bold, filled';
		}
		else {
			$t_node_attributes['style'] = 'filled';
		}
		$t_node_attributes['color'] = 'black';
		$t_node_attributes['fillcolor']	= get_status_color( $t_status_id );
		$t_node_attributes['URL']	= '';
		$t_node_attributes['tooltip']	= '';
		$t_graph->add_node( $t_status_id, $t_node_attributes );

		if( isset( $t_enum_workflow[$t_status_id] ) ) {
			$t_next_arr = explode_enum_string( $t_enum_workflow[$t_status_id] );
			foreach ( $t_next_arr as $t_next ) {
				if ( !is_blank( $t_next ) ) {
					list( $t_next_id, $t_next_label ) = explode_enum_arr( $t_next );

					# link  to itself
					if( $t_status_id == $t_next_id )
						continue;

					if( access_get_status_threshold( $t_next_id ) <= $f_access_level ) {
						$t_color = '#000000';
					}
					else {
						$t_color = '#C0C0C0';
					}

					$t_graph->add_edge( $t_status_id, $t_next_id, array ( 'style' => 'solid', 'color' => $t_color, 'dir' => 'forward' ) );
				}
			}
		}
	}

	# add user defined arcs and implicit reopen arcs
	foreach ( $t_status_arr as $t_status ) {
		list( $t_status_id, $t_status_label ) = explode_enum_arr( $t_status );
		if ( $t_status_id >= $t_bug_readonly_status_threshold ) {
			if( access_get_status_threshold( $t_bug_reopen_status ) <= $f_access_level ) {
				$t_color = '#0000ff';
			}
			else {
				$t_color = '#C0C0C0';
			}

			$t_graph->add_edge( $t_status_id, $t_bug_reopen_status, array ( 'style' => 'solid', 'color' => $t_color, 'dir' => 'forward' ) );
		}
	}

	$t_graph->output( 'png', true );
?>