Commit 6e2a6765 authored by Marco Villegas's avatar Marco Villegas
Browse files

Issue #847846 by mansspams, marvil07, jaydub, monotaga: Provide 'Reset your...

Issue #847846 by mansspams, marvil07, jaydub, monotaga: Provide 'Reset your vote' link on the widget.

This is a 7.x-1.x port of the original 6.x-3.x patch at #847846-25

Several changes was done in order to have D7 compatibility, but it's also
focused on vud and vud_field, trying to follow #1360572.
parent 5de154e8
Loading
Loading
Loading
Loading
+9 −0
Changes for vud.install: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -33,3 +33,12 @@ function vud_update_6200() {

  return $ret;
}

/**
 * Rebuild menu for 'votereset/%/%/%/%/%'.
 */
function vud_update_7000() {
  $result = array();
  menu_rebuild();
  return $result;
}
+3 −27
Changes for vud.module: 3 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -39,13 +39,14 @@ function vud_menu() {
    'file'             => 'vud.theme.inc',
  );

  $items['votereset/%/%/%/%'] = array(
  $items['votereset/%/%/%/%/%'] = array(
    'title'            => 'Reset vote',
    'page callback'    => 'vud_reset',
    'page arguments'   => array(1, 2, 3, 4),
    'page arguments'   => array(1, 2, 3, 4, 5),
    'access callback'  => 'vud_access_callback',
    'access arguments' => array('reset vote up/down votes', 1, 2, 3, 4),
    'type'             => MENU_CALLBACK,
    'file'             => 'vud.theme.inc',
  );

  $items['user/%user/votes'] = array(
@@ -205,31 +206,6 @@ function vud_user_votes() {
  }
}

/**
 * Callback to reset votes on an object.
 */
function vud_reset($type, $entity_id, $tag, $token) {
  if (drupal_valid_token($token, "votereset/$type/$entity_id/$tag", TRUE)) {
    $uid = votingapi_current_user_identifier();
    $criteria = array(
      'entity_type' => $type,
      'entity_id' => $entity_id,
      'tag' => $tag,
    );
    $fullc = $criteria + $uid;
    votingapi_delete_votes(votingapi_select_votes($fullc));
    votingapi_recalculate_results($type, $entity_id);

    drupal_goto($_SERVER['HTTP_REFERER']);
  }
  else {
    watchdog('vud',
      'Could not reset votes on @type @entity_id, with value @value, tag @tag and token @token',
      array('@type' => $type, '@entity_id' => $entity_id, '@value' => $value, '@tag' => $tag, '@token' => $token));
    drupal_set_message(t("Oops! There was an error in resetting your vote!", 'warning'));
  }
}

/**
 * Menu callback; show widget message.
 */
+74 −0
Changes for vud.theme.inc: 74 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -141,16 +141,22 @@ function vud_widget_proxy($variables) {
    'id' => 'widget-' . $variables['type'] . '-' . $variables['entity_id'],
    'link_class_up' => 'vud-link-up',
    'link_class_down' => 'vud-link-down',
    'link_class_reset' => 'vud-link-reset',

    'link_up' => '',
    'link_down' => '',

    'class_up' => 'up-active',
    'class_down' => 'down-active',
    'class_reset' => 'reset-inactive',

    'show_links' => FALSE,
    'show_reset' => FALSE,
    'show_up_as_link' => FALSE,
    'show_down_as_link' => FALSE,

    'reset_long_text' => t('Reset your vote'),
    'reset_short_text' => t('(reset)'),
  );

  $type = $variables['type'];
@@ -191,6 +197,7 @@ function vud_widget_proxy($variables) {

      $up_access = $user_vote <= 0;
      $down_access = $user_vote >= 0;
      $reset_active = vud_access_callback('reset vote up/down votes', $type, $entity_id, NULL, $tag);

      if (!$up_access && !$down_access) {
        // Although implementing modules have requested a not readonly widget,
@@ -213,6 +220,10 @@ function vud_widget_proxy($variables) {
      $variables['class_down'] = 'down-inactive';
      $variables['show_down_as_link'] = TRUE;
    }
    if ($up_access || $down_access) {
      $variables['show_reset'] = TRUE;
      $variables['class_reset'] = 'reset-active';
    }
    $variables['show_links'] = TRUE;
  }

@@ -228,6 +239,11 @@ function vud_widget_proxy($variables) {
      $variables['link_down'] = url("vote/$type/$entity_id/-1/$tag/$widget_theme/$token_down/nojs");
      $variables['link_class_down'] .= ' use-ajax';
    }
    if ($up_access || $down_access) {
      $token_reset = drupal_get_token("votereset/$type/$entity_id/$tag/$widget_theme");
      $variables['link_reset'] = url("votereset/$type/$entity_id/$tag/$widget_theme/$token_reset");
      $variables['link_class_reset'] .= ' use-ajax';
    }
  }
  elseif ($readonly && $message_on_deny) {
    // Readonly widget, but with a deny messgae.
@@ -239,16 +255,21 @@ function vud_widget_proxy($variables) {
    else {
      $variables['link_class_down'] .= ' denied ctools-use-modal';
    }
    if ($up_access || $down_access) {
      $variables['link_class_reset'] .= ' denied ctools-use-modal';
    }

    // TODO: Get default $widget_message_code or one from $variables.
    $widget_message_code = VUD_WIDGET_MESSAGE_DENIED;
    $variables['link_up'] = url(sprintf('vud/nojs/denied/%d', $widget_message_code));
    $variables['link_down'] = url(sprintf('vud/nojs/denied/%d', $widget_message_code));
    $variables['link_reset'] = url(sprintf('vud/nojs/denied/%d', $widget_message_code));
  }
  else {
    // Readonly widget.
    $variables['link_up'] = '#';
    $variables['link_down'] = '#';
    $variables['link_reset'] = '#';
  }

  $result_criteria = array(
@@ -447,3 +468,56 @@ function vud_vote($type, $entity_id, $value, $tag, $widget, $token, $ajax = 'aja
    drupal_goto($_SERVER['HTTP_REFERER']);
  }
}

/**
 * Function for the reset handler with Ajax support.
 */
function vud_reset($type, $entity_id, $tag, $widget, $token, $ajax = 'ajax') {
  if (drupal_valid_token($token, "votereset/$type/$entity_id/$tag/$widget", TRUE)) {
    $criteria = array(
      'entity_type' => $type,
      'entity_id' => $entity_id,
      'tag' => $tag,
    ) + votingapi_current_user_identifier();
    votingapi_delete_votes(votingapi_select_votes($criteria));
    votingapi_recalculate_results($type, $entity_id);
  }
  else {
    watchdog('vud',
      'Could not reset votes on @type @entity_id, tag @tag and token @token',
      array('@type' => $type, '@entity_id' => $entity_id, '@tag' => $tag, '@token' => $token));
    drupal_set_message(t('Oops! There was an error resetting your vote!'), 'warning');
  }

  if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
    if ($ajax == 'ajax') {
      $plugin = vud_widget_get($widget);
      $commands = array();

      if ($function = ctools_plugin_get_function($plugin, 'ajax render')) {
        $commands = $function($type, $entity_id, $value, $tag, $token, $widget);
      }
      else {
        $variables = array(
          'entity_id' =>$entity_id,
          'type' => $type,
          'tag' => $tag,
          'widget_theme' => $widget,
        );
        if (!empty($plugin['widget template'])) {
          $commands[] = ajax_command_replace("#widget-$type-$entity_id", theme('vud_widget', $variables));
        }

        if (!empty($plugin['votes template'])) {
          $commands[] = ajax_command_replace("#votes-$type-$entity_id", theme('vud_votes', $variables));
        }
      }

      print ajax_render($commands);
      exit;
    }
  }
  else {
    drupal_goto($_SERVER['HTTP_REFERER']);
  }
}
+2 −2
Changes for vud_comment/vud_comment.module: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -201,10 +201,10 @@ function vud_comment_link($type, $object, $teaser = FALSE) {
        $criteria += votingapi_current_user_identifier();
        $user_vote = votingapi_select_single_vote_value($criteria);
        if (!is_null($user_vote)) {
          $reset_token = drupal_get_token("votereset/comment/$comment->cid/$tag");
          $reset_token = drupal_get_token("votereset/comment/$comment->cid/$tag/$widget_theme");
          $links['vud_comment_votes_reset_link'] = array(
            'title' => t('Reset your vote'),
            'href'  => "votereset/comment/$comment->cid/$tag/$reset_token",
            'href'  => "votereset/comment/$comment->cid/$tag/$widget_theme/$reset_token",
            'attributes' => array('rel' => 'nofollow'),
            'html'  => TRUE,
          );
+2 −2
Changes for vud_node/vud_node.module: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -302,10 +302,10 @@ function vud_node_link($type, $object, $teaser = FALSE) {
        $criteria += votingapi_current_user_identifier();
        $user_vote = votingapi_select_single_vote_value($criteria);
        if (!is_null($user_vote)) {
          $reset_token = drupal_get_token("votereset/node/$node->nid/$tag");
          $reset_token = drupal_get_token("votereset/node/$node->nid/$tag/$widget_theme");
          $links['vud_node_votes_reset_link'] = array(
            'title' => t('Reset your vote'),
            'href'  => "votereset/node/$node->nid/$tag/$reset_token",
            'href'  => "votereset/node/$node->nid/$tag/$widget_theme/$reset_token",
            'attributes' => array('rel' => 'nofollow'),
            'html'  => TRUE,
          );
Loading