<?php

if($_GET['ANALIZA_HISTORII_ALL']) {
echo "ANALIZA_HISTORII_ALL <form action=$PHP_SELF method=\"GET\">";
echo "<input type=hidden name=ADD value=ADD>";
echo "<input type=hidden name=ANALIZA_HISTORII_ALL value=1>";


echo "<table border=1><tr><td>";
if(!isset($_GET['F_LIMIT'])) $_GET['F_LIMIT']=50;

echo "Dodaj Lancuch dla poszukiwan okresu , (zakoncz znakiem %) - 500 rekordow</td>";
echo "<td>  <input type=text name=A_RECORD_UPDATE_DATE size=10 value=$_GET[A_RECORD_UPDATE_DATE]></td>";
echo "<td>  <input type=text name=A_RECORD_UPDATE_DATE2 size=10 value=$_GET[A_RECORD_UPDATE_DATE2]></td>";
echo "<td>  <input type=text name=A_RECORD_UPDATE_AUTHOR  size=10 value=\"".$_GET['A_RECORD_UPDATE_AUTHOR']."\"></td>";
echo "<td>  <input type=text name=A_RECORD_UPDATE_AUTHOR2  size=10 value=\"".$_GET['A_RECORD_UPDATE_AUTHOR2']."\"></td> ";
echo "<td>  <input type=text name=TABLES_LIKE  size=10 value=\"".$_GET['TABLES_LIKE']."\"></td> ";

echo "<td>  <input type=text name=F_LIMIT  size=10 value=\"".$_GET['F_LIMIT']."\"></td>";


echo "</tr>";
echo "<tr><td colspan=1><input type=submit name=ADD value=SZUKAJ></td>
<td>DATA (np. 2007-%)</td>
<td>.. lub data DATA (np. 2008-%)</td>
<td>Autor (np. %sqix%)</td>
<td>Autor2</td>
<td>TABELE</td>
</tr></table>";

/*

    
$ZAP_SQL="SELECT  t2.* FROM USERS2_DEALS as t2 where
		  t2.A_RECORD_CREATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE']."' and
		  ( t2.A_RECORD_CREATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR']."' or  t2.A_RECORD_UPDATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR2']."'  ) ";
		if($_GET['A_RECORD_UPDATE_DATE']) ZAP_SQL($ZAP_SQL);
		
//		echo $ZAP_SQL."\n<br>";
 while($DWA=mysql_fetch_object($result)) {
      foreach ($DWA as $DWA_A=>$DWA_V ) {
  		 if((($DWA_V!='')&&($DWA_V!='0'))&&($DWA_A!=$AR)||($DWA_V=='S_ADDRESS_STREET_t1')) {
        	//echo "<td> ".str_replace('_',' ',$DWA_A)." = $DWA_V </td>";   
	    	$ARR[STAT][$DWA->A_RECORD_CREATE_AUTHOR][$DWA_A]++;
            if($DWA_A=='A_RECORD_CREATE_AUTHOR') $ARR[AUTH][$DWA_V]++;
     	 }
     if($DWA_A==$AR)$AR++;
     }
    }

//print_r($ARR);
unset($ARR);

*/


  $RAPORT['IGNORE_TABLES'][]="AUTOSHAPE";

//echo statystyka pracy historyczna w systemie 
	$ZAP_SQL="show tables ";
  	ZAP_SQL($ZAP_SQL);
    while($R=mysql_fetch_object($result)) {
		  
//		  $RAPORT['TABLES_ALL'][][$R->Tables_in_SES_USERS2]=$R->Tables_in_SES_USERS2;  
		 if(!in_array($R->Tables_in_SES_USERS2,$RAPORT['IGNORE_TABLES']))
		  $RAPORT['TABLES_ALL'][]=$R->Tables_in_SES_USERS2;		  
    
    }
  
    ZAP_SQL($ZAP_SQL);
    while($R=mysql_fetch_object($result)) {
		 if(strstr($R->Tables_in_SES_USERS2,"_HIST")) {
		  $RAPORT['TABLES_HIST'][$R->Tables_in_SES_USERS2]['TABLE_HIST']=$R->Tables_in_SES_USERS2;
//		  echo "<br> IF in array str_replace(_HIST, \"\", ".$R->Tables_in_SES_USERS2." RAPORT[TABLES_ALL] , czyli :**".str_replace("_HIST", "", $R->Tables_in_SES_USERS2)."**";
		  			$string=str_replace("_HIST", "", $R->Tables_in_SES_USERS2);
			  if(in_array($string,$RAPORT['TABLES_ALL'])) {
				//	echo "<br>   JEST IN ARRY";
			   		$RAPORT['TABLES_HIST'][$R->Tables_in_SES_USERS2]['TABLE']=str_replace("_HIST", "", $R->Tables_in_SES_USERS2);  
			   }
		  }
    
    }
  //  print_r($RAPORT);
    
    	unset($ZAP_SQL);
    	
	//	echo "<pre>\n";
    //	print_r($RAPORT);
    //	echo "</pre>";
    	
		foreach($RAPORT['TABLES_HIST'] as $RTH_I=>$RTH_A ) {
		//	echo "\n<br> if isset RTH_A[TABLE]==".$RTH_A['TABLE'];
			if(isset($RTH_A['TABLE'])) {
			//	 echo "-- Yes it is!";
			     ZAP_SQL("select * from ".$RTH_A['TABLE']." limit 1");
				 $r2=mysql_fetch_array($result);
			//	 print_r($r2);
			//	 echo "<hr>";
				  if( isset($r2['A_RECORD_CREATE_AUTHOR'])  &&   isset($r2['A_RECORD_CREATE_DATE'])  && isset($r2['A_RECORD_UPDATE_DATE'])  && isset($r2['A_RECORD_UPDATE_AUTHOR']) && isset($r2['ID'])  ) {	
			//	 echo "-- Yes it is again!!!!";
					if( isset($r2['T_WORKPOINTS_USER'])  &&   isset($r2['T_WORKPOINTS_DATE']) ) { 
					
					$ZAP_SQL['TABLES'][]="SELECT  A_RECORD_CREATE_DATE as C1, year(A_RECORD_CREATE_DATE) as C1_Y , month(A_RECORD_CREATE_DATE) as C1_M, day(A_RECORD_CREATE_DATE) as C1_D ,   A_RECORD_CREATE_AUTHOR as C2, ID as TAB_ID , '".$RTH_A['TABLE']."' as TABLEE from ".$RTH_A['TABLE']." as C2 where
				 	( A_RECORD_CREATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE']."' or A_RECORD_CREATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE2']."' ) and ( A_RECORD_CREATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR']."'  or  A_RECORD_CREATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR2']."' )
				 	having TABLEE like '".$_GET['TABLES_LIKE']."'
				 	 \n";
				 	 //workpoints table
				 	 $ZAP_SQL['TABLES'][]="SELECT  T_WORKPOINTS_DATE as C1, year(T_WORKPOINTS_DATE) as C1_Y , month(T_WORKPOINTS_DATE) as C1_M, day(T_WORKPOINTS_DATE) as C1_D ,   T_WORKPOINTS_USER as C2, ID as TAB_ID , '".$RTH_A['TABLE']."_WP' as TABLEE from ".$RTH_A['TABLE']." as C2 where
				 	 ( T_WORKPOINTS_DATE like '".$_GET['A_RECORD_UPDATE_DATE']."' or T_WORKPOINTS_DATE like '".$_GET['A_RECORD_UPDATE_DATE2']."' ) and ( T_WORKPOINTS_USER like '".$_GET['A_RECORD_UPDATE_AUTHOR']."' or  T_WORKPOINTS_USER like '".$_GET['A_RECORD_UPDATE_AUTHOR2']."'  )
				 	having TABLEE like '".$_GET['TABLES_LIKE']."'
				 	 \n";
					
					 //  die('for '.$RTH_A['TABLE']);
					
					} else {
			
					 $ZAP_SQL['TABLES'][]="SELECT  A_RECORD_CREATE_DATE as C1, year(A_RECORD_CREATE_DATE) as C1_Y , month(A_RECORD_CREATE_DATE) as C1_M, day(A_RECORD_CREATE_DATE) as C1_D ,   A_RECORD_CREATE_AUTHOR as C2, ID as TAB_ID , '".$RTH_A['TABLE']."' as TABLEE from ".$RTH_A['TABLE']." as C2 where
				 	A_RECORD_CREATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE']."' and ( A_RECORD_CREATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR']."'  or  A_RECORD_CREATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR2']."' )
				 	having TABLEE like '".$_GET['TABLES_LIKE']."'
				 	 \n";
				 	}
				
					ZAP_SQL("SELECT  A_RECORD_CREATE_DATE as C1, A_RECORD_CREATE_AUTHOR , ID as TAB_ID , '".$RTH_A['TABLE']."' as TABLEE from ".$RTH_A['TABLE']." as C2 where	( A_RECORD_CREATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE']."'  or  A_RECORD_CREATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE2']."') and ( A_RECORD_CREATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR']."' or A_RECORD_CREATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR2']."'  )  "); 
					}
				}

				
				 ZAP_SQL("select * from ".$RTH_A['TABLE_HIST']." limit 1");
				 $r2=mysql_fetch_array($result);
			//	 print_r($r2);
				 if( isset($r2['A_RECORD_CREATE_AUTHOR'])  &&   isset($r2['A_RECORD_CREATE_DATE'])  && isset($r2['A_RECORD_UPDATE_DATE'])  && isset($r2['A_RECORD_UPDATE_AUTHOR']) && isset($r2['ID_USERS2'])  ) {	
				 		if( isset($r2['T_WORKPOINTS_USER'])  &&   isset($r2['T_WORKPOINTS_DATE']) ) {
				 		$ZST="SELECT  A_RECORD_UPDATE_DATE as C1,  year(A_RECORD_UPDATE_DATE) as C1_Y , month(A_RECORD_UPDATE_DATE) as C1_M, day(A_RECORD_UPDATE_DATE) as C1_D,  A_RECORD_UPDATE_AUTHOR as C2 , ID_USERS2 as TAB_ID , '".$RTH_A['TABLE_HIST']."' as TABLEE from ".$RTH_A['TABLE_HIST']." where
						( A_RECORD_UPDATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE']."'  or A_RECORD_UPDATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE2']."' ) and  ( A_RECORD_UPDATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR']."' or A_RECORD_UPDATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR2']."'  )
										 	having TABLEE like '".$_GET['TABLES_LIKE']."'	 \n ";
						$ZAP_SQL['TABLES'][]=$ZST;
						//workpoints
						$ZST="SELECT  T_WORKPOINTS_DATE as C1,  year(T_WORKPOINTS_DATE) as C1_Y , month(T_WORKPOINTS_DATE) as C1_M, day(T_WORKPOINTS_DATE) as C1_D,  A_RECORD_UPDATE_AUTHOR as C2 , ID_USERS2 as TAB_ID , '".$RTH_A['TABLE_HIST']."_WP' as TABLEE from ".$RTH_A['TABLE_HIST']." where
						( T_WORKPOINTS_DATE like '".$_GET['A_RECORD_UPDATE_DATE']."'  or T_WORKPOINTS_DATE like '".$_GET['A_RECORD_UPDATE_DATE2']."' ) and  ( T_WORKPOINTS_USER like '".$_GET['A_RECORD_UPDATE_AUTHOR']."' or T_WORKPOINTS_USER like '".$_GET['A_RECORD_UPDATE_AUTHOR2']."'  )
										 	having TABLEE like '".$_GET['TABLES_LIKE']."'	 \n ";
				 		
				 		$ZAP_SQL['TABLES'][]=$ZST;
			             ZAP_SQL($ZST);
				 		 } else {

				 		
						 $ZST="SELECT  A_RECORD_UPDATE_DATE as C1,  year(A_RECORD_UPDATE_DATE) as C1_Y , month(A_RECORD_UPDATE_DATE) as C1_M, day(A_RECORD_UPDATE_DATE) as C1_D,  A_RECORD_UPDATE_AUTHOR as C2 , ID_USERS2 as TAB_ID , '".$RTH_A['TABLE_HIST']."' as TABLEE from ".$RTH_A['TABLE_HIST']." where
						( A_RECORD_UPDATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE']."'  or A_RECORD_UPDATE_DATE like '".$_GET['A_RECORD_UPDATE_DATE2']."' ) and  ( A_RECORD_UPDATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR']."' or A_RECORD_UPDATE_AUTHOR like '".$_GET['A_RECORD_UPDATE_AUTHOR2']."'  )
										 	having TABLEE like '".$_GET['TABLES_LIKE']."'	 \n ";
						$ZAP_SQL['TABLES'][]=$ZST;
			             ZAP_SQL($ZST);
						}
					//	echo $ZST."<hr>";
						
			         } else echo "Nie ma w tab ".$RTH_A['TABLE_HIST']."  kolumn . .";
			 
       }

	foreach($ZAP_SQL['TABLES'] as $ZSI=>$ZSV) {
	
		if($ZAP_SQL['SQL'])
		$ZAP_SQL['SQL'].=" union ".$ZSV ;
		else $ZAP_SQL['SQL'].=$ZSV ;
	
	}
	$ZAP_SQL['SQL'].=" order by date(C1) ";
	//$ZAP_SQL['SQL'].=" order by C1 , C2 ";
	//echo "<pre>\n";	print_r($ZAP_SQL);	echo "</pre>\n";

if($_GET['A_RECORD_UPDATE_DATE'])	ZAP_SQL($ZAP_SQL['SQL']);
	
	 while($DWA=mysql_fetch_object($result)) {
//	echo "<pre>";
//	print_r($DWA);
//	echo "</pre>";
	 //    $AR=0;
//      foreach ($DWA as $DWA_A=>$DWA_V ) {
  		// if((($DWA_V!='')&&($DWA_V!='0'))&&($DWA_A!=$AR)||($DWA_V=='S_ADDRESS_STREET_t1')) {
        	//echo "<td> ".str_replace('_',' ',$DWA_A)." = $DWA_V </td>";   
	    	$ARR['STAT'][$DWA->C2][$DWA->TABLEE]++;
	    	//czysczenie formatow dat:
 			list($DAT_L1,$DAT_L2)=split(" ", $DWA->C1);
 		//	echo "DATL1 to".$DAT_L1;
 			if(isset($DAT_L2)) $DWA->C1=$DAT_L1."-".$DAT_L2;
 			else $DWA->C1=$DWA->C1.":00";
	    	 
	    	$ARR['DATE'][$DWA->C2][$DWA->C1]=$DWA->TABLEE." : ".$DWA->TAB_ID;
	    	
	    	$ARR['DATE_CHART'][$DWA->C2][$DWA->C1_Y][$DWA->C1_M][$DWA->C1_D]['COUNT']++;
	    	
	    	  $C1_H=substr($DWA->C1,11,2);
	    	$ARR['DATE_CHART'][$DWA->C2][$DWA->C1_Y][$DWA->C1_M][$DWA->C1_D]['HOURS'][$C1_H]++;
	    	$ARR['DATE_CHART'][$DWA->C2][$DWA->C1_Y][$DWA->C1_M][$DWA->C1_D]['TABLE'][$DWA->TABLEE]++;

	    	
            //if($DWA_A=='C2')
             $ARR['AUTH'][$DWA->C2]++;
     	 //}
//     if($DWA_A==$AR)$AR++;
  //   }
      if($DWA_A==$AR)$AR++;

    }

foreach($ARR['DATE'] as $ARRAY_IND=>$ARRAY_VAL) {


  //asort($ARR['DATE'][$ARRAY_VAL]);
}
//session_start();
$_SESSION['CHART']['ARR']=$ARR;

$_SESSION['CHART']['TITLE']="COSTAM SQIX TITLE";

//print_r($ARR);
unset($ARR);



  $pok_gif="puste";
  $cnt=0;
foreach($_SESSION['CHART']['ARR']['DATE_CHART'] as $C2=>$C2V) {
	$i=0;
	$cnt++;

//	echo "\n wstawiam dla C2 ".$C2." dane ".$C2V;


	foreach($C2V as $C1_Y=>$C1_YV) {
//		echo "\n wstawiam dla C1_Y ".$C1_Y." dane ".$C1_YV;

		foreach($C1_YV as $C1_M=>$C1_MV) {
//		echo "\n wstawiam dla C1_M ".$C1_M." dane ".$C1_MV;

			foreach($C1_MV as $C1_D=>$C1_DV) {
				
//					echo "\n wstawiam dla ".$C1_MV." C1_D:".$C1_D." dane ".$C1_DV['COUNT'];
				$data[$C2][$C1_D]=$C1_DV['COUNT'];
				$i+=$C1_DV['COUNT'];
					//hours
					foreach($C1_DV['HOURS'] as $C1_H=>$C1_HV) {

					//		echo "\n wstawiam dla C1_H ".$C1_H." dane ".$C1_HV;
							$data_hours[$C2][$C1_H]+=$C1_HV;

					}

					foreach($C1_DV['TABLE'] as $C1_H=>$C1_HV) {

							//echo "\n wstawiam dla C1_H ".$C1_H." dane ".$C1_HV;
							$data_table[$C2][$C1_H]+=$C1_HV;

					}
				//if($i>14) break;
				}
			}
	
	}
	
		if($i>$_GET['F_LIMIT']) {
				 if($pok_gif=="puste")  { 
				    $pok_gif=$C2 ;
					} else {
					 echo "<br><img src=budynki-GRAPH.php?C2=".urlencode($C2)."&C22=".urlencode($pok_gif).">";
					echo "<br><img src=budynki-GRAPH-HOUR.php?C2=".urlencode($C2)."&C22=".urlencode($pok_gif).">";
					echo "<br><img src=budynki-GRAPH-TABLE.php?C2=".urlencode($C2)."&C22=".urlencode($pok_gif).">";


			//		 echo "\nwysylam src=budynki-GRAPH.php?C2=".$C2."&C22=".$pok_gif;
					 $pok_gif="puste";
					}
			//  echo "<br><img src=budynki-GRAPH.php?C2=".urlencode($C2).">";
			  //echo "POK to ".$pok_gif." a I to ".$i;

		}	

		echo "\n SIze dla ".$C2." wynioslo ".$i ;
//	break;
}

		 if(($pok_gif!="puste")) {
		  echo "<br><img src=budynki-GRAPH.php?C2=".urlencode($pok_gif).">";
		 echo "<br><img src=budynki-GRAPH-HOUR.php?C2=".urlencode($pok_gif).">";
		echo "<br><img src=budynki-GRAPH-TABLE.php?C2=".urlencode($pok_gif).">";

		  }
// echo "<br><img src=budynki-GRAPH.php?C2=".urlencode($pok_gif).">";



//tabela z godzinami pracy
foreach($data_hours as $user_i=>$user) {
	foreach($user as $TABLEE_I=>$TABLE_V) {
	    foreach($data_hours as $test_user_i=>$test_user_v) {
	    	if(!isset($test_user_v[$TABLEE_I]))  {
		    	//echo "\n W user ".$test_user_i." tabeli ".$TABLEE_I." brakuje!";
				$data_hours[$test_user_i][$TABLEE_I]=0;
	    	}
	    }
	}
}
foreach($data_hours as $user_i=>$user) {
echo "\n data duuppa:\n".$user_i;

  ksort($data_hours[$user_i]);
}



//$data_table - to dane w jakich tabelach byla praca
//Tutaj trzeba dodac brakujace do TABLE dane z innych array i posortowac dla dobrego wyswietlenia
foreach($data_table as $user_i=>$user) {
	foreach($user as $TABLEE_I=>$TABLE_V) {
	    foreach($data_table as $test_user_i=>$test_user_v) {
	    	if(!isset($test_user_v[$TABLEE_I]))  {
		    	//echo "\n W user ".$test_user_i." tabeli ".$TABLEE_I." brakuje!";
				$data_table[$test_user_i][$TABLEE_I]=0;
	    	}
	    }
	}
}


foreach($data_table as $user_i=>$user) {
//echo "\n data duuppa:\n".$user_i;
  ksort($data_table[$user_i]);
}

//Sortujemy i uzupelniamy daty prac o brakujace indeksy
foreach($_SESSION['CHART']['ARR']['DATE_CHART'] as $user_i=>$user) { //lecimy ARR[User]
	foreach($user as $TABLEE_I=>$TABLE_V) { //Lecimy ARR[User][Rok]
			foreach($TABLE_V as $TABLEE_II=>$TABLE_VV) { //Lecimy ARR[User][Rok][m-c]
				foreach($TABLE_VV as $TABLEE_III=>$TABLE_VVV) { //Lecimy ARR[User][Rok][m-c][day]

	
				    foreach($_SESSION['CHART']['ARR']['DATE_CHART'] as $test_user_i=>$test_user_v) { //arr[user]
	  				  	if(!isset($test_user_v[$TABLEE_I][$TABLEE_II][$TABLEE_III]))  {
		    //	echo "\n W user ".$test_user_i." tabeli ".$TABLEE_I."-".$TABLEE_II."-".$TABLEE_III." brakuje!";
				$_SESSION['CHART']['ARR']['DATE_CHART'][$test_user_i][$TABLEE_I][$TABLEE_II][$TABLEE_III]['COUNT']=0;
				    	}
	    }
				}
		}
	}
}
foreach($_SESSION['CHART']['ARR']['DATE_CHART'] as $user_i=>$user) { //lecimy ARR[User]
	ksort($_SESSION['CHART']['ARR']['DATE_CHART'][$user_i]);
	foreach($user as $TABLEE_I=>$TABLE_V) { //Lecimy ARR[User][Rok]
			ksort($_SESSION['CHART']['ARR']['DATE_CHART'][$user_i][$TABLEE_I]);
			foreach($TABLE_V as $TABLEE_II=>$TABLE_VV) { //Lecimy ARR[User][Rok][m-c]
				ksort($_SESSION['CHART']['ARR']['DATE_CHART'][$user_i][$TABLEE_I][$TABLEE_II]);
				foreach($TABLE_VV as $TABLEE_III=>$TABLE_VVV) { //Lecimy ARR[User][Rok][m-c][day]
					
					ksort($_SESSION['CHART']['ARR']['DATE_CHART'][$user_i][$TABLEE_I][$TABLEE_II][$TABLEE_III]);

				}
			}
	}
}



echo "<pre>";
//print_r($_SESSION['CHART']);
//print_r($data_table);//tabela z pracami w tabelach 
//print_r($data_hours); //tabela z godzinami pracy
//print_r($_SESSION['CHART']['ARR']['DATE_CHART']); //tabela z dniami ?


echo "</pre>";


//echo "<br><img src=budynki-GRAPH.php>";

//print_r($_SESSION['CHART']['TITTLE']);
//print_r($_SESSION['CHART']['TITLE']);



} //EOF IF






?>
