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

/**
 * This class provides OSB exporting capabilities.
 *
 * @package    Export
 * @category   Controllers/Reseller
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Controller_Reseller_Export extends Controller_Export {
	protected $secure_actions = array(
		'export'=>TRUE,
		'index'=>TRUE,
		'list'=>TRUE,
	);

	/**
	 * Export plugins must define an export action.
	 */
	public function action_export() {
		$this->auto_render = FALSE;

		if (empty($_POST['export_module_id']) OR ! ($emo = ORM::factory('Export_Module',$_POST['export_module_id'])) OR ! $emo->loaded())
			HTTP::redirect(URL::link('reseller','export/index'));

		$emo->export->plugin($emo)->export($this->response);
	}

	/**
	 * Select our primary export target
	 */
	public function action_index() {
		$output = '';

		$output .= Form::open(URL::link('reseller','export/list'));
		$output .= Form::select('eid',ORM::factory('Export')->list_select());
		$output .= Form::button('submit','Submit',array('class'=>'btn btn-primary'));
		$output .= Form::hidden('days',$this->request->param('id'));
		$output .= Form::close();

		Block::factory()
			->title('Select Export Target')
			->title_icon('icon-share')
			->body($output);
	}

	/**
	 * This is the main call to export, providing a list of items to export and
	 * setting up the page to call the export plugin when submitted.
	 */
	public function action_list() {
		if (empty($_POST['eid']))
			HTTP::redirect(URL::link('reseller','export/index'));

		$eo = ORM::factory('Export',$_POST['eid']);

		if (! $eo->loaded())
			HTTP::redirect(URL::link('reseller','export/index'));

		$c = 0;

		$output = '<div class="tabbable span11">';

		$output .= '<ul class="nav nav-tabs">';
		// @todo To limit to Reseller accounts only
		foreach ($eo->export_module->find_all() as $emo)
			$output .= sprintf('<li class="%s"><a href="#tab%s" data-toggle="tab">%s</a></li>',$c++ ? '' : 'active',$emo->id,ucfirst($emo->module->instance()->object_name()));
		$output .= '</ul>';

		$c = 0;
		$output .= '<div class="tab-content">';
		foreach ($eo->export_module->find_all() as $emo) {
			$output .= sprintf('<div class="tab-pane %s" id="tab%s">',$c++ ? '' : 'active',$emo->id);

			$output .= Table::factory()
				->data($emo->list_export($this->request->post('days')))
				->jssort($emo->id)
				->columns(Arr::merge(array(
					'id'=>'ID',
					'account->name(TRUE)'=>'Account',
					'date_orig'=>'Date',
					'total(TRUE)'=>'Total',
					'exported'=>'Exported',
				),$emo->display ? $emo->display : array()))
				->select(URL::link('reseller','export/export'),$emo->id,array('export_module_id'=>$emo->id))
				->prepend(array(
					'id'=>array('checkbox'=>'id[]'),
				))->filters(array(
					'exported'=>array(array('Config::date',array(':value'))),
				));

			$output .= '</div>';
		}
		$output .= '</div>';

		Block::factory()
			->title('Export Transactions')
			->title_icon('icon-share')
			->body($output);
	}
}
?>
