<?php

//2008-11-27 A.BInder addon for relations of files find
//2008-12-11 A.Binder addon size versioning of found fils
//2009-01-22 A.Binder looking for files at the same verions in dziennik_korespondencji
function M_DIST_FILES_RELATIONS($SRC_COLUMN_NAME,$SRC_COLUMN_NAME_ID,$DST_COLUMN_NAME,$HIDE) {
global $FOLDERS, $result ;

 if($SRC_COLUMN_NAME=='IN7_MK_BAZA_DYSTRYBUCJI_COLUMN') {

if(($_GET['ROZWIN']=="OGRANICZ")||(!$_GET[ROZWIN])) $OGRANICZ_SET="ROZWIN"; else $OGRANICZ_SET="OGRANICZ";
echo "<a href=\"".$PHP_SELF."?FUNCTION_INIT=".$_GET[FUNCTION_INIT]."&ARG1_VAL=".$_GET[ARG1_VAL]."&HEADER_NOT_INIT=".$_GET[HEADER_NOT_INIT]."&ROZWIN=".$OGRANICZ_SET."\">".$OGRANICZ_SET." SZEROKIE WYNIKI</a>";


 ZAP_SQL("select K_TYP_KORESP from IN7_DZIENNIK_KORESP where ID_PROJECT='".$SRC_COLUMN_NAME_ID."' group by K_TYP_KORESP ; ");
// print_r($result);
 $i=0;
 while ($h=mysql_fetch_array($result)) {
 
  $FOLDERS_ARR['PATH'][$i]=$FOLDERS['IN7_DZIENNIK_KORESP_COLUMN']."/".$h[0] ;
  $FOLDERS_ARR['K_TYP_KORESP'][$i]=$h[0];
 $i++;
 }
 
// echo "pr<hr>";
//                          echo " Otwieram $FOLDER";

 ZAP_SQL("select ID,K_TYP_KORESP,K_OD_KOGO,K_ZAWARTOS,K_DATA_OTRZYMANEJ_KORESP from IN7_DZIENNIK_KORESP where ID_PROJECT='".$SRC_COLUMN_NAME_ID."'  ; ");
 // print_r($result);
  $i=0;
   while ($h=mysql_fetch_array($result)) {
     $FIND_FILES[$h['ID']]['K_TYP_KORESP']=$h['K_TYP_KORESP'];
     $FIND_FILES[$h['ID']]['K_OD_KOGO']=$h['K_OD_KOGO'];
     $FIND_FILES[$h['ID']]['K_ZAWARTOS']=$h['K_ZAWARTOS'];   
     $FIND_FILES[$h['ID']]['K_DATA_OTRZYMANEJ_KORESP']=$h['K_DATA_OTRZYMANEJ_KORESP'];
     $FIND_FILES[$h['ID']]['ID']=$h['ID'];

        $i++;
         }
    
 foreach($FOLDERS_ARR[PATH] as $i=>$FOLDER ) {


// echo "WCHODZE DO  ".$FOLDER." ";

  
//  $FOLDER=$FOLDERS[IN7_DZIENNIK_KORESP_COLUMN];

    if ($handle = opendir($FOLDER)) {
      while (false !== ($file = readdir($handle))) {
//                echo "<br> $file ... ";
                    $files[LISTA1][]=$FOLDER."/".$file;
                    }
                    

    } //eof openddir
 }

//echo "<pre>";print_r($files);echo "</pre>";

//SZUKAMY DLA plikow kandydatow

    $i=0;
    foreach($FIND_FILES as $TO_FIND_ID=>$TO_FIND) {
//echo "<br>szukam dla STR ".$TO_FIND[K_DATA_OTRZYMANEJ_KORESP];
//print_r($files);

      $FOUND_FILES["IN7_DZIENNIK_KORESP_COLUMN"][$TO_FIND_ID]=array_isearch($TO_FIND['K_DATA_OTRZYMANEJ_KORESP'],$files['LISTA1']);
      $i++;
    }

//print_r($FOUND_FILES);
   foreach($FOUND_FILES["IN7_DZIENNIK_KORESP_COLUMN"] as $i=>$FOUND_ID) {

    echo "<br>L.dz.: ".$FIND_FILES[$i]['ID']." Dokumenty z dnia ".$FIND_FILES[$i]['K_DATA_OTRZYMANEJ_KORESP']." od ".$FIND_FILES[$i]['K_OD_KOGO']." zawartosc:  ".$FIND_FILES[$i]['K_ZAWARTOS'];
//    print_r($FIND_FILES);
     foreach($FOUND_FILES["IN7_DZIENNIK_KORESP_COLUMN"][$i] as $FOUND_ID_1) {
       $PATH_FILE_71=ereg_replace($FOLDERS[IN7_DZIENNIK_KORESP_COLUMN],$FOLDERS[IN7_DZIENNIK_KORESP_COLUMN."_WWW"],$files['LISTA1'][$FOUND_ID_1]);
          ZAP_SQL("select M_DIST_FILES from IN7_DZIENNIK_KORESP_HIST where ID_USERS2='".$i."' and M_DIST_FILES!='N/S;' and M_DIST_FILES!='' " );
          while ($h2=mysql_fetch_array($result)) {

          $DETECTED="";
		if($_SESSION[DEBUG]) echo "<br> sprawdzam if(stripos(".$h2[0].", ".$PATH_FILE_71.")) DETECTED=1 \n";
            if(stripos($h2[0], $PATH_FILE_71)) {
		 $DETECTED="1" ;             
            } else {  //szukamy dla INDEKS pliku = INDEKS DZIENNIKA KORESPONDENCJI
//		echo "else";
              $VAR_EXPLODE=explode('.',$PATH_FILE_71);
              $VAR_EXPLODE_SIZE=sizeof($VAR_EXPLODE);
//                          echo $i;
               if($i==$VAR_EXPLODE[$VAR_EXPLODE_SIZE-2]) $DETECTED="1";

            }
//            echo "<br> Szukam $PATH_FILE_71 dla".$h2[0] ;

          }	
//   echo "<br> znalazlem $FOUND_ID_1";
//  $PATH_WWW=ereg_replace($FOLDERS[$this->DETECT_TABLE_COLUMN],$FOLDERS[$this->DETECT_TABLE_COLUMN."_WWW"],$PATH_WWW);
      if(($_GET[ROZWIN]=="ROZWIN")||($DETECTED)) {
         if($DETECTED)  echo "<font color=\"red\">"; else echo "<font size=\"-2\">";
         echo  "<br>&nbsp;<a href=\"".ereg_replace($FOLDERS[IN7_DZIENNIK_KORESP_COLUMN],$FOLDERS[IN7_DZIENNIK_KORESP_COLUMN."_WWW"],$files[LISTA1][$FOUND_ID_1])."\">".$PATH_FILE_71."</a> ".round(filesize($files[LISTA1][$FOUND_ID_1])/1000000,2)."Mb";   
         echo "</font>";
      }

     }
   }

//print_r($FOUND_FILES);

 } else echo "<br> Error - unknown type $SRC_COLUMN_NAME ";





} //eof FUNC

