Commit b7f3f09b authored by Yaroslav Lushnikov's avatar Yaroslav Lushnikov Committed by Dmitry Kiselev
Browse files

Issue #2393809 by zvse, kala4ek, ParisLiakos: Do not require javascript to work

parent 81f37570
Loading
Loading
Loading
Loading
+24 −2
Original line number Diff line number Diff line
@@ -164,9 +164,31 @@ function comment_abuse_complaint_form_validate($form, &$form_state) {
 * Submit complaint form.
 */
function comment_abuse_complaint_form_submit($form, &$form_state) {
  module_load_include('inc', 'comment_abuse', 'comment_abuse.pages');
  $cid = $form_state['build_info']['args'][0]['cid'];
  comment_abuse_content($cid, $form_state);

  $values = $form_state['values'];

  $message = !empty($values['comment_abuse_complaint_message'])
    ? $values['comment_abuse_complaint_message']
    : '';
  $reason =  !empty($values['comment_abuse_complaint_reason'])
    ? $values['comment_abuse_complaint_reason']
    : '';

  $data = array('message' => $message, 'reason' => $reason);
  $js = !empty($form_state['ajax']);

  if ($result = comment_abuse_add_comment_abuse($cid, $data)) {
    $message = comment_abuse_success_message($cid, $js);
  }
  else {
    $message = comment_abuse_failure_message($cid, $js);
  }

  drupal_set_message(
    $message,
    $result ? 'status' : 'error'
  );
}

/**
+125 −11
Original line number Diff line number Diff line
@@ -16,11 +16,10 @@ require_once 'comment_abuse.form.inc';
function comment_abuse_menu() {
  $items = array();

  $items['comment-abuse/comment/%'] = array(
  $items['comment-abuse/comment/%/%ctools_js'] = array(
    'page callback' => 'comment_abuse_content',
    'page arguments' => array(2),
    'page arguments' => array(2, 3),
    'access arguments' => array('send complaints on comments'),
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
    'file' => 'comment_abuse.pages.inc',
  );
@@ -195,11 +194,19 @@ function comment_abuse_remove_abuses($comment, $context = array()) {

/**
 * Add new abuse for a comment.
 *
 * @param $cid int
 *  Comment identifier
 * @param $data array
 *  Abuse data. Could contain:
 *  - message - Message added to abuse
 *  - reason  - Abuse reason
 *
 * @return bool
 */
function comment_abuse_add_comment_abuse($cid, $form_state) {
function comment_abuse_add_comment_abuse($cid, $data = array()) {
  global $user;

  $data = $form_state['values'];
  $user_ip = ip2long(ip_address());

  $query = db_select('comment', 'c')
@@ -219,12 +226,13 @@ function comment_abuse_add_comment_abuse($cid, $form_state) {
      array(':uid' => $user->uid)
    );
  }

  $result = $query->condition('c.cid', $cid)
    ->isNull('ca.aid')
    ->execute()
    ->fetch();

  if ($result->nid != NULL) {
  if (!empty($result->nid)) {
    // Insert new abuse in database.
    $aid = db_insert('comment_abuse')
      ->fields(
@@ -232,8 +240,8 @@ function comment_abuse_add_comment_abuse($cid, $form_state) {
          'nid' => $result->nid,
          'cid' => $cid,
          'timestamp' => REQUEST_TIME,
          'message' => isset($data['comment_abuse_complaint_message']) ? $data['comment_abuse_complaint_message'] : '',
          'reason' => isset($data['comment_abuse_complaint_reason']) ? $data['comment_abuse_complaint_reason'] : '',
          'message' => isset($data['message']) ? $data['message'] : '',
          'reason' => isset($data['reason']) ? $data['reason'] : '',
          'uid' => $user->uid,
          'ip' => $user_ip,
        )
@@ -248,10 +256,15 @@ function comment_abuse_add_comment_abuse($cid, $form_state) {
    $abuse->timestamp = REQUEST_TIME;
    $abuse->uid = $user->uid;
    $abuse->ip = ip_address();
    $abuse->reason_id = isset($data['comment_abuse_complaint_reason']) ? $data['comment_abuse_complaint_reason'] : '';
    $abuse->message = isset($data['comment_abuse_complaint_message']) ? $data['comment_abuse_complaint_message'] : '';
    $abuse->reason_id = isset($data['reason']) ? $data['reason'] : '';
    $abuse->message = isset($data['message']) ? $data['message'] : '';
    // Invoke hook comment_abuse.
    module_invoke_all('comment_abuse', $abuse);
    // Invoke Rules event.
    if (module_exists('rules')) {
      $comment = comment_load($cid);
      rules_invoke_event('complaint_insert', $comment);
    }

    return TRUE;
  }
@@ -327,7 +340,7 @@ function comment_abuse_get_link($text, $cid, $popup = NULL) {
  else {
    return l(
      $text,
      "comment-abuse/comment/$cid",
      "comment-abuse/comment/$cid/nojs",
      array(
        'attributes' => array(
          'title' => $text,
@@ -556,3 +569,104 @@ function comment_abuse_locale_refresh($textgroup = 'comment_abuse') {
  // Update completed with no issues.
  return TRUE;
}

/**
 * Prepare success message for rendering.
 *
 * @param $cid integer
 *  Comment identifier
 * @param $js boolean
 *  Determines if we should use an ajax renderer.
 *
 * @return string
 *  Returns string in case if we should render message without JS.
 *  Otherwise it will break Drupal execution and render collected commands with
 *  a ajax_render.
 */
function comment_abuse_success_message($cid, $js) {
  $message = comment_abuse_get_text_for('success');

  if ($js) {
    // Render message as a commands array.
    $commands[] = ajax_command_replace(
      '.comment-abuse-' . $cid,
      '<span class="abuse-notice">' . check_plain($message) . '</span>'
    );

    $commands[] = ajax_command_remove('#modal-content .messages');

    if (variable_get('comment_abuse_use_popup', 1)) {
      $commands[] = comment_abuse_ajax_command_replace_wrapper($message, TRUE);
    }
    // Render commands.
    print ajax_render($commands);
    drupal_exit();
  }
  else {
    return $message;
  }
}

/**
 * Prepare failure message for rendering or render it as JS commands.
 *
 * @param $cid integer
 *  Comment identifier
 * @param $js boolean
 *  Determines if we should use an ajax renderer.
 *
 * @return string
 *  Returns string in case if we should render message without JS.
 *  Otherwise it will break Drupal execution and render collected commands with
 *  a ajax_render.
 */
function comment_abuse_failure_message($cid, $js) {
  // Check if we didn't receive comment ID. In this case we should alert site
  // manager about this issue.
  if (!$cid) {
    watchdog('Comment Abuse', 'Comment ID is missing');
  }

  $message = comment_abuse_get_text_for('fail');

  if ($js) {
    $commands[] = ajax_command_remove('#modal-content .messages');
    // Render as a command.
    $commands[] = ajax_command_replace(
      '.comment-abuse-' . $cid,
      '<span class="abuse-notice">' . check_plain($message) . '</span>'
    );

    if (variable_get('comment_abuse_use_popup', 1)) {
      $commands[] = comment_abuse_ajax_command_replace_wrapper($message, FALSE);
    }
    // Render commands.
    print ajax_render($commands);
    drupal_exit();
  }
  else {
    return $message;
  }
}

/**
 * Returns command to
 * @param $message string
 *  A message to display.
 * @param $result bool
 *  Result of the adding abuse.
 *
 * @return array.
 */
function comment_abuse_ajax_command_replace_wrapper($message, $result = TRUE) {
  return ajax_command_replace(
    '#complaint-form-wrapper',
    theme(
      'comment_abuse_complaint_popup_result',
      array(
        'message' => check_plain($message),
        'result' => $result,
      )
    )
  );
}
+5 −57
Original line number Diff line number Diff line
@@ -8,67 +8,15 @@
/**
 * Callback function for abuse comment.
 */
function comment_abuse_content($cid, $form_state) {

  // Create ajax commands.
  $commands = array();

  $abuse_result = FALSE;
  if (!empty($cid)) {
    // Add new abuse.
    $abuse_result = comment_abuse_add_comment_abuse($cid, $form_state);
  }

  if ($abuse_result) {
    // Translate message.
    $message = comment_abuse_get_text_for('success');

    // Replace abuse link with received message.
    $commands[] = ajax_command_replace(
      '.comment-abuse-' . $cid,
      '<span class="abuse-notice">' . check_plain($message) . '</span>'
    );

    // Invoke rules event.
    if (module_exists('rules')) {
      $comment = comment_load($cid);
      rules_invoke_event('complaint_insert', $comment);
    }
function comment_abuse_content($cid, $js = TRUE) {
  if (!empty($cid) && comment_abuse_add_comment_abuse($cid)) {
    $result = comment_abuse_success_message($cid, $js);
  }
  else {
    // Translate message.
    $message = comment_abuse_get_text_for('fail');

    // Replace abuse link with received message.
    $commands[] = ajax_command_replace(
      '.comment-abuse-' . $cid,
      '<span class="abuse-notice">' . check_plain($message) . '</span>'
    );
    $result = comment_abuse_failure_message($cid, $js);
  }

  // Remove old error messages.
  $commands[] = ajax_command_remove('#modal-content .messages');

  // Complaint form mode.
  if (variable_get('comment_abuse_use_popup', 1)) {
    $commands[] = ajax_command_replace(
      '#complaint-form-wrapper',
      theme(
        'comment_abuse_complaint_popup_result',
        array(
          'message' => check_plain($message),
          'result' => $abuse_result,
        )
      )
    );

    print ajax_render($commands);
    drupal_exit();
  }
  // Single link mode.
  else {
    return array('#type' => 'ajax', '#commands' => $commands);
  }
  return $result;
}

/**