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

/**
 * This class provides some default methods for viewing TSM objects
 *
 * @package    PTA
 * @category   Controllers
 * @author     Deon George
 * @copyright  (c) 2010 phpTSMadmin Development Team
 * @license    http://phptsmadmin.sf.net/license.html
 */
abstract class Controller_TemplateDefault_View extends Controller_TemplateDefault {
	protected $index_title = '';
	protected $detail_title = '';
	protected $orm = '';

	/**
	 * Default Index for Controller
	 */
	public function action_index() {
		$o = ORM::factory($this->orm);

		$output = '';

		$output .= sprintf(_('This server has <b>%s</b> %s.'),$o->count_all(),$o->table_name());
		$output .= '<br/>';
		$output .= '<br/>';

		$select = array();
		$select[NULL] = '';

		foreach ($o->find_all() as $oo)
			$select[$oo->pk()] = $oo->pk();

		$output .= Form::open(sprintf('%s/detail',strtolower($this->request->controller())),array('id'=>'object_detail'));
		$output .= sprintf('%s: %s',_('Choose to view'),Form::select('object_name',$select,NULL,array('id'=>'object_name')));
		$output .= Form::submit('form_submit',_('Go'));
		$output .= Form::close();

		Block::add(array(
			'title'=>$this->index_title,
			'body'=>$output,
		));
	}

	public function action_detail() {
		$object_name = $this->request->param('id');

		if (is_null($object_name) AND (empty($_POST['object_name']) OR ! $object_name = $_POST['object_name'])) {
			SystemMessage::add(array(
				'title'=>_('Missing required data'),
				'type'=>'error',
				'body'=>sprintf(_('The %s is required.'),ORM::factory($this->orm)->primary_key()),
			));

			HTTP::redirect(strtolower($this->request->controller()));
		}

		$o = ORM::factory($this->orm,$object_name);

		if (! $o->loaded()) {
			SystemMessage::add(array(
				'title'=>_('Unknown Entry'),
				'type'=>'error',
				'body'=>sprintf(_('The %s [%s] does not exist?.'),$o->table_name(),$object_name),
			));

			HTTP::redirect(strtolower($this->request->controller()));
		}

		Block::add(array(
			'title'=>$this->detail_title,
			'body'=>View::factory(sprintf('%s/detail',strtolower($this->request->controller())))->set('o',$o),
		));
	}
}
?>
