How to change a report cell format based on its value

Howto


Sometimes you might want to change the display format of a value in a report based on its contents or data. For example, in the realtime queue summary report, you might want to show abandoned calls in bold/red when they reach certain limit.

This can be achieved via report designer and a little bit of programming.

Report Designer

You can define a format via report designer with the value being a php function call, something like this:

keyword: COLUMN_REALTIME_FORMAT
parameter: Abandoned
value: show_alarm_abandon

This will apply the format function defined on the value field to the field ‘Abandoned’ (specified in the parameter field), for columns on the realtime view for the queue summary display. So, the next step is to create the “show_alarm_abandon” function:

Display Function

You must create a custom function named “show_alarm_abandon” in the file /var/www/html/stats/custom.php

You will see that there are already a couple of sample functions there, you can investigate them if you want. For this example, we will create a new function that will display the value in red if its higher or equal to 10, in yellow if is higher or equal to 5, or with no special format otherwise. Here is the function code:

function show_alarm_abandon($value) {

    $formatted_value = ""

    if($value>=10) {
        $formatted_value = "<span style='color: #F00; font-weight: bold'>$value</span>";
    } else if($value>=5) {
        $formatted_value = "<span style='color: #FF0; font-weight: bold'>$value</span>";
    } else {
        $formatted_value = "$value";
    }

    return array($formatted_value,$value);
}

Notice that the format function returns an array, containing two values. The first one is the formatted value, the 2nd one is unformmated (for JSON or CSV exports).