View source
<?php
define('SITE_DISCLAIMER_SETTINGS_PATH', 'admin/settings/site_disclaimer');
define('SITE_DISCLAIMER_ACCEPT_PATH', '_legal/accept_new');
define('SITE_DISCLAIMER_DEFAULT_TITLE', t('Terms of Use'));
define('SITE_DISCLAIMER_DEFAULT_CHECKBOX_LABEL', t('I agree with these terms'));
function site_disclaimer_help($path, $arg) {
switch ($path) {
case 'admin/help#site_disclaimer':
return '<p>' . t('Site Disclaimer form (which includes a Site Disclaimer statement and an [x] checkbox) is displayed on the registration page, and the visitor is required to accept it by checking the [x] checkbox in order to register. If Site Disclaimer statement is revised later, Site Disclaimer form will be displayed after registered user logs in and the user will be required to accept the new version of the terms by checking the [x] checkbox in order to proceed using the website.') . '</p>';
break;
case SITE_DISCLAIMER_SETTINGS_PATH:
return '<p>' . t('Site Disclaimer form (which includes a Site Disclaimer statement and an [x] checkbox) is displayed on the registration page, and the visitor is required to accept it by checking the [x] checkbox in order to register. If Site Disclaimer statement is revised later, Site Disclaimer form will be displayed after registered user logs in and the user will be required to accept the new version of the terms by checking the [x] checkbox in order to proceed using the website.') . '</p>';
break;
}
}
function site_disclaimer_menu() {
$items = array();
$items[SITE_DISCLAIMER_SETTINGS_PATH] = array(
'description' => 'Add Site Disclaimer to the registration page.',
'title' => 'Site Disclaimer',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'site_disclaimer_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'site_disclaimer.admin.inc',
);
$items['site_disclaimer/autocomplete'] = array(
'title' => 'Autocomplete node title',
'page callback' => 'site_disclaimer_autocomplete',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'file' => 'site_disclaimer.admin.inc',
);
$items['site_disclaimer/js'] = array(
'page callback' => 'site_disclaimer_js',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_CALLBACK,
'file' => 'site_disclaimer.admin.inc',
);
$items[SITE_DISCLAIMER_ACCEPT_PATH] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'site_disclaimer_confirm',
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'site_disclaimer.pages.inc',
);
return $items;
}
function _site_disclaimer_nid_translated($nid) {
if ($nid != '' && module_exists('translation')) {
global $language;
$langcode = $language->language;
$translations = translation_node_get_translations($nid);
if (!empty($translations[$langcode])) {
$nid = $translations[$langcode]->nid;
}
}
return $nid;
}
function _site_disclaimer_nid_all_translations($nid) {
global $language;
$nids = empty($nid) ? array() : array(
$language->language => $nid,
);
if (!empty($nid) && module_exists('translation')) {
$translations = translation_node_get_translations($nid);
foreach ($translations as $langcode => $translation) {
$nids[$langcode] = $translation->nid;
}
}
return $nids;
}
function _site_disclaimer_checkbox_label_substitute_links($checkbox_label, &$linked_nodes = NULL, &$linked_errors = NULL) {
$linked_nodes = array();
$linked_errors = array();
$output = str_replace('"', '"', $checkbox_label);
$i = 0;
while ($i < drupal_strlen($output)) {
if (drupal_substr($output, $i, 2) == '@"') {
$start = $i + 2;
$end = strpos($output, '"', $start);
while (drupal_substr($output, $end - 1, 1) == "\\") {
$end = strpos($output, '"', $end + 1);
}
$title = drupal_substr($output, $start, $end - $start);
$node = node_load(array(
'title' => str_replace('\\"', '"', $title),
));
if (empty($node)) {
$linked_errors[$title] = t('No post was found with "!title" title.', array(
'!title' => $title,
));
}
else {
$linked_nodes[] = $node->nid;
$link = l($node->title, 'node/' . $node->nid);
$output = str_replace('@"' . $title . '"', $link, $output);
$i += drupal_strlen($link) - 1;
}
}
$i++;
}
return $output;
}
function site_disclaimer_form_user_register_alter(&$form, $form_state, $langcode = NULL, $preview = FALSE) {
$args = isset($form['#parameters']) ? $form['#parameters'] : array(
'',
);
$form_id = array_shift($args);
if (user_access('administer users') && !$preview && $form_id == 'user_register') {
return;
}
$height = variable_get('site_disclaimer_node_height', '');
if ($height) {
$css = '#site-disclaimer { height:' . check_plain($height) . '; overflow:auto; }';
$css2 = '#site-disclaimer { height:auto; overflow:visible; }';
drupal_set_html_head('<style type="text/css">' . $css . '</style>');
drupal_set_html_head('<style type="text/css" media="print">' . $css2 . '</style>');
}
$nid = _site_disclaimer_nid_translated(variable_get('site_disclaimer_node_id', ''));
$node = NULL;
if (!empty($nid)) {
$node = node_load($nid);
if (empty($node)) {
watchdog('Site Disclaimer', 'The Site Disclaimer node !nid could not be loaded. Please check the settings and the node.', array(
'!nid' => $nid,
), WATCHDOG_ALERT, l(t('Administer Site Disclaimer'), SITE_DISCLAIMER_SETTINGS_PATH));
}
}
$checkbox_label = filter_xss_admin(variable_get('site_disclaimer_checkbox_label', SITE_DISCLAIMER_DEFAULT_CHECKBOX_LABEL));
$use_link = strpos($checkbox_label, '@link') !== FALSE;
$use_named_links = strpos($checkbox_label, '@"') !== FALSE;
$show_node = $node && !$use_link && !$use_named_links;
if (!$use_link && !$use_named_links && !$node) {
return;
}
$checkbox_label = theme('site_disclaimer_checkbox_label', $checkbox_label, $node);
$fieldset_en = variable_get('site_disclaimer_fieldset', 1);
if ($fieldset_en) {
$form['site_disclaimer'] = array(
'#type' => 'fieldset',
'#title' => check_plain(variable_get('site_disclaimer_title', SITE_DISCLAIMER_DEFAULT_TITLE)),
'#weight' => 10,
);
$fieldset =& $form['site_disclaimer'];
}
else {
$fieldset =& $form;
}
if ($show_node) {
$node = node_prepare($node);
if (!empty($node->body)) {
$fieldset['site_disclaimer_text'] = array(
'#value' => theme('site_disclaimer', $node->body, $node),
'#weight' => 9,
);
}
else {
watchdog('Site Disclaimer', 'The body field of the Site Disclaimer node !nid is empty. Please check the the node content.', array(
'!nid' => $nid,
), WATCHDOG_ALERT, l(t('Administer Site Disclaimer'), SITE_DISCLAIMER_SETTINGS_PATH));
}
}
$fieldset['I_agree'] = array(
'#type' => 'checkbox',
'#title' => $checkbox_label,
'#required' => TRUE,
'#element_validate' => array(
'_site_disclaimer_validate_checkbox',
),
'#return_value' => variable_get('site_disclaimer_version', 1),
'#weight' => 10,
);
return $form;
}
function _site_disclaimer_validate_checkbox($form, &$form_state) {
$value = $form_state['values']['I_agree'];
if ($value != variable_get('site_disclaimer_version', 1)) {
form_set_error('I_agree', t('You must agree with the !terms to get an account.', array(
'!terms' => check_plain(variable_get('site_disclaimer_title', SITE_DISCLAIMER_DEFAULT_TITLE)),
)));
}
}
function site_disclaimer_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'insert':
if (isset($edit['I_agree'])) {
$edit['site_disclaimer'] = $edit['I_agree'];
unset($edit['I_agree']);
}
break;
}
}
function _site_disclaimer_i18n() {
$variables = array(
'site_disclaimer_title',
'site_disclaimer_checkbox_label',
'site_disclaimer_version_details',
);
$i18n_variables = variable_get('i18n_variables', array());
if (in_array($variables[0], $i18n_variables)) {
return;
}
$i18n_variables = array_merge($i18n_variables, $variables);
variable_set('i18n_variables', $i18n_variables);
}
function _site_disclaimer_enabled() {
$nid = _site_disclaimer_nid_translated(variable_get('site_disclaimer_node_id', ''));
$checkbox_label = filter_xss_admin(variable_get('site_disclaimer_checkbox_label', SITE_DISCLAIMER_DEFAULT_CHECKBOX_LABEL));
$use_link = strpos($checkbox_label, '@link') !== FALSE;
$use_named_links = strpos($checkbox_label, '@"') !== FALSE;
$site_disclaimer_enabled = $use_link || $use_named_links || !empty($nid);
return $site_disclaimer_enabled;
}
function site_disclaimer_init() {
global $user;
_site_disclaimer_i18n();
if ($user->uid > 1 && !user_access('administer site configuration')) {
$site_disclaimer_enabled = _site_disclaimer_enabled();
$site_disclaimer_version = variable_get('site_disclaimer_version', 1);
if ($site_disclaimer_enabled && (!isset($user->site_disclaimer) || $user->site_disclaimer < $site_disclaimer_version)) {
$allow_nodes = variable_get('site_disclaimer_allow_nodes', array());
$allow_paths = array(
explode('/', SITE_DISCLAIMER_ACCEPT_PATH),
array(
'logout',
),
);
if (!empty($allow_nodes)) {
$allow_paths[] = array(
'node',
$allow_nodes,
);
$allow_paths[] = array(
'print',
'node',
$allow_nodes,
);
$allow_paths[] = array(
'printpdf',
'node',
$allow_nodes,
);
}
foreach ($allow_paths as $allow_path) {
while (1) {
$i = 0;
foreach ($allow_path as $part) {
if (is_array($part) && !in_array(arg($i), $part)) {
break 2;
}
if (!is_array($part) && arg($i) != $part) {
break 2;
}
$i++;
}
return;
}
}
drupal_goto(SITE_DISCLAIMER_ACCEPT_PATH, drupal_get_destination());
}
}
}
function site_disclaimer_theme() {
return array(
'site_disclaimer' => array(
'arguments' => array(
'terms' => NULL,
'node' => NULL,
),
),
'site_disclaimer_checkbox_label' => array(
'arguments' => array(
'checkbox_label' => '',
'node' => NULL,
),
),
);
}
function theme_site_disclaimer($terms, $node = NULL) {
$output = '<div id="site-disclaimer" class="content clear-block">';
$output .= $terms;
$output .= '</div>';
return $output;
}
function theme_site_disclaimer_checkbox_label($checkbox_label = '', $node = NULL) {
if (!drupal_strlen($checkbox_label)) {
return '';
}
$output = $checkbox_label;
if (!empty($node)) {
$output = str_replace('@link', l($node->title, 'node/' . $node->nid), $output);
}
$output = _site_disclaimer_checkbox_label_substitute_links($output);
if (!module_exists('checkbox_validate')) {
$output .= ' <span class="form-required" title="' . t('This field is required.') . '">*</span>';
}
return $output;
}
function _site_disclaimer_nodeapi_work($node, $op) {
$nid = variable_get('site_disclaimer_node_id', '');
$nids = $nid != '' ? _site_disclaimer_nid_all_translations($nid) : array();
$nids += variable_get('site_disclaimer_allow_nodes', array());
if (!empty($node->tnid)) {
$node_tnid = $node->tnid;
}
elseif (!empty($node->translation_source)) {
$node_tnid = $node->translation_source->tnid ? $node->translation_source->tnid : $node->translation_source->nid;
}
if (isset($node->nid) && in_array($node->nid, $nids) || !empty($node_tnid) && in_array($node_tnid, $nids)) {
module_load_include('inc', 'site_disclaimer', 'site_disclaimer.admin');
_site_disclaimer_prep_allowed_paths($nid, $op == 'delete' ? $node->nid : NULL);
}
}
function site_disclaimer_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'insert':
case 'update':
case 'delete':
_site_disclaimer_nodeapi_work($node, $op);
case 'prepare':
break;
}
}
function site_disclaimer_views_api() {
return array(
'api' => 2.0,
'path' => drupal_get_path('module', 'site_disclaimer') . '/views',
);
}