You are here

function vud_vote in Vote Up/Down 7.2

Same name and namespace in other branches
  1. 6.3 vud.theme.inc \vud_vote()
  2. 6.2 vud.theme.inc \vud_vote()
  3. 7 vud.theme.inc \vud_vote()

Function for the main voting handler with Ajax support.

1 string reference to 'vud_vote'
vud_menu in ./vud.module
Implementation of hook_menu().

File

./vud.theme.inc, line 394
Theme functions

Code

function vud_vote($type, $entity_id, $value, $tag, $widget, $token, $ajax = 'ajax') {

  // If the user is anonymous we don't need to check for a token.
  if (!is_numeric($value) || !drupal_valid_token($token, "vote/{$type}/{$entity_id}/{$value}/{$tag}/{$widget}", TRUE)) {
    return MENU_ACCESS_DENIED;
  }
  $vote = array();
  $casted_vote_criteria = array(
    'entity_type' => $type,
    'entity_id' => $entity_id,
    'tag' => $tag,
  ) + votingapi_current_user_identifier();
  $casted_vote = votingapi_select_single_vote_value($casted_vote_criteria);

  // Sanity-check the incoming values.
  if ($value > 0) {
    $value = 1;
  }
  elseif ($value < 0) {
    $value = -1;
  }
  else {

    // Invalid value.
    watchdog('vud', 'Invalid vote on @type @content_id, with value @value, tag @tag and token @token', array(
      '@type' => $type,
      '@content_id' => $content_id,
      '@value' => $value,
      '@tag' => $tag,
      '@token' => $token,
    ));
    return;
  }
  $vote['value'] = $value;
  $vote['value_type'] = 'points';
  $tag = $tag ? $tag : variable_get('vud_tag', 'vote');
  $vote['tag'] = $tag;
  $vote['entity_id'] = $entity_id;
  $vote['entity_type'] = $type;
  $votes = array(
    0 => $vote,
  );
  drupal_alter('vud_votes', $votes);

  // Do not allow to vote with the same value.
  if ($casted_vote == $votes[0]['value']) {
    return;
  }
  votingapi_set_votes($votes);
  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));
      }
    }
    return ajax_deliver(array(
      '#type' => 'ajax',
      '#commands' => $commands,
    ));
  }
  else {
    drupal_goto($_SERVER['HTTP_REFERER']);
  }
}