<?php
class Page{
	
	var $siteName = 'Servizi Cittadino';
	var $pageName;
	var $pageDescr;
	var $showNavbar = true;
	var $mainMenu;
	var $js = '';
	var $css = '';
	var $v = 0.9;
	var $headerTitle;
	var $brand_url;
	
	function __construct($pageName){
		
		//pagina ente?
		if($_GET['e']){
			
			$this->brand_url = BASE_URL . ( $_GET['e'] && $_SESSION['user']['grp'] ? '/home/'.$_SESSION['user']['grp'].'/' : '/');
			
			$result = $GLOBALS['mysqli']->query("SELECT * FROM enti WHERE ente_url =".dbClean($_GET['e']));
			if($result->num_rows == 0){
				header("Location: ".BASE_GLOBAL_URL."/errore.html");
				die();
			}else{
				$row = $result->fetch_assoc();
				
				//se sono loggato, controllo di essere nell'ente di mia competenza
				if($_SESSION['user']['logged'] === true && $row['ente_id'] <> $_SESSION['user']['ente_id']){
					header("Location: ".BASE_URL."/home/".$_SESSION['user']['grp']);
					die();
				}
 				//layout ente
				$this->siteName .= ' - '.$row['ente_anagr'];
				$this->headerTitle = $row['ente_anagr'].'<br /><span>Servizi Cittadino</span>';
			}
		}else{
			$this->brand_url = BASE_GLOBAL_URL;
			$this->headerTitle = '<h1>Servizi Cittadino</h1>';
		}
		
		$this->pageName = $pageName;
	}
	
	
	function printHeader(){

		//default page header
		echo '
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
'.$this->pageDescr.'
<title>'.$this->siteName. ( $this->pageName ? ' | ' . $this->pageName : '' ) .'</title>
<link href="'.BASE_GLOBAL_URL.'/css/bootstrap.min.css?v='.$this->v.'" rel="stylesheet">
<link href="'.BASE_GLOBAL_URL.'/css/style.css?v='.$this->v.'" rel="stylesheet">
<link href="'.BASE_GLOBAL_URL.'/css/my.bootstrap.css?v='.$this->v.'" rel="stylesheet">

'.$this->css.'
<!--[if lt IE 9]>
  <script src="'.BASE_GLOBAL_URL.'/js/html5shiv.js"></script>
  <script src="'.BASE_GLOBAL_URL.'/js/respond.min.js"></script>
<![endif]-->
<script src="'.BASE_GLOBAL_URL.'/js/jquery-1.11.0.min.js"></script>
<script src="'.BASE_GLOBAL_URL.'/js/bootstrap.min.js?v='.$this->v.'"></script>
<script src="'.BASE_GLOBAL_URL.'/js/iscroll-lite.js?v='.$this->v.'"></script>
<script src="'.BASE_GLOBAL_URL.'/js/bootstrap-switch.min.js?v='.$this->v.'"></script>
<script src="'.BASE_GLOBAL_URL.'/js/selectivizr-min.js?v='.$this->v.'"></script>
<script src="'.BASE_GLOBAL_URL.'/js/init.js?v='.$this->v.'"></script>
<script src="'.BASE_GLOBAL_URL.'/js/script.js?v='.$this->v.'"></script>
'.$this->js.'
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
		<div class="navbar-header">
            <button type="button" class="navbar-toggle'. (!$this->mainMenu ? ' hidden' : '') .'" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand txt-overflow" href="'. $this->brand_url .'">'. $this->headerTitle .'</a>
        </div>
		<div class="navbar-collapse collapse"><ul class="nav navbar-nav navbar-right">'.$this->mainMenu.'</ul></div>
	</div>
</div>
<div class="container">';
	}

	
	function printFooter(){
		echo '
</div>

<div id="footer">
  <div class="container text-center">
	Servizi Cittadino - Sviluppato da <a href="http://www.progettoinformatica.com/" target="_blank">Progetto Informatica s.a.s.</a>
  </div>
</div>
</body>
</html>';
	}
	
	function setPageDescr($str){
		$this->pageDescr = '<meta name="description" content="'.$str.'">';
	}
	function addJs($url){
		$this->js .= '<script src="'.BASE_GLOBAL_URL.'/js/'.$url.'.js?v='.$this->v.'"></script>';
	}
	function addExtJs($url){
		$this->js .= '<script src="'.$url.'"></script>';
	}	
	function addModuleJs($url,$module_name=false){
		$module = $module_name ? $module_name : $GLOBALS['incl_module'];
		$this->js .= '<script src="'.BASE_GLOBAL_URL.'/includes/modules/'.$module.'/js/'.$url.'.js?v='.$this->v.'"></script>';
	}
	
	function addCss($url){
		$this->css .= '<link href="'.BASE_GLOBAL_URL.'/css/'.$url.'.css?v='.$this->v.'" rel="stylesheet">';
	}
	function addModuleCss($url,$module_name=false){
		$module = $module_name ? $module_name : $GLOBALS['incl_module'];
		$this->css .= '<link href="'.BASE_GLOBAL_URL.'/includes/modules/'.$module.'/css/'.$url.'.css?v='.$this->v.'" rel="stylesheet">';
	}
	function addMenu($menuType){
		
		if($_GET['e'])
			$mainMenu['public'] = array('Home'=>$this->brand_url);
		else
			$mainMenu['public'] = array('Home'=>$this->brand_url, 'Servizi'=>'/servizi.html', 'Informazioni e Contatti'=>'contatti.html');
			
		$mainMenu['logged'] = array();
		
		switch($_SESSION['user']['grp']){
			
			//administrator
			case 'adm':
				$mainMenu['logged'] = array(
					'Anagrafiche'=>BASE_URL.'/anagrafiche/adm/',
					'Utenti'=>array(
						'Iscritti'=>BASE_URL.'/users/adm/',
						'Personale addetto'=>BASE_URL.'/personale/adm/',
						'Logs utenti'=>BASE_URL.'/logs/adm/',
						)
				);
				foreach(get_ente_servizi() as $v) //servizi per cui e' abilitato il comune
					$mainMenu['logged']['Servizi'][$v['serv_name']] = BASE_URL.'/serv_'.$v['serv_cod'].'/adm/';
				
				$mainMenu['logged']['Servizi']['<hr>'] = 'javascript:;';
				$mainMenu['logged']['Servizi']['Imposta anno corrente ('.$_SESSION['user']['ente_anno'].')'] = 'javascript:dialog(\'/servizi/diag/anno/\',1);';
				
				$mainMenu['logged']['Comunica'] = array(
					'Invio SMS'=>BASE_URL.'/sms/adm/',
					'Scarica indirizzi e-mail'=>BASE_URL.'/email/adm/'
				);
				
				break;
		}		
		
		
		$current_page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
			
		foreach($mainMenu[$menuType] as $label => $url){
		
			if(!is_array($url)){
				
				$this->mainMenu .= '<li><a href="'.$url.'">'.$label.'</a></li>';
			
			//dropdown menu
			}else{
				
				foreach($url as $k=>$v)
					$links .= '<li><a href="'.$v.'">'.$k.'</a></li>';
				
				$this->mainMenu .= '
				<li class="dropdown">
					<a href="#" class="dropdown-toggle" data-toggle="dropdown">'.$label.' <b class="caret"></b></a>
					<ul class="dropdown-menu">'.$links.'</ul>
				</li>';

				unset($links);
			}
		}
		
		//account
		if($_SESSION['user']['logged'] && $menuType == 'logged'){

			//pulsante home
			if($_SESSION['user']['grp'] <> 'adm')
				$this->mainMenu = '<li><a href="'.BASE_URL.'/home/'.$_SESSION['user']['grp'].'/">Pagina iniziale</a></li>'.$this->mainMenu;
			
			//utente puo modificare i propri dati
			if($_SESSION['user']['grp'] == 'usr'){
				$li = '<li><a href="#" data-open="modal" data-url="/users/diag/user_show/">Modifica i tuoi dati</a></li>';
                
			}
			
			$this->mainMenu .= '
			<li class="dropdown">
				<a href="#" class="dropdown-toggle user-menu" data-toggle="dropdown"><span>&#160;</span></a>
				<ul class="dropdown-menu">
					<li role="presentation" class="dropdown-header">'.$_SESSION['user']['nome'].'</li>		
					'.$li.'
					<li><a href="#" data-open="modal" data-url="/home/diag/opt_pass/" data-small="1">Cambia password</a></li>			
					<li><a href="'.BASE_URL.'/?logout">Esci</a></li>			
				</ul>
			</li>';
		}
		
	}
}
?>