Blame Identity/Models/Html/Mantis/1.1.2-1.fc9/core/bug_api.php

4c79b5
4c79b5
# Mantis - a php based bugtracking system
4c79b5
4c79b5
# Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
4c79b5
# Copyright (C) 2002 - 2007  Mantis Team   - mantisbt-dev@lists.sourceforge.net
4c79b5
4c79b5
# Mantis is free software: you can redistribute it and/or modify
4c79b5
# it under the terms of the GNU General Public License as published by
4c79b5
# the Free Software Foundation, either version 2 of the License, or
4c79b5
# (at your option) any later version.
4c79b5
#
4c79b5
# Mantis is distributed in the hope that it will be useful,
4c79b5
# but WITHOUT ANY WARRANTY; without even the implied warranty of
4c79b5
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4c79b5
# GNU General Public License for more details.
4c79b5
#
4c79b5
# You should have received a copy of the GNU General Public License
4c79b5
# along with Mantis.  If not, see <http://www.gnu.org/licenses/>.
4c79b5
4c79b5
	#------------------------------
4c79b5
	#   $Revision: 2643 $
4c79b5
	#     $Author: al $    
4c79b5
	#       $Date: 2009-06-18 19:06:27 -0400 (Thu, 18 Jun 2009) $  
4c79b5
	#------------------------------
4c79b5
4c79b5
	$t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR;
4c79b5
4c79b5
	require_once( $t_core_dir . 'history_api.php' );
4c79b5
	require_once( $t_core_dir . 'email_api.php' );
4c79b5
	require_once( $t_core_dir . 'bugnote_api.php' );
4c79b5
	require_once( $t_core_dir . 'file_api.php' );
4c79b5
	require_once( $t_core_dir . 'string_api.php' );
4c79b5
	require_once( $t_core_dir . 'sponsorship_api.php' );
4c79b5
	require_once( $t_core_dir . 'twitter_api.php' );
4c79b5
	require_once( $t_core_dir . 'tag_api.php' );
4c79b5
4c79b5
	# MASC RELATIONSHIP
4c79b5
	require_once( $t_core_dir.'relationship_api.php' );
4c79b5
	# MASC RELATIONSHIP
4c79b5
4c79b5
	### Bug API ###
4c79b5
4c79b5
	#===================================
4c79b5
	# Bug Data Structure Definition
4c79b5
	#===================================
4c79b5
	class BugData {
4c79b5
		var $project_id = null;
4c79b5
		var $reporter_id = 0;
4c79b5
		var $handler_id = 0;
4c79b5
		var $duplicate_id = 0;
4c79b5
		var $priority = NORMAL;
4c79b5
		var $severity = MINOR;
4c79b5
		var $reproducibility = 10;
4c79b5
		var $status = NEW_;
4c79b5
		var $resolution = OPEN;
4c79b5
		var $projection = 10;
4c79b5
		var $category = '';
4c79b5
		var $date_submitted = '';
4c79b5
		var $last_updated = '';
4c79b5
		var $eta = 10;
4c79b5
		var $os = '';
4c79b5
		var $os_build = '';
4c79b5
		var $platform = '';
4c79b5
		var $version = '';
4c79b5
		var $fixed_in_version = '';
4c79b5
		var $target_version = '';
4c79b5
		var $build = '';
4c79b5
		var $view_state = VS_PUBLIC;
4c79b5
		var $summary = '';
4c79b5
		var $sponsorship_total = 0;
4c79b5
		var $sticky = 0;
4c79b5
4c79b5
		# omitted:
4c79b5
		# var $bug_text_id
4c79b5
		var $profile_id;
4c79b5
4c79b5
		# extended info
4c79b5
		var $description = '';
4c79b5
		var $steps_to_reproduce = '';
4c79b5
		var $additional_information = '';
4c79b5
		
4c79b5
		#internal helper objects
4c79b5
		var $_stats = null;
4c79b5
	}
4c79b5
4c79b5
	#===================================
4c79b5
	# Caching
4c79b5
	#===================================
4c79b5
4c79b5
	#########################################
4c79b5
	# SECURITY NOTE: cache globals are initialized here to prevent them
4c79b5
	#   being spoofed if register_globals is turned on
4c79b5
4c79b5
	$g_cache_bug = array();
4c79b5
	$g_cache_bug_text = array();
4c79b5
4c79b5
	# --------------------
4c79b5
	# Cache an object as a bug.
4c79b5
	function bug_cache_database_result( $p_bug_datebase_result, $p_stats = null ) {
4c79b5
		global $g_cache_bug;
4c79b5
		
4c79b5
		if ( isset( $g_cache_bug[ $p_bug_datebase_result['id'] ] ) ) {
4c79b5
			return $g_cache_bug[ $p_bug_datebase_result['id'] ];
4c79b5
		}	
4c79b5
		
4c79b5
		if( !is_int( $p_bug_datebase_result['date_submitted'] ) )
4c79b5
			$p_bug_datebase_result['date_submitted']	= db_unixtimestamp( $p_bug_datebase_result['date_submitted']['date_submitted'] );
4c79b5
		if( !is_int( $p_bug_datebase_result['last_updated'] ) )
4c79b5
			$p_bug_datebase_result['last_updated']	= db_unixtimestamp( $p_bug_datebase_result['last_updated'] );
4c79b5
		$g_cache_bug[ $p_bug_datebase_result['id'] ] = $p_bug_datebase_result;
4c79b5
		if( !is_null( $p_stats ) ) {
4c79b5
			$g_cache_bug[ $p_bug_datebase_result['id'] ]['_stats'] = $p_stats;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Cache a bug row if necessary and return the cached copy
4c79b5
	#  If the second parameter is true (default), trigger an error
4c79b5
	#  if the bug can't be found.  If the second parameter is
4c79b5
	#  false, return false if the bug can't be found.
4c79b5
	function bug_cache_row( $p_bug_id, $p_trigger_errors=true ) {
4c79b5
		global $g_cache_bug;
4c79b5
4c79b5
		if ( isset( $g_cache_bug[$p_bug_id] ) ) {
4c79b5
			return $g_cache_bug[$p_bug_id];
4c79b5
		}
4c79b5
4c79b5
		$c_bug_id		= db_prepare_int( $p_bug_id );
4c79b5
		$t_bug_table	= config_get( 'mantis_bug_table' );
4c79b5
4c79b5
		$query = "SELECT *
4c79b5
				  FROM $t_bug_table
4c79b5
				  WHERE id='$c_bug_id'";
4c79b5
		$result = db_query( $query );
4c79b5
4c79b5
		if ( 0 == db_num_rows( $result ) ) {
4c79b5
			$g_cache_bug[$c_bug_id] = false;
4c79b5
4c79b5
			if ( $p_trigger_errors ) {
4c79b5
				error_parameters( $p_bug_id );
4c79b5
				trigger_error( ERROR_BUG_NOT_FOUND, ERROR );
4c79b5
			} else {
4c79b5
				return false;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$row = db_fetch_array( $result );
4c79b5
		$row['date_submitted']	= db_unixtimestamp( $row['date_submitted'] );
4c79b5
		$row['last_updated']	= db_unixtimestamp( $row['last_updated'] );
4c79b5
		$g_cache_bug[$c_bug_id] = $row;
4c79b5
4c79b5
		return $row;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Inject a bug into the bug cache
4c79b5
	function bug_add_to_cache( $p_bug_row ) {
4c79b5
		global $g_cache_bug;
4c79b5
4c79b5
		if ( !is_array( $p_bug_row ) )
4c79b5
			return false;
4c79b5
4c79b5
		$c_bug_id = db_prepare_int( $p_bug_row['id'] );
4c79b5
		$g_cache_bug[ $c_bug_id ] = $p_bug_row;
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Clear the bug cache (or just the given id if specified)
4c79b5
	function bug_clear_cache( $p_bug_id = null ) {
4c79b5
		global $g_cache_bug;
4c79b5
4c79b5
		if ( null === $p_bug_id ) {
4c79b5
			$g_cache_bug = array();
4c79b5
		} else {
4c79b5
			$c_bug_id = db_prepare_int( $p_bug_id );
4c79b5
			unset( $g_cache_bug[$c_bug_id] );
4c79b5
		}
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Cache a bug text row if necessary and return the cached copy
4c79b5
	#  If the second parameter is true (default), trigger an error
4c79b5
	#  if the bug text can't be found.  If the second parameter is
4c79b5
	#  false, return false if the bug text can't be found.
4c79b5
	function bug_text_cache_row( $p_bug_id, $p_trigger_errors=true ) {
4c79b5
		global $g_cache_bug_text;
4c79b5
4c79b5
		$c_bug_id			= db_prepare_int( $p_bug_id );
4c79b5
		$t_bug_table		= config_get( 'mantis_bug_table' );
4c79b5
		$t_bug_text_table	= config_get( 'mantis_bug_text_table' );
4c79b5
4c79b5
		if ( isset ( $g_cache_bug_text[$c_bug_id] ) ) {
4c79b5
			return $g_cache_bug_text[$c_bug_id];
4c79b5
		}
4c79b5
4c79b5
		$query = "SELECT bt.*
4c79b5
				  FROM $t_bug_text_table bt, $t_bug_table b
4c79b5
				  WHERE b.id='$c_bug_id' AND
4c79b5
				  		b.bug_text_id = bt.id";
4c79b5
		$result = db_query( $query );
4c79b5
4c79b5
		if ( 0 == db_num_rows( $result ) ) {
4c79b5
			$g_cache_bug_text[$c_bug_id] = false;
4c79b5
4c79b5
			if ( $p_trigger_errors ) {
4c79b5
				error_parameters( $p_bug_id );
4c79b5
				trigger_error( ERROR_BUG_NOT_FOUND, ERROR );
4c79b5
			} else {
4c79b5
				return false;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$row = db_fetch_array( $result );
4c79b5
4c79b5
		$g_cache_bug_text[$c_bug_id] = $row;
4c79b5
4c79b5
		return $row;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Clear the bug text cache (or just the given id if specified)
4c79b5
	function bug_text_clear_cache( $p_bug_id = null ) {
4c79b5
		global $g_cache_bug_text;
4c79b5
4c79b5
		if ( null === $p_bug_id ) {
4c79b5
			$g_cache_bug_text = array();
4c79b5
		} else {
4c79b5
			$c_bug_id = db_prepare_int( $p_bug_id );
4c79b5
			unset( $g_cache_bug_text[$c_bug_id] );
4c79b5
		}
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	#===================================
4c79b5
	# Boolean queries and ensures
4c79b5
	#===================================
4c79b5
4c79b5
	# --------------------
4c79b5
	# check to see if bug exists by id
4c79b5
	# return true if it does, false otherwise
4c79b5
	function bug_exists( $p_bug_id ) {
4c79b5
		if ( false == bug_cache_row( $p_bug_id, false ) ) {
4c79b5
			return false;
4c79b5
		} else {
4c79b5
			return true;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# check to see if bug exists by id
4c79b5
	# if it doesn't exist then error
4c79b5
	#  otherwise let execution continue undisturbed
4c79b5
	function bug_ensure_exists( $p_bug_id ) {
4c79b5
		if ( !bug_exists( $p_bug_id ) ) {
4c79b5
			error_parameters( $p_bug_id );
4c79b5
			trigger_error( ERROR_BUG_NOT_FOUND, ERROR );
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# check if the given user is the reporter of the bug
4c79b5
	# return true if the user is the reporter, false otherwise
4c79b5
	function bug_is_user_reporter( $p_bug_id, $p_user_id ) {
4c79b5
		if ( bug_get_field( $p_bug_id, 'reporter_id' ) == $p_user_id ) {
4c79b5
			return true;
4c79b5
		} else {
4c79b5
			return false;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# check if the given user is the handler of the bug
4c79b5
	# return true if the user is the handler, false otherwise
4c79b5
	function bug_is_user_handler( $p_bug_id, $p_user_id ) {
4c79b5
		if ( bug_get_field( $p_bug_id, 'handler_id' ) == $p_user_id ) {
4c79b5
			return true;
4c79b5
		} else {
4c79b5
			return false;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Check if the bug is readonly and shouldn't be modified
4c79b5
	# For a bug to be readonly the status has to be >= bug_readonly_status_threshold and
4c79b5
	# current user access level < update_readonly_bug_threshold.
4c79b5
	function bug_is_readonly( $p_bug_id ) {
4c79b5
		$t_status = bug_get_field( $p_bug_id, 'status' );
4c79b5
		if ( $t_status < config_get( 'bug_readonly_status_threshold' ) ) {
4c79b5
			return false;
4c79b5
		}
4c79b5
4c79b5
		if ( access_has_bug_level( config_get( 'update_readonly_bug_threshold' ), $p_bug_id ) ) {
4c79b5
			return false;
4c79b5
		}
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Check if the bug is resolved
4c79b5
	function bug_is_resolved( $p_bug_id ) {
4c79b5
		$t_status = bug_get_field( $p_bug_id, 'status' );
4c79b5
		return ( $t_status >= config_get( 'bug_resolved_status_threshold' ) );
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Validate workflow state to see if bug can be moved to requested state
4c79b5
	function bug_check_workflow( $p_bug_status, $p_wanted_status ) {
4c79b5
		$t_status_enum_workflow = config_get( 'status_enum_workflow' );
4c79b5
4c79b5
		if ( count( $t_status_enum_workflow ) < 1) {
4c79b5
			# workflow not defined, use default enum
4c79b5
			return true;
4c79b5
		} else if ( $p_bug_status == $p_wanted_status ) {
4c79b5
			# no change in state, allow the transition
4c79b5
			return true;
4c79b5
		} else {
4c79b5
			# workflow defined - find allowed states
4c79b5
			$t_allowed_states = $t_status_enum_workflow[$p_bug_status];
4c79b5
			$t_arr = explode_enum_string( $t_allowed_states );
4c79b5
4c79b5
			$t_enum_count = count( $t_arr );
4c79b5
4c79b5
			for ( $i = 0; $i < $t_enum_count; $i++ ) {
4c79b5
				# check if wanted status is allowed
4c79b5
				$t_elem  = explode_enum_arr( $t_arr[$i] );
4c79b5
				if ( $p_wanted_status == $t_elem[0] ) {
4c79b5
					return true;
4c79b5
				}
4c79b5
			} # end for
4c79b5
		}
4c79b5
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	#===================================
4c79b5
	# Creation / Deletion / Updating
4c79b5
	#===================================
4c79b5
4c79b5
	# --------------------
4c79b5
	# Create a new bug and return the bug id
4c79b5
	#
4c79b5
	function bug_create( $p_bug_data ) {
4c79b5
4c79b5
		$c_summary				= db_prepare_string( $p_bug_data->summary );
4c79b5
		$c_description			= db_prepare_string( $p_bug_data->description );
4c79b5
		$c_project_id			= db_prepare_int( $p_bug_data->project_id );
4c79b5
		$c_reporter_id			= db_prepare_int( $p_bug_data->reporter_id );
4c79b5
		$c_handler_id			= db_prepare_int( $p_bug_data->handler_id );
4c79b5
		$c_priority				= db_prepare_int( $p_bug_data->priority );
4c79b5
		$c_severity				= db_prepare_int( $p_bug_data->severity );
4c79b5
		$c_reproducibility		= db_prepare_int( $p_bug_data->reproducibility );
4c79b5
		$c_category				= db_prepare_string( $p_bug_data->category );
4c79b5
		$c_os					= db_prepare_string( $p_bug_data->os );
4c79b5
		$c_os_build				= db_prepare_string( $p_bug_data->os_build );
4c79b5
		$c_platform				= db_prepare_string( $p_bug_data->platform );
4c79b5
		$c_version				= db_prepare_string( $p_bug_data->version );
4c79b5
		$c_build				= db_prepare_string( $p_bug_data->build );
4c79b5
		$c_profile_id			= db_prepare_int( $p_bug_data->profile_id );
4c79b5
		$c_view_state			= db_prepare_int( $p_bug_data->view_state );
4c79b5
		$c_steps_to_reproduce	= db_prepare_string( $p_bug_data->steps_to_reproduce );
4c79b5
		$c_additional_info		= db_prepare_string( $p_bug_data->additional_information );
4c79b5
		$c_sponsorship_total 	= 0;
4c79b5
		$c_sticky 				= 0;
4c79b5
4c79b5
		# Summary cannot be blank
4c79b5
		if ( is_blank( $c_summary ) ) {
4c79b5
			error_parameters( lang_get( 'summary' ) );
4c79b5
			trigger_error( ERROR_EMPTY_FIELD, ERROR );
4c79b5
		}
4c79b5
4c79b5
		# Description cannot be blank
4c79b5
		if ( is_blank( $c_description ) ) {
4c79b5
			error_parameters( lang_get( 'description' ) );
4c79b5
			trigger_error( ERROR_EMPTY_FIELD, ERROR );
4c79b5
		}
4c79b5
4c79b5
		# If category is not specified and not defaulted + project has categories defined, then error.
4c79b5
		if ( is_blank( $c_category ) && ( category_get_count( $c_project_id ) > 0 ) ) {
4c79b5
			error_parameters( lang_get( 'category' ) );
4c79b5
			trigger_error( ERROR_EMPTY_FIELD, ERROR );
4c79b5
		}
4c79b5
4c79b5
		# Only set target_version if user has access to do so
4c79b5
		if ( access_has_project_level( config_get( 'roadmap_update_threshold' ) ) ) {
4c79b5
			$c_target_version	= db_prepare_string( $p_bug_data->target_version );
4c79b5
		} else { 
4c79b5
			$c_target_version	= '';
4c79b5
		}
4c79b5
4c79b5
		$t_bug_table				= config_get( 'mantis_bug_table' );
4c79b5
		$t_bug_text_table			= config_get( 'mantis_bug_text_table' );
4c79b5
		$t_project_category_table	= config_get( 'mantis_project_category_table' );
4c79b5
4c79b5
		# Insert text information
4c79b5
		$query = "INSERT INTO $t_bug_text_table
4c79b5
				    ( description, steps_to_reproduce, additional_information )
4c79b5
				  VALUES
4c79b5
				    ( '$c_description', '$c_steps_to_reproduce',
4c79b5
				      '$c_additional_info' )";
4c79b5
		db_query( $query );
4c79b5
4c79b5
		# Get the id of the text information we just inserted
4c79b5
		# NOTE: this is guarranteed to be the correct one.
4c79b5
		# The value LAST_INSERT_ID is stored on a per connection basis.
4c79b5
4c79b5
		$t_text_id = db_insert_id($t_bug_text_table);
4c79b5
4c79b5
		# check to see if we want to assign this right off
4c79b5
		$t_status = config_get( 'bug_submit_status' );
4c79b5
4c79b5
		# if not assigned, check if it should auto-assigned.
4c79b5
		if ( 0 == $c_handler_id ) {
4c79b5
			# if a default user is associated with the category and we know at this point
4c79b5
			# that that the bug was not assigned to somebody, then assign it automatically.
4c79b5
			$query = "SELECT user_id
4c79b5
					  FROM $t_project_category_table
4c79b5
					  WHERE project_id='$c_project_id' AND category='$c_category'";
4c79b5
			$result = db_query( $query );
4c79b5
4c79b5
			if ( db_num_rows( $result ) > 0 ) {
4c79b5
				$c_handler_id = $p_handler_id = db_result( $result );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		# Check if bug was pre-assigned or auto-assigned.
4c79b5
		if ( ( $c_handler_id != 0 ) && ( ON == config_get( 'auto_set_status_to_assigned' ) ) ) {
4c79b5
			$t_status = config_get( 'bug_assigned_status' );
4c79b5
		}
4c79b5
4c79b5
		# Insert the rest of the data
4c79b5
		$t_resolution = OPEN;
4c79b5
4c79b5
		$query = "INSERT INTO $t_bug_table
4c79b5
				    ( project_id,
4c79b5
				      reporter_id, handler_id,
4c79b5
				      duplicate_id, priority,
4c79b5
				      severity, reproducibility,
4c79b5
				      status, resolution,
4c79b5
				      projection, category,
4c79b5
				      date_submitted, last_updated,
4c79b5
				      eta, bug_text_id,
4c79b5
				      os, os_build,
4c79b5
				      platform, version,
4c79b5
				      build,
4c79b5
				      profile_id, summary, view_state, sponsorship_total, sticky, fixed_in_version,
4c79b5
				      target_version 
4c79b5
				    )
4c79b5
				  VALUES
4c79b5
				    ( '$c_project_id',
4c79b5
				      '$c_reporter_id', '$c_handler_id',
4c79b5
				      '0', '$c_priority',
4c79b5
				      '$c_severity', '$c_reproducibility',
4c79b5
				      '$t_status', '$t_resolution',
4c79b5
				      10, '$c_category',
4c79b5
				      " . db_now() . "," . db_now() . ",
4c79b5
				      10, '$t_text_id',
4c79b5
				      '$c_os', '$c_os_build',
4c79b5
				      '$c_platform', '$c_version',
4c79b5
				      '$c_build',
4c79b5
				      '$c_profile_id', '$c_summary', '$c_view_state', '$c_sponsorship_total', '$c_sticky', '',
4c79b5
				      '$c_target_version'
4c79b5
				    )";
4c79b5
		db_query( $query );
4c79b5
4c79b5
		$t_bug_id = db_insert_id($t_bug_table);
4c79b5
4c79b5
		# log new bug
4c79b5
		history_log_event_special( $t_bug_id, NEW_BUG );
4c79b5
4c79b5
		# log changes, if any (compare happens in history_log_event_direct)
4c79b5
		history_log_event_direct( $t_bug_id, 'status', config_get( 'bug_submit_status' ), $t_status );
4c79b5
		history_log_event_direct( $t_bug_id, 'handler_id', 0, $c_handler_id );
4c79b5
4c79b5
		return $t_bug_id;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Copy a bug from one project to another. Also make copies of issue notes, attachments, history,
4c79b5
	# email notifications etc.
4c79b5
	# @@@ Not managed FTP file upload
4c79b5
	# MASC RELATIONSHIP
4c79b5
	function bug_copy( $p_bug_id, $p_target_project_id = null, $p_copy_custom_fields = false, $p_copy_relationships = false,
4c79b5
		$p_copy_history = false, $p_copy_attachments = false, $p_copy_bugnotes = false, $p_copy_monitoring_users = false ) {
4c79b5
		global $g_db;
4c79b5
4c79b5
		$t_mantis_custom_field_string_table	= config_get( 'mantis_custom_field_string_table' );
4c79b5
		$t_mantis_bug_file_table			= config_get( 'mantis_bug_file_table' );
4c79b5
		$t_mantis_bugnote_table				= config_get( 'mantis_bugnote_table' );
4c79b5
		$t_mantis_bugnote_text_table		= config_get( 'mantis_bugnote_text_table' );
4c79b5
		$t_mantis_bug_monitor_table			= config_get( 'mantis_bug_monitor_table' );
4c79b5
		$t_mantis_bug_history_table			= config_get( 'mantis_bug_history_table' );
4c79b5
		$t_mantis_db = $g_db;
4c79b5
4c79b5
		$t_bug_id = db_prepare_int( $p_bug_id );
4c79b5
		$t_target_project_id = db_prepare_int( $p_target_project_id );
4c79b5
4c79b5
4c79b5
		$t_bug_data = new BugData;
4c79b5
		$t_bug_data = bug_get( $t_bug_id, true );
4c79b5
4c79b5
		# retrieve the project id associated with the bug
4c79b5
		if ( ( $p_target_project_id == null ) || is_blank( $p_target_project_id ) ) {
4c79b5
			$t_target_project_id = $t_bug_data->project_id;
4c79b5
		}
4c79b5
4c79b5
		$t_bug_data->project_id = $t_target_project_id;
4c79b5
4c79b5
		$t_new_bug_id = bug_create( $t_bug_data );
4c79b5
4c79b5
		# MASC ATTENTION: IF THE SOURCE BUG HAS TO HANDLER THE bug_create FUNCTION CAN TRY TO AUTO-ASSIGN THE BUG
4c79b5
		# WE FORCE HERE TO DUPLICATE THE SAME HANDLER OF THE SOURCE BUG
4c79b5
		# @@@ VB: Shouldn't we check if the handler in the source project is also a handler in the destination project?
4c79b5
		bug_set_field( $t_new_bug_id, 'handler_id', $t_bug_data->handler_id );
4c79b5
4c79b5
		bug_set_field( $t_new_bug_id, 'duplicate_id', $t_bug_data->duplicate_id );
4c79b5
		bug_set_field( $t_new_bug_id, 'status', $t_bug_data->status );
4c79b5
		bug_set_field( $t_new_bug_id, 'resolution', $t_bug_data->resolution );
4c79b5
		bug_set_field( $t_new_bug_id, 'projection', $t_bug_data->projection );
4c79b5
		bug_set_field( $t_new_bug_id, 'date_submitted', $t_mantis_db->DBTimeStamp( $t_bug_data->date_submitted ), false );
4c79b5
		bug_set_field( $t_new_bug_id, 'last_updated', $t_mantis_db->DBTimeStamp( $t_bug_data->last_updated ), false );
4c79b5
		bug_set_field( $t_new_bug_id, 'eta', $t_bug_data->eta );
4c79b5
		bug_set_field( $t_new_bug_id, 'fixed_in_version', $t_bug_data->fixed_in_version );
4c79b5
		bug_set_field( $t_new_bug_id, 'target_version', $t_bug_data->target_version );
4c79b5
		bug_set_field( $t_new_bug_id, 'sponsorship_total', 0 );
4c79b5
		bug_set_field( $t_new_bug_id, 'sticky', 0 );
4c79b5
4c79b5
		# COPY CUSTOM FIELDS
4c79b5
		if ( $p_copy_custom_fields ) {
4c79b5
			$query = "SELECT field_id, bug_id, value
4c79b5
					   FROM $t_mantis_custom_field_string_table
4c79b5
					   WHERE bug_id = '$t_bug_id';";
4c79b5
			$result = db_query( $query );
4c79b5
			$t_count = db_num_rows( $result );
4c79b5
4c79b5
			for ( $i = 0 ; $i < $t_count ; $i++ ) {
4c79b5
				$t_bug_custom = db_fetch_array( $result );
4c79b5
4c79b5
				$c_field_id = db_prepare_int( $t_bug_custom['field_id'] );
4c79b5
				$c_new_bug_id = db_prepare_int( $t_new_bug_id );
4c79b5
				$c_value = db_prepare_string( $t_bug_custom['value'] );
4c79b5
4c79b5
				$query = "INSERT INTO $t_mantis_custom_field_string_table
4c79b5
						   ( field_id, bug_id, value )
4c79b5
						   VALUES ('$c_field_id', '$c_new_bug_id', '$c_value')";
4c79b5
				db_query( $query );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		# COPY RELATIONSHIPS
4c79b5
		if ( $p_copy_relationships ) {
4c79b5
			if ( ON == config_get( 'enable_relationship' ) ) {
4c79b5
				relationship_copy_all( $t_bug_id,$t_new_bug_id );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		# Copy bugnotes
4c79b5
		if ( $p_copy_bugnotes ) {
4c79b5
			$query = "SELECT *
4c79b5
					  FROM $t_mantis_bugnote_table
4c79b5
					  WHERE bug_id = '$t_bug_id';";
4c79b5
			$result = db_query( $query );
4c79b5
			$t_count = db_num_rows( $result );
4c79b5
4c79b5
			for ( $i = 0; $i < $t_count; $i++ ) {
4c79b5
				$t_bug_note = db_fetch_array( $result );
4c79b5
				$t_bugnote_text_id = $t_bug_note['bugnote_text_id'];
4c79b5
4c79b5
				$query2 = "SELECT *
4c79b5
						   FROM $t_mantis_bugnote_text_table
4c79b5
						   WHERE id = '$t_bugnote_text_id';";
4c79b5
				$result2 = db_query( $query2 );
4c79b5
				$t_count2 = db_num_rows( $result2 );
4c79b5
4c79b5
				$t_bugnote_text_insert_id = -1;
4c79b5
				if ( $t_count2 > 0 ) {
4c79b5
					$t_bugnote_text = db_fetch_array( $result2 );
4c79b5
					$t_bugnote_text['note'] = db_prepare_string( $t_bugnote_text['note'] );
4c79b5
4c79b5
					$query2 = "INSERT INTO $t_mantis_bugnote_text_table
4c79b5
							   ( note )
4c79b5
							   VALUES ( '" . $t_bugnote_text['note'] . "' );";
4c79b5
					db_query( $query2 );
4c79b5
					$t_bugnote_text_insert_id = db_insert_id( $t_mantis_bugnote_text_table );
4c79b5
				}
4c79b5
4c79b5
				$query2 = "INSERT INTO $t_mantis_bugnote_table
4c79b5
						   ( bug_id, reporter_id, bugnote_text_id, view_state, date_submitted, last_modified )
4c79b5
						   VALUES ( '$t_new_bug_id',
4c79b5
						   			'" . $t_bug_note['reporter_id'] . "',
4c79b5
						   			'$t_bugnote_text_insert_id',
4c79b5
						   			'" . $t_bug_note['view_state'] . "',
4c79b5
						   			'" . $t_bug_note['date_submitted'] . "',
4c79b5
						   			'" . $t_bug_note['last_modified'] . "' );";
4c79b5
				db_query( $query2 );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		# Copy attachments
4c79b5
		if ( $p_copy_attachments ) {
4c79b5
			$query = "SELECT *
4c79b5
					  FROM $t_mantis_bug_file_table
4c79b5
					  WHERE bug_id = '$t_bug_id';";
4c79b5
			$result = db_query( $query );
4c79b5
			$t_count = db_num_rows( $result );
4c79b5
4c79b5
			$t_bug_file = array();
4c79b5
			for ( $i = 0; $i < $t_count; $i++ ) {
4c79b5
				$t_bug_file = db_fetch_array( $result );
4c79b5
4c79b5
				# prepare the new diskfile name and then copy the file
4c79b5
				$t_file_path = dirname( $t_bug_file['folder'] );
4c79b5
				$t_new_diskfile_name = $t_file_path . file_generate_unique_name( 'bug-' . $p_file_name, $t_file_path );
4c79b5
				$t_new_file_name = file_get_display_name( $t_bug_file['filename'] );
4c79b5
				if ( ( config_get( 'file_upload_method' ) == DISK ) ) {
4c79b5
					copy( $t_bug_file['diskfile'], $t_new_diskfile_name );
4c79b5
					chmod( $t_new_diskfile_name, config_get( 'attachments_file_permissions' ) );
4c79b5
				}
4c79b5
4c79b5
				$query = "INSERT INTO $t_mantis_bug_file_table
4c79b5
						( bug_id, title, description, diskfile, filename, folder, filesize, file_type, date_added, content )
4c79b5
						VALUES ( '$t_new_bug_id',
4c79b5
								 '" . db_prepare_string( $t_bug_file['title'] ) . "',
4c79b5
								 '" . db_prepare_string( $t_bug_file['description'] ) . "',
4c79b5
								 '" . db_prepare_string( $t_new_diskfile_name ) . "',
4c79b5
								 '" . db_prepare_string( $t_new_file_name ) . "',
4c79b5
								 '" . db_prepare_string( $t_bug_file['folder'] ) . "',
4c79b5
								 '" . db_prepare_int( $t_bug_file['filesize'] ) . "',
4c79b5
								 '" . db_prepare_string( $t_bug_file['file_type'] ) . "',
4c79b5
								 '" . db_prepare_string( $t_bug_file['date_added'] ) . "',
4c79b5
								 '" . db_prepare_string( $t_bug_file['content'] ) . "');";
4c79b5
				db_query( $query );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		# Copy users monitoring bug
4c79b5
		if ( $p_copy_monitoring_users ) {
4c79b5
			$query = "SELECT *
4c79b5
					  FROM $t_mantis_bug_monitor_table
4c79b5
					  WHERE bug_id = '$t_bug_id';";
4c79b5
			$result = db_query( $query );
4c79b5
			$t_count = db_num_rows( $result );
4c79b5
4c79b5
			for ( $i = 0; $i < $t_count; $i++ ) {
4c79b5
				$t_bug_monitor = db_fetch_array( $result );
4c79b5
				$query = "INSERT INTO $t_mantis_bug_monitor_table
4c79b5
						 ( user_id, bug_id )
4c79b5
						 VALUES ( '" . $t_bug_monitor['user_id'] . "', '$t_new_bug_id' );";
4c79b5
				db_query( $query );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		# COPY HISTORY
4c79b5
		history_delete( $t_new_bug_id );	# should history only be deleted inside the if statement below?
4c79b5
		if ( $p_copy_history ) {
4c79b5
			$query = "SELECT *
4c79b5
					  FROM $t_mantis_bug_history_table
4c79b5
					  WHERE bug_id = '$t_bug_id';";
4c79b5
			$result = db_query( $query );
4c79b5
			$t_count = db_num_rows( $result );
4c79b5
4c79b5
			for ( $i = 0; $i < $t_count; $i++ ) {
4c79b5
				$t_bug_history = db_fetch_array( $result );
4c79b5
				$query = "INSERT INTO $t_mantis_bug_history_table
4c79b5
						  ( user_id, bug_id, date_modified, field_name, old_value, new_value, type )
4c79b5
						  VALUES ( '" . db_prepare_int( $t_bug_history['user_id'] ) . "',
4c79b5
						  		   '$t_new_bug_id',
4c79b5
						  		   '" . db_prepare_string( $t_bug_history['date_modified'] ) . "',
4c79b5
						  		   '" . db_prepare_string( $t_bug_history['field_name'] ) . "',
4c79b5
						  		   '" . db_prepare_string( $t_bug_history['old_value'] ) . "',
4c79b5
						  		   '" . db_prepare_string( $t_bug_history['new_value'] ) . "',
4c79b5
						  		   '" . db_prepare_int( $t_bug_history['type'] ) . "' );";
4c79b5
				db_query( $query );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return $t_new_bug_id;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# allows bug deletion :
4c79b5
	# delete the bug, bugtext, bugnote, and bugtexts selected
4c79b5
	# used in bug_delete.php & mass treatments
4c79b5
	function bug_delete( $p_bug_id ) {
4c79b5
		$c_bug_id			= db_prepare_int( $p_bug_id );
4c79b5
		$t_bug_table		= config_get( 'mantis_bug_table' );
4c79b5
		$t_bug_text_table	= config_get( 'mantis_bug_text_table' );
4c79b5
4c79b5
		# call pre-deletion custom function
4c79b5
		helper_call_custom_function( 'issue_delete_validate', array( $p_bug_id ) );
4c79b5
4c79b5
		# log deletion of bug
4c79b5
		history_log_event_special( $p_bug_id, BUG_DELETED, bug_format_id( $p_bug_id ) );
4c79b5
4c79b5
		email_bug_deleted( $p_bug_id );
4c79b5
4c79b5
		# call post-deletion custom function.  We call this here to allow the custom function to access the details of the bug before 
4c79b5
		# they are deleted from the database given it's id.  The other option would be to move this to the end of the function and
4c79b5
		# provide it with bug data rather than an id, but this will break backward compatibility.
4c79b5
		helper_call_custom_function( 'issue_delete_notify', array( $p_bug_id ) );
4c79b5
4c79b5
		# Unmonitor bug for all users
4c79b5
		bug_unmonitor( $p_bug_id, null );
4c79b5
4c79b5
		# Delete custom fields
4c79b5
		custom_field_delete_all_values( $p_bug_id );
4c79b5
4c79b5
		# Delete bugnotes
4c79b5
		bugnote_delete_all( $p_bug_id );
4c79b5
4c79b5
		# Delete all sponsorships
4c79b5
		sponsorship_delete( sponsorship_get_all_ids( $p_bug_id ) );
4c79b5
4c79b5
		# MASC RELATIONSHIP
4c79b5
		# we delete relationships even if the feature is currently off.
4c79b5
		relationship_delete_all( $p_bug_id );
4c79b5
		# MASC RELATIONSHIP
4c79b5
4c79b5
		# Delete files
4c79b5
		file_delete_attachments( $p_bug_id );
4c79b5
4c79b5
		# Detach tags
4c79b5
		tag_bug_detach_all( $p_bug_id, false );
4c79b5
		
4c79b5
		# Delete the bug history
4c79b5
		history_delete( $p_bug_id );
4c79b5
4c79b5
		# Delete the bugnote text
4c79b5
		$t_bug_text_id = bug_get_field( $p_bug_id, 'bug_text_id' );
4c79b5
4c79b5
		$query = "DELETE FROM $t_bug_text_table
4c79b5
				  WHERE id='$t_bug_text_id'";
4c79b5
		db_query( $query );
4c79b5
4c79b5
		# Delete the bug entry
4c79b5
		$query = "DELETE FROM $t_bug_table
4c79b5
				  WHERE id='$c_bug_id'";
4c79b5
		db_query( $query );
4c79b5
4c79b5
		bug_clear_cache( $p_bug_id );
4c79b5
		bug_text_clear_cache( $p_bug_id );
4c79b5
4c79b5
		# db_query() errors on failure so:
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Delete all bugs associated with a project
4c79b5
	function bug_delete_all( $p_project_id ) {
4c79b5
		$c_project_id = db_prepare_int( $p_project_id );
4c79b5
4c79b5
		$t_bug_table = config_get( 'mantis_bug_table' );
4c79b5
4c79b5
		$query = "SELECT id
4c79b5
				  FROM $t_bug_table
4c79b5
				  WHERE project_id='$c_project_id'";
4c79b5
		$result = db_query( $query );
4c79b5
4c79b5
		$bug_count = db_num_rows( $result );
4c79b5
4c79b5
		for ( $i=0 ; $i < $bug_count ; $i++ ) {
4c79b5
			$row = db_fetch_array( $result );
4c79b5
4c79b5
			bug_delete( $row['id'] );
4c79b5
		}
4c79b5
4c79b5
		# @@@ should we check the return value of each bug_delete() and
4c79b5
		#  return false if any of them return false? Presumable bug_delete()
4c79b5
		#  will eventually trigger an error on failure so it won't matter...
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Update a bug from the given data structure
4c79b5
	#  If the third parameter is true, also update the longer strings table
4c79b5
	function bug_update( $p_bug_id, $p_bug_data, $p_update_extended = false, $p_bypass_mail = false ) {
4c79b5
		$c_bug_id		= db_prepare_int( $p_bug_id );
4c79b5
		$c_bug_data		= bug_prepare_db( $p_bug_data );
4c79b5
4c79b5
		# Summary cannot be blank
4c79b5
		if ( is_blank( $c_bug_data->summary ) ) {
4c79b5
			error_parameters( lang_get( 'summary' ) );
4c79b5
			trigger_error( ERROR_EMPTY_FIELD, ERROR );
4c79b5
		}
4c79b5
4c79b5
		if ( $p_update_extended ) {
4c79b5
			# Description field cannot be empty
4c79b5
			if ( is_blank( $c_bug_data->description ) ) {
4c79b5
				error_parameters( lang_get( 'description' ) );
4c79b5
				trigger_error( ERROR_EMPTY_FIELD, ERROR );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		if( !is_blank( $p_bug_data->duplicate_id ) && ( $p_bug_data->duplicate_id != 0 ) && ( $p_bug_id == $p_bug_data->duplicate_id ) ) {
4c79b5
			trigger_error( ERROR_BUG_DUPLICATE_SELF, ERROR );  # never returns
4c79b5
	    }
4c79b5
4c79b5
		$t_old_data = bug_get( $p_bug_id, true );
4c79b5
4c79b5
		$t_bug_table = config_get( 'mantis_bug_table' );
4c79b5
4c79b5
		# Update all fields
4c79b5
		# Ignore date_submitted and last_updated since they are pulled out
4c79b5
		#  as unix timestamps which could confuse the history log and they
4c79b5
		#  shouldn't get updated like this anyway.  If you really need to change
4c79b5
		#  them use bug_set_field()
4c79b5
		$query = "UPDATE $t_bug_table
4c79b5
				SET project_id='$c_bug_data->project_id',
4c79b5
					reporter_id='$c_bug_data->reporter_id',
4c79b5
					handler_id='$c_bug_data->handler_id',
4c79b5
					duplicate_id='$c_bug_data->duplicate_id',
4c79b5
					priority='$c_bug_data->priority',
4c79b5
					severity='$c_bug_data->severity',
4c79b5
					reproducibility='$c_bug_data->reproducibility',
4c79b5
					status='$c_bug_data->status',
4c79b5
					resolution='$c_bug_data->resolution',
4c79b5
					projection='$c_bug_data->projection',
4c79b5
					category='$c_bug_data->category',
4c79b5
					eta='$c_bug_data->eta',
4c79b5
					os='$c_bug_data->os',
4c79b5
					os_build='$c_bug_data->os_build',
4c79b5
					platform='$c_bug_data->platform',
4c79b5
					version='$c_bug_data->version',
4c79b5
					build='$c_bug_data->build',
4c79b5
					fixed_in_version='$c_bug_data->fixed_in_version',";
4c79b5
4c79b5
		$t_roadmap_updated = false;
4c79b5
		if ( access_has_project_level( config_get( 'roadmap_update_threshold' ) ) ) {
4c79b5
			$query .= "
4c79b5
					target_version='$c_bug_data->target_version',";
4c79b5
			$t_roadmap_updated = true;
4c79b5
		}
4c79b5
4c79b5
		$query .= "
4c79b5
					view_state='$c_bug_data->view_state',
4c79b5
					summary='$c_bug_data->summary',
4c79b5
					sponsorship_total='$c_bug_data->sponsorship_total',
4c79b5
					sticky='$c_bug_data->sticky'
4c79b5
				WHERE id='$c_bug_id'";
4c79b5
		db_query( $query );
4c79b5
4c79b5
		bug_clear_cache( $p_bug_id );
4c79b5
4c79b5
		# log changes
4c79b5
		history_log_event_direct( $p_bug_id, 'project_id', $t_old_data->project_id, $p_bug_data->project_id );
4c79b5
		history_log_event_direct( $p_bug_id, 'reporter_id', $t_old_data->reporter_id, $p_bug_data->reporter_id );
4c79b5
		history_log_event_direct( $p_bug_id, 'handler_id', $t_old_data->handler_id, $p_bug_data->handler_id );
4c79b5
		history_log_event_direct( $p_bug_id, 'duplicate_id', $t_old_data->duplicate_id, $p_bug_data->duplicate_id );
4c79b5
		history_log_event_direct( $p_bug_id, 'priority', $t_old_data->priority, $p_bug_data->priority );
4c79b5
		history_log_event_direct( $p_bug_id, 'severity', $t_old_data->severity, $p_bug_data->severity );
4c79b5
		history_log_event_direct( $p_bug_id, 'reproducibility', $t_old_data->reproducibility, $p_bug_data->reproducibility );
4c79b5
		history_log_event_direct( $p_bug_id, 'status', $t_old_data->status, $p_bug_data->status );
4c79b5
		history_log_event_direct( $p_bug_id, 'resolution', $t_old_data->resolution, $p_bug_data->resolution );
4c79b5
		history_log_event_direct( $p_bug_id, 'projection', $t_old_data->projection, $p_bug_data->projection );
4c79b5
		history_log_event_direct( $p_bug_id, 'category', $t_old_data->category, $p_bug_data->category );
4c79b5
		history_log_event_direct( $p_bug_id, 'eta',	$t_old_data->eta, $p_bug_data->eta );
4c79b5
		history_log_event_direct( $p_bug_id, 'os', $t_old_data->os, $p_bug_data->os );
4c79b5
		history_log_event_direct( $p_bug_id, 'os_build', $t_old_data->os_build, $p_bug_data->os_build );
4c79b5
		history_log_event_direct( $p_bug_id, 'platform', $t_old_data->platform, $p_bug_data->platform );
4c79b5
		history_log_event_direct( $p_bug_id, 'version', $t_old_data->version, $p_bug_data->version );
4c79b5
		history_log_event_direct( $p_bug_id, 'build', $t_old_data->build, $p_bug_data->build );
4c79b5
		history_log_event_direct( $p_bug_id, 'fixed_in_version', $t_old_data->fixed_in_version, $p_bug_data->fixed_in_version );
4c79b5
		if ( $t_roadmap_updated ) {
4c79b5
			history_log_event_direct( $p_bug_id, 'target_version', $t_old_data->target_version, $p_bug_data->target_version );
4c79b5
		}
4c79b5
		history_log_event_direct( $p_bug_id, 'view_state', $t_old_data->view_state, $p_bug_data->view_state );
4c79b5
		history_log_event_direct( $p_bug_id, 'summary', $t_old_data->summary, $p_bug_data->summary );
4c79b5
		history_log_event_direct( $p_bug_id, 'sponsorship_total', $t_old_data->sponsorship_total, $p_bug_data->sponsorship_total );
4c79b5
		history_log_event_direct( $p_bug_id, 'sticky', $t_old_data->sticky, $p_bug_data->sticky );
4c79b5
4c79b5
		# Update extended info if requested
4c79b5
		if ( $p_update_extended ) {
4c79b5
			$t_bug_text_table = config_get( 'mantis_bug_text_table' );
4c79b5
4c79b5
			$t_bug_text_id = bug_get_field( $p_bug_id, 'bug_text_id' );
4c79b5
4c79b5
			$query = "UPDATE $t_bug_text_table
4c79b5
						SET description='$c_bug_data->description',
4c79b5
							steps_to_reproduce='$c_bug_data->steps_to_reproduce',
4c79b5
							additional_information='$c_bug_data->additional_information'
4c79b5
						WHERE id='$t_bug_text_id'";
4c79b5
			db_query( $query );
4c79b5
4c79b5
			bug_text_clear_cache( $p_bug_id );
4c79b5
4c79b5
			if ( $t_old_data->description != $p_bug_data->description ) {
4c79b5
				history_log_event_special( $p_bug_id, DESCRIPTION_UPDATED );
4c79b5
			}
4c79b5
			if ( $t_old_data->steps_to_reproduce != $p_bug_data->steps_to_reproduce ) {
4c79b5
				history_log_event_special( $p_bug_id, STEP_TO_REPRODUCE_UPDATED );
4c79b5
			}
4c79b5
			if ( $t_old_data->additional_information != $p_bug_data->additional_information ) {
4c79b5
				history_log_event_special( $p_bug_id, ADDITIONAL_INFO_UPDATED );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		# Update the last update date
4c79b5
		bug_update_date( $p_bug_id );
4c79b5
4c79b5
		if ( false == $p_bypass_mail ) {		# allow bypass if user is sending mail separately
4c79b5
			$t_action_prefix = 'email_notification_title_for_action_bug_';
4c79b5
			$t_status_prefix = 'email_notification_title_for_status_bug_';
4c79b5
4c79b5
			# status changed
4c79b5
			if ( $t_old_data->status != $p_bug_data->status ) {
4c79b5
				$t_status = get_enum_to_string( config_get( 'status_enum_string' ), $p_bug_data->status );
4c79b5
				$t_status = str_replace( ' ', '_', $t_status );
4c79b5
				email_generic( $p_bug_id, $t_status, $t_status_prefix . $t_status );
4c79b5
				return true;
4c79b5
			}
4c79b5
4c79b5
			# bug assigned
4c79b5
			if ( $t_old_data->handler_id != $p_bug_data->handler_id ) {
4c79b5
				email_generic( $p_bug_id, 'owner', $t_action_prefix . 'assigned' );
4c79b5
				return true;
4c79b5
			}
4c79b5
4c79b5
			# @@@ handle priority change if it requires special handling
4c79b5
4c79b5
			# generic update notification
4c79b5
			email_generic( $p_bug_id, 'updated', $t_action_prefix . 'updated' );
4c79b5
		}
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	#===================================
4c79b5
	# Data Access
4c79b5
	#===================================
4c79b5
4c79b5
	# --------------------
4c79b5
	# Returns the extended record of the specified bug, this includes
4c79b5
	# the bug text fields
4c79b5
	# @@@ include reporter name and handler name, the problem is that
4c79b5
	#      handler can be 0, in this case no corresponding name will be
4c79b5
	#      found.  Use equivalent of (+) in Oracle.
4c79b5
	function bug_get_extended_row( $p_bug_id ) {
4c79b5
		$t_base = bug_cache_row( $p_bug_id );
4c79b5
		$t_text = bug_text_cache_row( $p_bug_id );
4c79b5
4c79b5
		# merge $t_text first so that the 'id' key has the bug id not the bug text id
4c79b5
		return array_merge( $t_text, $t_base );
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Returns the record of the specified bug
4c79b5
	function bug_get_row( $p_bug_id ) {
4c79b5
		return bug_cache_row( $p_bug_id );
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Returns an object representing the specified bug
4c79b5
	function bug_get( $p_bug_id, $p_get_extended = false ) {
4c79b5
		if ( $p_get_extended ) {
4c79b5
			$row = bug_get_extended_row( $p_bug_id );
4c79b5
		} else {
4c79b5
			$row = bug_get_row( $p_bug_id );
4c79b5
		}
4c79b5
4c79b5
		$t_bug_data = new BugData;
4c79b5
		$t_row_keys = array_keys( $row );
4c79b5
		$t_vars = get_object_vars( $t_bug_data );
4c79b5
4c79b5
		# Check each variable in the class
4c79b5
		foreach ( $t_vars as $var => $val ) {
4c79b5
			# If we got a field from the DB with the same name
4c79b5
			if ( in_array( $var, $t_row_keys, true ) ) {
4c79b5
				# Store that value in the object
4c79b5
				$t_bug_data->$var = $row[$var];
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return $t_bug_data;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# return the specified field of the given bug
4c79b5
	#  if the field does not exist, display a warning and return ''
4c79b5
	function bug_get_field( $p_bug_id, $p_field_name ) {
4c79b5
		$row = bug_get_row( $p_bug_id );
4c79b5
4c79b5
		if ( isset( $row[$p_field_name] ) ) {
4c79b5
			return $row[$p_field_name];
4c79b5
		} else {
4c79b5
			error_parameters( $p_field_name );
4c79b5
			trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
4c79b5
			return '';
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# return the specified text field of the given bug
4c79b5
	#  if the field does not exist, display a warning and return ''
4c79b5
	function bug_get_text_field( $p_bug_id, $p_field_name ) {
4c79b5
		$row = bug_text_cache_row( $p_bug_id );
4c79b5
4c79b5
		if ( isset( $row[$p_field_name] ) ) {
4c79b5
			return $row[$p_field_name];
4c79b5
		} else {
4c79b5
			error_parameters( $p_field_name );
4c79b5
			trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
4c79b5
			return '';
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# return the bug summary
4c79b5
	#  this is a wrapper for the custom function
4c79b5
	function bug_format_summary( $p_bug_id, $p_context ) {
4c79b5
		return 	helper_call_custom_function( 'format_issue_summary', array( $p_bug_id , $p_context ) );
4c79b5
	}
4c79b5
4c79b5
4c79b5
	# --------------------
4c79b5
	# Returns the number of bugnotes for the given bug_id
4c79b5
	function bug_get_bugnote_count( $p_bug_id ) {
4c79b5
		$c_bug_id = db_prepare_int( $p_bug_id );
4c79b5
4c79b5
		$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
4c79b5
4c79b5
		if ( !access_has_project_level( config_get( 'private_bugnote_threshold' ), $t_project_id ) ) {
4c79b5
			$t_restriction = 'AND view_state=' . VS_PUBLIC;
4c79b5
		} else {
4c79b5
			$t_restriction = '';
4c79b5
		}
4c79b5
4c79b5
		$t_bugnote_table = config_get( 'mantis_bugnote_table' );
4c79b5
		$query = "SELECT COUNT(*)
4c79b5
				  FROM $t_bugnote_table
4c79b5
				  WHERE bug_id ='$c_bug_id' $t_restriction";
4c79b5
		$result = db_query( $query );
4c79b5
4c79b5
		return db_result( $result );
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# return the timestamp for the most recent time at which a bugnote
4c79b5
	#  associated wiht the bug was modified
4c79b5
	function bug_get_newest_bugnote_timestamp( $p_bug_id ) {
4c79b5
		$c_bug_id			= db_prepare_int( $p_bug_id );
4c79b5
		$t_bugnote_table	= config_get( 'mantis_bugnote_table' );
4c79b5
4c79b5
		$query = "SELECT last_modified
4c79b5
				  FROM $t_bugnote_table
4c79b5
				  WHERE bug_id='$c_bug_id'
4c79b5
				  ORDER BY last_modified DESC";
4c79b5
		$result = db_query( $query, 1 );
4c79b5
		$row = db_result( $result );
4c79b5
4c79b5
		if ( false === $row ) {
4c79b5
			return false;
4c79b5
		} else {
4c79b5
			return db_unixtimestamp( $row );
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# return the timestamp for the most recent time at which a bugnote
4c79b5
	#  associated with the bug was modified and the total bugnote
4c79b5
	#  count in one db query
4c79b5
	function bug_get_bugnote_stats( $p_bug_id ) {
4c79b5
		global $g_cache_bug;
4c79b5
		$c_bug_id			= db_prepare_int( $p_bug_id );
4c79b5
4c79b5
		if( !is_null( $g_cache_bug[ $c_bug_id ]['_stats'] ) ) {
4c79b5
			if( $g_cache_bug[ $c_bug_id ]['_stats'] === false ) { 
4c79b5
				return false;			
4c79b5
			} else {
4c79b5
				$t_stats['last_modified'] = db_unixtimestamp( $g_cache_bug[ $c_bug_id ]['_stats']['last_modified'] );
4c79b5
				$t_stats['count'] = $g_cache_bug[ $c_bug_id ]['_stats']['count'];
4c79b5
			}
4c79b5
			return $t_stats;
4c79b5
		}
4c79b5
4c79b5
		$t_bugnote_table	= config_get( 'mantis_bugnote_table' );
4c79b5
4c79b5
		$query = "SELECT last_modified
4c79b5
				  FROM $t_bugnote_table
4c79b5
				  WHERE bug_id='$c_bug_id'
4c79b5
				  ORDER BY last_modified DESC";
4c79b5
		$result = db_query( $query );
4c79b5
		$row = db_fetch_array( $result );
4c79b5
4c79b5
		if ( false === $row )
4c79b5
			return false;
4c79b5
4c79b5
		$t_stats['last_modified'] = db_unixtimestamp( $row['last_modified'] );
4c79b5
		$t_stats['count'] = db_num_rows( $result );
4c79b5
4c79b5
		return $t_stats;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Get array of attachments associated with the specified bug id.  The array will be
4c79b5
	# sorted in terms of date added (ASC).  The array will include the following fields:
4c79b5
	# id, title, diskfile, filename, filesize, file_type, date_added.
4c79b5
	function bug_get_attachments( $p_bug_id ) {
4c79b5
		if ( !file_can_view_bug_attachments( $p_bug_id ) ) {
4c79b5
	        return;
4c79b5
		}
4c79b5
4c79b5
		$c_bug_id = db_prepare_int( $p_bug_id );
4c79b5
4c79b5
		$t_bug_file_table = config_get( 'mantis_bug_file_table' );
4c79b5
4c79b5
		$query = "SELECT id, title, diskfile, filename, filesize, file_type, date_added
4c79b5
		                FROM $t_bug_file_table
4c79b5
		                WHERE bug_id='$c_bug_id'
4c79b5
		                ORDER BY date_added";
4c79b5
		$db_result = db_query( $query );
4c79b5
		$num_notes = db_num_rows( $db_result );
4c79b5
4c79b5
		$t_result = array();
4c79b5
4c79b5
		for ( $i = 0; $i < $num_notes; $i++ ) {
4c79b5
			$t_result[] = db_fetch_array( $db_result );
4c79b5
		}
4c79b5
4c79b5
		return $t_result;
4c79b5
	}
4c79b5
4c79b5
	#===================================
4c79b5
	# Data Modification
4c79b5
	#===================================
4c79b5
4c79b5
	# --------------------
4c79b5
	# set the value of a bug field
4c79b5
	function bug_set_field( $p_bug_id, $p_field_name, $p_status, $p_prepare = true ) {
4c79b5
		$c_bug_id			= db_prepare_int( $p_bug_id );
4c79b5
		$c_field_name		= db_prepare_string( $p_field_name );
4c79b5
		if( $p_prepare ) {
4c79b5
			$c_status		= '\'' . db_prepare_string( $p_status ) . '\''; #generic, unknown type
4c79b5
		} else {
4c79b5
			$c_status		=  $p_status; #generic, unknown type
4c79b5
		}
4c79b5
4c79b5
		$h_status = bug_get_field( $p_bug_id, $p_field_name );
4c79b5
4c79b5
		# return if status is already set
4c79b5
		if ( $c_status == $h_status ) {
4c79b5
			return true;
4c79b5
		}
4c79b5
4c79b5
		$t_bug_table = config_get( 'mantis_bug_table' );
4c79b5
4c79b5
		# Update fields
4c79b5
		$query = "UPDATE $t_bug_table
4c79b5
				  SET $c_field_name=$c_status
4c79b5
				  WHERE id='$c_bug_id'";
4c79b5
		db_query( $query );
4c79b5
4c79b5
		# updated the last_updated date
4c79b5
		bug_update_date( $p_bug_id );
4c79b5
4c79b5
		# log changes
4c79b5
		history_log_event_direct( $p_bug_id, $p_field_name, $h_status, $p_status );
4c79b5
4c79b5
		bug_clear_cache( $p_bug_id );
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# assign the bug to the given user
4c79b5
	function bug_assign( $p_bug_id, $p_user_id, $p_bugnote_text='', $p_bugnote_private = false ) {
4c79b5
		$c_bug_id	= db_prepare_int( $p_bug_id );
4c79b5
		$c_user_id	= db_prepare_int( $p_user_id );
4c79b5
4c79b5
		if ( ( $c_user_id != NO_USER ) && !access_has_bug_level( config_get( 'handle_bug_threshold' ), $p_bug_id, $p_user_id ) ) {
4c79b5
		    trigger_error( ERROR_USER_DOES_NOT_HAVE_REQ_ACCESS );
4c79b5
		}
4c79b5
4c79b5
		# extract current information into history variables
4c79b5
		$h_status		= bug_get_field( $p_bug_id, 'status' );
4c79b5
		$h_handler_id	= bug_get_field( $p_bug_id, 'handler_id' );
4c79b5
4c79b5
		if ( ( ON == config_get( 'auto_set_status_to_assigned' ) ) &&
4c79b5
			 ( NO_USER != $p_user_id ) ) {
4c79b5
			$t_ass_val = config_get( 'bug_assigned_status' );
4c79b5
		} else {
4c79b5
			$t_ass_val = $h_status;
4c79b5
		}
4c79b5
4c79b5
		$t_bug_table = config_get( 'mantis_bug_table' );
4c79b5
4c79b5
		if ( ( $t_ass_val != $h_status ) || ( $p_user_id != $h_handler_id ) ) {
4c79b5
4c79b5
			# get user id
4c79b5
			$query = "UPDATE $t_bug_table
4c79b5
					  SET handler_id='$c_user_id', status='$t_ass_val'
4c79b5
					  WHERE id='$c_bug_id'";
4c79b5
			db_query( $query );
4c79b5
4c79b5
			# log changes
4c79b5
			history_log_event_direct( $c_bug_id, 'status', $h_status, $t_ass_val );
4c79b5
			history_log_event_direct( $c_bug_id, 'handler_id', $h_handler_id, $p_user_id );
4c79b5
4c79b5
			# Add bugnote if supplied ignore false return
4c79b5
			bugnote_add( $p_bug_id, $p_bugnote_text, 0, $p_bugnote_private, 0, '', NULL, FALSE );
4c79b5
4c79b5
			# updated the last_updated date
4c79b5
			bug_update_date( $p_bug_id );
4c79b5
4c79b5
			bug_clear_cache( $p_bug_id );
4c79b5
4c79b5
			# send assigned to email
4c79b5
			email_assign( $p_bug_id );
4c79b5
		}
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# close the given bug
4c79b5
	function bug_close( $p_bug_id, $p_bugnote_text = '', $p_bugnote_private = false, $p_time_tracking = '0:00' ) {
4c79b5
		$p_bugnote_text = trim( $p_bugnote_text );
4c79b5
4c79b5
		# Add bugnote if supplied ignore a false return
4c79b5
		# Moved bugnote_add before bug_set_field calls in case time_tracking_no_note is off.
4c79b5
		# Error condition stopped execution but status had already been changed
4c79b5
		bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', NULL, FALSE );
4c79b5
4c79b5
		bug_set_field( $p_bug_id, 'status', CLOSED );
4c79b5
4c79b5
		email_close( $p_bug_id );
4c79b5
4c79b5
		# MASC RELATIONSHIP
4c79b5
		if ( ON == config_get( 'enable_relationship' ) ) {
4c79b5
			email_relationship_child_closed( $p_bug_id );
4c79b5
		}
4c79b5
		# MASC RELATIONSHIP
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# resolve the given bug
4c79b5
	function bug_resolve( $p_bug_id, $p_resolution, $p_fixed_in_version = '', $p_bugnote_text = '', $p_duplicate_id = null, $p_handler_id = null, $p_bugnote_private = false, $p_time_tracking = '0:00' ) {
4c79b5
		$p_bugnote_text = trim( $p_bugnote_text );
4c79b5
4c79b5
		# Add bugnote if supplied
4c79b5
		# Moved bugnote_add before bug_set_field calls in case time_tracking_no_note is off.
4c79b5
		# Error condition stopped execution but status had already been changed
4c79b5
		bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', NULL, FALSE );
4c79b5
4c79b5
		$t_duplicate = !is_blank( $p_duplicate_id ) && ( $p_duplicate_id != 0 );
4c79b5
		if ( $t_duplicate ) {
4c79b5
			if ( $p_bug_id == $p_duplicate_id ) {
4c79b5
			    trigger_error( ERROR_BUG_DUPLICATE_SELF, ERROR );  # never returns
4c79b5
			}
4c79b5
4c79b5
			# the related bug exists...
4c79b5
			bug_ensure_exists( $p_duplicate_id );
4c79b5
4c79b5
			if ( ON == config_get( 'enable_relationship' ) ) {
4c79b5
				# check if there is other relationship between the bugs...
4c79b5
				$t_id_relationship = relationship_same_type_exists( $p_bug_id, $p_duplicate_id, BUG_DUPLICATE );
4c79b5
4c79b5
				if ( $t_id_relationship == -1 ) {
4c79b5
					# the relationship type is already set. Nothing to do
4c79b5
				}
4c79b5
				else if ( $t_id_relationship > 0 ) {
4c79b5
					# there is already a relationship between them -> we have to update it and not to add a new one
4c79b5
					helper_ensure_confirmed( lang_get( 'replace_relationship_sure_msg' ), lang_get( 'replace_relationship_button' ) );
4c79b5
4c79b5
					# Update the relationship
4c79b5
					relationship_update( $t_id_relationship, $p_bug_id, $p_duplicate_id, BUG_DUPLICATE );
4c79b5
4c79b5
					# Add log line to the history (both bugs)
4c79b5
					history_log_event_special( $p_bug_id, BUG_REPLACE_RELATIONSHIP, BUG_DUPLICATE, $p_duplicate_id );
4c79b5
					history_log_event_special( $p_duplicate_id, BUG_REPLACE_RELATIONSHIP, BUG_HAS_DUPLICATE, $p_bug_id );
4c79b5
				}
4c79b5
				else {
4c79b5
					# Add the new relationship
4c79b5
					relationship_add( $p_bug_id, $p_duplicate_id, BUG_DUPLICATE );
4c79b5
4c79b5
					# Add log line to the history (both bugs)
4c79b5
					history_log_event_special( $p_bug_id, BUG_ADD_RELATIONSHIP, BUG_DUPLICATE, $p_duplicate_id );
4c79b5
					history_log_event_special( $p_duplicate_id, BUG_ADD_RELATIONSHIP, BUG_HAS_DUPLICATE, $p_bug_id );
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			bug_set_field( $p_bug_id, 'duplicate_id', (int)$p_duplicate_id );
4c79b5
		}
4c79b5
		
4c79b5
		$c_resolution = db_prepare_int( $p_resolution );
4c79b5
4c79b5
		bug_set_field( $p_bug_id, 'status', config_get( 'bug_resolved_status_threshold' ) );
4c79b5
		bug_set_field( $p_bug_id, 'fixed_in_version', $p_fixed_in_version );
4c79b5
		bug_set_field( $p_bug_id, 'resolution', $c_resolution );
4c79b5
4c79b5
		# only set handler if specified explicitly or if bug was not assigned to a handler
4c79b5
		if ( null == $p_handler_id ) {
4c79b5
			if ( bug_get_field( $p_bug_id, 'handler_id' ) == 0 ) {
4c79b5
				$p_handler_id = auth_get_current_user_id();
4c79b5
				bug_set_field( $p_bug_id, 'handler_id', $p_handler_id );
4c79b5
			}
4c79b5
		} else {
4c79b5
			bug_set_field( $p_bug_id, 'handler_id', $p_handler_id );
4c79b5
		}
4c79b5
4c79b5
		email_resolved( $p_bug_id );
4c79b5
4c79b5
		if ( $c_resolution == FIXED ) {
4c79b5
			twitter_issue_resolved( $p_bug_id );
4c79b5
		}
4c79b5
4c79b5
		# MASC RELATIONSHIP
4c79b5
		if ( ON == config_get( 'enable_relationship' ) ) {
4c79b5
			email_relationship_child_resolved( $p_bug_id );
4c79b5
		}
4c79b5
		# MASC RELATIONSHIP
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# reopen the given bug
4c79b5
	function bug_reopen( $p_bug_id, $p_bugnote_text='', $p_time_tracking = '0:00', $p_bugnote_private = false ) {
4c79b5
		$p_bugnote_text = trim( $p_bugnote_text );
4c79b5
4c79b5
		# Add bugnote if supplied
4c79b5
		# Moved bugnote_add before bug_set_field calls in case time_tracking_no_note is off.
4c79b5
		# Error condition stopped execution but status had already been changed
4c79b5
		bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', NULL, FALSE );
4c79b5
4c79b5
		bug_set_field( $p_bug_id, 'status', config_get( 'bug_reopen_status' ) );
4c79b5
		bug_set_field( $p_bug_id, 'resolution', config_get( 'bug_reopen_resolution' ) );
4c79b5
4c79b5
		email_reopen( $p_bug_id );
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# updates the last_updated field
4c79b5
	function bug_update_date( $p_bug_id ) {
4c79b5
		$c_bug_id = db_prepare_int( $p_bug_id );
4c79b5
4c79b5
		$t_bug_table = config_get( 'mantis_bug_table' );
4c79b5
4c79b5
		$query = "UPDATE $t_bug_table
4c79b5
				  SET last_updated= " . db_now() . "
4c79b5
				  WHERE id='$c_bug_id'";
4c79b5
		db_query( $query );
4c79b5
4c79b5
		bug_clear_cache( $p_bug_id );
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# enable monitoring of this bug for the user
4c79b5
	function bug_monitor( $p_bug_id, $p_user_id ) {
4c79b5
		$c_bug_id	= db_prepare_int( $p_bug_id );
4c79b5
		$c_user_id	= db_prepare_int( $p_user_id );
4c79b5
4c79b5
		# Make sure we aren't already monitoring this bug
4c79b5
		if ( user_is_monitoring_bug( $p_user_id, $p_bug_id ) ) {
4c79b5
			return true;
4c79b5
		}
4c79b5
4c79b5
		$t_bug_monitor_table = config_get( 'mantis_bug_monitor_table' );
4c79b5
4c79b5
		# Insert monitoring record
4c79b5
		$query ="INSERT ".
4c79b5
				"INTO $t_bug_monitor_table ".
4c79b5
				"( user_id, bug_id ) ".
4c79b5
				"VALUES ".
4c79b5
				"( '$c_user_id', '$c_bug_id' )";
4c79b5
		db_query( $query );
4c79b5
4c79b5
		# log new monitoring action
4c79b5
		history_log_event_special( $p_bug_id, BUG_MONITOR, $c_user_id );
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# disable monitoring of this bug for the user
4c79b5
	# if $p_user_id = null, then bug is unmonitored for all users.
4c79b5
	function bug_unmonitor( $p_bug_id, $p_user_id ) {
4c79b5
		$c_bug_id	= db_prepare_int( $p_bug_id );
4c79b5
		$c_user_id	= db_prepare_int( $p_user_id );
4c79b5
4c79b5
		$t_bug_monitor_table = config_get( 'mantis_bug_monitor_table' );
4c79b5
4c79b5
		# Delete monitoring record
4c79b5
		$query ="DELETE ".
4c79b5
				"FROM $t_bug_monitor_table ".
4c79b5
				"WHERE bug_id = '$c_bug_id'";
4c79b5
4c79b5
		if ( $p_user_id !== null ) {
4c79b5
			$query .= " AND user_id = '$c_user_id'";
4c79b5
		}
4c79b5
4c79b5
		db_query( $query );
4c79b5
4c79b5
		# log new un-monitor action
4c79b5
		history_log_event_special( $p_bug_id, BUG_UNMONITOR, $p_user_id );
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	#===================================
4c79b5
	# Other
4c79b5
	#===================================
4c79b5
4c79b5
	# --------------------
4c79b5
	# Pads the bug id with the appropriate number of zeros.
4c79b5
	function bug_format_id( $p_bug_id ) {
4c79b5
		$t_padding = config_get( 'display_bug_padding' );
4c79b5
		return( str_pad( $p_bug_id, $t_padding, '0', STR_PAD_LEFT ) );
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Return a copy of the bug structure with all the instvars prepared for db insertion
4c79b5
	function bug_prepare_db( $p_bug_data ) {
4c79b5
		$t_bug_data = new BugData;
4c79b5
		$t_bug_data->project_id			= db_prepare_int( $p_bug_data->project_id );
4c79b5
		$t_bug_data->reporter_id		= db_prepare_int( $p_bug_data->reporter_id );
4c79b5
		$t_bug_data->handler_id			= db_prepare_int( $p_bug_data->handler_id );
4c79b5
		$t_bug_data->duplicate_id		= db_prepare_int( $p_bug_data->duplicate_id );
4c79b5
		$t_bug_data->priority			= db_prepare_int( $p_bug_data->priority );
4c79b5
		$t_bug_data->severity			= db_prepare_int( $p_bug_data->severity );
4c79b5
		$t_bug_data->reproducibility	= db_prepare_int( $p_bug_data->reproducibility );
4c79b5
		$t_bug_data->status				= db_prepare_int( $p_bug_data->status );
4c79b5
		$t_bug_data->resolution			= db_prepare_int( $p_bug_data->resolution );
4c79b5
		$t_bug_data->projection			= db_prepare_int( $p_bug_data->projection );
4c79b5
		$t_bug_data->category			= db_prepare_string( $p_bug_data->category );
4c79b5
		$t_bug_data->date_submitted		= db_prepare_string( $p_bug_data->date_submitted );
4c79b5
		$t_bug_data->last_updated		= db_prepare_string( $p_bug_data->last_updated );
4c79b5
		$t_bug_data->eta				= db_prepare_int( $p_bug_data->eta );
4c79b5
		$t_bug_data->os					= db_prepare_string( $p_bug_data->os );
4c79b5
		$t_bug_data->os_build			= db_prepare_string( $p_bug_data->os_build );
4c79b5
		$t_bug_data->platform			= db_prepare_string( $p_bug_data->platform );
4c79b5
		$t_bug_data->version			= db_prepare_string( $p_bug_data->version );
4c79b5
		$t_bug_data->build				= db_prepare_string( $p_bug_data->build );
4c79b5
		$t_bug_data->fixed_in_version	= db_prepare_string( $p_bug_data->fixed_in_version );
4c79b5
		$t_bug_data->target_version		= db_prepare_string( $p_bug_data->target_version );
4c79b5
		$t_bug_data->view_state			= db_prepare_int( $p_bug_data->view_state );
4c79b5
		$t_bug_data->summary			= db_prepare_string( $p_bug_data->summary );
4c79b5
		$t_bug_data->sponsorship_total	= db_prepare_int( $p_bug_data->sponsorship_total );
4c79b5
		$t_bug_data->sticky				= db_prepare_int( $p_bug_data->sticky );
4c79b5
4c79b5
		$t_bug_data->description		= db_prepare_string( $p_bug_data->description );
4c79b5
		$t_bug_data->steps_to_reproduce	= db_prepare_string( $p_bug_data->steps_to_reproduce );
4c79b5
		$t_bug_data->additional_information	= db_prepare_string( $p_bug_data->additional_information );
4c79b5
4c79b5
		return $t_bug_data;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Return a copy of the bug structure with all the instvars prepared for editing
4c79b5
	#  in an HTML form
4c79b5
	function bug_prepare_edit( $p_bug_data ) {
4c79b5
		$p_bug_data->category			= string_attribute( $p_bug_data->category );
4c79b5
		$p_bug_data->date_submitted		= string_attribute( $p_bug_data->date_submitted );
4c79b5
		$p_bug_data->last_updated		= string_attribute( $p_bug_data->last_updated );
4c79b5
		$p_bug_data->os					= string_attribute( $p_bug_data->os );
4c79b5
		$p_bug_data->os_build			= string_attribute( $p_bug_data->os_build );
4c79b5
		$p_bug_data->platform			= string_attribute( $p_bug_data->platform );
4c79b5
		$p_bug_data->version			= string_attribute( $p_bug_data->version );
4c79b5
		$p_bug_data->build				= string_attribute( $p_bug_data->build );
4c79b5
		$p_bug_data->target_version     = string_attribute( $p_bug_data->target_version );
4c79b5
		$p_bug_data->fixed_in_version	= string_attribute( $p_bug_data->fixed_in_version );
4c79b5
		$p_bug_data->summary			= string_attribute( $p_bug_data->summary );
4c79b5
		$p_bug_data->sponsorship_total	= string_attribute( $p_bug_data->sponsorship_total );
4c79b5
		$p_bug_data->sticky				= string_attribute( $p_bug_data->sticky );
4c79b5
4c79b5
		$p_bug_data->description		= string_textarea( $p_bug_data->description );
4c79b5
		$p_bug_data->steps_to_reproduce	= string_textarea( $p_bug_data->steps_to_reproduce );
4c79b5
		$p_bug_data->additional_information	= string_textarea( $p_bug_data->additional_information );
4c79b5
4c79b5
		return $p_bug_data;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Return a copy of the bug structure with all the instvars prepared for editing
4c79b5
	#  in an HTML form
4c79b5
	function bug_prepare_display( $p_bug_data ) {
4c79b5
		$p_bug_data->category			= string_display_line( $p_bug_data->category );
4c79b5
		$p_bug_data->date_submitted		= string_display_line( $p_bug_data->date_submitted );
4c79b5
		$p_bug_data->last_updated		= string_display_line( $p_bug_data->last_updated );
4c79b5
		$p_bug_data->os					= string_display_line( $p_bug_data->os );
4c79b5
		$p_bug_data->os_build			= string_display_line( $p_bug_data->os_build );
4c79b5
		$p_bug_data->platform			= string_display_line( $p_bug_data->platform );
4c79b5
		$p_bug_data->version			= string_display_line( $p_bug_data->version );
4c79b5
		$p_bug_data->build				= string_display_line( $p_bug_data->build );
4c79b5
		$p_bug_data->target_version		= string_display_line( $p_bug_data->target_version );
4c79b5
		$p_bug_data->fixed_in_version	= string_display_line( $p_bug_data->fixed_in_version );
4c79b5
		$p_bug_data->summary			= string_display_line_links( $p_bug_data->summary );
4c79b5
		$p_bug_data->sponsorship_total	= string_display_line( $p_bug_data->sponsorship_total );
4c79b5
		$p_bug_data->sticky				= string_display_line( $p_bug_data->sticky );
4c79b5
4c79b5
		$p_bug_data->description		= string_display_links( $p_bug_data->description );
4c79b5
		$p_bug_data->steps_to_reproduce	= string_display_links( $p_bug_data->steps_to_reproduce );
4c79b5
		$p_bug_data->additional_information	= string_display_links( $p_bug_data->additional_information );
4c79b5
4c79b5
		return $p_bug_data;
4c79b5
	}
4c79b5
?>