<?php

class ControlleurFrontal {

	private $url_action;
	private $url_format;
	private $url_sortie;

	public function __construct($action, $format, $sortie)
	{
		$this->url_action = $action;
		$this->url_format = $format;
		$this->url_sortie = $sortie;
	}

	public function getRegistre()
	{
		return Registre::getRegistre();
	}

	public function parserAction($url)
    {
    	if (preg_match('/^(.+?)(?:_(.+)|)$/', $url, $match)) {
    		$aso_compo_classe = explode('-', $match[1]);
    		$retour['classe_action'] = 'GttCtrlAction';
    		foreach ($aso_compo_classe as $mot) {
	    		$retour['classe_action'] .= ucfirst($mot);
    		}
    	}

    	$retour['tab_actions'] = array('__defaut__');
    	if (isset($match[2])) {
    		preg_match_all('/(.+)(?:_|$)/', $match[2], $match_actions);
    		//echo '<pre>'.print_r($match_actions[1], true).'</pre>';
    		foreach ($match_actions[1] as $action) {
				$aso_compo_action = explode('-', $action);
				$action = '';
				foreach ($aso_compo_action as $mot_action) {
    				$action .= ucfirst($mot_action);
				}
				$retour['tab_actions'][] = $action;
    		}
    	}
		return $retour;
    }

	private function chargerActionGenerique(&$tab_actions)
    {
    	// Gestion de l'identification
    	$GttCtrlActionIdentification = new GttCtrlActionIdentification($this->getRegistre());
    	$GttCtrlActionIdentification->setSuivant('__defaut__');
   		array_unshift($tab_actions, $GttCtrlActionIdentification);

    	// Gestion des menus
    	$GttCtrlActionMenu = new GttCtrlActionMenu($this->getRegistre());
    	$GttCtrlActionMenu->setSuivant('__defaut__');
    	$tab_actions[] = $GttCtrlActionMenu;
    }


	public function executer()
    {
    	$tab_info_url = $this->parserAction($this->url_action);
    	//trigger_error(print_r($tab_info_url, true), E_USER_NOTICE);
    	$classe_action = $tab_info_url['classe_action'];
    	$fichier_action = GTT_CHEMIN_ACTION.$classe_action.'.class.php';
    	if (file_exists($fichier_action)) {
			require_once $fichier_action;
			$Action = new $classe_action($this->getRegistre());
			$this->chargerActionGenerique($tab_info_url['tab_actions']);
			$Action->setSuivant($tab_info_url['tab_actions']);
			$Action->demarrer();
			if ($this->url_format == 'html') {
				$aso_principal['principal'] = $this->rendre();
				//echo '<pre>'.print_r($aso_principal, true).'</pre>';
				$aso_principal['principal']['titre'] = $this->getRegistre()->getTitre();
				$this->getRegistre()->setEspaces(array());
				$this->getRegistre()->setDonnees(array());
				$this->getRegistre()->ajouterEspace('Principal', 'principal');
    			$this->getRegistre()->ajouterDonnee('principal', $aso_principal['principal']);
			}
    		$sortie = $this->rendre();

			// Gestion de la sortie
    		switch ($this->url_sortie) {
    			case 'html' :
    				echo $sortie;
    				break;
    			case 'csv' :
					header('Content-Disposition: inline' );
					header('Content-Type: text/plain');
					header('Content-Length: '.strlen($sortie));
    				echo $sortie;
    				break;
    			case 'csvt' :
					header('Content-Type: text/csv');
					header('Content-Disposition: attachment; filename="'.str_replace(' ', '_', $this->getRegistre()->getTitre()).'.csv";' );
					header('Content-Length: '.strlen($sortie));
    				echo $sortie;
    				break;
    		}
    		exit();
    	} else {
    		$m = "Le fichier $fichier_action contenant l'action est introuvable!";
    		trigger_error($m, E_USER_ERROR);
    	}
    }

    public function rendre()
    {
    	$contenu_principal = null;
    	$aso_contenu = array('zone_contenu' => '', 'zone_menu' => '', 'zone_identification' => '', 'zone_calendrier' => '');
    	foreach ($this->getRegistre()->getEspaces() as $espace_de_nom) {
    		if (is_array($this->getRegistre()->getDonnees($espace_de_nom))) {
    			$squelette = $espace_de_nom;
    			if (false != $this->getRegistre()->getSquelettes($espace_de_nom)) {
    				$squelette = $this->getRegistre()->getSquelettes($espace_de_nom);
    			}
    			$fichier_squelette = GTT_CHEMIN_PRESENTATION.$squelette.'.tpl.'.$this->url_format;
    			$squelette_erreur = $fichier_squelette;
    			if (file_exists($fichier_squelette)) {
		    		$bool_squelette_erreur = false;
		    		ob_start();
		    		extract($GLOBALS['_GTT_']['i18n']['general'], EXTR_PREFIX_ALL, 'i18n_general');
		    		extract($this->getRegistre()->getDonnees($espace_de_nom));
					include_once $fichier_squelette;
					if ($this->url_format == 'html') {
						// Répartition dans des zones
						switch($espace_de_nom) {
			    			case 'principal' :
			    				$contenu_principal .= ob_get_contents();
			    				break;
			    			case 'zone_calendrier' :
			    				$aso_contenu['zone_calendrier'] .= ob_get_contents();
			    				break;
			    			case 'identification' :
			    				$aso_contenu['zone_identification'] .= ob_get_contents();
			    				break;
			    			case 'zone_menu' :
			    				$aso_contenu['zone_menu'] .= ob_get_contents();
			    				break;
			    			default:
			    				$aso_contenu['zone_contenu'] .= ob_get_contents();
			    		}
					} else {
						$contenu_principal = ob_get_contents();
					}
		    		ob_end_clean();
    			}
    		}
    	}
    	if ($bool_squelette_erreur) {
    		trigger_error("Absence du fichier de squelette : $squelette_erreur", E_USER_ERROR);
 		}
    	if (!is_null($contenu_principal)) {
    		return $contenu_principal;
    	}
    	return $aso_contenu;
    }
}
?>