|
This is the solution:
function BL_Config_getParentProjectIds($projectId, $arReturn=array()){
$parent_projectId = project_hierarchy_get_parent($projectId, $arReturn);
if($parent_projectId > 0){
$arReturn[] = $parent_projectId;
$arReturn = BL_Config_getParentProjectIds($parent_projectId, $arReturn);
}
return $arReturn;
}
function config_get( $p_option, $p_default = null, $p_user = null, $p_project = null ) {
global $g_cache_config, $g_cache_config_access, $g_cache_db_table_exists, $g_cache_filled;
global $g_cache_config_user, $g_cache_config_project, $g_project_override;
# @@ debug @@ echo "lu o=$p_option ";
# bypass table lookup for certain options
$t_bypass_lookup = !config_can_set_in_database( $p_option );
# @@ debug @@ if ($t_bypass_lookup) { echo "bp=$p_option match=$t_match_pattern <br />"; }
if( !$t_bypass_lookup ) {
if( $g_project_override !== null && $p_project === null ) {
$p_project = $g_project_override;
}
# @@ debug @@ if ( ! db_is_connected() ) { echo "no db "; }
# @@ debug @@ echo "lu table=" . ( db_table_exists( $t_config_table ) ? "yes " : "no " );
if( !$g_cache_db_table_exists ) {
$t_config_table = db_get_table( 'mantis_config_table' );
$g_cache_db_table_exists = ( TRUE === db_is_connected() ) && db_table_exists( $t_config_table );
}
if( $g_cache_db_table_exists ) {
# @@ debug @@ echo " lu db $p_option ";
# @@ debug @@ error_print_stack_trace();
# prepare the user's list
$t_users = array();
if( null === $p_user ) {
if( !isset( $g_cache_config_user ) ) {
$t_users[] = auth_is_user_authenticated() ? auth_get_current_user_id() : ALL_USERS;
if( !in_array( ALL_USERS, $t_users ) ) {
$t_users[] = ALL_USERS;
}
$g_cache_config_user = $t_users;
} else {
$t_users = $g_cache_config_user;
}
} else {
$t_users[] = $p_user;
if( !in_array( ALL_USERS, $t_users ) ) {
$t_users[] = ALL_USERS;
}
}
# prepare the projects list
$t_projects = array();
if(( null === $p_project ) ) {
if( !isset( $g_cache_config_project ) ) {
$t_projects[] = auth_is_user_authenticated() ? helper_get_current_project() : ALL_PROJECTS;
$t_projects = array_merge($t_projects, BL_Config_getParentProjectIds($t_projects[0]));
if( !in_array( ALL_PROJECTS, $t_projects ) ) {
$t_projects[] = ALL_PROJECTS;
}
$g_cache_config_project = $t_projects;
} else {
$t_projects = $g_cache_config_project;
}
} else {
$t_projects[] = $p_project;
$t_projects = array_merge($t_projects, BL_Config_getParentProjectIds($p_project));
if( !in_array( ALL_PROJECTS, $t_projects ) ) {
$t_projects[] = ALL_PROJECTS;
}
}
# @@ debug @@ echo 'pr= '; var_dump($t_projects);
# @@ debug @@ echo 'u= '; var_dump($t_users);
if( !$g_cache_filled ) {
$t_config_table = db_get_table( 'mantis_config_table' );
$query = "SELECT config_id, user_id, project_id, type, value, access_reqd FROM $t_config_table";
$result = db_query_bound( $query );
while( false <> ( $row = db_fetch_array( $result ) ) ) {
$t_config = $row['config_id'];
$t_user = $row['user_id'];
$t_project = $row['project_id'];
$g_cache_config[$t_config][$t_user][$t_project] = $row['type'] . ';' . $row['value'];
$g_cache_config_access[$t_config][$t_user][$t_project] = $row['access_reqd'];
}
$g_cache_filled = true;
}
if( isset( $g_cache_config[$p_option] ) ) {
$t_found = false;
reset( $t_users );
while(( list(, $t_user ) = each( $t_users ) ) && !$t_found ) {
reset( $t_projects );
while(( list(, $t_project ) = each( $t_projects ) ) && !$t_found ) {
if( isset( $g_cache_config[$p_option][$t_user][$t_project] ) ) {
$t_value = $g_cache_config[$p_option][$t_user][$t_project];
$t_found = true;
# @@ debug @@ echo "clu found u=$t_user, p=$t_project, v=$t_value ";
}
}
}
if( $t_found ) {
list( $t_type, $t_raw_value ) = explode( ';', $t_value, 2 );
switch( $t_type ) {
case CONFIG_TYPE_FLOAT:
$t_value = (float) $t_raw_value;
break;
case CONFIG_TYPE_INT:
$t_value = (int) $t_raw_value;
break;
case CONFIG_TYPE_COMPLEX:
$t_value = unserialize( $t_raw_value );
break;
case CONFIG_TYPE_STRING:
default:
$t_value = config_eval( $t_raw_value );
}
return $t_value;
}
}
}
}
return config_get_global( $p_option, $p_default );
} |