$mainframe->redirect('index.php?option=com_community_acl&task=set_functions&cid[]=' . $cid, $msg);
    } else {
        $mainframe->redirect('index.php?option=com_community_acl&task=' . $redirect, $msg);
    }
}
function setFunctions($id = 0)
{
    global $mainframe, $option;
    if ($id == 0) {
        return;
    }
    $db =& JFactory::getDBO();
    $lists = array();
    $query = 'SELECT *' . ' FROM #__community_acl_functions' . ' WHERE id = ' . $id;
    $db->setQuery($query);
    $function = $db->loadObjectList();
    $function = $function[0];
    $query = "SELECT `value` FROM `#__community_acl_config` WHERE `name` = 'default_action' ";
    $db->setQuery($query);
    $default_action = $db->loadResult();
    if ($default_action == null) {
        $default_action = 'deny';
    }
    $query = 'SELECT `name`, `value`' . ' FROM `#__community_acl_function_access`' . ' WHERE `option` = \'###\' AND `func_id` = \'' . $id . '\'';
    $db->setQuery($query);
    $tmp = $db->loadObjectList();
    $lists['joomla_actions'] = array();
    if (is_array($tmp) && count($tmp) > 0) {
        foreach ($tmp as $t) {
            $lists['joomla_actions'][$t->name] = (int) $t->value;
        }
    }
    $query = 'SELECT id, name AS title' . ' FROM #__components' . ' WHERE parent = 0' . ' ORDER BY iscore, name';
    $db->setQuery($query);
    $tmp = $db->loadObjectList();
    $components = array();
    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="1" ', 'id', 'title', 0);
    $query = 'SELECT b.id AS c_id , b.name AS title, a.*' . ' FROM #__community_acl_function_access AS a' . ' LEFT JOIN #__components AS b ON b.option = a.option' . " WHERE a.func_id = '" . $id . "'  AND a.option <> '###' AND (b.parent = 0 OR a.option IN ('com_sections', 'com_frontpage', 'com_categories', 'com_trash'))" . ' ORDER BY a.id';
    $db->setQuery($query);
    $tmp = $db->loadObjectList();
    for ($i = 0, $n = count($tmp); $i < $n; $i++) {
        if ($tmp[$i]->option == 'com_sections') {
            $tmp[$i]->c_id = 'com_sections';
            $tmp[$i]->title = 'Section Manager';
        } elseif ($tmp[$i]->option == 'com_frontpage') {
            $tmp[$i]->c_id = 'com_frontpage';
            $tmp[$i]->title = 'Front Page Manager';
        } elseif ($tmp[$i]->option == 'com_categories') {
            $tmp[$i]->c_id = 'com_categories';
            $tmp[$i]->title = 'Category Manager';
        } elseif ($tmp[$i]->option == 'com_trash') {
            $tmp[$i]->c_id = 'com_trash';
            $tmp[$i]->title = 'Trash Manager';
        }
    }
    $query = 'SELECT grouping AS n' . ' FROM #__community_acl_function_access ' . ' WHERE func_id = \'' . $id . '\' AND `option` <> \'###\'  ' . ' GROUP BY grouping' . ' ORDER BY grouping';
    $db->setQuery($query);
    $grouping = $db->loadObjectList();
    $lists['functions'] = array();
    if (is_array($grouping) && count($grouping)) {
        foreach ($grouping as $g) {
            $name = array();
            $value = array();
            $extra = array();
            $row = null;
            foreach ($tmp as $t) {
                if ($t->grouping == $g->n) {
                    $name[] = $t->name;
                    $value[] = $t->value;
                    $extra[] = $t->extra == '1' ? ' (' . ($default_action == 'deny' ? JText::_('Allow all values except this value') : JText::_('Only allow this value')) . ')' : '';
                    $row = $t;
                }
            }
            $row->name = $name;
            $row->value = $value;
            $row->extra = $extra;
            if ($row !== null) {
                $lists['functions'][] = $row;
            }
        }
    }
    $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);
    $query = 'SELECT a.item_id AS value, b.title' . ' FROM #__community_acl_content_actions AS a' . ' LEFT JOIN #__sections AS b ON b.id = a.item_id' . ' WHERE a.item_type = \'section\' AND a.action = \'add\'' . ' AND a.func_id = ' . $id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['sections_add'] = $db->loadObjectList();
    $query = 'SELECT a.item_id AS value, b.title' . ' FROM #__community_acl_content_actions AS a' . ' LEFT JOIN #__sections AS b ON b.id = a.item_id' . ' WHERE a.item_type = \'section\' AND a.action = \'edit\'' . ' AND a.func_id = ' . $id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['sections_edit'] = $db->loadObjectList();
    $query = 'SELECT a.item_id AS value, b.title' . ' FROM #__community_acl_content_actions AS a' . ' LEFT JOIN #__sections AS b ON b.id = a.item_id' . ' WHERE a.item_type = \'section\' AND a.action = \'publish\'' . ' AND a.func_id = ' . $id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['sections_publish'] = $db->loadObjectList();
    $query = 'SELECT a.item_id AS value, b.title' . ' FROM #__community_acl_content_actions AS a' . ' LEFT JOIN #__categories AS b ON b.id = a.item_id' . ' WHERE a.item_type = \'category\' AND a.action = \'add\'' . ' AND a.func_id = ' . $id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['categories_add'] = $db->loadObjectList();
    $query = 'SELECT a.item_id AS value, b.title' . ' FROM #__community_acl_content_actions AS a' . ' LEFT JOIN #__categories AS b ON b.id = a.item_id' . ' WHERE a.item_type = \'category\' AND a.action = \'edit\'' . ' AND a.func_id = ' . $id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['categories_edit'] = $db->loadObjectList();
    $query = 'SELECT a.item_id AS value, b.title' . ' FROM #__community_acl_content_actions AS a' . ' LEFT JOIN #__categories AS b ON b.id = a.item_id' . ' WHERE a.item_type = \'category\' AND a.action = \'publish\'' . ' AND a.func_id = ' . $id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['categories_publish'] = $db->loadObjectList();
    $query = 'SELECT a.item_id AS value, b.title' . ' FROM #__community_acl_content_actions AS a' . ' LEFT JOIN #__content AS b ON b.id = a.item_id' . ' WHERE a.item_type = \'content\' AND a.action = \'add\'' . ' AND a.func_id = ' . $id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['contents_add'] = $db->loadObjectList();
    $query = 'SELECT a.item_id AS value, b.title' . ' FROM #__community_acl_content_actions AS a' . ' LEFT JOIN #__content AS b ON b.id = a.item_id' . ' WHERE a.item_type = \'content\' AND a.action = \'edit\'' . ' AND a.func_id = ' . $id . ' ORDER BY a.id';
    $db->setQuery($query);
    $lists['contents_edit'] = $db->loadObjectList();