From a1fa80114b96dcf0f3f88eea4e3e7e42c6273a71 Mon Sep 17 00:00:00 2001
From: Matt Casey <mattwad@gmail.com>
Date: Tue, 5 Apr 2011 18:24:33 -0400
Subject: [PATCH] Added Terms of Use to anonymous Ubercart checkout form

---
 terms_of_use.module |   88 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/terms_of_use.module b/terms_of_use.module
index 930c8b9..c4cc7cb 100644
--- a/terms_of_use.module
+++ b/terms_of_use.module
@@ -215,6 +215,94 @@ function terms_of_use_form_user_register_alter(&$form, $form_state) {
 }
 
 /**
+ * Add to anonymous Ubercart checkout using hook_form_form_id_alter().
+ */
+function terms_of_use_form_uc_cart_checkout_form_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(tou_i18nstrings(
+    'fieldset_name',
+    variable_get('terms_of_use_fieldset_name', t('Terms of Use'))
+  ));
+  $checkbox_label = filter_xss_admin(tou_i18nstrings(
+    'checkbox_label',
+    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,
+  );
+  $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) {
-- 
1.7.4.msysgit.0

