You are here

function fasttoggle_comment_option in Fasttoggle 6

Same name and namespace in other branches
  1. 5 fasttoggle.module \fasttoggle_comment_option()

Menu callback. Toggle options for a comment if the action is confirmed via POST. Otherwise, display a confirmation form.

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

File

./fasttoggle.toggle.inc, line 175
Handles toggling of options.

Code

function fasttoggle_comment_option($comment, $group, $option) {
  global $user;
  $options = fasttoggle_get_options('comment', $comment);

  // 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 . '_' . $comment->cid, TRUE)) {

    // The action is confirmed: either via form submit or via AJAX/POST
    if (isset($_POST['confirm']) && $_POST['confirm']) {
      $oldClass = 'fasttoggle-status-comment-' . $group . '-' . $option . '-' . intval($comment->{$option});
      $comment->{$option} = !$comment->{$option};
      $newClass = 'fasttoggle-status-comment-' . $group . '-' . $option . '-' . intval($comment->{$option});
      comment_save((array) $comment);
      $labels = fasttoggle_fasttoggle_labels(FASTTOGGLE_LABEL_STATUS);
      watchdog('fasttoggle', 'Comment "@subject" @option toggled to @value by @username', array(
        '@subject' => $comment->subject,
        '@option' => $option,
        '@value' => $labels['comment_' . $option][$comment->{$option}],
        '@username' => $user->name,
      ));

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

      // Output the new status 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'][intval($comment->{$option})],
          'callback' => 'comment',
          'option' => $option,
          'status' => $comment->{$option},
          'className' => 'fasttoggle-status-comment-' . $group . '-' . $option . '-' . (1 - intval($comment->{$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.
      $labels = _fasttoggle_get_label('comment_' . $group . '_' . $option, FASTTOGGLE_LABEL_STATUS);
      return drupal_get_form('fasttoggle_comment_option_confirm', $comment, $labels[intval(!$comment->{$option})]);
    }
  }
  else {
    return MENU_NOT_FOUND;
  }
}