<?php class TemplateController{ 
  private $registry;
  
  public function __construct($registry) {
      $this->registry = $registry;
  } 
  
  public function getTemplateRoute($templateRoute){
    $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$templateRoute);
    
    if ($this->registry->get('config')->get('config_folder_template') == null){
      if(defined('PREFIX_NAME') && file_exists(DIR_TEMPLATE .'specific/'.PREFIX_NAME.'/'. $route . '.twig')){
        return  'specific/'.PREFIX_NAME.'/'.$route . '.twig';
      }else if(file_exists(DIR_TEMPLATE . $route . '.twig')){
        return  $route . '.twig';
      }else{
        return  $route . '.tpl';
      }
    }


    if( defined('PREFIX_NAME') && file_exists($this->registry->get('config')->get('config_folder_template') .'specific/'.PREFIX_NAME.'/'. $route . '.twig')){
      return  $this->registry->get('config')->get('config_template') . '//template//specific//'.PREFIX_NAME.'//' . $route . '.twig';
    }else if(defined('PREFIX_NAME') && file_exists($this->registry->get('config')->get('config_folder_template') . 'specific/'.PREFIX_NAME.'/'. $route . '.tpl')){
      return  $this->registry->get('config')->get('config_template') . '//template//specific//'.PREFIX_NAME.'//' . $route . '.tpl';
    }else if(file_exists($this->registry->get('config')->get('config_folder_template') . $route . '.twig')){
      return  $this->registry->get('config')->get('config_template') . '//template//' . $route . '.twig';
    }else{
       if(file_exists($this->registry->get('config')->get('config_folder_template') . $route . '.tpl')){
         return  $this->registry->get('config')->get('config_template') . '//template//' . $route . '.tpl'; 
       }else{
          return  'default/template/' . $route . '.tpl';
       }
    }     
  }
  
  public function loadOutPut($ptemplate, $data){    
    $path_parts = pathinfo(DIR_TEMPLATE . $ptemplate);
    
    if(strtolower($path_parts['extension']) == 'tpl'){
      $template 			= new Template($this->registry);
      $template->data = $data;
      return $template->fetch($ptemplate);
    } else {
      $template 			= new Twig($this->registry);
      $template->data = $data; 
      return $template->fetch($ptemplate);
    }	     	
  }
  
  
}