Blame Identity/Models/Html/Mantis/1.1.2-1.fc9/core/helper_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
	### Helper API ###
4c79b5
4c79b5
	# These are miscellaneous functions
4c79b5
4c79b5
	# --------------------
4c79b5
	# alternate color function
4c79b5
	#  If no index is given, continue alternating based on the last index given
4c79b5
	function helper_alternate_colors( $p_index, $p_odd_color, $p_even_color ) {
4c79b5
		static $t_index = 1;
4c79b5
4c79b5
		if ( null !== $p_index ) {
4c79b5
			$t_index = $p_index;
4c79b5
		}
4c79b5
4c79b5
		if ( 1 == $t_index++ % 2 ) {
4c79b5
			return $p_odd_color;
4c79b5
		} else {
4c79b5
			return $p_even_color;
4c79b5
		}
4c79b5
	}
4c79b5
	# --------------------
4c79b5
	# alternate classes for table rows
4c79b5
	#  If no index is given, continue alternating based on the last index given
4c79b5
	function helper_alternate_class( $p_index=null, $p_odd_class="row-1", $p_even_class="row-2" ) {
4c79b5
		static $t_index = 1;
4c79b5
4c79b5
		if ( null !== $p_index ) {
4c79b5
			$t_index = $p_index;
4c79b5
		}
4c79b5
4c79b5
		if ( 1 == $t_index++ % 2 ) {
4c79b5
			return "class=\"$p_odd_class\"";
4c79b5
		} else {
4c79b5
			return "class=\"$p_even_class\"";
4c79b5
		}
4c79b5
	}
4c79b5
	# --------------------
4c79b5
	# get the color string for the given status
4c79b5
	function get_status_color( $p_status ) {
4c79b5
		$t_status_enum_string	= config_get( 'status_enum_string' );
4c79b5
		$t_status_colors		= config_get( 'status_colors' );
4c79b5
4c79b5
		# This code creates the appropriate variable name
4c79b5
		# then references that color variable
4c79b5
		# You could replace this with a bunch of if... then... else
4c79b5
		# statements
4c79b5
4c79b5
		$t_color_str	= 'closed';
4c79b5
		$t_color = '#ffffff';
4c79b5
		$t_arr			= explode_enum_string( $t_status_enum_string );
4c79b5
		$t_arr_count	= count( $t_arr );
4c79b5
		for ( $i=0; $i < $t_arr_count ;$i++ ) {
4c79b5
			$elem_arr = explode_enum_arr( $t_arr[$i] );
4c79b5
			if ( $elem_arr[0] == $p_status ) {
4c79b5
				# now get the appropriate translation
4c79b5
				$t_color_str = $elem_arr[1];
4c79b5
				break;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
        if ( isset ( $t_status_colors[$t_color_str] ) ) {
4c79b5
			$t_color = $t_status_colors[$t_color_str];
4c79b5
		}
4c79b5
4c79b5
		return $t_color;
4c79b5
	}
4c79b5
	# --------------------
4c79b5
	# Given a enum string and num, return the appropriate string
4c79b5
	function get_enum_element( $p_enum_name, $p_val ) {
4c79b5
		$config_var = config_get( $p_enum_name.'_enum_string' );
4c79b5
		$string_var = lang_get(  $p_enum_name.'_enum_string' );
4c79b5
4c79b5
		# use the global enum string to search
4c79b5
		$t_arr			= explode_enum_string( $config_var );
4c79b5
		$t_arr_count	= count( $t_arr );
4c79b5
		for ( $i=0; $i < $t_arr_count ;$i++ ) {
4c79b5
			$elem_arr = explode_enum_arr( $t_arr[$i] );
4c79b5
			if ( $elem_arr[0] == $p_val ) {
4c79b5
				# now get the appropriate translation
4c79b5
				return get_enum_to_string( $string_var, $p_val );
4c79b5
			}
4c79b5
		}
4c79b5
		return '@' . $p_val . '@';
4c79b5
	}
4c79b5
	# --------------------
4c79b5
	# If $p_var is not an array and is equal to $p_val then we PRINT SELECTED.
4c79b5
	# If $p_var is an array, then if any member is equal to $p_val we PRINT SELECTED.
4c79b5
	# This is used when we want to know if a variable indicated a certain
4c79b5
	# option element is selected
4c79b5
	#
4c79b5
	# If the second parameter is not given, the first parameter is compared
4c79b5
	#  to the boolean value true
4c79b5
	function check_selected( $p_var, $p_val=true ) {
4c79b5
		if ( is_array( $p_var ) ) {
4c79b5
			foreach( $p_var as $t_this_var ) {
4c79b5
				# catch the case where one entry is 0 and the other is a string.
4c79b5
				if ( ( is_string( $t_this_var ) && !is_string( $p_val ) ) ) {
4c79b5
					if ( $t_this_var === $p_val ) {
4c79b5
						PRINT ' selected="selected" ';
4c79b5
						return ;
4c79b5
					}
4c79b5
				} else if ( $t_this_var == $p_val ) {
4c79b5
					PRINT ' selected="selected" ';
4c79b5
					return ; 
4c79b5
				}
4c79b5
			}
4c79b5
		} else {
4c79b5
		if ( is_string( $p_var ) && is_string( $p_val ) ) {
4c79b5
			if ( $p_var === $p_val ) {
4c79b5
				PRINT ' selected="selected" ';
4c79b5
				return ; 
4c79b5
			}
4c79b5
		} else if ( $p_var == $p_val ) {
4c79b5
			PRINT ' selected="selected" ';
4c79b5
			return ; 
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
	# --------------------
4c79b5
	# If $p_var and $p_val are equal to each other then we PRINT CHECKED
4c79b5
	# This is used when we want to know if a variable indicated a certain
4c79b5
	# element is checked
4c79b5
	#
4c79b5
	# If the second parameter is not given, the first parameter is compared
4c79b5
	#  to the boolean value true
4c79b5
	function check_checked( $p_var, $p_val=true ) {
4c79b5
		if ( $p_var == $p_val ) {
4c79b5
			PRINT ' checked="checked" ';
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Set up PHP for a long process execution
4c79b5
	# The script timeout is set based on the value of the
4c79b5
	#  long_process_timeout config option.
4c79b5
	# $p_ignore_abort specified whether to ignore user aborts by hitting
4c79b5
	#  the Stop button (the default is not to ignore user aborts)
4c79b5
	function helper_begin_long_process( $p_ignore_abort=false ) {
4c79b5
		$t_timeout = config_get( 'long_process_timeout' );
4c79b5
4c79b5
		# silent errors or warnings reported when safe_mode is ON.
4c79b5
		@set_time_limit( $t_timeout );
4c79b5
4c79b5
		ignore_user_abort( $p_ignore_abort );
4c79b5
		return $t_timeout;
4c79b5
	}
4c79b5
4c79b5
    # this allows pages to override the current project settings.
4c79b5
    #  This typically applies to the view bug pages where the "current"
4c79b5
    #  project as used by the filters, etc, does not match the bug being viewed.
4c79b5
    $g_project_override = null;
4c79b5
    $g_cache_current_project = null;
4c79b5
    
4c79b5
	# --------------------
4c79b5
	# Return the current project id as stored in a cookie
4c79b5
	#  If no cookie exists, the user's default project is returned
4c79b5
	function helper_get_current_project() {
4c79b5
        global $g_project_override, $g_cache_current_project;
4c79b5
        
4c79b5
        if ( $g_project_override !== null ) {
4c79b5
            return $g_project_override;
4c79b5
        }
4c79b5
4c79b5
        if( $g_cache_current_project === null) {
4c79b5
			$t_cookie_name = config_get( 'project_cookie' );
4c79b5
4c79b5
			$t_project_id = gpc_get_cookie( $t_cookie_name, null );
4c79b5
4c79b5
			if ( null === $t_project_id ) {
4c79b5
				$t_pref_row = user_pref_cache_row( auth_get_current_user_id(), ALL_PROJECTS, false );
4c79b5
				if ( false === $t_pref_row ) {
4c79b5
					$t_project_id = ALL_PROJECTS;
4c79b5
				} else {
4c79b5
					$t_project_id = $t_pref_row['default_project'];
4c79b5
				}
4c79b5
			} else {
4c79b5
				$t_project_id = split( ';', $t_project_id );
4c79b5
				$t_project_id = $t_project_id[ count( $t_project_id ) - 1 ];
4c79b5
			}
4c79b5
4c79b5
			if ( !project_exists( $t_project_id ) ||
4c79b5
				 ( 0 == project_get_field( $t_project_id, 'enabled' ) ) ||
4c79b5
				 !access_has_project_level( VIEWER, $t_project_id ) ) {
4c79b5
				$t_project_id = ALL_PROJECTS;
4c79b5
			}
4c79b5
			$g_cache_current_project = (int)$t_project_id;
4c79b5
		}
4c79b5
		return $g_cache_current_project;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Return the current project id as stored in a cookie, in an Array
4c79b5
	#  If no cookie exists, the user's default project is returned
4c79b5
	#  If the current project is a subproject, the return value will include
4c79b5
	#   any parent projects
4c79b5
	function helper_get_current_project_trace() {
4c79b5
		$t_cookie_name = config_get( 'project_cookie' );
4c79b5
4c79b5
		$t_project_id = gpc_get_cookie( $t_cookie_name, null );
4c79b5
4c79b5
		if ( null === $t_project_id ) {
4c79b5
			$t_bottom     = current_user_get_pref( 'default_project' );
4c79b5
			$t_project_id = Array( $t_bottom );
4c79b5
		} else {
4c79b5
			$t_project_id = split( ';', $t_project_id );
4c79b5
			$t_bottom     = $t_project_id[ count( $t_project_id ) - 1 ];
4c79b5
		}
4c79b5
4c79b5
		if ( !project_exists( $t_bottom ) ||
4c79b5
			 ( 0 == project_get_field( $t_bottom, 'enabled' ) ) ||
4c79b5
			 !access_has_project_level( VIEWER, $t_bottom ) ) {
4c79b5
			$t_project_id = Array( ALL_PROJECTS );
4c79b5
		}
4c79b5
4c79b5
		return $t_project_id;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Set the current project id (stored in a cookie)
4c79b5
	function helper_set_current_project( $p_project_id ) {
4c79b5
		$t_project_cookie_name	= config_get( 'project_cookie' );
4c79b5
4c79b5
		gpc_set_cookie( $t_project_cookie_name, $p_project_id, true );
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
	# --------------------
4c79b5
	# Clear all known user preference cookies
4c79b5
	function helper_clear_pref_cookies() {
4c79b5
		gpc_clear_cookie( config_get( 'project_cookie' ) );
4c79b5
		gpc_clear_cookie( config_get( 'manage_cookie' ) );
4c79b5
	}
4c79b5
	# --------------------
4c79b5
	# Check whether the user has confirmed this action.
4c79b5
	#
4c79b5
	# If the user has not confirmed the action, generate a page which asks
4c79b5
	#  the user to confirm and then submits a form back to the current page
4c79b5
	#  with all the GET and POST data and an additional field called _confirmed
4c79b5
	#  to indicate that confirmation has been done.
4c79b5
	function helper_ensure_confirmed( $p_message, $p_button_label ) {
4c79b5
		if (true == gpc_get_bool( '_confirmed' ) ) {
4c79b5
			return true;
4c79b5
		}
4c79b5
4c79b5
		html_page_top1();
4c79b5
		html_page_top2();
4c79b5
4c79b5
		# @@@ we need to improve this formatting.  I'd like the text to only
4c79b5
		#  be about 50% the width of the screen so that it doesn't become to hard
4c79b5
		#  to read.
4c79b5
4c79b5
		PRINT '
';
4c79b5
		PRINT '

' . $p_message . '

';
4c79b5
4c79b5
		PRINT '<form method="post" action="' . $_SERVER[ 'PHP_SELF' ] . "\">\n";
4c79b5
4c79b5
		print_hidden_inputs( gpc_strip_slashes( $_POST ) );
4c79b5
		print_hidden_inputs( gpc_strip_slashes( $_GET ) );
4c79b5
4c79b5
		PRINT "<input type=\"hidden\" name=\"_confirmed\" value=\"1\" />\n";
4c79b5
		PRINT '<input type="submit" class="button" value="' . $p_button_label . '" />';
4c79b5
		PRINT "\n</form>\n";
4c79b5
4c79b5
		PRINT "\n";
4c79b5
4c79b5
		html_page_bottom1();
4c79b5
		exit;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# Call custom function.
4c79b5
	#
4c79b5
	# $p_function - Name of function to call (eg: do_stuff).  The function will call custom_function_override_do_stuff()
4c79b5
	#		if found, otherwise, will call custom_function_default_do_stuff().
4c79b5
	# $p_args_array - Parameters to function as an array
4c79b5
	function helper_call_custom_function( $p_function, $p_args_array ) {
4c79b5
		$t_function = 'custom_function_override_' . $p_function;
4c79b5
4c79b5
		if ( !function_exists( $t_function ) ) {
4c79b5
			$t_function = 'custom_function_default_' . $p_function;
4c79b5
		}
4c79b5
4c79b5
		return call_user_func_array( $t_function, $p_args_array );
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	function helper_project_specific_where( $p_project_id, $p_user_id = null ) {
4c79b5
		if ( null === $p_user_id ) {
4c79b5
			$p_user_id = auth_get_current_user_id();
4c79b5
		}
4c79b5
4c79b5
		if ( ALL_PROJECTS == $p_project_id ) {
4c79b5
			$t_topprojects = $t_project_ids = user_get_accessible_projects( $p_user_id );
4c79b5
			foreach ( $t_topprojects as $t_project ) {
4c79b5
				$t_project_ids = array_merge( $t_project_ids, user_get_all_accessible_subprojects( $p_user_id, $t_project ) );
4c79b5
			}
4c79b5
4c79b5
			$t_project_ids = array_unique( $t_project_ids );
4c79b5
		} else {
4c79b5
			access_ensure_project_level( VIEWER, $p_project_id );
4c79b5
			$t_project_ids = user_get_all_accessible_subprojects( $p_user_id, $p_project_id );
4c79b5
			array_unshift( $t_project_ids, $p_project_id );
4c79b5
		}
4c79b5
4c79b5
		$t_project_ids = array_map( 'db_prepare_int', $t_project_ids );
4c79b5
4c79b5
		if ( 0 == count( $t_project_ids ) ) {
4c79b5
			$t_project_filter = ' 1<>1';
4c79b5
		} elseif ( 1 == count( $t_project_ids ) ) {
4c79b5
			$t_project_filter = ' project_id=' . $t_project_ids[0];
4c79b5
		} else {
4c79b5
			$t_project_filter = ' project_id IN (' . join( ',', $t_project_ids ) . ')';
4c79b5
		}
4c79b5
4c79b5
		return $t_project_filter;
4c79b5
	}
4c79b5
	
4c79b5
	# --------------------
4c79b5
	function helper_get_columns_to_view( $p_columns_target = COLUMNS_TARGET_VIEW_PAGE ) {
4c79b5
		$t_columns = helper_call_custom_function( 'get_columns_to_view', array( $p_columns_target ) );
4c79b5
4c79b5
		$t_enable_sponsorship = config_get( 'enable_sponsorship' );
4c79b5
		if ( OFF == $t_enable_sponsorship ) {
4c79b5
			$t_keys = array_keys( $t_columns, 'sponsorship_total' );
4c79b5
			foreach ( $t_keys as $t_key ) {
4c79b5
				unset( $t_columns[$t_key] );
4c79b5
			}
4c79b5
		}
4c79b5
	
4c79b5
		$t_show_attachments = config_get( 'show_attachment_indicator' );
4c79b5
		if ( OFF == $t_show_attachments ) {
4c79b5
			$t_keys = array_keys( $t_columns, 'attachment' );
4c79b5
			foreach ( $t_keys as $t_key ) {
4c79b5
				unset( $t_columns[$t_key] );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		if ( OFF == config_get( 'enable_relationship' ) ) {
4c79b5
			$t_keys = array_keys( $t_columns, 'duplicate_id' );
4c79b5
			foreach ( $t_keys as $t_key ) {
4c79b5
				unset( $t_columns[$t_key] );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$t_current_project_id = helper_get_current_project();
4c79b5
		if ( $t_current_project_id != ALL_PROJECTS && !access_has_project_level( config_get( 'roadmap_view_threshold' ), $t_current_project_id ) ) {
4c79b5
			$t_keys = array_keys( $t_columns, 'target_version' );
4c79b5
			foreach ( $t_keys as $t_key ) {
4c79b5
				unset( $t_columns[$t_key] );
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		# get the array values to remove gaps in the array which causes issue
4c79b5
		# if the array is accessed using an index.
4c79b5
		return array_values( $t_columns );
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# if all projects selected, default to <prefix><username><suffix><extension>, otherwise default to
4c79b5
	# <prefix><projectname><suffix><extension>.
4c79b5
	function helper_get_default_export_filename( $p_extension_with_dot, $p_prefix = '', $p_suffix = '' ) {
4c79b5
		$t_filename = $p_prefix;
4c79b5
	
4c79b5
		$t_current_project_id = helper_get_current_project();
4c79b5
4c79b5
		if ( ALL_PROJECTS == $t_current_project_id ) {
4c79b5
			$t_filename .= user_get_name( auth_get_current_user_id() );
4c79b5
		} else {
4c79b5
			$t_filename .= project_get_field( $t_current_project_id, 'name' );
4c79b5
		}
4c79b5
4c79b5
		return $t_filename . $p_suffix . $p_extension_with_dot;
4c79b5
	}
4c79b5
	
4c79b5
	# --------------------
4c79b5
	# returns a tab index value and increments it by one.  This is used to give sequential tab index on
4c79b5
	# a form.
4c79b5
	function helper_get_tab_index_value() {
4c79b5
		static $tab_index = 0;
4c79b5
		return ++$tab_index;
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# returns a tab index and increments internal state by 1.  This is used to give sequential tab index on
4c79b5
	# a form.  For example, this function returns: tabindex="1"
4c79b5
	function helper_get_tab_index() {
4c79b5
		return 'tabindex="' . helper_get_tab_index_value() . '"';
4c79b5
	}
4c79b5
4c79b5
	# --------------------
4c79b5
	# returns a boolean indicating whether SQL queries executed should be shown
4c79b5
	# or not.
4c79b5
	function helper_show_queries() {
4c79b5
		# Check is authenticated before checking access level, otherwise user gets 
4c79b5
		# redirected to login_page.php.  See #8461.
4c79b5
		return	ON == config_get( 'show_queries_count' ) && 
4c79b5
				auth_is_user_authenticated() &&
4c79b5
				access_has_global_level( config_get( 'show_queries_threshold' ) );
4c79b5
	}
4c79b5
?>