.
/**
* Misc. Helper Functions
* @package CoreAPI
* @subpackage HelperAPI
* @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright (C) 2002 - 2012 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
/**
* alternate color function
* If no index is given, continue alternating based on the last index given
* @param int $p_index
* @param string $p_odd_color
* @param string $p_even_color
* @return string
*/
function helper_alternate_colors( $p_index, $p_odd_color, $p_even_color ) {
static $t_index = 1;
if( null !== $p_index ) {
$t_index = $p_index;
}
if( 1 == $t_index++ % 2 ) {
return $p_odd_color;
} else {
return $p_even_color;
}
}
/**
* alternate classes for table rows
* If no index is given, continue alternating based on the last index given
* @param int $p_index
* @param string $p_odd_class default: row-1
* @param string $p_even_class default: row-2
* @return string
*/
function helper_alternate_class( $p_index = null, $p_odd_class = 'row-1', $p_even_class = 'row-2' ) {
static $t_index = 1;
if( null !== $p_index ) {
$t_index = $p_index;
}
if( 1 == $t_index++ % 2 ) {
return "class=\"$p_odd_class\"";
} else {
return "class=\"$p_even_class\"";
}
}
/**
* get the color string for the given status, user and project
* @param int $p_status
* @param int|null $p_user user id, defaults to null (all users)
* @param int|null $p_project project id, defaults to null (all projects)
* @return string
*/
function get_status_color( $p_status, $p_user = null, $p_project = null ) {
$t_status_label = MantisEnum::getLabel( config_get( 'status_enum_string', null, $p_user, $p_project ), $p_status );
$t_status_colors = config_get( 'status_colors', null, $p_user, $p_project );
$t_color = '#ffffff';
if ( isset( $t_status_colors[$t_status_label] ) ) {
$t_color = $t_status_colors[$t_status_label];
}
return $t_color;
}
/**
* Given a enum string and num, return the appropriate string for the
* specified user/project
* @param string $p_enum_name
* @param int $p_val
* @param int|null $p_user user id, defaults to null (all users)
* @param int|null $p_project project id, defaults to null (all projects)
* @return string
*/
function get_enum_element( $p_enum_name, $p_val, $p_user = null, $p_project = null ) {
$config_var = config_get( $p_enum_name . '_enum_string', null, $p_user, $p_project );
$string_var = lang_get( $p_enum_name . '_enum_string' );
return MantisEnum::getLocalizedLabel( $config_var, $string_var, $p_val );
}
/**
* If $p_var is not an array and is equal to $p_val then we PRINT SELECTED.
* If $p_var is an array, then if any member is equal to $p_val we PRINT SELECTED.
* This is used when we want to know if a variable indicated a certain
* option element is selected
*
* If the second parameter is not given, the first parameter is compared
* to the boolean value true
* @param mixed $p_var
* @param mixed $p_val
* @return null
*/
function check_selected( $p_var, $p_val = true ) {
if( is_array( $p_var ) ) {
foreach( $p_var as $t_this_var ) {
# catch the case where one entry is 0 and the other is a string.
if( is_string( $t_this_var ) && is_string( $p_val ) ) {
if( $t_this_var === $p_val ) {
echo ' selected="selected" ';
return;
}
}
else if( $t_this_var == $p_val ) {
echo ' selected="selected" ';
return;
}
}
} else {
if( is_string( $p_var ) && is_string( $p_val ) ) {
if( $p_var === $p_val ) {
echo ' selected="selected" ';
return;
}
}
else if( $p_var == $p_val ) {
echo ' selected="selected" ';
return;
}
}
}
/**
* If $p_var and $p_val are equal to each other then we PRINT CHECKED
* This is used when we want to know if a variable indicated a certain
* element is checked
*
* If the second parameter is not given, the first parameter is compared
* to the boolean value true
* @param mixed $p_var
* @param mixed $p_val
* @return null
*/
function check_checked( $p_var, $p_val = true ) {
if( $p_var == $p_val ) {
echo ' checked="checked" ';
}
}
/**
* Set up PHP for a long process execution
* The script timeout is set based on the value of the long_process_timeout config option.
* $p_ignore_abort specified whether to ignore user aborts by hitting
* the Stop button (the default is not to ignore user aborts)
* @param bool $p_ignore_abort
* @return int
*/
function helper_begin_long_process( $p_ignore_abort = false ) {
$t_timeout = config_get( 'long_process_timeout' );
# silent errors or warnings reported when safe_mode is ON.
@set_time_limit( $t_timeout );
ignore_user_abort( $p_ignore_abort );
return $t_timeout;
}
# this allows pages to override the current project settings.
# This typically applies to the view bug pages where the "current"
# project as used by the filters, etc, does not match the bug being viewed.
$g_project_override = null;
$g_cache_current_project = null;
/**
* Return the current project id as stored in a cookie
* If no cookie exists, the user's default project is returned
* @return int
*/
function helper_get_current_project() {
global $g_project_override, $g_cache_current_project;
if( $g_project_override !== null ) {
return $g_project_override;
}
if( $g_cache_current_project === null ) {
$t_cookie_name = config_get( 'project_cookie' );
$t_project_id = gpc_get_cookie( $t_cookie_name, null );
if( null === $t_project_id ) {
$t_pref = user_pref_get( auth_get_current_user_id(), ALL_PROJECTS, false );
$t_project_id = $t_pref->default_project;
} else {
$t_project_id = explode( ';', $t_project_id );
$t_project_id = $t_project_id[count( $t_project_id ) - 1];
}
if( !project_exists( $t_project_id ) || ( 0 == project_get_field( $t_project_id, 'enabled' ) ) || !access_has_project_level( VIEWER, $t_project_id ) ) {
$t_project_id = ALL_PROJECTS;
}
$g_cache_current_project = (int) $t_project_id;
}
return $g_cache_current_project;
}
/**
* Return the current project id as stored in a cookie, in an Array
* If no cookie exists, the user's default project is returned
* If the current project is a subproject, the return value will include
* any parent projects
* @return array
*/
function helper_get_current_project_trace() {
$t_cookie_name = config_get( 'project_cookie' );
$t_project_id = gpc_get_cookie( $t_cookie_name, null );
if( null === $t_project_id ) {
$t_bottom = current_user_get_pref( 'default_project' );
$t_parent = $t_bottom;
$t_project_id = Array(
$t_bottom,
);
while( true ) {
$t_parent = project_hierarchy_get_parent( $t_parent );
if( 0 == $t_parent ) {
break;
}
array_unshift($t_project_id, $t_parent);
}
} else {
$t_project_id = explode( ';', $t_project_id );
$t_bottom = $t_project_id[count( $t_project_id ) - 1];
}
if( !project_exists( $t_bottom ) || ( 0 == project_get_field( $t_bottom, 'enabled' ) ) || !access_has_project_level( VIEWER, $t_bottom ) ) {
$t_project_id = Array(
ALL_PROJECTS,
);
}
return $t_project_id;
}
/**
* Set the current project id (stored in a cookie)
* @param int $p_project_id
* @return bool always true
*/
function helper_set_current_project( $p_project_id ) {
$t_project_cookie_name = config_get( 'project_cookie' );
gpc_set_cookie( $t_project_cookie_name, $p_project_id, true );
return true;
}
/**
* Clear all known user preference cookies
* @return null
*/
function helper_clear_pref_cookies() {
gpc_clear_cookie( config_get( 'project_cookie' ) );
gpc_clear_cookie( config_get( 'manage_cookie' ) );
}
/**
* Check whether the user has confirmed this action.
*
* If the user has not confirmed the action, generate a page which asks
* the user to confirm and then submits a form back to the current page
* with all the GET and POST data and an additional field called _confirmed
* to indicate that confirmation has been done.
* @param string $p_message
* @param string $p_button_label
* @return bool
* @todo improve this formatting - to only be about 50% of the screen width so that it doesn't become hard to read.
*/
function helper_ensure_confirmed( $p_message, $p_button_label ) {
if( true == gpc_get_bool( '_confirmed' ) ) {
return true;
}
html_page_top();
echo "
\n