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

/**
 * This class provides MODULE management
 *
 * @package    OSB
 * @category   Controllers/Admin
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Controller_Admin_Module_Method extends Controller_Admin_Module {
	/**
	 * Add a method to the database
	 */
	public function action_add() {
		$id = $this->request->param('id');
		$method = $this->request->param('sid');

		$mo = ORM::factory('Module',$id);
		$mm = $this->_methods($mo->name);

		if (! $mo->loaded() OR ! in_array($method,$mm['methods']))
			HTTP::redirect(URL::link('admin','module/list'));

		if ($_POST) {
			$mmo = $mo->module_method;
			$mmo->name = $method;
			$mmo->module_id = $mo->id;
			$mmo->values($_POST);

			if (! $mmo->check() OR ! $mmo->save())
				throw HTTP_Exception::factory(501,'Unable to save data :post',array(':post'=>serialize($_POST)));

			SystemMessage::factory()
				->title('Record added')
				->type('success')
				->body(_('Method record has been added.'));

			HTTP::redirect(URL::link('admin','/module/edit/'.$mo->id));
		}

		Block::factory()
			->title(sprintf(_('Add Method (%s) to Database for (%s)'),strtoupper($method),strtoupper($mo->name)))
			->title_icon('icon-plus-sign')
			->type('form-horizontal')
			->body(View::factory('module/method/admin/add')
				->set('name',$method)
				->set('o',$mo)
			);
	}

	/**
	 * Edit a Module Configuration
	 */
	public function action_edit() {
		$id = $this->request->param('id');
		$mmo = ORM::factory('Module_Method',$id);

		if (! $mmo->loaded()) {
			SystemMessage::factory()
				->title(_('Invalid Method ID'))
				->type('error')
				->body(sprintf(_('Method with ID %s doesnt appear to exist?'),$id));

			HTTP::redirect(URL::link('admin','module/list'));
		}

		if ($_POST) {
			$mmo->values($_POST);

			if (! $mmo->check() OR ! $mmo->save())
				throw HTTP_Exception::factory(501,'Unable to save data :post',array(':post'=>serialize($_POST)));

			foreach (ORM::factory('Group')->find_all() as $go) {
				// If the group was defined and no longer
				if ($mmo->has('group',$go) AND (! isset($_POST['groups']) OR ! in_array($go->id,$_POST['groups']))) {
					$gmo = ORM::factory('Group_Method',array('method_id'=>$mmo->id,'group_id'=>$go->id));

					if (! $gmo->delete())
						SystemMessage::factory()
							->title(_('Unable to DELETE Group Method'))
							->type('error')
							->body(sprintf(_('Unable to delete Group Method for method %s and group %s'),$mmo->name,$go->name));

				// If the group was not defined and now is
				} elseif (! $mmo->has('group',$go) AND isset($_POST['groups']) AND in_array($go->id,$_POST['groups'])) {
					$gmo = ORM::factory('Group_Method')
						->values(array(
							'method_id'=>$mmo->id,
							'group_id'=>$go->id,
						));

					if (! $gmo->check() OR ! $gmo->save())
						SystemMessage::factory()
							->title(_('Unable to SAVE Group Method'))
							->type('error')
							->body(sprintf(_('Unable to save Group Method for method %s and group %s'),$mmo->name,$go->name));
				}
			}
		}

		Block::factory()
			->title(sprintf(_('Configure access to method (%s::%s)'),$mmo->controller(),$mmo->method()))
			->title_icon('icon-plus-sign')
			->type('form')
			->body(View::factory('module/method/admin/edit')
				->set('o',$mmo)
			);
	}
}
?>