function sportal_admin_shoutbox_edit()
{
    global $txt, $context, $modSettings, $smcFunc;
    $context['SPortal']['is_new'] = empty($_REQUEST['shoutbox_id']);
    if (!empty($_POST['submit'])) {
        checkSession();
        if (!isset($_POST['name']) || $smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['name'], ENT_QUOTES)) === '') {
            fatal_lang_error('sp_error_shoutbox_name_empty', false);
        }
        $result = $smcFunc['db_query']('', '
			SELECT id_shoutbox
			FROM {db_prefix}sp_shoutboxes
			WHERE name = {string:name}
				AND id_shoutbox != {int:current}
			LIMIT 1', array('limit' => 1, 'name' => $smcFunc['htmlspecialchars']($_POST['name'], ENT_QUOTES), 'current' => (int) $_POST['shoutbox_id']));
        list($has_duplicate) = $smcFunc['db_fetch_row']($result);
        $smcFunc['db_free_result']($result);
        if (!empty($has_duplicate)) {
            fatal_lang_error('sp_error_shoutbox_name_duplicate', false);
        }
        $permission_set = 0;
        $groups_allowed = $groups_denied = '';
        if (!empty($_POST['permission_set'])) {
            $permission_set = (int) $_POST['permission_set'];
        } elseif (!empty($_POST['membergroups']) && is_array($_POST['membergroups'])) {
            $groups_allowed = $groups_denied = array();
            foreach ($_POST['membergroups'] as $id => $value) {
                if ($value == 1) {
                    $groups_allowed[] = (int) $id;
                } elseif ($value == -1) {
                    $groups_denied[] = (int) $id;
                }
            }
            $groups_allowed = implode(',', $groups_allowed);
            $groups_denied = implode(',', $groups_denied);
        }
        if (isset($_POST['moderator_groups']) && is_array($_POST['moderator_groups']) && count($_POST['moderator_groups']) > 0) {
            foreach ($_POST['moderator_groups'] as $id => $group) {
                $_POST['moderator_groups'][$id] = (int) $group;
            }
            $_POST['moderator_groups'] = implode(',', $_POST['moderator_groups']);
        } else {
            $_POST['moderator_groups'] = '';
        }
        if (!empty($_POST['allowed_bbc']) && is_array($_POST['allowed_bbc'])) {
            foreach ($_POST['allowed_bbc'] as $id => $tag) {
                $_POST['allowed_bbc'][$id] = $smcFunc['htmlspecialchars']($tag, ENT_QUOTES);
            }
            $_POST['allowed_bbc'] = implode(',', $_POST['allowed_bbc']);
        } else {
            $_POST['allowed_bbc'] = '';
        }
        $fields = array('name' => 'string', 'permission_set' => 'int', 'groups_allowed' => 'string', 'groups_denied' => 'string', 'moderator_groups' => 'string', 'warning' => 'string', 'allowed_bbc' => 'string', 'height' => 'int', 'num_show' => 'int', 'num_max' => 'int', 'reverse' => 'int', 'caching' => 'int', 'refresh' => 'int', 'status' => 'int');
        $shoutbox_info = array('id' => (int) $_POST['shoutbox_id'], 'name' => $smcFunc['htmlspecialchars']($_POST['name'], ENT_QUOTES), 'permission_set' => $permission_set, 'groups_allowed' => $groups_allowed, 'groups_denied' => $groups_denied, 'moderator_groups' => $_POST['moderator_groups'], 'warning' => $smcFunc['htmlspecialchars']($_POST['warning'], ENT_QUOTES), 'allowed_bbc' => $_POST['allowed_bbc'], 'height' => (int) $_POST['height'], 'num_show' => (int) $_POST['num_show'], 'num_max' => (int) $_POST['num_max'], 'reverse' => !empty($_POST['reverse']) ? 1 : 0, 'caching' => !empty($_POST['caching']) ? 1 : 0, 'refresh' => (int) $_POST['refresh'], 'status' => !empty($_POST['status']) ? 1 : 0);
        if ($context['SPortal']['is_new']) {
            unset($shoutbox_info['id']);
            $smcFunc['db_insert']('', '{db_prefix}sp_shoutboxes', $fields, $shoutbox_info, array('id_shoutbox'));
            $shoutbox_info['id'] = $smcFunc['db_insert_id']('{db_prefix}sp_shoutboxes', 'id_shoutbox');
        } else {
            $update_fields = array();
            foreach ($fields as $name => $type) {
                $update_fields[] = $name . ' = {' . $type . ':' . $name . '}';
            }
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}sp_shoutboxes
				SET ' . implode(', ', $update_fields) . '
				WHERE id_shoutbox = {int:id}', $shoutbox_info);
        }
        sportal_update_shoutbox($shoutbox_info['id']);
        if ($context['SPortal']['is_new'] && allowedTo(array('sp_admin', 'sp_manage_blocks'))) {
            redirectexit('action=admin;area=portalshoutbox;sa=blockredirect;shoutbox=' . $shoutbox_info['id']);
        } else {
            redirectexit('action=admin;area=portalshoutbox');
        }
    }
    if ($context['SPortal']['is_new']) {
        $context['SPortal']['shoutbox'] = array('id' => 0, 'name' => $txt['sp_shoutbox_default_name'], 'permission_set' => 3, 'groups_allowed' => array(), 'groups_denied' => array(), 'moderator_groups' => array(), 'warning' => '', 'allowed_bbc' => array('b', 'i', 'u', 's', 'url', 'code', 'quote', 'me'), 'height' => 200, 'num_show' => 20, 'num_max' => 1000, 'reverse' => 0, 'caching' => 1, 'refresh' => 0, 'status' => 1);
    } else {
        $_REQUEST['shoutbox_id'] = (int) $_REQUEST['shoutbox_id'];
        $context['SPortal']['shoutbox'] = sportal_get_shoutbox($_REQUEST['shoutbox_id']);
    }
    loadLanguage('Post');
    $context['SPortal']['shoutbox']['groups'] = sp_load_membergroups();
    sp_loadMemberGroups($context['SPortal']['shoutbox']['moderator_groups'], 'moderator', 'moderator_groups');
    $context['allowed_bbc'] = array('b' => $txt['bold'], 'i' => $txt['italic'], 'u' => $txt['underline'], 's' => $txt['strike'], 'pre' => $txt['preformatted'], 'flash' => $txt['flash'], 'img' => $txt['image'], 'url' => $txt['hyperlink'], 'email' => $txt['insert_email'], 'ftp' => $txt['ftp'], 'glow' => $txt['glow'], 'shadow' => $txt['shadow'], 'sup' => $txt['superscript'], 'sub' => $txt['subscript'], 'tt' => $txt['teletype'], 'code' => $txt['bbc_code'], 'quote' => $txt['bbc_quote'], 'size' => $txt['font_size'], 'font' => $txt['font_face'], 'color' => $txt['change_color'], 'me' => 'me');
    $disabled_tags = array();
    if (!empty($modSettings['disabledBBC'])) {
        $disabled_tags = explode(',', $modSettings['disabledBBC']);
    }
    if (empty($modSettings['enableEmbeddedFlash'])) {
        $disabled_tags[] = 'flash';
    }
    foreach ($disabled_tags as $tag) {
        if ($tag == 'list') {
            $context['disabled_tags']['orderlist'] = true;
        }
        $context['disabled_tags'][trim($tag)] = true;
    }
    $context['page_title'] = $context['SPortal']['is_new'] ? $txt['sp_admin_shoutbox_add'] : $txt['sp_admin_shoutbox_edit'];
    $context['sub_template'] = 'shoutbox_edit';
}
 /**
  * Edit an existing shoutbox or add a new one
  */
 public function action_sportal_admin_shoutbox_edit()
 {
     global $txt, $context, $modSettings, $editortxt;
     $context['SPortal']['is_new'] = empty($_REQUEST['shoutbox_id']);
     if (!empty($_POST['submit'])) {
         checkSession();
         if (!isset($_POST['name']) || Util::htmltrim(Util::htmlspecialchars($_POST['name'], ENT_QUOTES)) === '') {
             fatal_lang_error('sp_error_shoutbox_name_empty', false);
         }
         // No two the same
         $has_duplicate = sp_check_duplicate_shoutbox($_POST['name'], $_POST['shoutbox_id']);
         if (!empty($has_duplicate)) {
             fatal_lang_error('sp_error_shoutbox_name_duplicate', false);
         }
         if (isset($_POST['moderator_groups']) && is_array($_POST['moderator_groups']) && count($_POST['moderator_groups']) > 0) {
             foreach ($_POST['moderator_groups'] as $id => $group) {
                 $_POST['moderator_groups'][$id] = (int) $group;
             }
             $_POST['moderator_groups'] = implode(',', $_POST['moderator_groups']);
         } else {
             $_POST['moderator_groups'] = '';
         }
         if (!empty($_POST['allowed_bbc']) && is_array($_POST['allowed_bbc'])) {
             foreach ($_POST['allowed_bbc'] as $id => $tag) {
                 $_POST['allowed_bbc'][$id] = Util::htmlspecialchars($tag, ENT_QUOTES);
             }
             $_POST['allowed_bbc'] = implode(',', $_POST['allowed_bbc']);
         } else {
             $_POST['allowed_bbc'] = '';
         }
         $shoutbox_info = array('id' => (int) $_POST['shoutbox_id'], 'name' => Util::htmlspecialchars($_POST['name'], ENT_QUOTES), 'permissions' => (int) $_POST['permissions'], 'moderator_groups' => $_POST['moderator_groups'], 'warning' => Util::htmlspecialchars($_POST['warning'], ENT_QUOTES), 'allowed_bbc' => $_POST['allowed_bbc'], 'height' => (int) $_POST['height'], 'num_show' => (int) $_POST['num_show'], 'num_max' => (int) $_POST['num_max'], 'reverse' => !empty($_POST['reverse']) ? 1 : 0, 'caching' => !empty($_POST['caching']) ? 1 : 0, 'refresh' => (int) $_POST['refresh'], 'status' => !empty($_POST['status']) ? 1 : 0);
         // Update existing or add a new shoutbox
         $shoutbox_info['id'] = sp_edit_shoutbox($shoutbox_info, $context['SPortal']['is_new']);
         sportal_update_shoutbox($shoutbox_info['id']);
         if ($context['SPortal']['is_new'] && allowedTo(array('sp_admin', 'sp_manage_blocks'))) {
             redirectexit('action=admin;area=portalshoutbox;sa=blockredirect;shoutbox=' . $shoutbox_info['id']);
         } else {
             redirectexit('action=admin;area=portalshoutbox');
         }
     }
     if ($context['SPortal']['is_new']) {
         $context['SPortal']['shoutbox'] = array('id' => 0, 'name' => $txt['sp_shoutbox_default_name'], 'permissions' => 3, 'moderator_groups' => array(), 'warning' => '', 'allowed_bbc' => array('b', 'i', 'u', 's', 'url', 'code', 'quote', 'me'), 'height' => 200, 'num_show' => 20, 'num_max' => 1000, 'reverse' => 0, 'caching' => 1, 'refresh' => 0, 'status' => 1);
     } else {
         $_REQUEST['shoutbox_id'] = (int) $_REQUEST['shoutbox_id'];
         $context['SPortal']['shoutbox'] = sportal_get_shoutbox($_REQUEST['shoutbox_id']);
     }
     loadLanguage('Editor');
     $context['SPortal']['shoutbox']['permission_profiles'] = sportal_get_profiles(null, 1, 'name');
     sp_loadMemberGroups($context['SPortal']['shoutbox']['moderator_groups'], 'moderator', 'moderator_groups');
     if (empty($context['SPortal']['shoutbox']['permission_profiles'])) {
         fatal_lang_error('error_sp_no_permission_profiles', false);
     }
     $context['allowed_bbc'] = array('b' => $editortxt['Bold'], 'i' => $editortxt['Italic'], 'u' => $editortxt['Underline'], 's' => $editortxt['Strikethrough'], 'pre' => $editortxt['Preformatted Text'], 'img' => $editortxt['Insert an image'], 'url' => $editortxt['Insert a link'], 'email' => $editortxt['Insert an email'], 'sup' => $editortxt['Superscript'], 'sub' => $editortxt['Subscript'], 'tt' => $editortxt['Teletype'], 'code' => $editortxt['Code'], 'quote' => $editortxt['Insert a Quote'], 'size' => $editortxt['Font Size'], 'font' => $editortxt['Font Name'], 'color' => $editortxt['Font Color'], 'me' => 'me');
     $disabled_tags = array();
     if (!empty($modSettings['disabledBBC'])) {
         $disabled_tags = explode(',', $modSettings['disabledBBC']);
     }
     if (empty($modSettings['enableEmbeddedFlash'])) {
         $disabled_tags[] = 'flash';
     }
     foreach ($disabled_tags as $tag) {
         if ($tag == 'list') {
             $context['disabled_tags']['orderlist'] = true;
         }
         $context['disabled_tags'][trim($tag)] = true;
     }
     $context['page_title'] = $context['SPortal']['is_new'] ? $txt['sp_admin_shoutbox_add'] : $txt['sp_admin_shoutbox_edit'];
     $context['sub_template'] = 'shoutbox_edit';
 }