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

/**
 * This class provides Admin Email management
 *
 * @package    Email
 * @category   Controllers/Admin
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Controller_Admin_Email extends Controller_Email {
	protected $secure_actions = array(
		'ajaxtemplatetranslate'=>TRUE,
		'templateadd'=>TRUE,
		'templateedit'=>TRUE,
		'templatelist'=>TRUE,
	);

	public function action_ajaxtemplatetranslate() {
		$eto = ORM::factory('Email_Template',$this->request->param('id'));

		if (! $eto->loaded() OR ! isset($_REQUEST['key'])) {
			$output = _('Unable to find translate data');

		} else {
			$eto = $eto->translate->where('language_id','=',$_REQUEST['key'])->find();

			$output = View::factory('email/admin/ajaxtemplatetranslate')
				->set('o',$eto);
		}

		$this->template->content = $output;
	}

	/**
	 * Add a template
	 */
	public function action_templateadd() {
		Block::factory()
			->type('form-horizontal')
			->title('Add Email Template')
			->title_icon('icon-wrench')
			->body($this->add_edit_template());
	}

	/**
	 * List our defined email templates
	 */
	public function action_templatelist() {
		Block::factory()
			->title(_('System Emails Templates Available'))
			->title_icon('icon-th')
			->body(Table::factory()
				->page_items(25)
				->data(ORM::factory('Email_Template')->find_all())
				->columns(array(
					'id'=>'ID',
					'name'=>'Name',
					'status'=>'Active',
					'description'=>'Descrption',
				))
				->prepend(array(
					'id'=>array('url'=>URL::link('admin','email/templateedit/')),
				))
			);
	}

	/**
	 * Edit Template Definition
	 * @todo Change this into an add_view function like payment()
	 */
	public function action_templateedit() {
		list($id,$output) = Table::page(__METHOD__);

		Block::factory()
			->type('form-horizontal')
			->title('Update Email Template')
			->title_icon('icon-wrench')
			->body($this->add_edit_template($id,$output));
	}

	private function add_edit_template($id=NULL,$output='') {
		$eto = ORM::factory('Email_Template',$id);

		if ($_POST) {
			// @todo To update the setup ID
			$eto->email_setup_id = '1';

			if (! $this->save($eto))
				$eto->reload();
		}

		// @todo With tinymce, if the user reselects a different language, we loose the editor?
		Script::factory()
			->type('stdin')
			->data('
$(document).ready(function() {
	$("select[name=language_id]").change(function() {
		// If we select a blank, then dont continue
		if (this.value == 0)
			return false;
		// Send the request and update sub category dropdown
		$.ajax({
			type: "GET",
			data: "key="+$(this).val(),
			dataType: "html",
			cache: false,
			url: "'.URL::link('admin','email/ajaxtemplatetranslate/'.$eto->id,TRUE).'",
			timeout: 2000,
			error: function(x) {
				alert("Failed to submit");
			},
			success: function(data) {
				$("div[id=translate]").empty().append(data);
			}
		});
	});
});
			');

		return View::factory('email/admin/add_edit_template')
			->set('o',$eto);

		$output .= Form::open();
		$output .= View::factory($this->viewpath());
		$output .= View::factory($this->viewpath().'/translate');
		$output .= '<div>'.Form::submit('submit',_('Add'),array('class'=>'form_button')).'</div>';
		$output .= Form::close();

		Block::add(array(
			'title'=>_('Available Email Templates'),
			'body'=>$output,
		));
	}
}
?>
