<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class admin_group_class extends ASK_MODEL
{
    
	/**
	 * User access to group permission
	 * @param unknown_type $group_id
	 */
	function get_permission_by_group_id($group_id)
	{		
		if (!$group = $this->fetch_row('admin_group', 'group_id = ' . intval($group_id)))
		{
			return false;
		}
		
		$data = array();
		
		foreach(explode(",", $group['permission']) as $key => $val)
		{
			$pri_arr = explode("->", $val);

			$data[$pri_arr[0]][] = $pri_arr[1];
		}
		
		return $data;
	}
	
	
	/**
	 * Get group visible part
	 * @param unknown_type $group_id
	 */
	function get_menu_list($group_id, $select_id = 0)
	{
		if (!$group = $this->fetch_row('admin_group', 'group_id = ' . intval($group_id)))
		{
			return false;
		}
		
		return $this->get_menu_by_ids($group['menu'], $group['no_menu'], $select_id);
	}
	
	/**
	 * Column id set according to get the list of columns
	 * @param unknown_type $ids
	 */
	public function get_menu_by_ids($in_ids, $noin_ids = null, $select_id = 0)
	{
		$menu_array = (array)ASK_APP::config()->get('admin_menu');
		
		if (empty($menu_array))
		{
			return false;
		}
		
		if ($in_ids != 'all')
		{
			$in_ids_arr = explode(',', $in_ids);
		}
		
		if ($noin_ids)
		{
			$noin_ids_arr = explode(',', $noin_ids);
		}
		
		foreach($menu_array as $m_id => $menu)
		{
			if ($menu['status'] == '-1')
			{
				unset($menu_array[$m_id]);
				continue;
			}
		
			if ((is_array($in_ids) && (!in_array($menu['id'], $in_ids))) || (is_array($noin_ids) && (in_array($menu['id'], $noin_ids))))
			{
				unset($menu_array[$m_id]);
				continue;
			}
				
			if ($menu['children'])
			{
				foreach($menu['children'] as $c_id => $c_menu)
				{
					if ($c_menu['status'] == '-1')
					{
						unset($menu_array[$m_id]['children'][$c_id]);
						continue;
					}
					
					if ((is_array($in_ids) && (!in_array($c_menu['id'], $in_ids))) || (is_array($noin_ids) && (in_array($c_menu['id'], $noin_ids))))
					{
						unset($menu_array[$m_id]['children'][$c_id]);
						continue;
					}
					
					if ($select_id == $c_menu['id'])
					{
						$menu_array[$m_id]['children'][$c_id]['select'] = true;
						$menu_array[$m_id]['select'] = true;
					}
				}
			}
		}
		
		return $menu_array;
	}
}
