Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

do_report()

Description

do_report - Runs the specified report. This is used in a number of ways:-
1) Outputs an HTML table to screen ($download = false)
2) Produces a CSV
- for direct download from team_report.php
- captured and saved as a CSV file if called by send_periodic_report_emails() and over 100 rows are returned

Parameters

ColumnTypeDefaultDescription
$ref int Report ID
$from_y mixed Start year (used for reprts with date placholders)
$from_m mixed Start month
$from_d mixed Start day
$to_y mixed End year
$to_m mixed To month
$to_d mixed To day
$download mixed true Output as CSV attachment (default)/output directly to client
$add_border mixed false Optional table border (not for download)
$foremail mixed false Sending as email?

Return

void | string | array Outputs CSV file, returns HTML table or returns an array with path to the CSV file, rows and filename

Location

include/reporting_functions.php lines 55 to 217

Definition

 
function do_report($ref,$from_y,$from_m,$from_d,$to_y,$to_m,$to_d,$download=true,$add_border=false,$foremail=false)
    {
    
# Run report with id $ref for the date range specified. Returns a result array.
    
global $lang$baseurl$report_rows_attachment_limit;

    
$report=sql_query("select * from report where ref='$ref'");$report=$report[0];
    
$report['name'] = get_report_name($report);
    if(
$download || $foremail)
        {
        
$filename=str_replace(array(" ","(",")","-","/"),"_",$report["name"]) . "_" $from_y "_" $from_m "_" $from_d "_" $lang["to"] . "_" $to_y "_" $to_m "_" $to_d ".csv";
        }

    if(
$results hook("customreport""", array($ref,$from_y,$from_m,$from_d,$to_y,$to_m,$to_d,$download,$add_border$report)))
        {
        
// Hook has created the $results array
        
}
    else
        {
        
// Generate report results normally
        
$sql=$report["query"];
        
$sql=str_replace("[from-y]",$from_y,$sql);
        
$sql=str_replace("[from-m]",$from_m,$sql);
        
$sql=str_replace("[from-d]",$from_d,$sql);
        
$sql=str_replace("[to-y]",$to_y,$sql);
        
$sql=str_replace("[to-m]",$to_m,$sql);
        
$sql=str_replace("[to-d]",$to_d,$sql);

        global 
$view_title_field;
        
$sql=str_replace("[title_field]",$view_title_field,$sql);
        
$results=sql_query($sql);
        }
    
    
$resultcount count($results);    
    if(
$resultcount == 0)
        {
        
// No point downloading as the resultant file will be empty
        
$download=false;
        }            
    if (
$download)
        {
        
header("Content-type: application/octet-stream");
        
header("Content-disposition: attachment; filename=" $filename "");
        }

    if (
$download || ($foremail && $resultcount $report_rows_attachment_limit))
        {
        if(
$foremail)
            {
            
ob_clean();
            
ob_start();
            }
        for (
$n=0;$n<$resultcount;$n++)
            {
            
$result=$results[$n];
            if (
$n==0)
                {
                
$f=0;
                foreach (
$result as $key => $value)
                    {
                    
$f++;
                    if (
$f>1) {echo ",";}
                    if (
$key!="thumbnail")
                        {echo 
"\"" lang_or_i18n_get_translated($key,"columnheader-") . "\"";}
                    }
                echo 
"\n";
                }
            
$f=0;
            foreach (
$result as $key => $value)
                {
                
$f++;
                if (
$f>1) {echo ",";}
                
$custom hook('customreportfield''', array($result$key$value$download));
                if (
$custom !== false)
                    {
                    echo 
$custom;
                    }
                else if (
$key!="thumbnail")
                    {
                    
$value=lang_or_i18n_get_translated($value"usergroup-");
                    
$value=str_replace('"','""',$value); # escape double quotes
                    
if (substr($value,0,1)==",") {$value=substr($value,1);} # Remove comma prefix on dropdown / checkbox values 
                    
echo "\"" $value  "\"";
                        
                    }
                }
            echo 
"\n";
            }

        if(
$foremail)
            {
            
$output ob_get_contents();
            
ob_end_clean();
            
$unique_id=uniqid();
            
$reportfile get_temp_dir(false"Reports") . "/Report_" $unique_id ".csv";
            
file_put_contents($reportfile,$output);
            return array(
"file" => $reportfile,"filename" => $filename"rows" => $resultcount);
            }
        }
    else
        {
        
# Not downloading - output a table
        
$border="";
        if (
$add_border) {$border="border=\"1\"";}
        
$output="<br /><h2>" $report['name'] . "</h2><style>.InfoTable td {padding:5px;}</style><table $border class=\"InfoTable\">";
        for (
$n=0;$n<count($results);$n++)
            {
            
$result=$results[$n];
            if (
$n==0)
                {
                
$f=0;
                
$output.="<tr>\r\n";
                foreach (
$result as $key => $value)
                    {
                    
$f++;
                    if (
$key=="thumbnail")
                        {
$output.="<td><strong>Link</strong></td>\r\n";}
                    else
                        {
                        
$output.="<td><strong>" lang_or_i18n_get_translated($key,"columnheader-") . "</strong></td>\r\n";
                        }
                    }
                
$output.="</tr>\r\n";
                }
            
$f=0;
            
$output.="<tr>\r\n";
            foreach (
$result as $key => $value)
                {
                
$f++;
                if (
$key=="thumbnail")
                    {
                    
$thm_path=get_resource_path($value,true,"thm",false,"",$scramble=-1,$page=1,false);
                    if (!
file_exists($thm_path)){
                        
$resourcedata=get_resource_data($value);
                        
$thm_url$baseurl "/gfx/" get_nopreview_icon($resourcedata["resource_type"],$resourcedata["file_extension"],true);
                        }
                    else
                        {
                        
$thm_url=get_resource_path($value,false,"col",false,"",-1,1,false);
                        }
                    
$output.="<td><a href=\"" $baseurl "/?r=" $value .  "\" target=\"_blank\"><img src=\"" $thm_url "\"></a></td>\r\n";
                    }
                else
                    {
                    
$custom hook('customreportfield''', array($result$key$value$download));
                    if (
$custom !== false)
                        {
                        
$output .= $custom;
                        }
                    else
                        {
                        
$output.="<td>" strip_tags_and_attributes(lang_or_i18n_get_translated($value"usergroup-"),array("a"),array("href","target")) . "</td>\r\n";
                        }
                    }
                }
            
$output.="</tr>\r\n";
            }
        
$output.="</table>\r\n";
        if (
count($results)==0) {$output.=$lang["reportempty"];}
        return 
$output;
        }

    exit();
    }

This article was last updated 24th November 2020 20:35 Europe/London time based on the source file dated 18th November 2020 08:15 Europe/London time.