You are here

ajax_poll.module in AJAX Poll 6

Same filename and directory in other branches
  1. 7 ajax_poll.module

Enables voting on polls without reloading the page.

File

ajax_poll.module
View source
<?php

/**
 * @file
 * Enables voting on polls without reloading the page.
 */

/**
 * Implements hook_menu().
 */
function ajax_poll_menu() {
  return array(
    'poll/ajax/vote/%node/%/%' => array(
      'type' => MENU_CALLBACK,
      'page callback' => 'ajax_poll_callback',
      'page arguments' => array(
        2,
        3,
        4,
        5,
      ),
      'access arguments' => array(
        'vote on polls',
      ),
    ),
    'poll/ajax/cancel/%node/%/%' => array(
      'type' => MENU_CALLBACK,
      'page callback' => 'ajax_poll_callback',
      'page arguments' => array(
        2,
        3,
        4,
        5,
      ),
      'access arguments' => array(
        'cancel own vote',
      ),
    ),
  );
}

/**
 * Implements hook_init().
 */
function ajax_poll_init() {

  // It would be ideal to not load this JavaScript on every page. However since
  // block caching will not add the necessary files if block caching is enabled,
  // we have to add these files on every page.
  if (variable_get('block_cache', 0)) {
    drupal_add_js('misc/jquery.form.js');
    drupal_add_js(drupal_get_path('module', 'ajax_poll') . '/ajax_poll.js');
  }
}

/**
 * Implements hook_form_alter().
 */
function ajax_poll_form_alter(&$form, $form_state, $form_id) {
  if (in_array($form_id, array(
    'poll_view_voting',
    'poll_cancel_form',
  ))) {
    drupal_add_js('misc/jquery.form.js');
    drupal_add_js(drupal_get_path('module', 'ajax_poll') . '/ajax_poll.js');
    $node = isset($form['#node']) ? $form['#node'] : node_load($form['#nid']);
    $teaser = (int) isset($node->teaser);
    $block = (int) (!empty($form['#block']));
    $action = $form_id == 'poll_view_voting' ? 'vote' : 'cancel';
    $form['ajax_url'] = array(
      '#type' => 'hidden',
      '#value' => url('poll/ajax/' . $action . '/' . $node->nid . '/' . $teaser . '/' . $block),
    );
    $form['ajax_text'] = array(
      '#type' => 'hidden',
      '#value' => $action == 'vote' ? t('Voting...') : t('Canceling...'),
    );

    // Add the AJAX Poll class to the form.
    if (empty($form['#attributes']['class'])) {
      $form['#attributes']['class'] = 'ajax-poll';
    }
    else {
      $form['#attributes']['class'] .= ' ajax-poll';
    }
    $form['#attributes']['class'] .= ' ajax-' . $action;

    // Add submit handler to supress redirection on AJAX requests.
    if ($action == 'vote') {
      $form['vote']['#submit'][] = 'ajax_poll_submit';
    }
    else {
      $form['submit']['#submit'][] = 'ajax_poll_submit';
    }
  }
}

/**
 * Form API #submit handler. Disable redirects if doing an AJAX vote.
 */
function ajax_poll_submit(&$form, &$form_state) {
  if (strpos($_GET['q'], 'poll/ajax') === 0) {
    $form_state['redirect'] = FALSE;
  }
}

/**
 * Menu callback for poll/ajax.
 */
function ajax_poll_callback($type, $node, $teaser, $block) {

  // Call poll_view to trigger the current submit handlers.
  poll_view($node);

  // Reset POST and the $node so that we get fresh copies.
  unset($_POST);
  $node = node_load($node->nid, NULL, TRUE);
  $poll = poll_view($node, $teaser, FALSE, $block);
  $status = count(drupal_get_messages('error', FALSE)) == 0;
  $messages = theme('status_messages');
  $output = drupal_render($poll->content);
  drupal_json(array(
    'status' => $status,
    'messages' => $messages,
    'output' => $output,
  ));
}

Functions

Namesort descending Description
ajax_poll_callback Menu callback for poll/ajax.
ajax_poll_form_alter Implements hook_form_alter().
ajax_poll_init Implements hook_init().
ajax_poll_menu Implements hook_menu().
ajax_poll_submit Form API #submit handler. Disable redirects if doing an AJAX vote.