<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides the default template controller for rendering pages.
 *
 * @package    PLA
 * @subpackage Page/Template
 * @category   Controllers
 * @author     Deon George
 * @copyright  (c) phpLDAPadmin Development Team
 * @license    http://dev.phpldapadmin.org/license.html
 */
abstract class PLA_Controller_Template extends Kohana_Controller_Template {
	// @var object meta object information as per [meta]
	private $meta;

	public function __construct(Request $request, Response $response) {
		$this->template = Kohana::$config->load('config')->theme;

		return parent::__construct($request,$response);
	}

	public function before() {
		// Do not template media files
		if ($this->request->action() === 'media') {
			$this->auto_render = FALSE;
			return;
		}

		parent::before();

		// For AJAX calls, we dont need to render the complete page.
		if ($this->request->is_ajax()) {
			$this->auto_render = FALSE;
			return;
		}

		$this->template->content = '';

		// Setup the page template
		$this->meta = new Meta;
		View::bind_global('meta',$this->meta);
	}

	public function after() {
		if ($this->auto_render === TRUE) {
			// Application Title
			$this->meta->title = Kohana::$config->load('config')->appname;

			// Language
			// @todo
			$this->meta->language = '';

			// Description
			$this->meta->description = sprintf('%s::%s',$this->request->controller(),$this->request->action());

			// Control Line
			// @todo
			$this->template->control = '';

			// System Messages line
			// @todo
			$this->template->sysmsg = '';

			// Left Item
			// @todo
			$this->template->left = '';
			$this->template->right = '';
			$this->template->center = '';

			if (! $this->response->body())
				$this->response->body((string)Block::factory());

			if (empty($this->template->content))
				$this->template->content = $this->response->body();

			// Footer
			// @todo
			$this->template->footer = '';

			// Our default script(s)
			foreach (array('file'=>array_reverse(array(
				))) as $type => $datas) {

				foreach ($datas as $data) {
					Script::add(array(
						'type'=>$type,
						'data'=>$data,
					),TRUE);
				}
			}

		// For any ajax rendered actions, we'll need to capture the content and put it in the response
		// @todo
		} elseif ($this->request->is_ajax() && isset($this->template->content) && ! $this->response->body()) {
			// @todo move this formatting to a view?
			if ($s = $this->_sysmsg() AND (string)$s)
				$this->response->body(sprintf('<table class="sysmsg"><tr><td>%s</td></tr></table>',$s));

			// Since we are ajax, we should re-render the breadcrumb
			Session::instance()->set('breadcrumb',(string)Breadcrumb::factory());
			$this->response->bodyadd(Script::add(array('type'=>'stdin','data'=>'$().ready($("#ajCONTROL").load("'.URL::site('welcome/breadcrumb').'",null,function(x,s,r) {}));')));

			// In case there any javascript for this render.
			$this->response->bodyadd(Script::factory());

			// Get the response body
			$this->response->bodyadd(sprintf('<table class="content"><tr><td>%s</td></tr></table>',$this->template->content));
		}

		parent::after();

		// Generate and check the ETag for this file
		if (Kohana::$environment === Kohana::PRODUCTION)
			$this->response->check_cache(NULL,$this->request);
	}
}