$mainframe->redirect('index.php?option=com_community_acl&mode=' . $mode . '&task=show_access&cid[]=' . $cid, $msg);
    } else {
        $mainframe->redirect('index.php?option=com_community_acl&task=' . $redirect, $msg);
    }
}
function showAccess($group_id = 0, $role_id = 0)
{
    global $mainframe, $option;
    $db =& JFactory::getDBO();
    $lists = array();
    $query = 'SELECT a.value, b.title, b.published' . ' FROM #__community_acl_access AS a' . ' LEFT JOIN #__sections AS b ON b.id = a.value' . ' WHERE a.option = \'com_sections\' AND a.name = \'cid\'' . ' AND a.group_id = ' . $group_id . ' AND a.role_id = ' . $role_id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['sections'] = $db->loadObjectList();
    $query = 'SELECT b.id, b.title, b.published ' . ' FROM `#__sections` AS b ';
    $db->setQuery($query);
    $lists['sections_arr'] = $db->loadObjectList();
    $query = 'SELECT a.value, b.title, b.published, c.title AS section_name' . ' FROM #__community_acl_access AS a' . ' LEFT JOIN #__categories AS b ON b.id = a.value' . ' LEFT JOIN #__sections AS c ON b.section = c.id' . ' WHERE a.option = \'com_categories\' AND a.name = \'cid\'' . ' AND a.group_id = ' . $group_id . ' AND a.role_id = ' . $role_id . ' ORDER BY b.section, a.id';
    $db->setQuery($query);
    $lists['categories'] = $db->loadObjectList();
    $query = 'SELECT b.id, b.title, b.published, c.title AS section_name' . ' FROM #__categories AS b ' . ' LEFT JOIN #__sections AS c ON b.section = c.id';
    $db->setQuery($query);
    $lists['categories_arr'] = $db->loadObjectList();
    $query = 'SELECT a.value, b.title, b.state AS published, c.title AS section_name, d.title AS cat_name, e.name AS author_name' . ' FROM #__community_acl_access AS a' . ' LEFT JOIN #__content AS b ON b.id = a.value' . ' LEFT JOIN #__sections AS c ON b.sectionid = c.id' . ' LEFT JOIN #__categories AS d ON b.catid = d.id' . ' LEFT JOIN #__users AS e ON b.created_by = e.id' . ' WHERE a.option = \'com_content\' AND a.name = \'cid\'' . ' AND a.group_id = ' . $group_id . ' AND a.role_id = ' . $role_id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['contents'] = $db->loadObjectList();
    $query = 'SELECT b.id, b.title, b.state AS published, c.title AS section_name, d.title AS cat_name, e.name AS author_name' . ' FROM #__content AS b ' . ' LEFT JOIN #__sections AS c ON b.sectionid = c.id' . ' LEFT JOIN #__categories AS d ON b.catid = d.id' . ' LEFT JOIN #__users AS e ON b.created_by = e.id';
    $db->setQuery($query);
    $lists['contents_arr'] = $db->loadObjectList();
    $query = 'SELECT b.id AS value , b.name AS title, a.isfrontend, a.isbackend, a.option' . ' FROM #__community_acl_access AS a' . ' LEFT JOIN #__components AS b ON b.id = a.value' . ' WHERE a.name = \'###\' AND a.option <> \'menu\' AND (b.parent = 0 OR a.option IN (\'com_sections\', \'com_frontpage\', \'com_categories\', \'com_trash\' )) ' . ' AND a.group_id = ' . $group_id . ' AND a.role_id = ' . $role_id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['components'] = $db->loadObjectList();
    for ($i = 0, $n = count($lists['components']); $i < $n; $i++) {
        if ($lists['components'][$i]->option == 'com_sections') {
            $lists['components'][$i]->value = 'com_sections';
            $lists['components'][$i]->title = 'Section Manager';
        } elseif ($lists['components'][$i]->option == 'com_frontpage') {
            $lists['components'][$i]->value = 'com_frontpage';
            $lists['components'][$i]->title = 'Front Page Manager';
        } elseif ($lists['components'][$i]->option == 'com_categories') {
            $lists['components'][$i]->value = 'com_categories';
            $lists['components'][$i]->title = 'Category Manager';
        } elseif ($lists['components'][$i]->option == 'com_trash') {
            $lists['components'][$i]->value = 'com_trash';
            $lists['components'][$i]->title = 'Trash Manager';
        }
    }
    $query = 'SELECT b.*, b.id AS value , b.name AS mtitle, a.isfrontend, a.isbackend, d.name AS parent_name, c.title AS menu_name' . ' FROM #__community_acl_access AS a' . ' LEFT JOIN #__menu AS b ON b.id = a.value' . ' LEFT JOIN #__menu_types AS c ON c.menutype = b.menutype' . ' LEFT JOIN #__menu AS d ON d.id = b.parent' . ' WHERE a.name = \'###\' AND a.option = \'menu\'' . ' AND a.group_id = ' . $group_id . ' AND a.role_id = ' . $role_id . ' ORDER BY c.menutype, b.parent, b.ordering';
    $db->setQuery($query);
    $lists['menus'] = $db->loadObjectList();
    # - Kobby Sam - Search for the Redirect link
    # - Get the mode to verify if its a role/group
    $mode = JRequest::getVar('mode');
    $id = JRequest::getVar('cid');
    $id = $id[0];
    if ($mode == 'role_id') {
        # - Get the front_redirect and backend redirect from DB
        $query = 'SELECT  redirect_FRONT, redirect_ADMIN FROM #__community_acl_roles WHERE id = ' . $id;
        $db->setQuery($query);
        $result = $db->loadObjectList();
        $query = 'SELECT choices as choice ' . ' FROM `#__community_acl_submit_form_role_level`' . ' WHERE role_id =  ' . $id . ' ORDER BY id ASC';
        $db->setQuery($query);
        $article_submission = $db->loadResultArray();
    } elseif ($mode == 'group_id') {
        # - Get the front_redirect and backend redirect from DB
        $query = 'SELECT redirect_URL_FRONT AS redirect_FRONT, redirect_URL_ADMIN  AS redirect_ADMIN FROM #__community_acl_groups WHERE id = ' . $id;
        $db->setQuery($query);
        $result = $db->loadObjectList();
        $query = 'SELECT choices as choice ' . ' FROM `#__community_acl_submit_form_group_level`' . ' WHERE group_id =  ' . $id . ' ORDER BY id ASC';
        $db->setQuery($query);
        $article_submission = $db->loadResultArray();
    }
    //print_R($result);
    if ($result) {
        $lists['frontend_redirect'] = $result[0]->redirect_FRONT;
        $lists['backend_redirect'] = $result[0]->redirect_ADMIN;
    }
    //print_r($article_submission);
    if ($article_submission) {
        $lists['article_submissions'] = $article_submission;
    }
    /* * /
    	Kobby Sam-This caused a bug.
    	When a Main menu was taken off a Forbidden List,
    	the Sub menu was taken off the Forbidden List too.
    
    	$children = array();
    	if ( $lists['menus'] )
    	{
    		// first pass - collect children
    		foreach ( $lists['menus'] as $v )
    		{
    			$pt 	= $v->parent;
    			$list 	= @$children[$pt] ? $children[$pt] : array();
    			array_push( $list, $v );
    			$children[$pt] = $list;
    		}
    	}
    	$lists['menus'] = JHTML::_('menu.treerecurse', 0, '', array(), $children, 9999, 0, 1 );
    	/* */
    $query = 'SELECT b.*, b.id AS value, b.name AS title, d.name AS parent_name, c.title AS menu_name ' . ' FROM `#__menu` AS b ' . ' LEFT JOIN `#__menu_types` AS c ON c.menutype = b.menutype ' . ' LEFT JOIN `#__menu` AS d ON d.id = b.parent ' . ' WHERE b.published <> -2 ' . ' ORDER BY c.menutype, b.parent, b.ordering';
    $db->setQuery($query);
    $lists['menus_arr'] = $db->loadObjectList();
    $children = array();
    if ($lists['menus_arr']) {
        // first pass - collect children
        foreach ($lists['menus_arr'] as $v) {
            $pt = $v->parent;
            $list = @$children[$pt] ? $children[$pt] : array();
            array_push($list, $v);
            $children[$pt] = $list;
        }
    }
    $lists['menus_arr'] = JHTML::_('menu.treerecurse', 0, '', array(), $children, 9999, 0, 1);
    $query = 'SELECT b.id AS value , b.title, b.position, b.module, b.published' . ' FROM #__community_acl_access AS a' . ' LEFT JOIN #__modules AS b ON b.id = a.value' . ' WHERE a.name = \'@@@\' ' . ' AND a.group_id = ' . $group_id . ' AND a.role_id = ' . $role_id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['modules'] = $db->loadObjectList();
    $query = 'SELECT `id`, `title`, `position`, `module`, `published`' . ' FROM `#__modules`' . ' WHERE `client_id` = 0' . ' ORDER BY `title`';
    $db->setQuery($query);
    $lists['modules_arr'] = $db->loadObjectList();
    $query = 'SELECT s.id, s.title' . ' FROM #__sections AS s' . ' ORDER BY s.ordering';
    $db->setQuery($query);
    $sections[] = JHTML::_('select.option', '0', JText::_('Uncategorized'), 'id', 'title');
    $sections = @array_merge($sections, $db->loadObjectList());
    $lists['sectionid'] = JHTML::_('select.genericlist', $sections, 'sectionid', 'class="inputbox" size="10" multiple="multiple" style="min-width:100px;max-width:200px;" ', 'id', 'title', 0);
    $section_list = array();
    foreach ($sections as $section) {
        $section_list[] = (int) $section->id;
    }
    $section_list = implode('\', \'', $section_list);
    $query = 'SELECT a.id, a.title, a.section, b.title AS stitle' . ' FROM `#__categories` AS a LEFT JOIN `#__sections` AS b ON b.id = a.section ' . ' WHERE section IN ( \'' . $section_list . '\' )' . ' ORDER BY b.ordering, a.ordering';
    $db->setQuery($query);
    $cat_list_tmp = $db->loadObjectList();
    $cat_list = array();
    // Uncategorized category mapped to uncategorized section
    $uncat = new stdClass();
    $uncat->id = 0;
    $uncat->title = JText::_('Uncategorized');
    $uncat->section = 0;
    $cat_list[] = $uncat;
    $first_sec = -1;
    foreach ($cat_list_tmp as $cl_tmp) {
        if ($first_sec != $cl_tmp->section) {
            if ($first_sec != -1) {
                $tmp = new stdClass();
                $tmp->id = '</OPTGROUP>';
                $tmp->title = $cl_tmp->stitle;
                $tmp->section = -1;
                $cat_list[] = $tmp;
            }
            $tmp = new stdClass();
            $tmp->id = '<OPTGROUP>';
            $tmp->title = $cl_tmp->stitle;
            $tmp->section = -1;
            $cat_list[] = $tmp;
            $first_sec = $cl_tmp->section;
        }
        $tmp = new stdClass();
        $tmp->id = $cl_tmp->id;
        $tmp->title = $cl_tmp->title;
        $tmp->section = $cl_tmp->section;
        $cat_list[] = $tmp;
    }
    if ($first_sec != -1) {
        $tmp = new stdClass();
        $tmp->id = '</OPTGROUP>';
        $tmp->title = '';
        $tmp->section = -1;
        $cat_list[] = $tmp;
    }
    $lists['catid'] = JHTML::_('select.genericlist', $cat_list, 'catid', 'class="inputbox" size="10" multiple="multiple" style="min-width:100px;max-width:200px;" ', 'id', 'title', 0);
    $query = 'SELECT a.id, a.title, a.sectionid, a.catid, b.title AS stitle, c.title AS ctitle ' . ' FROM `#__content` AS a LEFT JOIN `#__sections` AS b ON b.id = a.sectionid LEFT JOIN `#__categories` AS c ON c.id = a.catid ' . ' ORDER BY a.sectionid, a.catid, a.ordering, a.title ';
    $db->setQuery($query);
    $con_list_tmp = $db->loadObjectList();
    $con_list = array();
    $first_cat = -1;
    foreach ($con_list_tmp as $cl_tmp) {
        if ($first_cat != $cl_tmp->catid) {
            if ($first_cat != -1) {
                $tmp = new stdClass();
                $tmp->id = '</OPTGROUP>';
                $tmp->title = $cl_tmp->stitle . ' / ' . $cl_tmp->ctitle;
                $tmp->sectionid = -1;
                $tmp->catid = -1;
                $con_list[] = $tmp;
            }
            $tmp = new stdClass();
            $tmp->id = '<OPTGROUP>';
            if ($cl_tmp->catid == 0) {
                $tmp->title = JText::_('Uncategorized');
            } else {
                $tmp->title = $cl_tmp->stitle . ' / ' . $cl_tmp->ctitle;
            }
            $tmp->sectionid = -1;
            $tmp->catid = -1;
            $con_list[] = $tmp;
            $first_cat = $cl_tmp->catid;
        }
        $tmp = new stdClass();
        $tmp->id = $cl_tmp->id;
        $tmp->title = $cl_tmp->title;
        $tmp->sectionid = $cl_tmp->sectionid;
        $tmp->catid = $cl_tmp->catid;
        $con_list[] = $tmp;
    }
    if ($first_cat != -1) {
        $tmp = new stdClass();
        $tmp->id = '</OPTGROUP>';
        $tmp->title = '';
        $tmp->sectionid = -1;
        $tmp->catid = -1;
        $con_list[] = $tmp;
    }
    $lists['contentid'] = JHTML::_('select.genericlist', $con_list, 'contentid', 'class="inputbox" size="10" multiple="multiple"  style="min-width:100px;max-width:200px;" ', 'id', 'title', 0);
    // get a list of the menu items
    // excluding the current menu item and its child elements
    $query = 'SELECT m.*, m.name AS title, b.title AS mtitle' . ' FROM #__menu m LEFT JOIN `#__menu_types` AS b ON b.menutype = m.menutype ' . ' WHERE m.published != -2 ' . ' ORDER BY m.menutype, m.parent, m.ordering';
    $db->setQuery($query);
    $mitems = $db->loadObjectList();
    //print_r($query);die();
    // establish the hierarchy of the menu
    $children = array();
    if ($mitems) {
        // first pass - collect children
        foreach ($mitems as $v) {
            $pt = $v->parent;
            $list = @$children[$pt] ? $children[$pt] : array();
            array_push($list, $v);
            $children[$pt] = $list;
        }
    }
    // second pass - get an indent list of the items
    $menu_list_tmp = JHTML::_('menu.treerecurse', 0, '', array(), $children, 9999, 0, 0);
    $menu_list = array();
    $first_menu = -1;
    foreach ($menu_list_tmp as $cl_tmp) {
        if ($first_menu != $cl_tmp->mtitle) {
            if ($first_menu != -1) {
                $tmp = new stdClass();
                $tmp->id = '</OPTGROUP>';
                $tmp->title = $cl_tmp->mtitle;
                $menu_list[] = $tmp;
            }
            $tmp = new stdClass();
            $tmp->id = '<OPTGROUP>';
            $tmp->title = $cl_tmp->mtitle;
            $menu_list[] = $tmp;
            $first_menu = $cl_tmp->mtitle;
        }
        $tmp = new stdClass();
        $tmp->id = $cl_tmp->id;
        $tmp->title = $cl_tmp->treename;
        $menu_list[] = $tmp;
    }
    if ($first_menu != -1) {
        $tmp = new stdClass();
        $tmp->id = '</OPTGROUP>';
        $tmp->title = '';
        $menu_list[] = $tmp;
    }
    $lists['menuid'] = JHTML::_('select.genericlist', $menu_list, 'menuid', 'class="inputbox" size="10" multiple="multiple" style="min-width:100px;max-width:200px;" ', 'id', 'title', 0);
    $query = 'SELECT id, name AS title' . ' FROM #__components' . ' WHERE parent = 0' . ' ORDER BY iscore, name';
    $db->setQuery($query);
    $tmp = $db->loadObjectList();
    $components = array();
    //$components[] = array('id'=>'0', 'title'=>'- '. JText::_( 'All Components' ) .' -');
    foreach ($tmp as $t) {
        $components[] = array('id' => $t->id, 'title' => $t->title);
    }
    $components[] = array('id' => 'com_sections', 'title' => 'Section Manager');
    $components[] = array('id' => 'com_frontpage', 'title' => 'Front Page Manager');
    $components[] = array('id' => 'com_categories', 'title' => 'Category Manager');
    $components[] = array('id' => 'com_trash', 'title' => 'Trash Manager');
    $lists['componentid'] = JHTML::_('select.genericlist', $components, 'componentid', 'class="inputbox" size="5" multiple="multiple" ', 'id', 'title', 0);
    $query = 'SELECT id, title' . ' FROM #__modules' . ' WHERE client_id = 0' . ' ORDER BY title';
    $db->setQuery($query);
    $tmp = $db->loadObjectList();
    $modules = array();
    //$components[] = array('id'=>'0', 'title'=>'- '. JText::_( 'All Components' ) .' -');
    foreach ($tmp as $t) {
        $modules[] = array('id' => $t->id, 'title' => $t->title);
    }
    $lists['moduleid'] = JHTML::_('select.genericlist', $modules, 'moduleid', 'class="inputbox" size="5" multiple="multiple" ', 'id', 'title', 0);
    if ($group_id == 0 && $role_id > 0) {
        $id = $role_id;
        $mode = 'role_id';
        $redirect = 'list_roles';
    } else {
        $id = $group_id;