mcd.module in Maintenance Countdown 6
Same filename and directory in other branches
This module provides the maintenance page with nice countdown timer. So now you may set time for your maintenance work and your visitors will see when time is up.
File
mcd.moduleView source
<?php
/**
* @file
*
* This module provides the maintenance page with nice countdown timer.
* So now you may set time for your maintenance work and your visitors
* will see when time is up.
*/
/**
* Implementation of hook_init().
*
* Add module UI css for settings page only
*/
function mcd_init() {
if (arg(0) == 'admin' && arg(2) == 'site-maintenance') {
drupal_add_css(drupal_get_path('module', 'mcd') . '/mcd.css');
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function mcd_form_system_site_maintenance_settings_alter(&$form, $form_state) {
$form['mcd'] = array(
'#type' => 'fieldset',
'#title' => t('Maintenance Countdown settings'),
'#collapsible' => TRUE,
'#element_validate' => array(
'mcd_form_validate',
),
'#weight' => 0.001,
);
$form['mcd']['help_info'] = array(
'#type' => 'markup',
'#value' => '<p>' . t("Here you may set maintenance time. i.e. how long site will be offline. For example, if you plan switch site offline for 5 minutes, enter '5' (without apostrophes) in 'Minutes' field. If you need 100 hours, just do it — feel free to enter '100' (without apostrophes) in 'Hours' field.") . '</p>',
);
$form['mcd']['mcd_months'] = array(
'#title' => t('Month(s)'),
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('mcd_months', 0),
'#prefix' => '<div class="mcd">',
);
$form['mcd']['mcd_days'] = array(
'#title' => t('Day(s)'),
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('mcd_days', 0),
);
$form['mcd']['mcd_hours'] = array(
'#title' => t('Hours(s)'),
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('mcd_hours', 0),
);
$form['mcd']['mcd_minutes'] = array(
'#title' => t('Minute(s)'),
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('mcd_minutes', 0),
);
$form['mcd']['mcd_seconds'] = array(
'#title' => t('Seconds'),
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('mcd_seconds', 0),
'#suffix' => '</div>',
);
$form['mcd']['mcd_time_up_message'] = array(
'#title' => t('Message after time is up'),
'#type' => 'textarea',
'#default_value' => variable_get('mcd_time_up_message', t('Time is up. Please reload this page by hitting <kbd>F5</kbd> on your keyboard or click on "Reload" button below.')),
'#description' => t('Write some message that will be shown after time is up. HTML allowed.'),
);
// Reload settings
$form['mcd']['reload_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Maintenance page reload settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['mcd']['reload_settings']['mcd_reload_button'] = array(
'#type' => 'radios',
'#default_value' => variable_get('mcd_reload_button', 0),
'#options' => array(
t('Show reload button'),
t("Don't show reload button"),
t("Don't show reload button, but add auto-reloading maintenance page every 15 seconds after time is up"),
),
);
// Page theme setings
$form['mcd']['theme_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Maintenance page theme settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['mcd']['theme_settings']['mcd_page_themes'] = array(
'#type' => 'select',
'#title' => t('Select page theme'),
'#default_value' => variable_get('mcd_page_themes', 'light'),
'#options' => array(
'light' => t('Light'),
'dark' => t('Dark'),
),
);
$form['#submit'][] = 'mcd_submit';
}
/**
* Validation handler for time fields.
*
* All fields must be numeric. So if nothing given fill zero.
*/
function mcd_form_validate($form, &$form_state) {
foreach (array(
'mcd_months',
'mcd_days',
'mcd_hours',
'mcd_minutes',
'mcd_seconds',
) as $field) {
// check if fields are numeric
if (!empty($form_state['values'][$field])) {
if (!is_numeric($form_state['values'][$field])) {
form_set_error($field, t('Time must be entered as number, check field(s).'));
}
}
else {
$form_state['values'][$field] = 0;
}
}
}
/**
* Add system_site_maintenance_settings form submit processing.
*
* We need to use date("c"), because drupal-6 function format_date()
* does not support "c".
*
* @see http://drupal.org/node/494434
*/
function mcd_submit($form, &$form_state) {
// If website offline
if (variable_get('site_offline', 0) == 1) {
// get time like 1315776380
$starttime = time();
$offtime = array(
variable_get('mcd_months', 0) . ' months',
variable_get('mcd_days', 0) . ' days',
variable_get('mcd_hours', 0) . ' hours',
variable_get('mcd_minutes', 0) . ' minutes',
variable_get('mcd_seconds', 0) . ' seconds',
);
$stoptime = strtotime(date("c", $starttime) . " +" . implode(" ", $offtime));
// save stop-time in var, output like 1315776380
variable_set('mcd_stoptime', $stoptime);
// for messages
$start = format_date($starttime, 'small');
$stop = format_date($stoptime, 'small');
watchdog('mcd', 'Maintenance time @start — @stop', array(
'@start' => $start,
'@stop' => $stop,
), WATCHDOG_INFO);
if ($starttime == $stoptime) {
variable_set('mcd_maintenance', 0);
drupal_set_message(t('The timer is not set, because the time is not given.'), 'warning');
}
else {
variable_set('mcd_maintenance', 1);
drupal_set_message(t('Maintenance time @start — @stop', array(
'@start' => $start,
'@stop' => $stop,
)));
}
}
else {
variable_set('mcd_maintenance', 0);
drupal_set_message(t('Site on-line.'));
watchdog('mcd', 'Site on-line', NULL, WATCHDOG_INFO);
}
// Rebuild some caches
drupal_rebuild_theme_registry();
// it does cache_clear_all('theme_registry', 'cache', TRUE);
// If setted cache lifefime, then clear page cache
if (variable_get('cache_lifetime', 0) > 0) {
cache_clear_all('*', 'cache_page', TRUE);
drupal_set_message(t('Page cache was cleared!'));
}
}
/**
* If time is set process our maintenance page with countdown
*/
if (variable_get('mcd_maintenance', 0) == 1) {
include_once 'mcd.countdown.inc';
}
Functions
Name![]() |
Description |
---|---|
mcd_form_system_site_maintenance_settings_alter | Implements hook_form_FORM_ID_alter(). |
mcd_form_validate | Validation handler for time fields. |
mcd_init | Implementation of hook_init(). |
mcd_submit | Add system_site_maintenance_settings form submit processing. |