<?php
interface InterfaceController {	
	public function getPage($graphical_theme);
}

class ClassController extends ClassConfig implements InterfaceController {
	private $_returnRequest;
	
	public function __construct($request){
		return $this->_returnRequest = $request;
	}
	
	public function getPage($graphical_theme, $lang_code='source'){
		$this->_graphicalTheme = $graphical_theme;
		$this->_langCode = $lang_code;
		$fileName = $this->_returnRequest['page'];
		$oPDOLink = ClassConfig::databaseConnect();
		
		// Select good model for each view with all languages
		$sql="
		SELECT model
		FROM core_feature
		WHERE url_feature_translation_id=(
				SELECT id
				FROM core_feature_translation
				WHERE (".$this->_langCode."=:url
						OR source=:url)
				LIMIT 1)
		LIMIT 1
		";
		$execSQL = $oPDOLink->prepare($sql);
		$execSQL->execute(array(':url'=>$this->_returnRequest['page']));
		$row = $execSQL->fetch(PDO::FETCH_ASSOC);
		
		define("FILENAME_MODEL" , "./models/".$row['model'].".php");
		if(file_exists(FILENAME_MODEL)){
			require_once(FILENAME_MODEL);
		} else {
			//return false;
      echo "Impossible de trouver le model ".$row['model'];
		}
		
		define("FILENAME_VIEW" , "./themes/".$this->_graphicalTheme."/views/".$row['model'].".php");
		if(file_exists(FILENAME_VIEW)){
			require_once(FILENAME_VIEW);
		} else {
			//return false;
      echo "Impossible de trouver la vue ".$row['model'];
		}
	}
	
	private function _getModel($page, $lang_code){
		// Select good model for each view with all languages
		$this->_page = $page;
		$oPDOLink = ClassConfig::databaseConnect();
		$sql="
		SELECT model
		FROM core_feature
		WHERE url_feature_translation_id=(SELECT id FROM core_feature_translation WHERE ".$this->_langCode."=:url LIMIT 1)
		LIMIT 1
		";
		$execSQL = $oPDOLink->prepare($sql);
		$execSQL->execute(array(':url'=>$this->_page));
		$row = $execSQL->fetch(PDO::FETCH_ASSOC);
		return $row['model'];
	}
	
	public function getMetaHTML($meta_tag, $lang_code){
		$this->_metaTag = $meta_tag;
		$this->_langCode = $lang_code;
		$page = $this->_returnRequest['page'];
		$model = $this->_getModel($page, $this->_langCode);
		
		$oPDOLink = ClassConfig::databaseConnect();
		$sql="
		SELECT (SELECT ".$this->_langCode." FROM core_feature_translation WHERE id=cf.".$this->_metaTag.") AS meta
		FROM core_feature cf
		WHERE cf.model=:model
		LIMIT 1;
		";
		$execSQL = $oPDOLink->prepare($sql);
		$execSQL->execute(array(':model'=>$model));
		$row = $execSQL->fetch(PDO::FETCH_ASSOC);
		return $row['meta'];
	}
	
}

?>
