<?php
class CDController {

	protected $view;
	protected $data;
	private static $default_controller = 'Page';

	public static function init_controller($post_type, $params) {
		$folder = $post_type;
		$template = '';
		if( isset($params['template']) ){
			$template = $params['template'];
		}

		$controller = cd_get_controller_class($folder.'_'.$template);

		if( !class_exists ( $controller ) ){
			$controller = cd_get_controller_class($folder);
			if( !class_exists ( $controller ) ){
				$controller = self::$default_controller;
				if( !class_exists ( $controller ) ){
					echo 'El controlador default '.$controller.' NO existe';
				}
			}
		}

		if( isset($params['args']) ){
			$args = $params['args'];
		}else{
			$args = null;
		}

		$instance = new $controller($args);

		$instance->get_context();

		$instance->render();
	}

	public function get_context(){
		$this->data = CDTimber::get_context();
	}

	public function render() {
		Timber::render($this->view, $this->data );
	}

}
?>
