<?php
/*
OpenDataBag - Data Web Interface
Copyright (C) 2004 Nawara
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
global $filter_array;
$color_array=array('#ff0000','#00ff00','#0000ff','#ff00ff','#00ffff','#cc0000','#00cc00','#dddddd','#ffff00','#0000cc');
$color_border_array=array('#555555');
$title=get('title');
if(!$title)
$title=$mm2;
if(!$title)
$title='Report';
$key2=get('k2');
$key3=get('k3');
$key4=get('k4');
$show_result_array=search($search);
$show_result_array_sizeof=sizeof($show_result_array);
$max_access=0;
if($show_result_array_sizeof>0)
{
$group_names_array=array();
//showtime();
$sizeof=sizeof($show_result_array);
for($ii=0;$ii<$sizeof;$ii++)
{
$variable_array=$show_result_array[$ii];
if($max_access==0)
{
$access=check_access(read($variable_array,'MyBag'));
if($access>$max_access)
$max_access=$access;
}
$variable_array=$show_result_array[$ii];
$group_name=strtolower(read($variable_array,'sysGroupBy'));
if(isset($group_name_cnt_array[$group_name]))
$group_name_cnt_array[$group_name]++;
else
$group_name_cnt_array[$group_name]=1;
$group_array[$group_name][]=$variable_array;
$group_name_array[$group_name]=$group_name;
}
//showtime('loop');
}
//-------------------------------------------------------------------------------------------------------------
function stat_add($column_name,$filter,$condition=1,$add=1)
{
global $agregation_array;
global $val1_array;
global $line;
global $val2;
global $val3;
global $val4;
$val1=$column_name;
if($add!=0 and $condition)
{
$filtered=1;
$key=$filter;
$lc_line=$line;
if(strlen($filter)!=0)
{
$lt_pos=strpos($key,'<');
$gt_pos=strpos($key,'>');
$minus_pos=strpos('_'.$key,'-');
if($grep_key and $key==$grep_key)
{
//already filtered
}
elseif($lt_pos)
{
if($minus_pos==1)
{
$val=substr($key,$lt_pos+1);
$key=substr($key,1,$lt_pos-1);
//echo $key.' '.$val;
if(stristr($lc_line,''.$key) and read($lc_line,$key)<$val)
$filtered=0;
}
else
{
$val=substr($key,$lt_pos+1);
$key=substr($key,0,$lt_pos);
if(!stristr($lc_line,''.$key))
$filtered=0;
elseif(read($lc_line,$key)>=$val)
$filtered=0;
}
}
elseif($gt_pos)
{
$val=substr($key,$gt_pos+1);
$key=substr($key,0,$gt_pos);
//if(strpos('_'.$lc_line,''.$key)==0)
if(!stristr($lc_line,''.$key))
$filtered=0;
elseif(read($lc_line,''.$key)<=$val)
$filtered=0;
}
elseif($minus_pos==1)
{
$key=substr($key,1);
//if(strpos('_'.$lc_line,''.$key)>0)
if(stristr('_'.$lc_line,''.$key))
$filtered=0;
}
//elseif(strpos('_'.$lc_line,''.$key)==0)
elseif(!stristr($lc_line,''.$key))
{
$filtered=0;
}
}
if($filtered)//strlen($filter)==0 or strpos('_'.$line,$filter))
{
$val1_array[$val1]=$filter;
$agregation_array[$val2][$val3][$val4][$val1]+=$add;
//~ $agregation_array[$val2][$val3]['TOTAL'][$val1]+=$add;
//~ $agregation_array[$val2]['TOTAL']['TOTAL'][$val1]+=$add;
$agregation_array['TOTAL']['TOTAL']['TOTAL'][$val1]+=$add;
}
}
}
function stat_percent($column_name,$column_part,$column_total)
{
global $agregation_array;
global $val1_array;
global $line;
global $val2;
global $val3;
global $val4;
$val1=$column_name;
if($agregation_array[$val2][$val3][$val4][$column_total]!=0)
{
$val1_array[$val1]=1;
$agregation_array[$val2][$val3][$val4][$val1]=round(100*$agregation_array[$val2][$val3][$val4][$column_part]/$agregation_array[$val2][$val3][$val4][$column_total]).'%';;
}
}
function stat_calc($column_name,$column_formula,$column_format='%d')
{
global $agregation_array;
global $val1_array;
global $line;
global $val2;
global $val3;
global $val4;
global $variable_array;
global $fn_ret;
$val1=$column_name;
$fn_ret=eval_formula($column_formula,$variable_array);
$val1_array[$val1]=1;
if($fn_ret>0)
$agregation_array[$val2][$val3][$val4][$val1]=sprintf($column_format,$fn_ret);
}
$agregation_array=array();
$val1_array=array();
$val2_array=array();
$val3_array=array();
$val4_array=array();
//$record_array=search($search.' '.$key2.' '.$key3.' '.$key4);
$record_array=$show_result_array;
//showtime();
while(list($tmp,$line)=each($record_array))
{
{
$val2=read($line,$key2);
$val3=read($line,$key3);
$val4=read($line,$key4);
$val2_array[$val2]++;
$val3_array[$val3]++;
$val4_array[$val4]++;
$compare_check='';
for($kk=1;$kk<10;$kk++)
{
$key1name=get('n'.$kk);
$key1filter=get('e'.$kk);
$key1formula=get('v'.$kk);
if($key1filter=='DUBLETS')
{
$key1filter='';
$compare_check=$kk.' '.$key1name;
}
if($key1formula=='sum')
$key1formulaval=0+str_replace(',','.',read($line,$key1name));
else
$key1formulaval=1;
if(strpos($key1name,']'))
{
$key1filter=trim($key1filter);
$value=$key1name;
$pos=strpos('_'.$value,'[');
$replacements=0;
while($pos>0)
{
$variable=substr($value,$pos,strpos($value.']',']',$pos)-$pos);
$vv=read($line,$variable);
if(strlen($vv))
{
//echo 'VAR:'.$variable.'<br/>';
$value=str_replace('['.$variable.']',$vv,$value);
$key1filter=str_replace('['.$variable.']',$vv,$key1filter);
$pos=strpos('_'.$value,'[');
$replacements++;
$key1filter=$variable.'='.$vv;
}
else
{
$value=str_replace('['.$variable.']','',$value);
$value=trim(str_replace(' ',' ',$value));
if($pos+1<strlen('_'.$value))
$pos=strpos('_'.$value,'[',$pos+1);
else
$pos=0;
}
}
if($replacements==0)
$value='';
$key1name=$value;
}
if($key1name)
{
stat_add($kk.' '.$key1name,$key1filter,1,$key1formulaval);
}
//stat_add('50 Test Drive Duration','',1,read('Test Drive Duration Hours'));
//stat_add('60 MOI '.read('MOI'),'');
}
}
}
//showtime('loop');
$report_link='';
$report_link.='&odb_search='.urlencode($search);
$report_link.='&odb_script='.urlencode($script);
$report_link.='&odb_title='.urlencode($title);
$report_link.='&k2='.urlencode(get('k2'));
$report_link.='&k3='.urlencode(get('k3'));
$report_link.='&k4='.urlencode(get('k4'));
for($ii=1;$ii<10;$ii++)
{
$report_link.='&n'.$ii.'='.urlencode(get('n'.$ii));
$report_link.='&e'.$ii.'='.urlencode(get('e'.$ii));
$report_link.='&v'.$ii.'='.urlencode(get('v'.$ii));
}
if($compare_check)
{
$tmp_agregation_array=array();
if(count($agregation_array))
{
reset($val2_array);
while(list($val2,$tmp)=each($val2_array))
{
if(count($agregation_array[$val2]))
{
reset($agregation_array[$val2]);
while(list($val3,$tmp)=each($agregation_array[$val2]))
{
reset($agregation_array[$val2][$val3]);
while(list($val4,$tmp)=each($agregation_array[$val2][$val3]))
{
if($agregation_array[$val2][$val3][$val4][$compare_check]>1 and strpos('_'.$val2.$val3.$val4,'TOTAL')==0)
$tmp_agregation_array[$val2][$val3][$val4]=$agregation_array[$val2][$val3][$val4];
}
}
}
}
}
$agregation_array=$tmp_agregation_array;
}
if(count($agregation_array))
{
$sort_column=get('sort1');
$sort_desc=get('sort2');
@ksort($val2_array);
@ksort($val3_array);
@ksort($val4_array);
$sort_index_array=array();
$sort_dict_array=array();
$sort_ii=0;
reset($val2_array);
while(list($val2,$tmp)=each($val2_array))
{
if(count($agregation_array[$val2]))
{
reset($agregation_array[$val2]);
while(list($val3,$tmp)=each($agregation_array[$val2]))
{
reset($agregation_array[$val2][$val3]);
while(list($val4,$tmp)=each($agregation_array[$val2][$val3]))
{
if($key2 and $sort_column==$key2)
$sort_index_array[$sort_ii]=$val2;
elseif($key3 and $sort_column==$key3)
$sort_index_array[$sort_ii]=$val3;
elseif($key4 and $sort_column==$key4)
$sort_index_array[$sort_ii]=$val4;
elseif($sort_column)
$sort_index_array[$sort_ii]=$agregation_array[$val2][$val3][$val4][$sort_column];
else
$sort_index_array[$sort_ii]=$val2.$val3.$val4;
$sort_dict_array[$sort_ii][2]=$val2;
$sort_dict_array[$sort_ii][3]=$val3;
$sort_dict_array[$sort_ii][4]=$val4;
$sort_ii++;
}
}
}
}
if($sort_column and !$sort_desc)
arsort($sort_index_array);
else
asort($sort_index_array);
$sort_index_array[$sort_ii]=1;
$sort_dict_array[$sort_ii][2]='TOTAL';
$sort_dict_array[$sort_ii][3]='TOTAL';
$sort_dict_array[$sort_ii][4]='TOTAL';
@ksort($val1_array);
$cnt=0;
reset($sort_index_array);
while(list($sort_ii,$tmp)=each($sort_index_array))
{
$val2=$sort_dict_array[$sort_ii][2];
$val3=$sort_dict_array[$sort_ii][3];
$val4=$sort_dict_array[$sort_ii][4];
if(isset($agregation_array[$val2][$val3][$val4]))
{
$cnt++;
//$data_array[$cnt][0]='x';
$fld_cnt=0;
reset($val1_array);
while(list($val1,$tmp)=each($val1_array))
{
$legend_array[$fld_cnt]=$val1; //problem
$data_array[$cnt][0]=$cnt.'. '.substr(trim(strip_tags($val2.' '.$val3.' '.$val4)),0,30);
$data_array[$cnt][$fld_cnt+1]=$agregation_array[$val2][$val3][$val4][$val1];
$fld_cnt++;
}
}
}
}
//~ echo '<pre>';
//~ print_r($data_array);
//~ echo '</pre>';
//~ exit;
header("Content-type: image/png");
//---------------------------------------------------------------
// Profit Agregated Chart
include_once(cfg_data_path.'/script/phplot/phplot.php');
if(get('size')!='big')
$graph =& new PHPlot(950,430);
else
$graph =& new PHPlot(950,600);
$graph->SetTTFPath(cfg_data_path.'/script/fonts');
$graph->SetUseTTF(true);
$graph->SetFont('x_label','abadimtcondensedlight.ttf',11);
$graph->SetFont('y_label','abadimtcondensedlight.ttf',11);
$graph->SetPlotAreaPixels(70,10,880,250);
//~ $graph->SetLegend($legend_array);
//~ $graph->SetLegendPixels(10,300);
//$graph->SetTitle(utf8_decode(myecho($title,'RETURN')));
//$graph->SetXTitle('Time ['.$variable_array['Time Unit'].']');
//$graph->SetYTitle('Money ['.$variable_array['Money Unit'].']');
$graph->SetXDataLabelPos('none');
//$graph->SetYDataLabelPos('both');
//$graph->SetXTickIncrement(1);
//$graph->SetYTickIncrement(100000);
$graph->SetXGridLabelType("title");
$graph->SetXDataLabelAngle(-45);
$graph->SetDataColors($color_array);
$graph->SetDataBorderColors($color_border_array);
$graph->SetNumXTicks(30);
//$graph->DrawPlotBorder('full');
$graph->SetDataType("text-data"); //Must be called before SetDataValues
$graph->SetDataValues($data_array);
$graph->SetPlotType("bars");
$graph->SetShading(0);
//$graph->SetYScaleType("log");
//~ $graph->SetPlotType("lines ");
$graph->SetLineWidth(3);
$graph->SetIsInline(1);
$graph->DrawGraph();
exit;
//---------------------------------------------------------------
?>