You are here

function fasttoggle_user_option in Fasttoggle 5

Same name and namespace in other branches
  1. 6 fasttoggle.toggle.inc \fasttoggle_user_option()

Menu callback. Toggle the status of a user if the action is confirmed via POST. Otherwise, display a confirmation form.

1 string reference to 'fasttoggle_user_option'
fasttoggle_menu in ./fasttoggle.module
Implementation of hook_menu().

File

./fasttoggle.module, line 456
Enables fast toggling of binary or not so binary settings

Code

function fasttoggle_user_option($user, $option) {
  $options = fasttoggle_get_options('user', $user);

  // Check if the action is valid. This is essential to ensure the user has
  // access to the action.
  if (isset($options[$option]) && isset($_GET['token']) && drupal_valid_token($_GET['token'], $option . '_' . $user->uid, true)) {
    if (isset($_POST['confirm']) && $_POST['confirm']) {
      $array = array(
        $option => !$user->{$option},
      );
      $user = user_save($user, $array);

      // Output the new option for the updated link text on AJAX changes
      if (isset($_POST['javascript']) && $_POST['javascript']) {
        drupal_set_header('Content-Type: text/javascript; charset=utf-8');
        echo drupal_to_js(array(
          'text' => $options[$option][intval($array[$option])],
        ));
        exit;
      }
      else {
        drupal_goto();
      }
    }
    else {

      // The action is not confirmed. The user came here through a regular link;
      // no AJAX was involved. That means, we need a confirmation form so that
      // we get a POST form.
      return drupal_get_form('fasttoggle_user_option_confirm', $user, $options[$option][intval($array[$option])]);
    }
  }
  else {
    return MENU_NOT_FOUND;
  }
}