<?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_Module extends Controller_TemplateDefault_Admin {
	protected $secure_actions = array(
		'add'=>TRUE,
		'edit'=>TRUE,
		'list'=>TRUE,
	);

	/**
	 * Get the list of methods for a class
	 */
	protected function _methods($class) {
		// Get a list of methods this module has
		$ch = 'Controller_%s';
		$methods = array();

		// List of classes where all our methods are, including this one.
		$classes = Kohana::config('config.method_directory');
		array_unshift($classes,'');

		foreach ($classes as $c) {
			$cn = sprintf($ch,$c ? $c.'_'.$class : $class);

			if (class_exists($cn)) {
				$r = new ReflectionClass($cn);

				foreach ($r->getMethods() as $method)
					if (preg_match('/^Controller_(.*_)?'.$class.'$/i',$method->class) AND ! preg_match('/^_/',$method->name))
						array_push($methods,str_replace('action_',($c ? $c.'_' : $c),$method->name));
			}
		}

		return $methods;
	}

	/**
	 * List our installed modules
	 */
	public function action_list() {
		$mo = ORM::factory('module');

		Block::add(array(
			'title'=>_('Defined Modules'),
			'body'=>Table::display(
				$mo->find_all(),
				25,
				array(
					'id'=>array('label'=>'ID','url'=>'admin/module/edit/'),
					'name'=>array('label'=>'Name'),
					'status'=>array('label'=>'Active'),
				),
				array(
					'page'=>TRUE,
					'type'=>'list',
				)),
			));
	}

	/**
	 * Edit a Module Configuration
	 *
	 * @todo Highlight those methods that have security, but the class does not have auth_required set to YES or the method isnt defined in secure_actions
	 */
	public function action_edit() {
		$mid = $this->request->param('id');
		$mo = ORM::factory('module',$mid);

		if (! $mo->loaded()) {
			SystemMessage::add(array(
				'title'=>_('Invalid Module ID'),
				'type'=>'error',
				'body'=>sprintf(_('Module with ID %s doesnt appear to exist?'),$mid),
			));

			return;
		}

		$output = '';
		$methods = $this->_methods($mo->name);

		// Show methods defined in the DB already.
		Block::add(array(
			'title'=>sprintf('%s: %s ',_('Defined Module Methods For'),$mo->display('name')),
			'body'=>Table::display(
				$mo->module_method->find_all(),
				25,
				array(
					'id'=>array('label'=>'ID','url'=>'admin/module_method/edit/'),
					'name'=>array('label'=>'Name'),
					'notes'=>array('label'=>'Notes'),
					'menu_display'=>array('label'=>'Menu'),
				),
				array(
					'page'=>TRUE,
					'type'=>'list',
				)),
			));

		// Show new methods NOT defined in the DB already.
		foreach ($mo->module_method->find_all() as $meo)
			if (($method = array_search($meo->name,$methods)) !== false)
				unset($methods[$method]);

		if (count($methods))
			Block::add(array(
				'title'=>sprintf('%s: %s ',_('Undefined Module Methods For'),$mo->display('name')),
				'body'=>Table::display(
					$methods,
					25,
					array(
						'__VALUE__'=>array('label'=>'Name','url'=>sprintf('admin/module_method/add/%s/',$mo->id)),
					),
					array(
						'page'=>TRUE,
						'type'=>'list',
					)),
				));
	}
}
?>
