<?php
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);
		
    
		if(file_exists(ClassConfig::getPathCustom("models/".$row['model'].".php"))){
  		$path = ClassConfig::getPathCustom("models/".$row['model'].".php");
    }
		elseif(file_exists(ClassConfig::getPathRoot("models/".$row['model'].".php"))){
  		$path = ClassConfig::getPathRoot("models/".$row['model'].".php");
    } else {
      $path = false;
    }
    define("FILENAME_MODEL", $path);
    
//    elseif(file_exists(ClassConfig::getPathRoot("models/".$row['model'].".php"))){
//  		define("FILENAME_MODEL" , ClassConfig::getPathRoot("models/".$row['model'].".php"));
//			require_once(FILENAME_MODEL);
//		}
		
    if(file_exists(FILENAME_MODEL)){
			require_once(FILENAME_MODEL);
    }
    else {
			//return false;
      //echo "Impossible de trouver le model ".$row['model'];
      echo "Impossible de trouver le model ".FILENAME_MODEL;
		}
		
		if(file_exists(ClassConfig::getPathCustom("themes/".$this->_graphicalTheme."/views/".$row['model'].".php"))){
  		$path = ClassConfig::getPathCustom("themes/".$this->_graphicalTheme."/views/".$row['model'].".php");
    }
		elseif(file_exists(ClassConfig::getPathRoot("themes/".$this->_graphicalTheme."/views/".$row['model'].".php"))){
  		$path = ClassConfig::getPathRoot("themes/".$this->_graphicalTheme."/views/".$row['model'].".php");
    } else {
      $path = false;
    }
    define("FILENAME_VIEW", $path);
		//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'];
	}
	
}

?>