You are here

function vud_reset in Vote Up/Down 7

Same name and namespace in other branches
  1. 6.3 vud.theme.inc \vud_reset()
  2. 6.2 vud.module \vud_reset()
  3. 7.2 vud.theme.inc \vud_reset()

Function for the reset handler with Ajax support.

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

File

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

Code

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));
        }
      }
      return ajax_deliver(array(
        '#type' => 'ajax',
        '#commands' => $commands,
      ));
    }
  }
  else {
    drupal_goto($_SERVER['HTTP_REFERER']);
  }
}