<?php


function tree_znajdz($PARENT,$POZIOM,$LAST) {
global $TREE,$thiss,$result,$ARG1,$ARG1_VAL;

ZAP_SQL("select 1+1");

//echo "||| parent $PARENT ||| <br>\n";
   if($PARENT==0) $SPECIAL=" or P_ID=ID "; else $SPECIAL=" and P_ID!=ID ";
   $SQL2="select * from ".$thiss->DETECT_TABLE_NAME." where ( P_ID like '$PARENT %' or P_ID like '% $PARENT %' or P_ID like '% $PARENT' or P_ID like '$PARENT' )  $SPECIAL ";
//echo "||| parent $PARENT $SQL2 ||| <br>\n";
   $result2=mysql_query($SQL2);   $result2_numrows=mysql_num_rows($result2);

    $lp=0;
    while ($res2=mysql_fetch_array($result2)) {
    $lp++;
      if($lp==$result2_numrows) $LLAST=1; else $LLAST=0 ;
       if($LLAST) $TREE[$POZIOM]="   "; else $TREE[$POZIOM]=" | ";


        if(!$LLAST) {
        $cnt=0;
         while ($cnt <= $POZIOM) {
            if($cnt<>$POZIOM) echo $TREE["$cnt"]; else {
		if($TREE[$cnt]==" | ") echo " | ";
                else echo " | ";
            }
	    $cnt++;
         }
	     echo "\n"; 
        }

        $cnt=0;
         while ($cnt <= $POZIOM) {
            if($cnt<>$POZIOM) echo $TREE["$cnt"]; else {
		if($TREE[$cnt]==" | ") echo " +==";
                else echo " \\==";
            }
	    $cnt++;
         }
	     echo ""."<font color=red>[ <b>ID <a href=".$PHP_SELF."?FUNCTION_INIT=".$thiss->DETECT_EDIT_FUNCTION."&ARG1=EDIT&ARG1_VAL=".$res2[ID]."> $res2[ID] </a> s: <font ".USERS_COLUMN_COLOR_RETURN("A_STATUS",$res2[A_STATUS],'color').">".$res2[A_STATUS]."</font> <i>".$res2[M_DIST_DESC]."</i> <font color=black>".$res2[M_DISTRIBUTOR]."</font> <font size=-1>".$res2[M_DIST_INVENTOR]."</font></b> ]</font> <a href=\"$PHP_SELF?$ZASOBY_TABLE=1&EDIT=1&ADD_PROCES_ID=$res2[0]\" target=\"_blank\">Z+</a> \n"; 
//             $A_STATUS_COLOR=USERS_COLUMN_COLOR_RETURN("A_STATUS",$res2[A_STATUS],'color');
//	     echo ""."<font color=red>[ <b>$res2[ID] s:<font $A_STATUS_COLOR> ".$res2[A_STATUS]."</font> <i>$res2[M_DIST_DESC]</i>  ".$res2[M_DIST_DESC]."</b> ]</font> <a href=\"$PHP_SELF?$ZASOBY_TABLE=1&EDIT=1&ADD_PROCES_ID=$res2[0]\" target=\"_blank\">Z+</a> \n"; 
	if($POZIOM>200) die("problem petli");      
	  if($_GET[ROZWIN])  {
            	    
	    $_SESSION[$thiss->DETECT_TABLE_COLUMN][EDITRECORD][ID]=$res2[ID];
            $ARG1_VAL="DODAJ_PLIK";
            M_DIST_FILES("DODAJ_PLIK",'','HIDE') ;
            M_DIST_FILES_RELATIONS($thiss->DETECT_TABLE_COLUMN,$res2[ID],'');            
          }
     tree_znajdz($res2[0],$POZIOM+1,$LLAST);
    }


}// EOF Func



function TREE() {
global $result,$thiss;

echo '&lt&lt<a href="?MENU_INIT=IN7_MK_BAZA_DYSTRYBUCJI" title="Powrot">Powrot do spraw</a>';


ZAP_SQL("select 1+1");
//$TREE[0]="|";
$TREE=array();

//$TREE="|";

//$SQL1="select * from CRM_PROCES where PARENT_ID='0' ";
echo "<pre>";

echo "POWIAZANE DOKUMENTY <a href=\"$PHP_SELF?CRM_WYSWIETL_PROCES=1";
if (!$_GET[ROZWIN]) echo "&ROZWIN=1";
echo "\">";
if (!$_GET[ROZWIN]) echo "ROZWIN";
else echo "ZWIN";
echo "</a>\n";
tree_znajdz(0,1,0);

//echo "$SQL1";

//$result1=mysql_query($SQL1);
//ZAP_SQL($SQL1);
//while ($res1=mysql_fetch_array($result)) {

//echo "$TREE"."$res1[0]"."\n";
//
 //  $SQL2="select * from CRM_PROCES where PARENT_ID='$res1[0]'";
 //  $result2=mysql_query($SQL2);
 //   while ($res2=mysql_fetch_array($result2)) {
 //   echo $TREE."\-"."$res2[0]  \n";
 //   }
//} //EOF SQL1


//echo "$SQL1";
echo "</pre>";
 //EOF IF
}
