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

/**
 * This class provides Reseller SSL functions
 *
 * @package    SSL
 * @category   Controllers/Reseller
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Controller_Reseller_SSL extends Controller_SSL {
	protected $secure_actions = array(
		'add'=>TRUE,
		'edit'=>TRUE,
		'list'=>TRUE,
		'renew'=>TRUE,
		'listchildca'=>TRUE,
		'listchildcrt'=>TRUE,
	);

	public function action_list() {
		Block::factory()
			->title('SSL CA Certificates')
			->title_icon('icon-th-list')
			->body(Table::factory()
				->jssort('ca')
				->data(ORM::factory('SSL_CA')->where_authorised($this->ao)->find_all())
				->columns(array(
					'id'=>'ID',
					'sign_cert'=>'Cert',
					'valid_to(TRUE)'=>'Expires',
					'validParent(TRUE)'=>'Valid',
					'childca(TRUE)'=>'cCA',
					'childcrt(TRUE)'=>'Crts',
					'issuer()'=>'Issuer',
				))
				->prepend(array(
					'id'=>array('url'=>URL::link('reseller','ssl/edit/')),
					'childca(TRUE)'=>array('url_resolve'=>URL::link('reseller','ssl/listchildca/%id%')),
					'childcrt(TRUE)'=>array('url_resolve'=>URL::link('reseller','ssl/listchildcrt/%id%')),
				))
			);
	}

	public function action_listchildca() {
		list($id,$output) = Table::page(__METHOD__);
		$sco = ORM::factory('SSL_CA',$id);

		if ($sco->childca())
			Block::factory()
				->title(sprintf('SSL CA Certificates for CA: %s',$sco->dn()))
				->title_icon('icon-th-list')
				->body(Table::factory()
					->jssort('ca')
					->data($sco->where_authorised($this->ao)->list_childca())
					->columns(array(
						'id'=>'ID',
						'sign_cert'=>'Cert',
						'ski()'=>'Identifier',
						'valid_to(TRUE)'=>'Expires',
						'validParent(TRUE)'=>'Valid',
						'childca()'=>'cCA',
						'childcrt()'=>'Crts',
					))
					->prepend(array(
						'id'=>array('url'=>URL::link('reseller','ssl/edit/')),
						'childca()'=>array('url_resolve'=>URL::link('reseller','ssl/listchildca/%id%')),
						'childcrt()'=>array('url_resolve'=>URL::link('reseller','ssl/listchildcrt/%id%')),
					))
				);

		if ($sco->childcrt())
			$this->action_listchildcrt();
	}

	public function action_listchildcrt() {
		list($id,$output) = Table::page(__METHOD__);
		$sco = ORM::factory('SSL_CA',$id);

		Block::factory()
			->title(sprintf('SSL Certificates for CA: %s',$sco->dn()))
			->title_icon('icon-th-list')
			->body(Table::factory()
				->jssort('crt')
				->data($sco->where_authorised($this->ao)->list_childcrt())
				->columns(array(
					'id'=>'ID',
					'plugin()->dn()'=>'Cert',
					'plugin()->valid_to(TRUE)'=>'Expires',
				))
				->prepend(array(
					'id'=>array('url'=>URL::link('user','service/view/')),
				))
			);
	}

	public function action_add() {
		Block::factory()
			->type('form-horizontal')
			->title('Add/View SSL CA')
			->title_icon('icon-wrench')
			->body($this->add_edit());
	}

	public function action_edit() {
		list($id,$output) = Table::page(__METHOD__);

		Block::factory()
			->type('form-horizontal')
			->title(sprintf('%s: %s',_('Add/View SSL CA'),$id))
			->title_icon('icon-wrench')
			->body($this->add_edit($id,$output));
	}

	public function action_renew() {
		$so = ORM::factory('Service',Request::current()->param('id'));

		if (! $so->loaded() OR ! Auth::instance()->authorised($so->account))
			throw HTTP_Exception::factory(403,'Service either doesnt exist, or you are not authorised to see it');

		$so->plugin()->renew();

		HTTP::redirect(URL::link('user','service/view/'.$so->id));
	}

	private function add_edit($id=NULL,$output='') {
		$sco = ORM::factory('SSL_CA',$id);

		if ($this->request->post()) {
			if (! $sco->account_id)
				$sco->account_id = (string)Auth::instance()->get_user();

			// Set our values, so that our filters have data
			$sco->values($this->request->post());
			// To trigger our filter to get the correct parent
			$sco->parent_ssl_ca_id = -1;

			if ($sco->changed() AND ! $this->save($sco))
				$sco->reload();
		}

		return View::factory('ssl/reseller/add_edit')
			->set('o',$sco);
	}
}
?>
