<?php


class download{

	private $obj_menu;
	private $obj_graph;
	private $obj_mysql;
	private $arrCodici;
	private $arrFatCod;
	private $arrIdCat;

	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 setThisSection(){
		
		$arr = $this->obj_menu->getArrMenu();
		// indici: 'label', 'sez' e 'link'
		$this->arrMenu=$arr;
		
	}
		
	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_download/download.css\" />";

		$stringa.="<div class=\"blocco-titolo\">\r\n";
		$stringa.="<h1>AREA DOWNLOAD</h1>\r\n";
		$stringa.="</div>\r\n";


		$stringa.="<div class=\"allegati\" id=\"tabsAllegati\">";

			// Carico le TAB
			$stringa.="<ul>\r\n";
				$stringa.= $this->sezioniArea("documenti");
				if($_SESSION['id_clienti'] && $_SESSION['mostra_prezzo']) 
					$stringa.= $this->sezioniArea("documenti_agenti", "#edc");
			$stringa.="</ul>\r\n";

			// Carico i contenuti
			$stringa.= $this->downloadArea("documenti");
			if($_SESSION['id_clienti'] && $_SESSION['mostra_prezzo']) 
				$stringa.= $this->downloadArea("documenti_agenti","#edc", "AGENTI");

		$stringa.="</div>\r\n";

		$stringa.="
			<script>
				$( function() {
					$('#tabsAllegati').tabs();
				} );
			</script>
		";

		return $stringa;
	}


	function sezioniArea($dirDown, $color="", $group="")
	{
		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++)
		{
			$stringa.="<li ".(($color)?"style=\"background-color:".$color."\"":"")."><a href=\"#".$dirDown.$r."\">".$arrFilesAllegati[$r][0]."</a></li>";
		}

		return $stringa;
	}

	function downloadArea($dirDown, $color="", $group="")
	{
		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++)
		{
			$stringa.="<div id=\"".$dirDown.$r."\">";
				$stringa.= "<p>";

	         // 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" || $arrFilesAllegati[$r][0] == "Brochures")
				{
					// GESTIONE CATALOGHI

					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";

							$copertinacatalogo = "/PDF/pdf.png";
							if (is_file($dir.$UrlCatalogoSfogliabile)) $copertinacatalogo =  str_replace("index.html", "/files/shot.png", $UrlCatalogoSfogliabile);

							if(is_file($dir.$UrlCatalogoSfogliabile))
							{
								$stringa.="<span class=\"ScaricaCatalogo\">";
							    $stringa.="<a href=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$UrlCatalogoSfogliabile."\" style=\"text-decoration: none;\">";
								$stringa.="<img src=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$copertinacatalogo."\" />";
							    $stringa.="</a>";

								$stringa.="<a href=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$UrlCatalogoSfogliabile."\" style=\"text-decoration: none;\">";
								$stringa.="<span class=\"downloadAllegatiSpanCatalogo\">";
								$stringa.="SFOGLIA";
								$stringa.="</span>";


							    $stringa.="<a href=\"".LP()."".$dirDown."/".$arrFilesAllegati[$r][0]."/".$arrFilesAllegatiSotto[$r2][0]."\" style=\"text-decoration: none;\">";
								$stringa.="<span class=\"downloadAllegatiSpanCatalogo\">";
								$stringa.="DOWNLOAD";
								$stringa.="</span>";
							    $stringa.="</a>";

							    $stringa.="</span>";
							}
							else
							{
								$stringa.="<span 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.="</span>";
							}
				        }
				    }
				}
				else
				{	
					// GESTIONE DOWNLOAD
	            $stringa.="<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\" class=\"allegatiCategorieTable\">";
			        	$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.="</table>";

				}
					$stringa.="<div class=\"clearBoth\"></div>";
				$stringa.= "</p>";

			$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])
				{
					$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;
	}

}



?>