mcd.module
12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* @file
* mcd.module
*
* 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.
*
*/
/**
* Implements hook_help().
*
* $lang = (array) $GLOBALS['language'];
echo "Lang prefix is: " . $lang['prefix'];
*/
function mcd_help($path, $arg) {
// Care about lang prefix
$lang = (array) $GLOBALS['language'];
if ($lang['prefix']) {
$base_url = $GLOBALS['base_url'] . '/' . $lang['prefix'];
} else {
$base_url = $GLOBALS['base_url'];
}
// Help starts here
$output = '';
switch ($path) {
case "admin/help#mcd":
$output .= '<h3>' . t('Setting up MCD') . '</h3>';
$output .= '<p>' . t('After enabling MCD module goto !url page,
set time for maintenance and switch site off. Choose other additional options.
After you done your maintenance works, go back and turn your site on-line.', array(
'!url' => l(t('Maintenance mode'), $base_url . '/admin/config/development/maintenance'),
)) . '</p>';
$output .= '<h3>' . t('Simplenews integration') . '</h3>';
$output .= '<p>' . t('If you want enable visitors to subscribe on your
news by email (only email), install !simplenews module.', array(
'!simplenews' => l('Simplenews', 'http://drupal.org/project/simplenews')
)) . '</p>';
$output .= '<p>' . t('NOTE: While site is off your subscribers will able read your new by email ONLY.') . '</p>';
$output .= '<p>' . t("After you enable Simplenews module, you need to create your website launch category or use default one. Edit:
<ul>
<li><strong>Select:</strong> @method → @method_select (because this
method don't require confirmation email pages which will be never shown while your site is off-line)</li>
<li><strong>Check:</strong> @subsblock (that's all about it)</li>
</ul></p><p>
Then, go to maintenance page and select subscription form category, to
be shown. Switch website offline.", array(
'@method' => t('Opt-in/out method'),
'@method_select' => t('Single'),
'@subsblock' => t('Subscription block'),
)) . '</p>';
$output .= '<h4>' . t('Create a newsletter') . '</h4>';
$output .= '<p>' . t('After switching the site online, go to "@newsltr"
tab of your newsletter node and send the newsletter to your subscribers
manually.', array('@newsltr' => t('Newsletter'))) . '</p>';
$output .= '<p>' . 'to be continued / продолжение следует / à poursuivre / se continuó / fortgesetzt werden' . '</p>';
break;
}
return $output;
}
/**
* Implements of hook_init().
*/
function mcd_init() {
if (arg(0) == 'admin' && arg(3) == 'maintenance') {
drupal_add_css(drupal_get_path('module', 'mcd') . '/mcd.css', array('type' => 'file', 'every_page' => FALSE, 'preprocess' => FALSE));
}
// if (variable_get('maintenance_mode', 0) && arg(0) != 'admin') {
// drupal_add_css(drupal_get_path('theme', 'jollyness') . '/assets/css/style-preset1.css', array('type' => 'file', 'every_page' => FALSE, 'preprocess' => FALSE));
// }
}
/**
* Check if module simplenews is enabled
*/
function mcd_check_simplenews() {
return module_exists('simplenews');
}
/**
* Implements hook_form_alter().
*/
function mcd_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'system_site_maintenance_mode') {
$form['mcd'] = array(
'#type' => 'fieldset',
'#title' => t('Maintenance Countdown settings'),
'#weight' => 1,
'#collapsible' => TRUE,
'#element_validate' => array('mcd_form_validate'),
);
$form['mcd']['help_info'] = array(
'#markup' => '<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', NULL),
'#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', NULL)
);
$form['mcd']['mcd_hours'] = array(
'#title' => t('Hours(s)'),
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('mcd_hours', NULL)
);
$form['mcd']['mcd_minutes'] = array(
'#title' => t('Minute(s)'),
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('mcd_minutes', NULL)
);
$form['mcd']['mcd_seconds'] = array(
'#title' => t('Seconds'),
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('mcd_seconds', NULL),
'#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 customization'),
'#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['mcd']['theme_settings']['mcd_site_logo'] = array(
'#type' => 'checkbox',
'#title' => t('Hide site logo'),
'#default_value' => variable_get('mcd_site_logo', 0),
);
$form['mcd']['theme_settings']['mcd_site_name'] = array(
'#type' => 'checkbox',
'#title' => t('Hide site name with link'),
'#default_value' => variable_get('mcd_site_name', 0),
);
$form['mcd']['theme_settings']['mcd_site_slogan'] = array(
'#type' => 'checkbox',
'#title' => t('Hide site slogan'),
'#default_value' => variable_get('mcd_site_slogan', 0),
);
if (mcd_check_simplenews()) {
$form['mcd']['emailsubs_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Subscription form settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['mcd']['emailsubs_settings']['mcd_email_subscr'] = array(
'#type' => 'select',
'#title' => t('Select form to be shown on maintenance page'),
'#options' => array(t('<none>')) + simplenews_category_list(),
'#default_value' => variable_get('mcd_email_subscr', 0),
);
$form['mcd']['emailsubs_settings']['mcd_email_place'] = array(
'#description' => t('If you want some placeholder for email field, so type it here.'),
'#type' => 'textfield',
'#title' => t('Placeholder for email field'),
'#default_value' => variable_get('mcd_email_place'),
);
}
$form['#submit'][] = 'mcd_submit';
}
if ($form_id == 'simplenews_block_form_' . variable_get('mcd_email_subscr') && variable_get('mcd_email_place')) {
$form['mail']['#attributes'] = array('placeholder' => variable_get('mcd_email_place'));
}
}
/**
* Validation handler for time fields. All fields must be numeric
*/
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).'));
}
}
// fill '0' in empty fields to prevent incorrect time set
else {
$form_state['values'][$field] = 0;
}
}
}
/**
* Add system_site_maintenance_settings form submit processing.
*/
function mcd_submit($form, &$form_state) {
// If website offline
if (variable_get('maintenance_mode', 0) == 1) {
// get time like 1315776380
$starttime = REQUEST_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(format_date($starttime, 'custom', 'c') . " +" . 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');
}
// process
else {
variable_set('mcd_maintenance', 1);
drupal_set_message(t('Maintenance time @start — @stop', array('@start' => $start, '@stop' => $stop)));
}
}
// If website online
else {
variable_set('mcd_maintenance', 0);
drupal_set_message(t('Site on-line.'));
watchdog('mcd', 'Site on-line', NULL, WATCHDOG_INFO);
}
// Update the registry based on the latest files listed in the DB.
registry_rebuild();
system_rebuild_theme_data();
drupal_theme_rebuild();
// If cache lifetime has been set, then clear page cache
if (variable_get('cache_lifetime', 0) > 0) {
cache_clear_all('*', 'cache', TRUE);
cache_clear_all('*', 'cache_page', TRUE);
}
}
/**
* If time is set process our maintenance page with countdown
*/
if (variable_get('mcd_maintenance', 0) == '1') {
include_once 'mcd.countdown.inc';
}