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

/**
 * This class provides MODULE management
 *
 * @package    lnApp
 * @subpackage Page/Module
 * @category   Controllers
 * @author     Deon George
 * @copyright  (c) 2010 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
class Controller_Admin_EmailTemplate extends Controller_TemplateDefault {
	public $secure_actions = array(
	);

	public function action_menu() {
	}

	/**
	 * List our defined email templates
	 */
	public function action_list() {
		$eto = ORM::factory('emailtemplate');
		$output = '';

		$output .= View::factory('admin/emailtemplate/list_header');
		foreach ($eto->find_all() as $et) {
			$output .= View::factory('admin/emailtemplate/list_body')
				->set('template',$et);
		}
		$output .= View::factory('admin/emailtemplate/list_footer');

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

	/**
	 * Add a template
	 */
	public function action_add() {
		$eto = ORM::factory('emailtemplate');
		$output = '';

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

			// Entry updated
			if ($eto->save()) {
				$x = $eto->emailtemplate_translate->values($_POST['translate']['new']);

				$x->email_template_id = $eto->id;
				if ($x->check())
					$x->save();
			}
		}

		$output .= Form::open();
		$output .= View::factory('admin/emailtemplate/add');
		$output .= View::factory('admin/emailtemplate/add_translate');
		$output .= '<div>'.Form::submit('submit',_('Add')).'</div>';
		$output .= Form::close();

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

	/**
	 * Edit Template Definition
	 */
	public function action_edit($id) {
		$eto = ORM::factory('emailtemplate',$id);

		if (! $eto->loaded())
			Request::current()->redirect('admin/emailtemplate/list');

		$output = '';

		if ($_POST AND $eto->values($_POST)->check()) {
			// Entry updated
			if ($eto->save()) {
				foreach ($_POST['translate'] as $id => $details) {
					$x = $eto->emailtemplate_translate->where('id','=',$id)->find();

					if ($x->values($details)->check())
						$x->save();
				}
			}
		}

		$output .= Form::open();

		$output .= View::factory('admin/emailtemplate/edit')
			->set('template',$eto);

		foreach ($eto->emailtemplate_translate->find_all() as $to) {
			$output .= View::factory('admin/emailtemplate/edit_translate')
				->set('translate',$to);

			SystemMessage::add(array(
				'title'=>_('Available Variables'),
				'type'=>'info',
				'body'=>sprintf('This template uses the following TEXT variables (%s) and HTML variables (%s)',
					implode('|',array_values($to->variables('message_text'))),
					implode('|',array_values($to->variables('message_html')))),
			));
		}

		$output .= '<div>'.Form::submit('submit',_('Update')).'</div>';
		$output .= Form::close();

		Editor::add();
		Block::add(array(
			'title'=>sprintf(_('Edit Template '),$eto->name),
			'body'=>$output,
		));
	}
}
?>