uc_termsofservice.module in Ubercart Terms of Service 6
Same filename and directory in other branches
Ubercart Terms of Service.
File
uc_termsofservice.moduleView source
<?php
/**
* @file
* Ubercart Terms of Service.
*/
// Include the conditional actions for displaying the panes.
require_once 'uc_termsofservice.ca.inc';
/**
* Implements hook_menu().
*/
function uc_termsofservice_menu() {
$items = array();
$items['uc_termsofservice/node/autocomplete'] = array(
'title' => 'Autocomplete of nodes',
'page callback' => 'uc_termsofservice_node_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
/**
* modalframe callback items
*/
if (module_exists('modalframe')) {
$items['uc_termsofservice/show/%node'] = array(
'title' => 'Show Terms of Service',
'title callback' => 'uc_termsofservice_title_callback',
'title arguments' => array(
2,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uc_termsofservice_general_form',
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
}
return $items;
}
/**
* Implements hook_theme().
*/
function uc_termsofservice_theme() {
return array(
'uc_termsofservice_agreement_form' => array(
'arguments' => array(
'form' => NULL,
),
'template' => 'uc_termsofservice_agreement_form',
),
);
}
/**
* Ubercart hooks.
*/
/**
* Implements hook_cart_pane().
*/
function uc_termsofservice_cart_pane($items) {
$node = uc_termsofservice_get_node('cart');
$title = t('Terms of Service');
if (isset($node->title)) {
$title = $node->title;
}
$panes[] = array(
'id' => 'uc_termsofservice_agreement_cart',
'title' => t('@title', array(
'@title' => $title,
)),
'desc' => t('Please confirm if you agree with our terms and conditions that apply on all our purchases.'),
'weight' => 6,
'body' => !is_null($items) ? drupal_get_form('uc_termsofservice_agreement_cart_callback', $items) : '',
);
return $panes;
}
/**
* Implements hook_checkout_pane().
*/
function uc_termsofservice_checkout_pane() {
$node = uc_termsofservice_get_node('checkout');
$title = t('Terms of Service');
if (isset($node->title)) {
$title = $node->title;
}
$panes[] = array(
'id' => 'uc_termsofservice_agreement_checkout',
'callback' => 'uc_termsofservice_checkout_pane_callback',
'title' => t('@title', array(
'@title' => $title,
)),
'desc' => t('Please confirm if you agree with our terms and conditions that apply on all our purchases.'),
'weight' => 6,
'collapsible' => TRUE,
);
return $panes;
}
/**
* Callback form for cart pane.
*/
function uc_termsofservice_agreement_cart_callback($items) {
if (module_exists('modalframe') && variable_get('uc_termsofservice_cart_popup', 0)) {
// If the modalframe module is enabled and the config for popups is
// then the ToS is shown in a popup.
modalframe_parent_js();
drupal_add_js(drupal_get_path('module', 'uc_termsofservice') . '/uc_termsofservice.js');
$node = uc_termsofservice_get_node('cart');
$width = variable_get('uc_termsofservice_cart_popup_width', 500);
$height = variable_get('uc_termsofservice_cart_popup_height', 300);
$form = uc_termsofservice_get_item('cart', $node->title, 'uc_termsofservice/show/' . $node->nid, "{$width},{$height}");
}
else {
$form = uc_termsofservice_general_form(array(), 'cart');
}
return $form;
}
/**
* General form for both checkout & cart modes.
*
* @see uc_termsofservice_general_form_submit()
*/
function uc_termsofservice_general_form($form_state, $type = NULL) {
$form = array();
if (!$type) {
$type = arg(2);
}
$node = uc_termsofservice_get_node($type);
if ($node) {
$form['tos_text'] = array(
'#value' => node_view($node, FALSE, TRUE, FALSE),
);
$form['tos_agree'] = array(
'#type' => 'checkboxes',
'#options' => array(
'agreed' => t('I agree with the terms above'),
),
);
$form['#theme'] = 'uc_termsofservice_agreement_form';
if (module_exists('modalframe') && variable_get('uc_termsofservice_' . $type . '_popup', 0)) {
// Send the Modal Frame javascript for child windows to the page.
modalframe_child_js();
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['tos_agree']['#attributes'] = array(
'onclick' => 'this.form.submit();',
);
}
return $form;
}
return;
}
/**
* Submit handler for uc_termsofservice_general_form().
*
* @see uc_termsofservice_general_form()
*/
function uc_termsofservice_general_form_submit($form, &$form_state) {
modalframe_close_dialog(array(
'tos_selected' => $form_state['values']['tos_agree'],
));
}
/**
* Callback form for checkout pane.
*/
function uc_termsofservice_checkout_pane_callback($op) {
switch ($op) {
case 'view':
$show_tos = TRUE;
// Check the conditional actions only if the user explicitly says to.
if (variable_get('uc_termsofservice_checkout_ca', 0)) {
$arguments = array(
'cart' => array(
'#entity' => 'cart',
'#title' => t('Cart'),
'#data' => uc_cart_get_contents(),
),
);
$predicates = ca_load_trigger_predicates('uc_termsofservice_display_pane');
$predicate = $predicates['uc_termsofservice_display_pane'];
if (!$predicate || !ca_evaluate_conditions($predicate, $arguments)) {
$show_tos = FALSE;
}
}
if ($show_tos) {
if (module_exists('modalframe') && variable_get('uc_termsofservice_checkout_popup', 0)) {
// If the modalframe module is enabled and the config for popups is
// then the ToS is shown in a popup.
modalframe_parent_js();
drupal_add_js(drupal_get_path('module', 'uc_termsofservice') . '/uc_termsofservice.js');
$node = uc_termsofservice_get_node('checkout');
$width = variable_get('uc_termsofservice_checkout_popup_width', 500);
$height = variable_get('uc_termsofservice_checkout_popup_height', 300);
$form = uc_termsofservice_get_item('checkout', $node->title, 'uc_termsofservice/show/' . $node->nid, "{$width},{$height}");
}
else {
$form = uc_termsofservice_general_form(array(), 'checkout');
}
return array(
'contents' => $form,
);
}
break;
case 'settings':
$form = uc_termsofservice_admin_form('checkout');
return $form;
break;
}
}
/**
* Function that filters the node nid from the autocomplete string.
*/
function uc_termsofservice_get_nid_from_variable($type = NULL) {
$nid = 0;
$tos_node = variable_get('uc_termsofservice_' . $type . '_node', $nid);
$preg_matches = array();
$match = preg_match('/\\[nid: (\\d+)\\]/', $tos_node, $preg_matches);
if ($match) {
$nid = $preg_matches[1];
}
return $nid;
}
/**
* Retrieves the ToS node from database.
*/
function uc_termsofservice_get_node($type = NULL, $nid = NULL) {
if (!$nid) {
$nid = uc_termsofservice_get_nid_from_variable($type);
}
if ($nid) {
if (module_exists('translation')) {
global $language;
$translations = translation_node_get_translations($nid);
if (isset($translations[$language->language])) {
$nid = $translations[$language->language]->nid;
}
}
$node = node_load($nid);
return $node;
}
return;
}
/**
* Helper function for ModalFrame to build links to popup page.
*/
function uc_termsofservice_get_item($type = NULL, $title, $path, $size = NULL) {
$options = array(
'attributes' => array(
'class' => 'uc_termsofservice-child' . (!empty($size) ? ' uc_termsofservice-size[' . $size . ']' : ''),
),
);
$form['tos_agree_popup'] = array(
'#type' => 'checkboxes',
'#options' => array(
'agreed' => t('I agree with the !tos', array(
'!tos' => l($title, $path . '/' . $type, $options),
)),
),
);
return $form;
}
/**
* Settings form for checkout & cart panes.
*/
function uc_termsofservice_admin_form($type = NULL) {
if ($type) {
$form = array();
// Required option only for checkout by the moment.
if ($type == 'checkout') {
$form['uc_termsofservice_' . $type . '_required'] = array(
'#type' => 'checkbox',
'#title' => t('ToS agreement is required'),
'#default_value' => variable_get('uc_termsofservice_' . $type . '_required', 0),
'#weight' => 1,
);
}
$form['uc_termsofservice_' . $type . '_node'] = array(
'#type' => 'textfield',
'#title' => t('Terms of Service node'),
'#description' => t('Enter the title of the node that contains your Terms of Service. Note that this node will be shown regardless of node access permissions.'),
'#autocomplete_path' => 'uc_termsofservice/node/autocomplete',
'#default_value' => variable_get('uc_termsofservice_' . $type . '_node', NULL),
'#weight' => 0,
);
$form['uc_termsofservice_advanced_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced settings'),
'#weight' => 2,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['uc_termsofservice_advanced_settings']['uc_termsofservice_' . $type . '_ca'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Conditional Actions'),
'#description' => t('You can set the product classes in which the pane will be displayed with <a href="@url">conditional actions</a>', array(
'@url' => url('admin/store/ca/uc_termsofservice_display_pane/edit/conditions'),
)),
'#default_value' => variable_get('uc_termsofservice_' . $type . '_ca', 0),
);
if (module_exists('modalframe')) {
$form['uc_termsofservice_advanced_settings']['uc_termsofservice_' . $type . '_popup'] = array(
'#type' => 'checkbox',
'#title' => t('Open ToS in a popup modal window.'),
'#default_value' => variable_get('uc_termsofservice_' . $type . '_popup', 0),
);
$form['uc_termsofservice_advanced_settings']['uc_termsofservice_' . $type . '_popup_width'] = array(
'#type' => 'textfield',
'#title' => t('Width of the popup window'),
'#default_value' => variable_get('uc_termsofservice_' . $type . '_popup_width', NULL),
'#size' => 4,
);
$form['uc_termsofservice_advanced_settings']['uc_termsofservice_' . $type . '_popup_height'] = array(
'#type' => 'textfield',
'#title' => t('Height of the popup window'),
'#default_value' => variable_get('uc_termsofservice_' . $type . '_popup_height', NULL),
'#size' => 4,
);
}
return $form;
}
}
/**
* Implements hook_form_alter().
*/
function uc_termsofservice_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'uc_cart_cart_panes_form') {
$form['uc_termsofservice'] = array(
'#type' => 'fieldset',
'#title' => t('Terms of Service settings'),
'#weight' => 98,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['uc_termsofservice'] += uc_termsofservice_admin_form('cart');
$form['buttons']['#weight'] = 99;
return;
}
if ($form_id == 'uc_cart_checkout_form') {
$form['#validate'][] = 'uc_termsofservice_checkout_form_validate';
return;
}
}
/**
* Validate function for checkout, if required by config.
*
* This way, we can display a better required message.
*/
function uc_termsofservice_checkout_form_validate($form, &$form_state) {
// Only check for validation when the pane really exists.
if (isset($form_state['values']['panes']['uc_termsofservice_agreement_checkout'])) {
$required = variable_get('uc_termsofservice_checkout_required', 0);
if ($required) {
$popup = variable_get('uc_termsofservice_checkout_popup', 0);
if (!$popup) {
$agreed = $form_state['values']['panes']['uc_termsofservice_agreement_checkout']['tos_agree']['agreed'];
}
else {
$agreed = $form_state['values']['panes']['uc_termsofservice_agreement_checkout']['tos_agree_popup']['agreed'];
}
if (!$agreed) {
$node = uc_termsofservice_get_node('checkout');
form_set_error('tos_agree', t('In order to continue with the checkout process, you must first accept the !tos', array(
'!tos' => $node->title,
)));
}
}
}
}
/**
* Autocomplete callback, taken from panels module.
*/
function uc_termsofservice_node_autocomplete($string) {
// If there are node_types passed, we'll use those in a MySQL IN query.
if ($string != '') {
$preg_matches = array();
$match = preg_match('/\\[nid: (\\d+)\\]/', $string, $preg_matches);
if (!$match) {
$match = preg_match('/^nid: (\\d+)/', $string, $preg_matches);
}
if ($match) {
$arg = $preg_matches[1];
$where = "n.nid = %d";
}
else {
$arg = $string;
$where = "LOWER(title) LIKE LOWER('%%%s%%')";
}
if (!user_access('administer nodes')) {
$where .= " AND n.status = 1";
}
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, u.name FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE {$where}"), $arg, 0, 10);
$matches = array();
while ($node = db_fetch_object($result)) {
$name = empty($node->name) ? variable_get('anonymous', t('Anonymous')) : check_plain($node->name);
$matches[$node->title . " [nid: {$node->nid}]"] = '<span class="autocomplete_title">' . check_plain($node->title) . '</span> <span class="autocomplete_user">(' . t('by @user', array(
'@user' => $name,
)) . ')</span>';
}
drupal_json($matches);
}
}
/**
* Menu title callback.
*/
function uc_termsofservice_title_callback($node = NULL) {
$title = t('Terms of Service');
if ($node->title) {
$title = $node->title;
}
return t('@title', array(
'@title' => $title,
));
}