<?php
include	("_dirinfo.php");
include (PATH_TO_ROOT."init.php");

$db->ChangeDatabase(DB_WARPIT_HELP);

$newLine = "\n";

require('./fpdf/fpdf.php');
require('./fpdf/toc.php');
require('./fpdf/pdfExtendRm.php');

$_GET['id'] = explode("_", $_GET['id']);

$id = $_GET['id'][0];

if($id=='' || $id<0) $id=0;
$used_lang = '1';

$s = $_REQUEST['s'];

if($s == "a"){
	
	function help_g_getId($g_arr, $g_id){
		
		for($i=0;$i<count($g_arr);$i++){
			if($g_arr[$i]["id"]==$g_id){
				$temp_arr = $g_arr[$i];
				break;
			}
		}
		
		return $temp_arr;
	}
	
	function help_g_getChild($g_id){
		
		global $help_discover_arr;
		global $db;
		global $used_lang;
		global $pdf;
		global $g_numbering;
		global $help_g_top;
		global $help_title_numbering;
		
		
		$help_all = $db->getArray("SELECT f.*, g.title, g.text, g.date_m, g.id_lang FROM ".$_REQUEST['table']." AS f, ".$_REQUEST['table']."Lang AS g WHERE f.id=g.id_sup AND g.id_lang=".$used_lang." ORDER BY f.`topic` ASC, f.`orderBy` ASC ");
		
		if(count($help_all)>count($help_discover_arr)){
			
			$help = $db->getArray("SELECT f.*, g.title, g.text, g.date_m, g.id_lang FROM ".$_REQUEST['table']." AS f, ".$_REQUEST['table']."Lang AS g WHERE f.id=g.id_sup AND f.id_sup=".$g_id." AND g.id_lang=".$used_lang." ORDER BY f.`topic` ASC, f.`orderBy` ASC ");
			
			if(count($help)>0){
				for($j=0;$j<count($help);$j++){
				
					if(!in_array($help[$j]["id"], $help_discover_arr)){
						
						$tmp_g_arr = help_g_getId($help_g_top, $help[$j]["id"]);
						
						
						for($z=0;$z<count($g_numbering[$tmp_g_arr["depth"]]);$z++){
							
							if($g_numbering[$tmp_g_arr["depth"]][$z]["id"]==$help[$j]["id"]){
								$g_numbering[$tmp_g_arr["depth"]][$z]["number"] = $z;
								$tmp_g_numbering = $z;
								break;
							}
							
						}
						
						$tmp_title_num = "";
						if($help[$j]["id_sup"]==0){
							$tmp_title_num_i = count($help_title_numbering[$help[$j]["id_sup"]]);
							$help_title_numbering[$help[$j]["id_sup"]][$tmp_title_num_i]["id"] = $help[$j]["id"];
							$tmp_title_num = ($tmp_title_num_i+1).".";
							
							for($k=0;$k<count($help_g_top);$k++){
								
								if($help_g_top[$k]["id"]==$help[$j]["id"]){
									$help_g_top[$k]["num"] = $tmp_title_num;
								}
								
							}
							
							
						}else{
							$tmp_title_num_i = count($help_title_numbering[$help[$j]["id_sup"]]);
							$help_title_numbering[$help[$j]["id_sup"]][$tmp_title_num_i]["id"] = $help[$j]["id"];
														
							$tmp_title_num_sup = help_g_getId($help_g_top, $help[$j]["id_sup"]);
							
							$tmp_title_num = $tmp_title_num_sup["num"].($tmp_title_num_i+1).".";
							
							for($k=0;$k<count($help_g_top);$k++){
								
								if($help_g_top[$k]["id"]==$help[$j]["id"]){
									$help_g_top[$k]["num"] = $tmp_title_num;
								}
								
							}
							
						
							$help_title_numbering[$help[$j]["id_sup"]][$tmp_title_num_i]["num"] = $tmp_title_num;
						}
						
						
						if($tmp_g_arr["depth"]==0){
							$pdf->Ln(4);
							$pdf->SetFont('Arial','',20);
							$pdf->SetTextColor(0,0,255);
						}elseif($tmp_g_arr["depth"]==1){				
							$pdf->SetTextColor(0,0,0);
							$pdf->SetFont('Times', 'I', 14);
						}else{			
							$pdf->SetTextColor(0,0,0);
							$pdf->SetFont('Times', '', 12);
						}
						$pdf->Cell(0, 10, $tmp_title_num." ".$help[$j]["title"], 0, 1, 'L');
													
						$pdf->SetTextColor(0,0,0);
						
						//$pdf->TOC_Entry($tmp_title_num." ".$help[$j]["title"].' - '.$help[$j]["id"].' - '.$help[$j]["id_sup"].' - '.$tmp_g_numbering.' - '.$tmp_g_arr["depth"].' - fn()', $tmp_g_arr["depth"]);
						$pdf->TOC_Entry($tmp_title_num." ".$help[$j]["title"], $tmp_g_arr["depth"]);
						
						
						$pdf->SetFont('Times', '', 12);
						$pdf->Cell(0, 5, strip_tags($help[$j]["text"]), 0, 1, 'L');
						
						//-------------------------------------------------------
						$help_discover_arr[] = $help[$j]["id"];
						
						help_g_getChild($help[$j]["id"]);
					}
					
				}
			}else{
				
				$help = $db->getArray("SELECT f.*, g.title, g.text, g.date_m, g.id_lang FROM ".$_REQUEST['table']." AS f, ".$_REQUEST['table']."Lang AS g WHERE f.id=g.id_sup AND f.id=".$g_id." AND g.id_lang=".$used_lang." ORDER BY f.`topic` ASC, f.`orderBy` ASC ");
									
				help_g_getChild($help[0]["id_sup"]);
				
			}
		
		}
		
		
		
	}
	
	//pridobitev max globine...
	$help_g_top = $db->getArray("SELECT f.*, g.title, g.text, g.date_m, g.id_lang FROM ".$_REQUEST['table']." AS f, ".$_REQUEST['table']."Lang AS g WHERE f.id=g.id_sup AND g.id_lang=".$used_lang." ORDER BY f.`topic` ASC, f.`orderBy` ASC ");
	
	for($i=0;$i<count($help_g_top);$i++){
		
		$help_g_top[$i]["g_look"] = 0;
		
	}
	
	$g_max = 0;
	$g_max_e = 1;
	$g_numbering = array();
	do{
		$g_max_e_tmp = 1;
		$g_max_e = 1;
		for($i=0;$i<count($help_g_top);$i++){
			
			
			if($help_g_top[$i]['depth']==NULL){
				$g_id_sup = $help_g_top[$i]['id_sup'];
				
				$g = 0;
				
				if($g_id_sup>0){
					$help_g_parent = help_g_getId($help_g_top, $g_id_sup);
					if($help_g_parent["depth"]>=0 && $help_g_parent["g_look"]==1){
						$help_g_top[$i]["depth"] = (int)$help_g_parent["depth"] + 1;
						$help_g_top[$i]["g_look"] = 1;
						
						$g_numbering[$help_g_top[$i]["depth"]][]["id"] = $help_g_top[$i]["id"];
						
						$g_max_e_tmp = 1;
					}else{
						$help_g_top[$i]["g_look"] = 0;
						
						$g_max_e_tmp = 0;
					}
				}elseif($g_id_sup==0 && ($help_g_top[$i]["g_look"]==0 || $help_g_top[$i]["g_look"]==NULL)){
					$help_g_top[$i]["depth"] = 0;
					
					$g_numbering[$help_g_top[$i]["depth"]][]["id"] = $help_g_top[$i]["id"];
					
					$help_g_top[$i]["g_look"] = 1;
					$g_max_e_tmp = 1;
				}
			}
			
			if($help_g_top[$i]["depth"]>$g_max) $g_max = $help_g_top[$i]["depth"];
			
			if($g_max_e_tmp==0) $g_max_e = 0;			
			
		}
		
		
	}while ($g_max_e==0);
	//pridobitev max globine EOF
	
	
	function print_r_tree($data)
	{
	    // capture the output of print_r
	    $out = print_r($data, true);
	
	    // replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
	    $out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',"'\\1<a href=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'\\0'), 0, 7)).'\');\">\\2</a><div id=\"'.\$id.'\" style=\"display: none;\">'", $out);
	
	    // replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
	    $out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);
	
	    // print the javascript function toggleDisplay() and then the transformed output
	    echo '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n$out";
	}
	
	
	
	//print_r_tree($g_numbering);
	
	
	//echo '<br><br>'.$g_max;
	
	
	
	//pridobitev opisa...
	$help = $db->getArray("SELECT f.*, g.title, g.text, g.date_m, g.id_lang FROM ".$_REQUEST['table']." AS f, ".$_REQUEST['table']."Lang AS g WHERE f.id=g.id_sup AND g.id_lang=".$used_lang." ORDER BY f.`topic` ASC, f.`orderBy` ASC ");
	
	
	
	$pdf=new PDF_RM();
	//First page
	//$pdf->AddPage();
	
	$pdf->SetFont('Times', '', 18);
	
	
	$pdf->AddPage();
	
	
	$pdf->Cell(0, 5, 'Cover', 0, 1, 'C');
	
	
	$pdf->AddPage();	
	
	
	
	$pdf->startPageNums();
	
	$help_discover_arr = array();
	
	$help_title_numbering = array();
	
	
	
	for($j=0;$j<count($help);$j++){
		if(!in_array($help[$j]["id"], $help_discover_arr)){
			
			$tmp_g_arr = help_g_getId($help_g_top, $help[$j]["id"]);
			
			
			for($z=0;$z<count($g_numbering[$tmp_g_arr["depth"]]);$z++){
				
				if($g_numbering[$tmp_g_arr["depth"]][$z]["id"]==$help[$j]["id"]){
					$g_numbering[$tmp_g_arr["depth"]][$z]["number"] = $z;
					$tmp_g_numbering = $z;
					break;
				}
				
			}
			$tmp_title_num = "";
			if($help[$j]["id_sup"]==0){
				$tmp_title_num_i = count($help_title_numbering[$help[$j]["id_sup"]]);
				$help_title_numbering[$help[$j]["id_sup"]][$tmp_title_num_i]["id"] = $help[$j]["id"];
				$tmp_title_num = ($tmp_title_num_i+1).".";				
			}
			
						
			
			//$pdf->TOC_Entry($tmp_title_num." ".$help[$j]["title"].' - '.$help[$j]["id"].' - '.$help[$j]["id_sup"].' - '.$tmp_g_numbering.' - '.$tmp_g_arr["depth"], $tmp_g_arr["depth"]);
			
			if($tmp_g_arr["depth"]==0){
				$pdf->SetFont('Arial','',20);
				$pdf->SetTextColor(0,0,255);
			}elseif($tmp_g_arr["depth"]==1){				
				$pdf->SetTextColor(0,0,0);
				$pdf->SetFont('Times', 'I', 14);
			}else{			
				$pdf->SetTextColor(0,0,0);
				$pdf->SetFont('Times', '', 12);
			}
			
			
			$pdf->Cell(0, 10, $tmp_title_num." ".$help[$j]["title"], 0, 1, 'L');
			
				
			$pdf->SetTextColor(0,0,0);		
			$pdf->TOC_Entry($tmp_title_num." ".$help[$j]["title"], $tmp_g_arr["depth"]);
			
			$pdf->SetFont('Times', '', 12);
			$pdf->Cell(0, 5, strip_tags($help[$j]["text"]), 0, 1, 'L');
			
			//-------------------------------------------------------
			$help_discover_arr[] = $help[$j]["id"];
			
			help_g_getChild($help[$j]["id"]);
		}
		
	}
	
	$pdf->insertTOC(2);
	
	$pdf->Output();
	
}else{

	//pridobitev opisa...
	$help_res = $db->SQLexecute("SELECT f.*, g.title, g.text, g.date_m, g.id_lang FROM ".$_GET['table']." AS f, ".$_GET['table']."Lang AS g WHERE f.id=".$id." AND f.id=g.id_sup AND g.id_lang=".$used_lang." ORDER BY f.`orderBy` ASC ");
	$help = $db->fetchArray($help_res);
	
	
	$pdf=new PDF_RM();
	//First page
	$pdf->AddPage();
	
	
	
	$rule['main'] = '
		<b>'.$help["title"].'</b>
	';
	
	$export = $rule['main'];
	
	//echo $export;
	
	$pdf->SetLeftMargin(10);
	$pdf->SetFont('Arial','',20);
	$pdf->SetFontSize(26);
	$pdf->SetTextColor(0,0,255);
	$pdf->WriteHTML($export);
	
	
	
	$rule['main'] = '
		<br />
		<br />
		<i>'.strip_tags($help["text"]).'</i>
		<br />
	';
	
	$help_topic_res = $db->SQLexecute("SELECT f.*, g.title, g.text, g.date_m, g.id_lang FROM ".$_GET['table']." AS f, ".$_GET['table']."Lang AS g WHERE f.topic=1 AND f.id_sup=".$id." AND f.id=g.id_sup AND g.id_lang=".$used_lang." ORDER BY f.orderBy ASC");
	
	$rule['listSub'] = "";
	
	while($help_topic = $db->fetchArray($help_topic_res)){
		$rule['listSub'] .= '
			<br />
			<br />
			<br />
			<u><b>'.$help_topic["title"].'</b></u>
			<br />
			<br />
			<i>'.strip_tags($help_topic["text"]).'</i>
			<br />
		';
	}
	
	
	$export = $rule['main'];
	
	$export .= $rule['listSub'];
	
	//echo $export;
	
	$pdf->SetLeftMargin(15);
	$pdf->SetFont('Arial','',20);
	$pdf->SetFontSize(14);
	$pdf->SetTextColor(0,0,0);
	$pdf->WriteHTML($export);
	
	$pdf->Output();

	
}


?>