<?php defined('SYSPATH') or die('No direct access allowed.');
/**
 * This class provides Host Server functions
 *
 * @package    OSB
 * @subpackage HostServer
 * @category   Controllers/Admin
 * @author     Deon George
 * @copyright  (c) 2010 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
class Controller_Admin_Host extends Controller_TemplateDefault_Admin {
	protected $secure_actions = array(
		'ajaxmanage'=>TRUE,
		'list'=>TRUE,
		'update'=>TRUE,
	);

	public function action_ajaxmanage() {
		$this->auto_render = FALSE;

		$hso = ORM::factory('Host_Server',$this->request->param('id'));
		$k = Session::instance()->get_once('manage_button');

		$o = array(
			'u'=>$hso->manage_username ? $hso->manage_username : strtolower($hso->name),
			'p'=>(! $k OR ! $this->request->is_ajax() OR ! $hso->loaded() OR ! isset($_REQUEST['k']) OR $k != $_REQUEST['k']) ? Random::char() : $hso->manage_password,
		);

		$this->response->headers('Content-Type','application/json');
		$this->response->body(json_encode($o));
	}

	/**
	 * Show a list of hosting servers
	 */
	public function action_list() {
		Block::add(array(
			'title'=>_('Customer Services'),
			'body'=>Table::display(
				ORM::factory('Host_Server')->find_all(),
				25,
				array(
					'id'=>array('label'=>'ID','url'=>'admin/host/update/'),
					'name'=>array('label'=>'Details'),
				),
				array(
					'page'=>TRUE,
					'type'=>'select',
					'form'=>'admin/host/update/',
			)),
		));
	}

	public function action_update() {
		$hso = ORM::factory('Host_Server',$this->request->param('id'));
		$output = '';

		if (! $hso->loaded())
			HTTP::redirect('welcome/index');

		if ($_POST) {
			$hso->values($_POST);

			if ($hso->changed() AND ! $hso->save())
				throw new Kohana_Exception('Unable to save record?');
		}

		$output .= View::factory($this->viewpath())
			->set('hso',$hso)
			->set('plugin_form',$hso->admin_update());

		Block::add(array(
			'title'=>sprintf('%s %s:%s',_('Update Host Server'),$hso->id,$hso->name),
			'body'=>$output,
		));
	}
}
?>