You are here

webform_ajax_page.module in Webform Ajax Page 6

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

Ajax page element for webform

File

webform_ajax_page.module
View source
<?php

/**
 * @file
 * Ajax page element for webform
 */

/**
 * Implements hook_webform_component_info
 */
function webform_ajax_page_webform_component_info() {
  $components = array();
  $components['ajax_page'] = array(
    'label' => t('Ajax page'),
    'description' => t('Ajax page for webform'),
    'features' => array(
      'csv' => FALSE,
      'required' => FALSE,
      'conditional' => FALSE,
      'group' => TRUE,
    ),
  );
  return $components;
}

/**
 * Implements hook_form_alter
 */
function webform_ajax_page_form_alter(&$form, $form_state, $form_id) {

  //if there are more than 1 webforms on the page;
  static $amount = 0;

  //amount of forms
  static $panelamount = 0;

  //amount of ajax pages
  if (strpos($form_id, 'webform_client_form') !== FALSE) {
    $panels = array();
    $setting = array(
      'WebFormAjaxPage' => array(),
    );
    foreach (element_children($form['submitted']) as $name) {
      $component = $form['submitted'][$name];
      if ($component['#type'] == 'ajax_page') {
        $panels[] = "<a href='#{$component['#id']}-{$panelamount}'>{$component['#title']}</a>";
        $form['submitted'][$name]['#id'] = "{$component['#id']}-{$panelamount}";
        $setting['WebFormAjaxPage']['labels']["{$component['#id']}-{$panelamount}"] = array(
          'next' => $component['#webform_component']['extra']['next_label'],
          'previous' => $component['#webform_component']['extra']['prev_label'],
        );
        $panelamount++;
      }
    }
    if (!empty($setting['WebFormAjaxPage'])) {
      drupal_add_js($setting, 'setting');
    }
    $hide_submit_nids = variable_get('webform_ajax_page_hide_submit_nodes', array());
    $hide_submit = array_key_exists($form['#node']->nid, $hide_submit_nids) ? $hide_submit_nids[$form['#node']->nid] : variable_get('webform_ajax_page_hide_submit', 1);
    if (!empty($panels)) {
      $setting = array(
        'WebFormAjaxPage' => array(
          'forms' => array(
            $amount => array(
              'id' => str_replace('_', '-', $form_id),
              'wrapper' => "webform-page-tabs-{$amount}",
              'hide_submit' => $hide_submit,
            ),
          ),
        ),
      );
      drupal_add_js($setting, 'setting');
      $list = "<ul>\n";
      $i = 0;
      $panelnumber = 0;
      foreach ($panels as $panel) {
        $list .= "<li class='panel-{$panelnumber}'>{$panel}</li>\n";
        $panelnumber++;
      }
      $list .= "</ul>\n";
      $form['submitted']['tabs_prefix'] = array(
        '#type' => 'markup',
        '#value' => "<div id='webform-page-tabs-{$amount}'>" . $list,
        '#weight' => -1,
      );
      $form['submitted']['tabs_suffix'] = array(
        '#type' => 'markup',
        '#value' => '</div>',
        '#weight' => 1500,
      );
      $amount++;
    }
  }
  elseif ($form_id == 'webform_admin_settings') {
    $form['buttons']['#weight'] = 1;
    $form['ajax_page'] = array(
      '#type' => 'fieldset',
      '#title' => 'Webform Ajax Page',
      '#description' => 'Webform ajax page settings',
    );
    $form['ajax_page']['webform_ajax_page_css_choice'] = array(
      '#type' => 'radios',
      '#title' => t('CSS libraries to include'),
      '#description' => t('Select the CSS library to include when rendering the ajax pages.'),
      '#default_value' => variable_get('webform_ajax_page_css_choice', 'module_css'),
      '#options' => array(
        0 => t('None'),
        'module_css' => t('Default module CSS (No tabs, previous/next buttons for paging)'),
      ),
    );

    // Add in the option to use the default jQuery Theme CSS if available.
    $jquery_ui_path = jquery_ui_get_path();
    $jquery_ui_theme_css_path = $jquery_ui_path . '/themes/base/';
    if (realpath($jquery_ui_theme_css_path) !== FALSE) {
      $form['ajax_page']['webform_ajax_page_css_choice']['#options']['jquery_ui_css'] = t('jQuery UI Base Theme CSS (Tabs display for pages)');
    }
    else {
      $form['ajax_page']['webform_ajax_page_css_choice']['#description'] .= '<br />' . t('NOTE: Add the jQuery UI Base theme to your jQuery UI library folder in order to enable additional options.');
    }
    $form['ajax_page']['webform_ajax_page_prev_label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label for the "previous page" button'),
      '#description' => t('This is the label that will appear on the button to go to the previous page'),
      '#default_value' => t(variable_get('webform_ajax_page_prev_label', '<< Previous Page')),
    );
    $form['ajax_page']['webform_ajax_page_next_label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label for the "next page" button'),
      '#description' => t('This is the label that will appear on the button to go to the next page'),
      '#default_value' => t(variable_get('webform_ajax_page_next_label', 'Next Page >>')),
    );
    $form['ajax_page']['webform_ajax_page_hide_submit'] = array(
      '#type' => 'radios',
      '#title' => t('Hide the submit button until the last page?'),
      '#description' => t('Determines if the "Submit" button on the form is hidden until the user gets to the last page of the form.'),
      '#default_value' => variable_get('webform_ajax_page_hide_submit', 1),
      '#options' => array(
        0 => t('No'),
        1 => t('Yes'),
      ),
    );
  }
  elseif ($form_id == "webform_components_form") {
    $types = array();
    $node = $form['#node'];
    foreach ($node->webform['components'] as $component) {
      $types[] = $component['type'];
    }
    if (in_array('pagebreak', $types) && in_array('ajax_page', $types)) {
      drupal_set_message(t('Warning: You currently have page breaks and ajax pages in your webform. This will break the ajax page functionality.'), 'warning');
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function webform_ajax_page_form_webform_configure_form_alter(&$form, $form_state) {
  if (webform_node_has_ajax_page($form['#node'])) {
    $form['ajax_page_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Ajax Page settings'),
      '#description' => t('Configure Ajax Page settings for this webform'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $hide_submit_nids = variable_get('webform_ajax_page_hide_submit_nodes', array());
    $default = array_key_exists($form['#node']->nid, $hide_submit_nids) ? $hide_submit_nids[$form['#node']->nid] : variable_get('webform_ajax_page_hide_submit', 1);
    $form['ajax_page_settings']['ajax_page_hide_submit_override'] = array(
      '#type' => 'checkbox',
      '#title' => t('Override submit button visibility setting'),
      '#description' => t('Override the default setting to hide the submit button until the last page or to show it from the beginning'),
      '#default_value' => array_key_exists($form['#node']->nid, $hide_submit_nids),
    );
    $form['ajax_page_settings']['ajax_page_hide_submit'] = array(
      '#type' => 'radios',
      '#title' => t('Hide the submit button until the last page?'),
      '#description' => t('Determines if the "Submit" button on the form is hidden until the user gets to the last page of the form.'),
      '#default_value' => $default,
      '#options' => array(
        0 => t('No'),
        1 => t('Yes'),
      ),
    );
    drupal_add_js(drupal_get_path('module', 'webform_ajax_page') . '/webform_ajax_page.admin.js');
    $form['#submit'][] = 'webform_ajax_page_webform_configure_form_submit';
  }
}

/**
 * Submit callback.
 */
function webform_ajax_page_webform_configure_form_submit($form, $form_state) {
  $hide_submit_nids = variable_get('webform_ajax_page_hide_submit_nodes', array());
  if (!$form_state['values']['ajax_page_hide_submit_override']) {
    unset($hide_submit_nids[$form['#node']->nid]);
  }
  else {
    $hide_submit_nids[$form['#node']->nid] = $form_state['values']['ajax_page_hide_submit'];
  }
  variable_set('webform_ajax_page_hide_submit_nodes', $hide_submit_nids);
}

/**
 * Implementation of _webform_defaults_component().
 */
function _webform_defaults_ajax_page() {
  return array(
    'name' => '',
    'form_key' => NULL,
    'pid' => 0,
    'weight' => 0,
    'extra' => array(
      'title_display' => 0,
      'collapsible' => 0,
      'collapsed' => 0,
      'description' => '',
      'prev_label' => t(variable_get('webform_ajax_page_prev_label', '<< Previous Page')),
      'next_label' => t(variable_get('webform_ajax_page_next_label', 'Next Page >>')),
    ),
  );
}

/**
 * Implementation of _webform_render_component().
 */
function _webform_render_ajax_page($component, $value = NULL, $filter = TRUE) {
  $component['weight'] += 10;
  $class = 'webform-component-' . str_replace('_', '-', $component['type']) . ' ui-tabs-panel';
  $element = array(
    '#type' => $component['type'],
    '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : NULL,
    '#weight' => $component['weight'],
    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
    '#attributes' => array(
      'class' => $class,
    ),
    '#pre_render' => array(
      'webform_element_title_display',
    ),
    '#webform_component' => $component,
    //id's are made unique in hook_form_alter
    '#id' => 'webform-component-' . str_replace('_', '-', $component['type']),
  );
  $ajax_page_count++;
  return $element;
}

/**
 * Implements _webform_theme_component
 */
function _webform_theme_ajax_page() {
  return array(
    'ajax_page' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
}

/**
 * Theme callback
 */
function theme_ajax_page($element) {
  webform_ajax_page_insert_js_css();
  return '<div id="' . $element['#id'] . '" ' . drupal_attributes($element['#attributes']) . ' >' . ($element['#title'] ? '<h3>' . $element['#title'] . '</h3>' : '') . (isset($element['#description']) && $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '') . (!empty($element['#children']) ? $element['#children'] : '') . (isset($element['#value']) ? $element['#value'] : '') . "</div>\n";
}

/**
 * Helper function for embedding the module js and css files.
 */
function webform_ajax_page_insert_js_css() {
  static $embedded = FALSE;
  if (!$embedded) {
    jquery_ui_add(array(
      'ui.tabs',
    ));
    drupal_add_js(drupal_get_path('module', 'webform_ajax_page') . '/webform_ajax_page.js');
    switch (variable_get('webform_ajax_page_css_choice', 'module_css')) {
      case 'module_css':
        drupal_add_css(drupal_get_path('module', 'webform_ajax_page') . '/webform_ajax_page.css');
        break;
      case 'jquery_ui_css':
        $jquery_ui_path = jquery_ui_get_path();
        $jquery_ui_theme_path = $jquery_ui_path . '/themes/base/';
        if (realpath($jquery_ui_theme_path) !== FALSE) {
          drupal_add_css($jquery_ui_theme_path . 'ui.all.css');
          drupal_add_css($jquery_ui_theme_path . 'jquery-ui.css');
          drupal_add_css($jquery_ui_theme_path . 'ui.tabs.css');
        }
        break;
    }
    $embedded = TRUE;
  }
}

/**
 * Implementation of _webform_display_component().
 */
function _webform_display_ajax_page($component, $value, $format = 'html') {
  if ($format == 'text') {
    $element = array(
      '#title' => $component['name'],
      '#weight' => $component['weight'],
      '#post_render' => array(
        'webform_element_wrapper',
      ),
      '#theme_wrappers' => array(
        'webform_element_text',
      ),
    );
  }
  else {
    $element = _webform_render_ajax_page($component, $value);
  }
  $element['#webform_component'] = $component;
  $element['#format'] = $format;
  return $element;
}

/**
 * Implementation of _webform_edit_component().
 */
function _webform_edit_ajax_page($component) {
  $form['display']['prev_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label for the "previous page" button'),
    '#description' => t('This is the label that will appear on the button to go to the previous page (e.g. "Go to Page 1")'),
    '#default_value' => $component['extra']['prev_label'],
    '#parents' => array(
      'extra',
      'prev_label',
    ),
  );
  $form['display']['next_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label for the "next page" button'),
    '#description' => t('This is the label that will appear on the button to go to the next page (e.g. "Go to Page 3")'),
    '#default_value' => $component['extra']['next_label'],
    '#parents' => array(
      'extra',
      'next_label',
    ),
  );
  return $form;
}

/**
 * Helper function to check if a webform has an ajax page component.
 */
function webform_node_has_ajax_page($node) {
  if (!isset($node->webform) || !isset($node->webform['components']) || empty($node->webform['components'])) {
    return FALSE;
  }
  foreach ($node->webform['components'] as $component) {
    if ($component['type'] == 'ajax_page') {
      return TRUE;
    }
  }
  return FALSE;
}

Functions

Namesort descending Description
theme_ajax_page Theme callback
webform_ajax_page_form_alter Implements hook_form_alter
webform_ajax_page_form_webform_configure_form_alter Implements hook_form_FORM_ID_alter().
webform_ajax_page_insert_js_css Helper function for embedding the module js and css files.
webform_ajax_page_webform_component_info Implements hook_webform_component_info
webform_ajax_page_webform_configure_form_submit Submit callback.
webform_node_has_ajax_page Helper function to check if a webform has an ajax page component.
_webform_defaults_ajax_page Implementation of _webform_defaults_component().
_webform_display_ajax_page Implementation of _webform_display_component().
_webform_edit_ajax_page Implementation of _webform_edit_component().
_webform_render_ajax_page Implementation of _webform_render_component().
_webform_theme_ajax_page Implements _webform_theme_component