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

/**
 * This class extends renders OSB menu tree.
 *
 * @package    lnApp
 * @subpackage Tree
 * @category   Controllers
 * @author     Deon George
 * @copyright  (c) 2010 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Controller_Tree extends lnApp_Controller_Tree {
	protected $auth_required = TRUE;

	/**
	 * Draw the Tree Menu
	 *
	 * The incoming ID is either a Branch B_x or a Node N_x
	 * Where X is actually the module.
	 *
	 * @param array $data Tree data passed in by inherited methods
	 */
	public function action_json(array $data=array()) {
		// Get the user details
		$id = (is_null($this->request->param('id')) AND isset($_REQUEST['id'])) ? substr($_REQUEST['id'],2) : $this->request->param('id');
		$user = Auth::instance()->get_user();

		if ($user) {
			if (! $id) {
				$modules = array();
				foreach ($user->groups() as $go)
					foreach ($go->list_parentgrps(TRUE) as $cgo)
						foreach ($cgo->module_method->find_all() as $mmo)
							if ($mmo->menu_display AND empty($modules[$mmo->module_id]))
								$modules[$mmo->module_id] = $mmo->module;

				Sort::MAsort($modules,'name');

				foreach ($modules as $id => $mo)
					if (! $mo->parent_id)
						array_push($data,array('id'=>$id,'name'=>$mo->name,'state'=>'closed'));

			} else {
				$idx = NULL;
				if (preg_match('/_/',$id))
					list($id,$idx) = explode('_',$id,2);

				$mo = ORM::factory('Module',$id);

				$methods = array();
				if ($mo->loaded()) {
					foreach ($mo->module_method->find_all() as $mmo)
						if ($mmo->menu_display)
							foreach ($mmo->group->find_all() as $gmo)
								if ($user->has_any('group',$gmo->list_childgrps(TRUE)))
									$methods[$mmo->id] = $mmo;

					Sort::MASort($modules,'name');

					$subdata = array();
					foreach ($methods as $id => $mmo) {
						if (preg_match('/_/',$mmo->name)) {
							list($mode,$action) = explode('_',$mmo->name);
							$url = URL::site(sprintf('%s/%s/%s',$mode,$mmo->module->name,$action));
						} else {
							$url = URL::site(sprintf('%s/%s',$mmo->module->name,$mmo->name));
						}

						// We can split our menus into sub menus using the _ char.
						if (preg_match('/_/',$mmo->name)) {
							list($sub,$name) = explode('_',$mmo->name,2);
							$subdata[$sub][$name]['name'] = preg_replace('/^(.*: )/','',$mmo->notes);
							$subdata[$sub][$name]['id'] = sprintf('%s_%s',$mmo->module_id,$id);
							$subdata[$sub][$name]['href'] = (empty($details['page']) ? $url : $details['page']);

						} else {
							// We dont want to show these items again, if we can through on a submenu
							if (! $idx)
								array_push($data,array(
									'id'=>sprintf('%s_%s',$mmo->module_id,$id),
									'name'=>$mmo->name,
									'state'=>'none',
									'attr_id'=>sprintf('%s_%s',$mmo->module->name,$id),
									'attr_href'=>(empty($details['page']) ? $url : $details['page'])
								));
						}
					}

					// If our sub menus only have 1 branch, then we'll display it as normal.
					if (count($subdata) == 1) {
						$sk = array_keys($subdata);
						$idx = array_shift($sk);
					}

					if ($idx)
						foreach ($subdata[$idx] as $k=>$v) {
							array_push($data,array(
								'id'=>$v['id'],
								'name'=>$v['name'],
								'state'=>'none',
								'attr_id'=>$v['id'],
								'attr_href'=>$v['href']
							));
						}

					else
						foreach ($subdata as $t=>$x)
							array_push($data,array('id'=>$mmo->module_id.'_'.$t,'name'=>$t,'state'=>'closed'));

				}
			}
		}

		$this->output = array();

		foreach ($data as $branch) {
			array_push($this->output,array(
				'attr'=>array('id'=>sprintf('B_%s',$branch['id'])),
				'state'=>$branch['state'],
				'data'=>array('title'=>$branch['name']),
				'attr'=>array('id'=>sprintf('N_%s',$branch['id']),'href'=>empty($branch['attr_href']) ? URL::site(sprintf('%s/menu',$branch['name'])) : $branch['attr_href']),
				)
			);
		}

		return parent::action_json($data);
	}
}
?>