You are here

terms_of_use.module in Terms of Use 6

Same filename and directory in other branches
  1. 8.2 terms_of_use.module
  2. 8 terms_of_use.module
  3. 7 terms_of_use.module

Adds Terms of Use to the 'user_register' form.

This module adds Terms of Use to the registration page.

File

terms_of_use.module
View source
<?php

/**
 * @file
 * Adds Terms of Use to the 'user_register' form.
 *
 * This module adds Terms of Use to the registration page.
 */

/**
 * Implementation of hook_menu().
 */
function terms_of_use_menu() {
  $items = array();
  $items['admin/settings/terms_of_use'] = array(
    'description' => 'Add Terms of Use to the registration page.',
    'title' => 'Terms of Use',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'terms_of_use_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  $items['node/autocomplete'] = array(
    'title' => 'Autocomplete node title',
    'page callback' => 'terms_of_use_autocomplete',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'terms_of_use.pages.inc',
  );
  $items['terms_of_use/js'] = array(
    'page callback' => 'terms_of_use_js',
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'terms_of_use.pages.inc',
  );
  return $items;
}

/**
 * Menu callback; show settings form.
 * @see terms_of_use_admin_settings_validate()
 *
 */
function terms_of_use_admin_settings() {

  // Adding the fieldset for node specification.
  $form['terms_of_use_text'] = array(
    '#type' => 'fieldset',
    '#prefix' => '<div id="fieldset-wrapper">',
    '#suffix' => '</div>',
  );
  $form['terms_of_use_text']['terms_of_use_node_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title of the post where your Terms of Use are published'),
    '#default_value' => variable_get('terms_of_use_node_title', ''),
    '#description' => t('Node <em>title</em> of the page or story (or blog entry or book page) where your Terms of Use are published.'),
    '#autocomplete_path' => 'node/autocomplete',
  );
  $form['terms_of_use_text']['terms_of_use_pick_node_id'] = array(
    '#type' => 'button',
    '#value' => t('I prefer to specify the node id'),
    '#weight' => 10,
    '#ahah' => array(
      'path' => 'terms_of_use/js',
      'wrapper' => 'fieldset-wrapper',
    ),
  );

  // Adding the fieldset for form specification.
  $form['terms_of_use_form'] = array(
    '#type' => 'fieldset',
  );
  $form['terms_of_use_form']['terms_of_use_fieldset_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Label for the fieldset'),
    '#default_value' => variable_get('terms_of_use_fieldset_name', t('Terms of Use')),
    '#description' => t('The text for the Terms of Use and the [x] checkbox are contained in a fieldset. Type here the title for that fieldset.'),
  );
  $form['terms_of_use_form']['terms_of_use_checkbox_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label for the checkbox'),
    '#default_value' => variable_get('terms_of_use_checkbox_label', t('I agree with these terms.')),
    '#description' => t('Type here something like "I agree with these terms." or "I CERTIFY THAT I AM OVER THE AGE OF 18 YEARS OLD.", without quotes. You can use the token @link to insert a link to the Terms in this label. For example, the label can be: "I agree with the @link.", without quotes. You may want to link to the Terms if you prefer not to show the full text of the Terms in the registration form. If you use the token, the Terms will not be shown.'),
  );
  return system_settings_form($form);
}

/**
 * Validate the terms_of_use_admin_settings form.
 *
 * @see terms_of_use_admin_settings()
 */
function terms_of_use_admin_settings_validate($form, &$form_state) {
  if (isset($form_state['values']['terms_of_use_node_id'])) {
    $nid = $form_state['values']['terms_of_use_node_id'];
    if (empty($nid)) {
      form_set_error('terms_of_use_node_id', t('You must specify a node <em>nid</em>.'));
    }
    else {
      $node = node_load($nid);
      if ($node == FALSE) {
        form_set_error('terms_of_use_node_id', t('No post was published with <em>nid</em> !nid.', array(
          '!nid' => $nid,
        )));
      }
      else {
        variable_set('terms_of_use_node_title', $node->title);
      }
    }
  }
  elseif (!empty($form_state['values']['terms_of_use_node_title'])) {
    $node = node_load(array(
      'title' => $form_state['values']['terms_of_use_node_title'],
    ));
    if ($node == FALSE) {
      form_set_error('terms_of_use_node_title', t('No post was published with this title.'));
    }
    else {
      variable_set('terms_of_use_node_id', $node->nid);
    }
  }
  else {
    form_set_error('terms_of_use_node_title', t('You must specify a node title.'));
  }
}

/**
 * Implementation of hook_form_form_id_alter().
 */
function terms_of_use_form_user_register_alter(&$form, $form_state) {

  // Administrative users can skip this. So admin/user/user/create won't show
  // the terms of use.
  if (user_access('administer users')) {
    return;
  }
  $fieldset_name = filter_xss(variable_get('terms_of_use_fieldset_name', t('Terms of Use')));
  $checkbox_label = filter_xss_admin(variable_get('terms_of_use_checkbox_label', t('I agree with these terms.')));

  // Adding the fieldset.
  $form['terms_of_use'] = array(
    '#type' => 'fieldset',
    '#title' => $fieldset_name,
    '#weight' => 10,
  );
  $show_terms = TRUE;

  // Getting the nid of the the Terms of Use node.
  $terms_of_use_node_id = variable_get('terms_of_use_node_id', 0);

  // If the translation module is active the node might be available in other
  // languages.
  if (module_exists('translation')) {
    $translations = translation_node_get_translations($terms_of_use_node_id);
    if (!empty($translations[$GLOBALS['language']->language])) {
      $terms_of_use_node_id = $translations[$GLOBALS['language']->language]->nid;
    }
  }

  // A nid for the desired language was found.
  if ($terms_of_use_node_id) {
    $node = node_load($terms_of_use_node_id);

    // The node could be loaded.
    if ($node->nid) {

      // Show terms on the registration for or just a link?s
      if (strpos($checkbox_label, '@link') !== FALSE) {
        $checkbox_label = str_replace('@link', l($node->title, 'node/' . $node->nid), $checkbox_label);
        $show_terms = FALSE;
      }

      // Adding the nodes body by theme_terms_of_use() to the fieldset if desired.
      if ($show_terms) {
        $node = node_prepare(node_load($terms_of_use_node_id));
        if ($node->body) {
          drupal_add_css(drupal_get_path('module', 'terms_of_use') . '/terms_of_use.css');
          $form['terms_of_use']['terms_of_use_text'] = array(
            '#value' => theme('terms_of_use', $node->body, $node),
          );
        }
        else {
          watchdog('Terms of use', 'The body field of the terms of use node was empty. Please check the the nodes content.', array(), WATCHDOG_ALERT, l('Administer terms of use', 'admin/settings/terms_of_use'));
        }
      }
    }
    else {
      watchdog('Terms of use', 'The terms of use node could not be loaded. Please check the settings and the node.', array(), WATCHDOG_ALERT, l('Administer terms of use', 'admin/settings/terms_of_use'));
    }
  }
  else {
    watchdog('Terms of use', 'No node is set to use as terms of use in the currecnt language [' . $GLOBALS['language']->language . '].', array(), WATCHDOG_NOTICE, l('Administer terms of use', 'admin/settings/terms_of_use'));
  }

  // Checkbox validate handles denoting required checkboxes for us
  if (module_exists('checkbox_validate')) {
    $asterisk = '';
  }
  else {
    $asterisk = '&nbsp;<span class="form-required" title="' . t('This field is required') . '">*</span>';
  }

  // Adding the checkbox to the fieldset.
  $form['terms_of_use']['terms_of_use'] = array(
    '#type' => 'checkbox',
    '#title' => $checkbox_label . $asterisk,
    '#required' => TRUE,
    '#element_validate' => array(
      '_terms_of_use_validate_checkbox',
    ),
  );
  return $form;
}

/**
 * Validation callback; verify that checkbox is checked.
 */
function _terms_of_use_validate_checkbox($form, &$form_state) {
  $value = $form_state['values']['terms_of_use'];
  if ($value == 0) {
    form_set_error('terms_of_use', t('You must agree with the !terms to get an account.', array(
      '!terms' => filter_xss(variable_get('terms_of_use_fieldset_name', t('Terms of Use'))),
    )));
  }
}

/**
 * Implementation of hook_theme().
 */
function terms_of_use_theme() {
  return array(
    'terms_of_use' => array(
      'arguments' => array(
        'terms' => NULL,
        'node' => NULL,
      ),
    ),
  );
}

/**
 * Output the terms of use.
 *
 * @param $terms
 *  The terms of service, already formatted.
 * @param $node
 *  The $node object, in case we need it.
 * @return
 *  HTML output.
 * @ingroup themeable
 */
function theme_terms_of_use($terms, $node = NULL) {
  $output = '<div id="terms-of-use" class="content clear-block">';
  $output .= $terms;
  $output .= '</div>';
  return $output;
}

Functions

Namesort descending Description
terms_of_use_admin_settings Menu callback; show settings form.
terms_of_use_admin_settings_validate Validate the terms_of_use_admin_settings form.
terms_of_use_form_user_register_alter Implementation of hook_form_form_id_alter().
terms_of_use_menu Implementation of hook_menu().
terms_of_use_theme Implementation of hook_theme().
theme_terms_of_use Output the terms of use.
_terms_of_use_validate_checkbox Validation callback; verify that checkbox is checked.