Generic method to add icons for custom fields

On August 3, 2014, in Mantis, by terencejackson

Hi,

Normally if you have a bug in your system you want to have an effort estimate for it. In some cases it is enough to get an estimate if the effort is high, medium or low. In Mantis you can create a custom field for it. At least I do this for all my projects 😉

The values will then be displayed as a text in your view issues page, but since all people are very visual I wanted to have icons here to clearly see which efforts are high and which are low. Unfortunately there is no easy way to do this in the current version of Mantis. That’s why I hit the keys and created a small enhancement to show icons for all custom fields, if you want to show icons for them.

The result for the effort looks like this after my enhancement:

effort

 

You can download the Patch and the images directly here.

The following files are affected by the patch:

The following configuration parameter has to be added in config_defaults_inc.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * Associative Array for icons for custom fields
 * <Custom Field Name> => array ("Single Values" => "Picture"). Always add ShowText as an option. If it is set to ON the text is displayed in addition to the icon.
 * @global array $g_custom_status_icon_arr
 */
$g_custom_status_icon_arr = array (
        "Effort" => array(
                "" => "none.png",
                "Low" => "low.png",
                "Medium" => "medium.png",
                "High" => "high.png",
                "ShowText" => OFF
        )
);
/**
 * Associative Array for icons for custom fields
 * <Custom Field Name> => array ("Single Values" => "Picture"). Always add ShowText as an option. If it is set to ON the text is displayed in addition to the icon.
 * @global array $g_custom_status_icon_arr
 */
$g_custom_status_icon_arr = array (
		"Effort" => array(
				"" => "none.png",
				"Low" => "low.png",
				"Medium" => "medium.png",
				"High" => "high.png",
				"ShowText" => OFF
		)
);

If you want to add icons for a new custom field (e.g. module) you simply have to add it to the array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$g_custom_status_icon_arr = array (
        "Effort" => array(
                "" => "none.png",
                "Low" => "low.png",
                "Medium" => "medium.png",
                "High" => "high.png",
                "ShowText" => OFF
        ),
                "Effort" => array(
                "User Management" => "userManagement.png",
                "Contracts" => "contracts.png",
                "ShowText" => ON
        )
);
$g_custom_status_icon_arr = array (
		"Effort" => array(
				"" => "none.png",
				"Low" => "low.png",
				"Medium" => "medium.png",
				"High" => "high.png",
				"ShowText" => OFF
		),
                "Effort" => array(
				"User Management" => "userManagement.png",
				"Contracts" => "contracts.png",
				"ShowText" => ON
		)
);

With switching the ShowText property to ON the text is shown right to the icon.

The method print_custom_field_value has to be replaced by the following source code in custom_field_api.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function print_custom_field_value( array $p_def, $p_field_id, $p_bug_id ) {
    $f_icon_set = get_icon_set_for_field($p_def["name"]);
    $f_print_value = "";
    if($f_icon_set->icon_set_exists){
        $f_icon_path = config_get( 'icon_path' );
        $f_custom_field_value = custom_field_get_value( $p_field_id, $p_bug_id );
        $f_print_value .= '<img src="' . $f_icon_path . $f_icon_set->icon_array[$f_custom_field_value] . '" alt="" title="' . $f_custom_field_value . '" />';
    }
    if(!$f_icon_set->icon_set_exists || (array_key_exists("ShowText", $f_icon_set->icon_array) && $f_icon_set->icon_array["ShowText"] == ON)) {
        $f_print_value .= string_custom_field_value( $p_def, $p_field_id, $p_bug_id );
    }
    echo $f_print_value;
}
 
/**
* Check if an icon set is defined for the given parameter
* @param String $p_name The name of the field to look up
* @return mixed stdClass with the values icon_set_exists (True if icon set exists) and icon_array (the array with the icons)
*/
function get_icon_set_for_field($p_name){
    $f_custom_status_icon_arr = config_get("custom_status_icon_arr", array());
    $f_return = new stdClass;
    $f_return->icon_set_exists = array_key_exists($p_name, $f_custom_status_icon_arr);
    $f_return->icon_array = $f_custom_status_icon_arr[$p_name];
    return $f_return;
}
function print_custom_field_value( array $p_def, $p_field_id, $p_bug_id ) {
	$f_icon_set = get_icon_set_for_field($p_def["name"]);
	$f_print_value = "";
	if($f_icon_set->icon_set_exists){
		$f_icon_path = config_get( 'icon_path' );
		$f_custom_field_value = custom_field_get_value( $p_field_id, $p_bug_id );
		$f_print_value .= '<img src="' . $f_icon_path . $f_icon_set->icon_array[$f_custom_field_value] . '" alt="" title="' . $f_custom_field_value . '" />';
	}
	if(!$f_icon_set->icon_set_exists || (array_key_exists("ShowText", $f_icon_set->icon_array) && $f_icon_set->icon_array["ShowText"] == ON)) {
		$f_print_value .= string_custom_field_value( $p_def, $p_field_id, $p_bug_id );
	}
	echo $f_print_value;
}

/**
* Check if an icon set is defined for the given parameter
* @param String $p_name The name of the field to look up
* @return mixed stdClass with the values icon_set_exists (True if icon set exists) and icon_array (the array with the icons)
*/
function get_icon_set_for_field($p_name){
	$f_custom_status_icon_arr = config_get("custom_status_icon_arr", array());
	$f_return = new stdClass;
	$f_return->icon_set_exists = array_key_exists($p_name, $f_custom_status_icon_arr);
	$f_return->icon_array = $f_custom_status_icon_arr[$p_name];
	return $f_return;
}
Tagged with:  

Leave a Reply

Your email address will not be published. Required fields are marked *