/**
  * This handles all saves of social group data.
  */
 public function actionSocialgroup()
 {
     $fields = array('title', 'description', 'nodeid', 'filedataid', 'invite_usernames', 'parentid', 'invite_userids', 'group_type', 'viewperms', 'commentperms', 'moderate_topics', 'autoparselinks', 'disablesmilies', 'allow_post', 'approve_subscription', 'group_type');
     // forum options map
     $channelOpts = array('allowsmilies' => 'disablesmilies', 'allowposting' => 'allow_post');
     $input = array();
     foreach ($fields as $field) {
         if (isset($_POST[$field])) {
             $input[$field] = $_POST[$field];
         }
     }
     //If this is the "permission" step, we must pass the four checkboxes
     if (isset($_POST['next']) and $_POST['next'] == 'contributors') {
         foreach (array('moderate_comments', 'autoparselinks', 'disablesmilies', 'allow_post', 'approve_subscription', 'moderate_topics') as $field) {
             // channeloptions
             if ($idx = array_search($field, $channelOpts)) {
                 // some options means totally the oppositve than the bf when enable, tweak then
                 if (isset($_POST[$field])) {
                     $input['options'][$idx] = in_array($field, array('disablesmilies')) ? 0 : 1;
                 } else {
                     $input['options'][$idx] = in_array($field, array('disablesmilies')) ? 1 : 0;
                 }
             }
             if (!isset($_POST[$field])) {
                 $input[$field] = 0;
             }
         }
     }
     // default input values
     $input['displayorder'] = 1;
     $api = Api_InterfaceAbstract::instance();
     if (count($input) > 1) {
         if (!isset($input['nodeid']) or intval($input['nodeid']) == 0) {
             $nodeid = $api->callApi('socialgroup', 'createSocialGroup', array($input));
             $url = vB5_Template_Options::instance()->get('options.frontendurl') . '/sgadmin/create/settings';
             if (is_array($nodeid) and array_key_exists('errors', $nodeid)) {
                 $message = $api->callApi('phrase', 'fetch', array('phrases' => $nodeid['errors'][0][0]));
                 if (empty($message)) {
                     $message = $api->callApi('phrase', 'fetch', array('phrases' => 'pm_ajax_error_desc'));
                 }
                 vB5_ApplicationAbstract::handleFormError(array_pop($message), $url);
             }
             if (!is_numeric($nodeid) and !empty($nodeid['errors'])) {
                 $urlparams = array('sgaction' => 'create', 'action2' => 'settings');
                 $url = $api->callApi('route', 'getUrl', array('sgadmin', $urlparams, array()));
                 header('Location: ' . vB5_Template_Options::instance()->get('options.frontendurl') . $url);
                 vB5_Cookie::set('sgadmin_error', $nodeid['errors'][0][0]);
                 if (isset($input['title'])) {
                     vB5_Cookie::set('sg_title', $input['title']);
                 }
                 if (isset($input['description'])) {
                     vB5_Cookie::set('sg_description', $input['description']);
                 }
                 die;
             }
             if ($nodeid and !empty($nodeid['errors'])) {
                 $urlparams = array('sgaction' => 'create', 'action2' => 'settings');
                 $url = $api->callApi('route', 'getUrl', array('sgadmin', $urlparams, array()));
                 header('Location: ' . vB5_Template_Options::instance()->get('options.frontendurl') . $url);
                 vB5_Cookie::set('sgadmin_error', $nodeid['errors'][0][0]);
                 if (isset($input['title'])) {
                     vB5_Cookie::set('sg_title', $input['title']);
                 }
                 if (isset($input['description'])) {
                     vB5_Cookie::set('sg_description', $input['description']);
                 }
                 die;
             }
         } else {
             if (isset($input['invite_usernames']) and $input['nodeid']) {
                 $inviteUnames = explode(',', $input['invite_usernames']);
                 $inviteIds = isset($input['invite_userids']) ? $input['invite_userids'] : array();
                 $nodeid = $input['nodeid'];
                 $api->callApi('user', 'inviteMembers', array($inviteIds, $inviteUnames, $nodeid, 'sg_member_to'));
             } else {
                 $nodeid = $input['nodeid'];
                 unset($input['nodeid']);
                 $update = $api->callApi('content_channel', 'update', array($nodeid, $input));
                 // set group type nodeoptions
                 if (empty($update['errors']) and isset($input['group_type'])) {
                     $bitfields = array();
                     switch ($input['group_type']) {
                         case 2:
                             $bitfields['invite_only'] = 1;
                             $bitfields['approve_membership'] = 0;
                             break;
                         case 1:
                             $bitfields['invite_only'] = 0;
                             $bitfields['approve_membership'] = 0;
                             break;
                         default:
                             $bitfields['invite_only'] = 0;
                             $bitfields['approve_membership'] = 1;
                             break;
                     }
                     $api->callApi('node', 'setNodeOptions', array($nodeid, $bitfields));
                 }
                 //if this is for the permission page we handle differently
             }
         }
         //			set_exception_handler(array('vB5_ApplicationAbstract','handleException'));
         //
         //			if (!is_numeric($nodeid) AND !empty($nodeid['errors']))
         //			{
         //				throw new exception($nodeid['errors'][0][0]);
         //			}
     } else {
         if (isset($_POST['nodeid'])) {
             $nodeid = $_POST['nodeid'];
             if (isset($_POST['next']) and $_POST['next'] == 'contributors') {
                 $updates = array();
                 foreach (array('allow_post', 'moderate_comments', 'autoparselinks', 'disablesmilies', 'approve_subscription') as $bitfield) {
                     if (empty($_POST[$bitfield])) {
                         $updates[$bitfield] = 0;
                     } else {
                         $updates[$bitfield] = 1;
                     }
                 }
                 $api->callApi('node', 'setNodeOptions', array($nodeid, $updates));
                 $updates = array();
                 if (isset($_POST['viewperms'])) {
                     $updates['viewperms'] = $_POST['viewperms'];
                 }
                 if (isset($_POST['commentperms'])) {
                     $updates['commentperms'] = $_POST['commentperms'];
                 }
                 if (!empty($updates)) {
                     $results = $api->callApi('node', 'setNodePerms', array($nodeid, $updates));
                 }
             }
         } else {
             $nodeid = 0;
         }
     }
     //If the user clicked Next we go to the permissions page. Otherwise we go to the node.
     if (isset($_POST['btnSubmit'])) {
         if (isset($_POST['next'])) {
             $action2 = $_POST['next'];
         } else {
             $action2 = 'permissions';
         }
         if (isset($_POST['sgaction'])) {
             $sgaction = $_POST['sgaction'];
         } else {
             $sgaction = 'admin';
         }
         $urlparams = array('nodeid' => $nodeid, 'sgaction' => $sgaction, 'action2' => $action2);
         $url = $api->callApi('route', 'getUrl', array('sgadmin', $urlparams, array()));
     } else {
         $node = $api->callApi('node', 'getNode', array('nodeid' => $nodeid));
         $url = $api->callApi('route', 'getUrl', array($node['routeid'], array('nodeid' => $nodeid, 'title' => $node['title'], 'urlident' => $node['urlident']), array()));
     }
     header('Location: ' . vB5_Template_Options::instance()->get('options.frontendurl') . $url);
 }
 public function actionSavePrivacySettings()
 {
     $userId = intval($_REQUEST['userid']);
     if ($userId > 0) {
         // privacy settings
         $options = array();
         $userInfo = array('privacy_options' => $_POST['privacyOptions']);
         $tempOptions = array();
         $options['moderatefollowers'] = isset($_POST['follower_request']) ? false : true;
         $api = Api_InterfaceAbstract::instance();
         $response = $api->callApi('user', 'save', array('userid' => $userId, 'password' => '', 'user' => $userInfo, 'options' => $options, 'adminoptions' => array(), 'userfield' => array()));
         $url = vB5_Template_Options::instance()->get('options.frontendurl') . '/settings/privacy';
         if (is_array($response) and array_key_exists('errors', $response)) {
             $message = $api->callApi('phrase', 'fetch', array('phrases' => $response['errors'][0][0]));
             vB5_ApplicationAbstract::handleFormError(array_pop($message), $url);
         } else {
             // and get back to settings
             header('Location: ' . $url);
         }
     }
 }