<?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)) {
			SystemMessage::factory()
				->title('SSL Certificate not updated')
				->type('error')
				->body('Either the Service doesnt exist, or you are not authorised to see it');

			HTTP::redirect('welcome');
		}

		$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 ($_POST) {
			// Entry updated
			if ($sco->values($_POST)->check()) {
				try {
					$sco->save();
					SystemMessage::factory()
						->title('Record updated')
						->type('success')
						->body(_('Your Charge record has been recorded/updated.'));

				} catch (ORM_Validation_Exception $e) {
					$errors = $e->errors('models');

					SystemMessage::factory()
						->title('Record NOT updated')
						->type('error')
						->body(join('<br/>',array_values($errors)));

					$sco->reload();
				}
			}
		}

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