You are here

function fasttoggle_user_option in Fasttoggle 6

Same name and namespace in other branches
  1. 5 fasttoggle.module \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.toggle.inc, line 91
Handles toggling of options.

Code

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

  // Check if the action is valid. This is essential to ensure the user has
  // access to the action.
  if (isset($options[$group][$option]) && isset($_GET['token']) && drupal_valid_token($_GET['token'], $group . '_' . $option . '_' . $obj_user->uid, TRUE)) {
    if (isset($_POST['confirm']) && $_POST['confirm']) {
      $selectorClass = 'fasttoggle-status-user-' . $obj_user->uid . '-' . $group . '-' . $option;
      if (isset($options[$group][$option]['array_fn'])) {
        $array = $options[$group][$option]['array_fn']($obj_user, $options[$group][$option]['value_key']);
      }
      else {
        $array = array(
          $option => intval(!$obj_user->{$option}),
        );
      }
      $obj_user = user_save($obj_user, $array);
      $labels = fasttoggle_fasttoggle_labels(FASTTOGGLE_LABEL_STATUS);
      if (isset($options[$group][$option]['value_fn'])) {
        $value = $options[$group][$option]['value_fn']($obj_user, $options[$group][$option]['value_key']);
        $label = $labels[$group][$value];
      }
      else {
        if (isset($options[$group][$option]['value_key'])) {
          $value = $obj_user->{$options}[$group][$option]['value_key'];
        }
        else {
          $value = intval($obj_user->{$option});
        }
        $label = $labels['user_' . $option][$value];
      }
      watchdog('fasttoggle', 'User "@obj_user" @group "@option" toggled to @value by @username', array(
        '@obj_user' => $obj_user->name,
        '@group' => $group,
        '@option' => $option,
        '@value' => $label,
        '@username' => $user->name,
      ));

      // Let other modules respond.
      module_invoke_all('fasttoggle_toggle', 'user', $obj_user, $option);

      // 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[$group][$option]['label'][$value],
          'className' => 'fasttoggle-status-user-' . $group . '-' . $option . '-' . $value,
        ));
        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.
      $labels = _fasttoggle_get_label('user_' . $group . '_' . $option, FASTTOGGLE_LABEL_STATUS);
      return drupal_get_form('fasttoggle_user_option_confirm', $obj_user, $labels[intval(!$obj_user->{$option})]);
    }
  }
  else {
    return MENU_NOT_FOUND;
  }
}