Skip to content

Commit

Permalink
Fixes #9946: Refactor enumeration handling and add unit tests for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
vboctor committed Dec 12, 2008
1 parent 4c9785c commit d882929
Show file tree
Hide file tree
Showing 27 changed files with 543 additions and 323 deletions.
14 changes: 7 additions & 7 deletions adm_permissions_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@
print_manage_config_menu( 'adm_permissions_report.php' );

function get_section_begin( $p_section_name ) {
$t_access_levels = explode_enum_string( config_get( 'access_levels_enum_string' ) );
$t_access_levels = MantisEnum::getValues( config_get( 'access_levels_enum_string' ) );

$t_output = '<table class="width100">';
$t_output .= '<tr><td class="form-title" colspan="' . ( count( $t_access_levels ) + 1 ) . '">' . strtoupper( $p_section_name ) . '</td></tr>' . "\n";
$t_output .= '<tr><td class="form-title" width="40%">' . lang_get( 'perm_rpt_capability' ) . '</td>';

foreach( $t_access_levels as $t_access_level ) {
$t_entry_array = explode_enum_arr( $t_access_level );
$t_output .= '<td class="form-title" style="text-align:center">&nbsp;' . get_enum_to_string( lang_get( 'access_levels_enum_string' ), $t_entry_array[0] ) . '&nbsp;</td>';
$t_output .= '<td class="form-title" style="text-align:center">&nbsp;' . MantisEnum::getLabel( lang_get( 'access_levels_enum_string' ), $t_access_level ) . '&nbsp;</td>';
}

$t_output .= '</tr>' . "\n";

return $t_output;
}

function get_capability_row( $p_caption, $p_access_level ) {
$t_access_levels = explode_enum_string( config_get( 'access_levels_enum_string' ) );
$t_access_levels = MantisEnum::getValues( config_get( 'access_levels_enum_string' ) );

$t_output = '<tr ' . helper_alternate_class() . '><td>' . string_display( $p_caption ) . '</td>';
foreach( $t_access_levels as $t_access_level ) {
$t_entry_array = explode_enum_arr( $t_access_level );

if ( (int)$t_entry_array[0] >= (int)$p_access_level ) {
if ( $t_access_level >= (int)$p_access_level ) {
$t_value = '<img src="images/ok.gif" width="20" height="15" alt="X" title="X" />';
} else {
$t_value = '&nbsp;';
Expand Down
19 changes: 4 additions & 15 deletions api/soap/mc_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,10 @@ function mci_get_mantis_path() {

# Given a enum string and num, return the appropriate localized string
function mci_get_enum_element( $p_enum_name, $p_val, $p_lang ) {
$config_var = config_get( $p_enum_name . '_enum_string' );
$string_var = lang_get( $p_enum_name . '_enum_string', $p_lang );

# use the global enum string to search
$t_arr = explode_enum_string( $config_var );
$t_arr_count = count( $t_arr );
for( $i = 0;$i < $t_arr_count;$i++ ) {
$elem_arr = explode_enum_arr( $t_arr[$i] );
if( $elem_arr[0] == $p_val ) {

# now get the appropriate translation
return get_enum_to_string( $string_var, $p_val );
}
}
return '@' . $p_val . '@';
$t_enum_string = config_get( $p_enum_name . '_enum_string' );
$t_localized_enum_string = lang_get( $p_enum_name . '_enum_string', $p_lang );

return MantisEnum::getLocalizedLabel( $t_enum_string, $t_localized_enum_string, $p_val );
}

# Gets the sub-projects that are accessible to the specified user / project.
Expand Down
26 changes: 13 additions & 13 deletions api/soap/mc_enum_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,18 @@ function mci_explode_to_objectref( $p_config_enum_string ) {
if( get_class( $p_config_enum_string ) == 'soap_fault' ) {
return $p_config_enum_string;
}
foreach( explode_enum_string( $p_config_enum_string ) as $t_enum_element ) {
list( $t_id, $t_name ) = explode_enum_arr( $t_enum_element );

$t_result = array();

$t_assoc_array = MantisEnum::getAssocArrayIndexedByValues( $p_config_enum_string );

foreach ( $t_assoc_array as $t_id => $t_name ) {
$t_result[] = array(
'id' => $t_id,
'name' => $t_name,
);
};
}

return $t_result;
}

Expand Down Expand Up @@ -203,17 +208,12 @@ function mci_enum_get_array_by_id( $p_enum_id, $p_enum_type, $p_lang ) {
* @return The id corresponding to the given label, or 0 if not found.
*/
function mci_get_enum_value_from_label( $p_enum_string, $p_label ) {
$t_arr = explode_enum_string( $p_enum_string );
$enum_count = count( $t_arr );

for( $i = 0;$i < $enum_count;$i++ ) {
$t_s = explode_enum_arr( $t_arr[$i] );
if( $t_s[1] == $p_label ) {
return $t_s[0];
}
$t_value = MantisEnum::getValue( $p_enum_string, $p_label );
if ( $t_value === false ) {
return 0;
}

return 0;
return $t_value;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion bug_change_status_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
}
}

$t_status_label = str_replace( " ", "_", get_enum_to_string( config_get( 'status_enum_string' ), $f_new_status ) );
$t_status_label = str_replace( " ", "_", MantisEnum::getLabel( config_get( 'status_enum_string' ), $f_new_status ) );
$t_resolved = config_get( 'bug_resolved_status_threshold' );

$t_bug = bug_get( $f_bug_id );
Expand Down
4 changes: 2 additions & 2 deletions bug_graph_bystatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
$t_start = $t_interval->get_start_timestamp();

// grab all status levels
$t_status_arr = get_enum_to_array( config_get( 'status_enum_string' ) );
$t_status_labels = get_enum_to_array( lang_get( 'status_enum_string' ) );
$t_status_arr = MantisEnum::getAssocArrayIndexedByValues( config_get( 'status_enum_string' ) );
$t_status_labels = MantisEnum::getAssocArrayIndexedByValues( lang_get( 'status_enum_string' ) );

$t_bug = array();
$t_view_status = array();
Expand Down
14 changes: 13 additions & 1 deletion core.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,20 @@ function microtime_float() {
$t_core_path = dirname(__FILE__).DIRECTORY_SEPARATOR.'core'.DIRECTORY_SEPARATOR;
if (isset($GLOBALS['g_core_path']) && !isset( $HTTP_GET_VARS['g_core_path'] ) && !isset( $HTTP_POST_VARS['g_core_path'] ) && !isset( $HTTP_COOKIE_VARS['g_core_path'] ) ) {
$t_core_path = $g_core_path;
}
}

$g_core_path = $t_core_path;

# Define an autoload function to automatically load classes when referenced.
function __autoload( $className ) {
global $g_core_path;

$t_require_path = $g_core_path . 'classes' . DIRECTORY_SEPARATOR . $className . '.class.php';

if ( file_exists( $t_require_path ) ) {
require_once( $t_require_path );
}
}

if ( ($t_output = ob_get_contents()) != '') {
echo 'Possible Whitespace/Error in Configuration File - Aborting. Output so far follows:<br />';
Expand Down
26 changes: 6 additions & 20 deletions core/bug_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,30 +449,16 @@ function bug_check_workflow( $p_bug_status, $p_wanted_status ) {
# workflow not defined, use default enum
return true;
}
elseif( $p_bug_status == $p_wanted_status ) {

if ( $p_bug_status == $p_wanted_status ) {
# no change in state, allow the transition
return true;
} else {
# workflow defined - find allowed states
$t_allowed_states = $t_status_enum_workflow[$p_bug_status];
$t_arr = explode_enum_string( $t_allowed_states );

$t_enum_count = count( $t_arr );

for( $i = 0;$i < $t_enum_count;$i++ ) {

# check if wanted status is allowed
$t_elem = explode_enum_arr( $t_arr[$i] );
if( $p_wanted_status == $t_elem[0] ) {
return true;
}
}

# end for
}

return false;
# workflow defined - find allowed states
$t_allowed_states = $t_status_enum_workflow[$p_bug_status];

return MantisEnum::hasValue( $t_allowed_states, $p_wanted_status );
}

# ===================================
Expand Down Expand Up @@ -1137,7 +1123,7 @@ function bug_update( $p_bug_id, $p_bug_data, $p_update_extended = false, $p_bypa

# status changed
if( $t_old_data->status != $p_bug_data->status ) {
$t_status = get_enum_to_string( config_get( 'status_enum_string' ), $p_bug_data->status );
$t_status = MantisEnum::getLabel( config_get( 'status_enum_string' ), $p_bug_data->status );
$t_status = str_replace( ' ', '_', $t_status );
email_generic( $p_bug_id, $t_status, $t_status_prefix . $t_status );
return true;
Expand Down
Loading

0 comments on commit d882929

Please sign in to comment.