<?php


class download{

	//private $obj_menu;
	private $obj_graph;
	private $obj_mysql;
	private $arrCodici;
	private $arrFatCod;
	private $arrIdCat;
	private $arrPageParts;

	function __construct(){
	
		//if(!$_SESSION['id_clienti']) header('Location: '.LP().'login.html');
	
		// classi istanziate
		//$this->obj_menu = new menu_ordini();
		$this->obj_graph = new GRAPHICS_class();
		$this->obj_mysql = new MYSQL_class();
		$this->smista();
	}
	
	function smista(){	
			
		### ---> output pagina	
		// (1) ---> TOP BANNER INSIDE
		$this->arrPageParts['topBannerInside']='';
		
		// ---> NAVIGATOR INSIDE	
		$this->arrPageParts['navigatorInside']='';	
		
		// ---> MAIN CONTENT INSIDE
		$this->arrPageParts['mainContentInside'].=$this->mainContentInside();
		
		// ---> OTHER LEFTMENU INSIDE
		$this->arrPageParts['otherLeftMenuInside']='';
	}
	
	function mainContentInside()
	{
		$stringa="<link rel=\"stylesheet\" type=\"text/css\" href=\"".LP()."sezioni/mod_(C)_download/download.css?v=30042025\" />";

		$stringa.="<div class=\"blocco-titolo\">\r\n";
		$stringa.="<h1>AREA DOWNLOAD</h1>\r\n";
		$stringa.="</div>\r\n";

		$stringa.= $this->downloadArea("download/documenti");

		if($_SESSION['id_clienti'] && $_SESSION['mostra_prezzo'])
		{
			$stringa.="<div class=\"blocco-titolo\">\r\n";
			$stringa.="<h1>AREA DOWNLOAD AGENTI</h1>\r\n";
			$stringa.="</div>\r\n";

			$stringa.= $this->downloadArea("download/documenti_agenti","brown");
		}

		return $stringa;
	}


	function downloadArea($dirDown, $color="")
	{
		//$dir="./documenti/";
		if ($handle = opendir("./".$dirDown."/")) 
		{
			while (false !== ($entry = readdir($handle))) 
			{
				$ext = pathinfo($entry, PATHINFO_EXTENSION);
				if (substr($entry,0,1)!='.' && $ext == '')  // Tralascio tutti i file e le cartelle che iniziano con il punto e i file senza sottocartella
				{
					$arrFilesAllegati[]=array($entry,filesize("./".$dirDown."/".$entry));
				}
			}
			closedir($handle);
		}

		// lista files allegati collegati ad una categoria	
		$arrFilesAllegati = $this->riordinaAllegati($arrFilesAllegati);
		
		for($r=0;$r<count($arrFilesAllegati);$r++)
		{
			// TITOLO CATEGORIA FILE
        	$stringa.="<div class=\"categorie_lista_allegati\">";
            $stringa.="<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\" class=\"allegatiCategorieTable\">";
			$stringa.="<caption ".(($color)?"style=\"background-color:".$color."\"":"").">";
  			$stringa.=str_replace("Depliant","Brochures",$arrFilesAllegati[$r][0]);
         	$stringa.="</caption>";

	         // LEGGO I FILE DELLA SOTTO CARTELLA
	         unset($arrFilesAllegatiSotto);
	    		$dir="./".$dirDown."/".$arrFilesAllegati[$r][0]."/";

            if ($handle = opendir($dir)) 
            {
               while (false !== ($entry = readdir($handle))) 
               {
    		      if(is_file($dir.$entry))  // Tralascio tutte le sottocartelle
	              {
		             if (substr($entry,0,1)!='.')  // Tralascio tutti i file che iniziano con il punto
		             {
			            $arrFilesAllegatiSotto[]=array($entry,filesize($dir.$entry));
		             }
	              }
               }
               closedir($handle);
            }

         	// lista files allegati collegati ad una sottocartella	
         	$arrFilesAllegatiSotto = $this->riordinaAllegati($arrFilesAllegatiSotto);

         if ($arrFilesAllegati[$r][0] == "Cataloghi Settore" || $arrFilesAllegati[$r][0] == "Cataloghi" || $arrFilesAllegati[$r][0] == "Cataloghi Borin Line" || $arrFilesAllegati[$r][0] == "Brochures" || $arrFilesAllegati[$r][0] == "Depliant" || $arrFilesAllegati[$r][0] == "Portfolio")
			{
				// GESTIONE CATALOGHI
				$stringa.="<tr><td>";

				for($r2=0;$r2<count($arrFilesAllegatiSotto);$r2++)
      			{
	             	if(is_file($dir.$arrFilesAllegatiSotto[$r2][0]))
			        {
						// Cerco Catalogo Sfogliabile
						$UrlCatalogoSfogliabile = str_replace(".pdf", "/", $arrFilesAllegatiSotto[$r2][0])."index.html";
						$catalogoName = str_replace(".pdf", "", str_replace("_", " ", $arrFilesAllegatiSotto[$r2][0]));

						$copertinacatalogo = "/PDF/pdf.png";
						if (is_file($dir.$UrlCatalogoSfogliabile)) $copertinacatalogo =  str_replace("index.html", "/files/shot.png", $UrlCatalogoSfogliabile);

						if(is_file($dir.$UrlCatalogoSfogliabile))
						{
							$stringa.="<div class=\"ScaricaCatalogo\">";
							 $stringa.="<span class=\"titoloCatalogo\">".$catalogoName."</span>";
						    $stringa.="<a target=\"_blank\" href=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$UrlCatalogoSfogliabile."\" style=\"text-decoration: none;\">";
							  $stringa.="<img src=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$copertinacatalogo."\" />";
						    $stringa.="</a>";

							$stringa.="<a target=\"_blank\" href=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$UrlCatalogoSfogliabile."\" style=\"text-decoration: none;\">";
							$stringa.="<span class=\"downloadAllegatiSpanCatalogo\">";
							$stringa.="SFOGLIA";
							$stringa.="</span>";


						    $stringa.="<a target=\"_blank\" href=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$arrFilesAllegatiSotto[$r2][0]."\" style=\"text-decoration: none;\">";
							$stringa.="<span class=\"downloadAllegatiSpanCatalogo\">";
							$stringa.="DOWNLOAD";
							$stringa.="</span>";
						    $stringa.="</a>";

						    $stringa.="</div>";
						}
						else
						{
							$stringa.="<div class=\"ScaricaCatalogo\">";
						    $stringa.="<a href=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$arrFilesAllegatiSotto[$r2][0]."\" style=\"text-decoration: none;\">";
							$stringa.="<span class=\"downloadAllegatiSpan\" ";
							$stringa.=" >";
							$stringa.="<img src=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$copertinacatalogo."\" />";
							$stringa.="</span>";
						    $stringa.="</a>";
						    $stringa.="</div>";
						}
			        }
			    }
			    $stringa.="</td></tr>";
			}
			else
			{	
				// GESTIONE DOWNLOAD
		        $stringa.="<tr>";
			    $stringa.="<th class=\"th1_allegati\">Nome file</th>";
			    $stringa.="<th class=\"th2_allegati\">DIMENSIONE</th>";
			    $stringa.="<th class=\"th4_allegati\">URL</th>";
		        $stringa.="</tr>";

				for($r2=0;$r2<count($arrFilesAllegatiSotto);$r2++)
	      		{
	             	if(is_file($dir.$arrFilesAllegatiSotto[$r2][0]))
			        {
	                    $stringa.="<tr>";
		         			$stringa.="<td class=\"td1_allegati\">".$arrFilesAllegatiSotto[$r2][0]."</td>";
				           	$stringa.="<td class=\"td2_allegati\">".format_bytes($arrFilesAllegatiSotto[$r2][1])."</td>";
					        $stringa.="<td class=\"td4_allegati\">";
							    $stringa.="<a href=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$arrFilesAllegatiSotto[$r2][0]."\" target=\"_blank\" style=\"text-decoration: none;\">";
								$stringa.="<span class=\"downloadAllegatiSpan\" ";
								$stringa.=" >";
								$stringa.="DOWNLOAD";
								$stringa.="</span>";
								$stringa.="</a>";
							$stringa.="</td>";
			            $stringa.="</tr>";
					}
                }
			}
			$stringa.="<tr>&nbsp</tr>";
			$stringa.="</table>";
			$stringa.="</div>";
		}

		return $stringa;
	}
	
	function riordinaAllegati($arr)
	{
		for($r=0;$r<count($arr)-1;$r++)
		{
			for($r2=$r+1;$r2<count($arr);$r2++)
			{
				if($arr[$r]>$arr[$r2])  		// Ordine Alfabetico
				//if($arr[$r]<$arr[$r2])		// Ordine ANALfabetico
				{
					$arrPass=$arr[$r];
					$arr[$r]=$arr[$r2];
					$arr[$r2]=$arrPass;
				}
			}
		}
		return $arr;
	}	
	
	###########################################################################################################
	
	function topBanner(){
		$stringa=$this->arrPageParts['topBannerInside'];
		return $stringa;
	}
	
	function navigator(){
		$stringa=$this->arrPageParts['navigatorInside'];
		return $stringa;
	}
	
	function mainContent(){
		$stringa=$this->arrPageParts['mainContentInside'];
		return $stringa;
	}
	
	function otherLeftMenu(){
		$stringa=$this->arrPageParts['otherLeftMenuInside'];
		return $stringa;
	}

}



?>