<?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;
//$title=get('title');
//if(!$title)
$title=$mm2;
if(!$title)
$title='Report';
echo '<a name="top" ></a>';
echo '<h2>';
myecho($title);
echo '</h2>';
echo '<a class="noprint" style="margin-left:10px;" href="#" onclick="document.getElementById(\'reportdef\').style.display=\'block\'; this.style.display=\'none\';">';
myecho('Show definition');
echo '</a>';
//echo '<a class="noprint" style="margin-left:10px;" href="#chart">';
// myecho('Show chart');
//echo '</a>';
echo('<form action="index.php" method="get">');
echo('<div id="reportdef" style="display:none;"><input name="odb_script" type="hidden" value="report"></input>');
echo('<input name="odb_search" type="hidden" value="'.$search.'"></input>');
$key2=get('k2');
$key3=get('k3');
$key4=get('k4');
echo '<div class="supergroup">';
echo '<table class="noprint">';
echo '<tr>';
//-----------------------------------------------------
echo '<th>';
echo '</th>';
echo '<th>';
myecho('Keys');
echo '</th>';
echo '<th>';
echo '</th>';
for($ii=1;$ii<REPORT_COLUMN_COUNT;$ii++)
{
echo '<th>';
myecho('Column %1',$ii);
echo '</th>';
}
echo '<th>';
echo '</th>';
echo '</tr>';
echo '<tr>';
//-----------------------------------------------------
echo '<th>';
echo '1';
echo '</th>';
echo '<td>';
$show_name='k2';
$show_value=$key2;
$variable_name='ALLWORDS';
echo('<input class="fld_" name="'.$show_name.'" id="'.$show_name.'" type="text" title="'.$show_value.'" onkeydown1="new AutoSuggestControl(this, \''.$variable_name.'\', new RemoteStateSuggestions());" value="'.$show_value.'"/>');
echo '</td>';
echo '<th>';
myecho('Name');
echo '</th>';
for($ii=1;$ii<REPORT_COLUMN_COUNT;$ii++)
{
$key1name=get('n'.$ii);
echo '<td>';
echo '<input class="fld_" name="n'.$ii.'" value="'.$key1name.'" />';
echo '</td>';
}
echo '<th>';
echo '</th>';
echo '</tr>';
echo '<tr>';
//-----------------------------------------------------
echo '<th>';
echo '2';
echo '</th>';
echo '<td>';
$show_name='k3';
$show_value=$key3;
$variable_name='ALLWORDS';
echo('<input class="fld_" name="'.$show_name.'" id="'.$show_name.'" type="text" title="'.$show_value.'" onkeydown1="new AutoSuggestControl(this, \''.$variable_name.'\', new RemoteStateSuggestions());" value="'.$show_value.'"/>');
echo '</td>';
echo '<th>';
myecho('Filter');
echo '</th>';
for($ii=1;$ii<REPORT_COLUMN_COUNT;$ii++)
{
$key1filter=get('e'.$ii);
echo '<td>';
echo '<input class="fld_" name="e'.$ii.'" value="'.$key1filter.'" />';
echo '</td>';
}
echo '<th>';
echo '</th>';
echo '</tr>';
echo '<tr>';
//-----------------------------------------------------
echo '<th>';
echo '3';
echo '</th>';
echo '<td>';
$show_name='k4';
$show_value=$key4;
$variable_name='ALLWORDS';
echo('<input class="fld_" name="'.$show_name.'" id="'.$show_name.'" type="text" title="'.$show_value.'" onkeydown1="new AutoSuggestControl(this, \''.$variable_name.'\', new RemoteStateSuggestions());" value="'.$show_value.'"/>');
echo '</td>';
echo '<th>';
myecho('Formula');
echo '</th>';
for($ii=1;$ii<REPORT_COLUMN_COUNT;$ii++)
{
$key1var=get('v'.$ii);
echo '<td>';
echo '<input class="fld_" name="v'.$ii.'" value="'.$key1var.'" />';
echo '</td>';
}
echo '<td>';
echo('<input type="submit" class="submit" value="'.myecho('Report','RETURN').'" />');
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<a class="noprint" style="margin-left:10px;" href="index.php?odb_sys=structure" target="_blank">';
myecho('Show all available fields');
echo '</a>';
echo '</div>';
echo('</div></form>');
flush();
//-------------------------------------------------------------------------------------------------------------
function stat_add($column_name,$filter,$condition=1,$add=1,$avg=0,$nototal=0)
{
global $agregation_array;
global $val1_array;
global $line;
global $val2;
global $val3;
global $val4;
global $avg_count_array;
$val1=$column_name;
if(($add!=0 or $avg) and $condition)
{
$filtered=1;
$word_array=index_explode($filter);
$lc_line=$line;
reset($word_array);
while(list($key,$tmp)=each($word_array))
{
$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)
break;
}
if($filtered)//strlen($filter)==0 or strpos('_'.$line,$filter))
{
$val1_array[$val1]=$filter;
$agregation_array[$val2][$val3][$val4][$val1]+=$add;
if(!$nototal)
$agregation_array['TOTAL']['TOTAL']['TOTAL'][$val1]+=$add;
if($avg)
{
$avg_count_array[$val2][$val3][$val4][$val1]+=1;
if(!$nototal)
$avg_count_array['TOTAL']['TOTAL']['TOTAL'][$val1]+=1;
}
}
}
}
$agregation_array=array();
$val1_array=array();
$val2_array=array();
$val3_array=array();
$val4_array=array();
$field_type_array=array();
$avg_count_array=array();
$compare_check='';
//$record_array=search($search.' '.$key2.' '.$key3.' '.$key4);
$record_array=$show_result_array;
$fld_name_array=search_dictionary('systemplate');
for($kk=1;$kk<REPORT_COLUMN_COUNT;$kk++)
{
$key1name=get('n'.$kk);
$key1filter=get('e'.$kk);
$key1formula=get('v'.$kk);
if(strpos('_'.$key1formula,'avg')>0)
$field_type_array[substr('0'.$kk,-2).' '.$key1name]='money';
elseif(strpos('_'.$key1formula,'sum')>0)
$field_type_array[substr('0'.$kk,-2).' '.$key1name]='money';
else
$field_type_array[substr('0'.$kk,-2).' '.$key1name]='number';
foreach($fld_name_array as $fld_name)
{
if(strpos('_'.$key1formula,$fld_name)>0)
$field_type_array[substr('0'.$kk,-2).' '.$key1name]=$fld_name;
}
}
//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]++;
for($kk=1;$kk<REPORT_COLUMN_COUNT;$kk++)
{
$key1name=get('n'.$kk);
$key1filter=get('e'.$kk);
$key1formula=get('v'.$kk);
if($key1name=='')
break;
if($key1filter=='DUBLETS')
{
$key1filter='';
$compare_check=substr('0'.$kk,-2).' '.$key1name;
}
if(strpos('_'.$key1formula,'avg')>0)
$key1formulaval=0+str_replace(',','.',read($line,$key1name));
elseif(strpos('_'.$key1formula,'sum')>0)
$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(strpos('_'.$key1formula,'YM')>0) //date agregation
{
$vv=read($line,$variable);
$vv=substr($vv,0,6);
$key1filter.=' ['.$variable.'='.$vv.']';
}
if(strlen($vv))
{
//echo 'VAR:'.$variable.'<br/>';
$value=str_replace('['.$variable.']',$vv,$value);
$key1filter=str_replace('['.$variable.']',$vv,$key1filter);
$pos=strpos('_'.$value,'[');
$replacements++;
if(strpos('_'.$key1formula,'YM')>0)
$key1filter.=' ['.$variable.'='.$vv.']';
elseif(strpos($variable.'='.$vv,' ')>0)
$key1filter.=' ['.$variable.'='.$vv.'$]';
else
$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(strpos('_'.$key1formula,'YM')>0)
$key1filter.=' -['.$variable.'='.$vv.']';
elseif(strpos($variable,' ')>0)
$key1filter.=' -['.$variable.'$]';
else
$key1filter.=' -'.$variable.'$';
}
}
if($replacements==0)
$value='';
$key1name=$value;
}
if($key1name)
{
if(strpos('_'.$key1formula,'nototal')>0)
$nototal=1;
else
$nototal=0;
if(strpos('_'.$key1formula,'avg')>0)
stat_add(substr('0'.$kk,-2).' '.$key1name,$key1filter,1,$key1formulaval,1,$nototal);
else
stat_add(substr('0'.$kk,-2).' '.$key1name,$key1filter,1,$key1formulaval,0,$nototal);
}
}
}
}
//showtime('loop');
//choosing only one column to show
$chart_link=REPORTLINK;
$tmp_chart_link='';
for($ii=1;$ii<REPORT_COLUMN_COUNT;$ii++)
{
if(strlen(trim(get('n'.$ii))))
{
$tmp_chart_link='';
$tmp_chart_link.='&n'.$ii.'='.urlencode(get('n'.$ii));
$tmp_chart_link.='&e'.$ii.'='.urlencode(get('e'.$ii));
$tmp_chart_link.='&v'.$ii.'='.urlencode(get('v'.$ii));
}
if(strlen(get('sort1')) and get('n'.$ii)==substr(get('sort1'),3))
{
$tmp_chart_link='';
$tmp_chart_link.='&n'.$ii.'='.urlencode(get('n'.$ii));
$tmp_chart_link.='&e'.$ii.'='.urlencode(get('e'.$ii));
$tmp_chart_link.='&v'.$ii.'='.urlencode(get('v'.$ii));
break;
}
}
$chart_link.=$tmp_chart_link;
//$chart_link.=$tmp_chart_link.'&odb_search='.urlencode($search).'+Week=4';
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))
{
$color_array=array('#ff0000','#00ff00','#0000ff','#ff00ff','#00ffff','#cc0000','#00cc00','#dddddd','#ffff00','#0000cc');
$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);
$rowsshown=0;
$cnt=0;
echo '<div class="supergroup">';
echo '<table>';
echo '<tr>';
echo '<th>';
echo '</th>';
if($key2)
{
echo '<th>';
if($key2==$sort_column)
if($sort_desc)
echo '<a href="index.php?odb_sort1='.urlencode($sort_column).'&'.REPORTLINK.'"><img style="margin:3px;" alt="Sorted a-z" src="images/down.gif" />';
else
echo '<a href="index.php?'.REPORTLINK.'"><img style="margin:3px;" alt="Sorted z-a" src="images/up.gif" />';
else
echo '<a href="index.php?odb_sort2=1&odb_sort1='.urlencode($key2).'&'.REPORTLINK.'">';
myecho($key2);
echo '</a>';
echo '</th>';
}
if($key3)
{
echo '<th>';
if($key3==$sort_column)
if($sort_desc)
echo '<a href="index.php?odb_sort1='.urlencode($sort_column).'&'.REPORTLINK.'"><img style="margin:3px;" alt="Sorted a-z" src="images/down.gif" />';
else
echo '<a href="index.php?'.REPORTLINK.'"><img style="margin:3px;" alt="Sorted z-a" src="images/up.gif" />';
else
echo '<a href="index.php?odb_sort2=1&odb_sort1='.urlencode($key3).'&'.REPORTLINK.'">';
myecho($key3);
echo '</a>';
echo '</th>';
}
if($key4)
{
echo '<th>';
if($key4==$sort_column)
if($sort_desc)
echo '<a href="index.php?odb_sort1='.urlencode($sort_column).'&'.REPORTLINK.'"><img style="margin:3px;" alt="Sorted a-z" src="images/down.gif" />';
else
echo '<a href="index.php?'.REPORTLINK.'"><img style="margin:3px;" alt="Sorted z-a" src="images/up.gif" />';
else
echo '<a href="index.php?odb_sort2=1&odb_sort1='.urlencode($key4).'&'.REPORTLINK.'">';
myecho($key4);
echo '</a>';
echo '</th>';
}
reset($val1_array);
while(list($val1,$tmp)=each($val1_array))
{
echo '<th>';
if($val1==$sort_column)
if(!$sort_desc)
echo '<a href="index.php?odb_sort2=1&odb_sort1='.urlencode($sort_column).'&'.REPORTLINK.'"><img style="margin:3px;" alt="Sorted a-z" src="images/down.gif" />';
else
echo '<a href="index.php?'.REPORTLINK.'"><img style="margin:3px;" alt="Sorted z-a" src="images/up.gif" />';
else
echo '<a href="index.php?odb_sort1='.urlencode($val1).'&'.REPORTLINK.'">';
myecho(substr($val1,3));
echo '</a>';
echo '</th>';
}
echo '</tr>';
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++;
if(strpos('_'.$val2.$val3.$val4,'TOTAL'))
echo '<tr>';
else
echo '<tr class="record'.(($cnt-1)%2).'">';
echo '<th>';
echo $cnt.'.';
echo '</th>';
if($key2)
{
echo '<td><div class="fld_">';
if(strlen($val2)==8 and $val2>19900101 and $val2<30000101)
echo substr($val2,6,2).'.'.substr($val2,4,2).'.'.substr($val2,0,4);
else
echo str_replace(' ',' ',$val2);
echo '</div></td>';
}
if($key3)
{
echo '<td><div class="fld_">';
if(strlen($val3)==8 and $val3>19900101 and $val3<30000101)
echo substr($val3,6,2).'.'.substr($val3,4,2).'.'.substr($val3,0,4);
else
echo str_replace(' ',' ',$val3);
echo '</div></td>';
}
if($key4)
{
echo '<td><div class="fld_">';
if(strlen($val4)==8 and $val4>19900101 and $val4<30000101)
echo substr($val4,6,2).'.'.substr($val4,4,2).'.'.substr($val4,0,4);
else
echo str_replace(' ',' ',$val4);
echo '</div></td>';
}
reset($val1_array);
while(list($val1,$tmp)=each($val1_array))
{
echo '<td>';
$drill_search=$search;
if(strlen($key2))
if($val2=='TOTAL')
$drill_search.=' ['.$key2.']';
elseif(strpos('_'.$key2,'['))
$drill_search.=' '.$filter_array[$key2.'***'.$val2];
elseif(strlen($val2))
$drill_search.=' [$'.$key2.'='.$val2.'$]';
elseif(strlen($key2))
$drill_search.=' -[$'.$key2.'=]';
if(strlen($key3))
if($val3=='TOTAL')
$drill_search.=' ['.$key3.']';
elseif(strpos('_'.$key3,'['))
$drill_search.=' '.$filter_array[$key3.'***'.$val3];
elseif(strlen($val3))
$drill_search.=' [$'.$key3.'='.$val3.'$]';
elseif(strlen($key3))
$drill_search.=' -[$'.$key3.'=]';
if(strlen($key4))
if($val4=='TOTAL')
$drill_search.=' ['.$key4.']';
elseif(strpos('_'.$key4,'['))
$drill_search.=' '.$filter_array[$key4.'***'.$val4];
elseif(strlen($val4))
$drill_search.=' [$'.$key4.'='.$val4.'$]';
elseif(strlen($key4))
$drill_search.=' -[$'.$key4.'=]';
if(strlen($val1_array[$val1])) $drill_search.=' '.$val1_array[$val1].'';
$drill_search=trim($drill_search);
$drill_search=urlencode($drill_search);
if($val2!='TOTAL' and $val3!='TOTAL' and $val4!='TOTAL')
if($val1==$compare_check)
echo '<a href="index.php?odb_compare=all&odb_search='.$drill_search.'">';
else
echo '<a href="index.php?odb_search='.$drill_search.'">';
if(1)
{
$show_mode=CFG_SHOW;
$show_value=$agregation_array[$val2][$val3][$val4][$val1];
if(isset($avg_count_array[$val2][$val3][$val4][$val1]))
$show_value=$agregation_array[$val2][$val3][$val4][$val1]/$avg_count_array[$val2][$val3][$val4][$val1];
$template=$field_type_array[$val1];
//$template='number';
if(file_exists(cfg_data_path.'/config/script/fld_'.$template.'.txt'))
{
include(cfg_data_path.'/config/script/fld_'.$template.'.txt');
}
else
{
if(!file_exists(cfg_data_path.'/script/fld_'.$template.'.txt'))
$template='';
include(cfg_data_path.'/script/fld_'.$template.'.txt');
}
//echo $field_type_array[$val1];
}
if($val2!='TOTAL' and $val3!='TOTAL' and $val4!='TOTAL')
echo '</a>';
//echo $val1.$val2.$val3.$val4;
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
echo '</div>';
//off speed-up
/*
echo '<a class="noprint" style="margin-left:10px;" href="#top">';
myecho('Show report');
echo '</a>';
echo '<a name="chart"/><div style="margin:10px 20px;"><a href="index.php?odb_sort1='.get('sort1').'&odb_sort2='.get('sort2').'&'.$chart_link.'&odb_sys=chart&odb_size=big"><img style="border:#888 1px solid;" alt="Chart" src="index.php?odb_sort1='.get('sort1').'&odb_sort2='.get('sort2').'&'.$chart_link.'&odb_sys=chart" /></a><br />';
$tmp_color_ii=0;
myecho('Legend:');
$val1=get('sort1');
if(substr($val1,1,2)!=' ')
{
reset($val1_array);
while(list($tmp_val1,$tmp)=each($val1_array)) //only one column to be shown
{
if($tmp_val1)
$val1=$tmp_val1;
}
}
//reset($val1_array);
//while(list($val1,$tmp)=each($val1_array)) //only one column to be shown
{
echo ' <span style="background-color:'.$color_array[$tmp_color_ii].'; border:#000 1px solid;"> </span> ';
myecho(substr($val1,3));
$tmp_color_ii++;
}
echo '</div>';
*/
}
else
{
echo '<br /><h2>';
myecho('No data found');
echo '</h2><br /><br /><br />';
}
?>