-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_event_unpublishing.module
More file actions
69 lines (60 loc) · 2.28 KB
/
ding_event_unpublishing.module
File metadata and controls
69 lines (60 loc) · 2.28 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
<?php
/**
* @file
* Enables automatic unpublish scheduling for Ding Event content type nodes.
*/
/**
* Implements hook_form_alter().
*/
function ding_event_unpublishing_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'ding_event_admin_setting_form') {
$form['ding_event_unpublishing'] = [
'#type' => 'fieldset',
'#title' => t('Events un-publish scheduling settings'),
];
$form['ding_event_unpublishing']['ding_event_unpublishing_schedule'] = [
'#type' => 'checkbox',
'#title' => t('Enable event un-publish scheduling'),
'#description' => t('Allow to enable automatic unpublish scheduling.'),
'#default_value' => variable_get('ding_event_unpublishing_schedule', FALSE),
];
$form['ding_event_unpublishing']['ding_event_unpublishing_schedule_buffer'] = [
'#type' => 'textfield',
'#title' => t('Un-publish schedule buffer (in hours)'),
'#description' => t('The amount of time (<strong>in hours</strong>) after which the event node will be unpublished.'),
'#default_value' => variable_get('ding_event_unpublishing_schedule_buffer', 0),
'#size' => 60,
'#states' => [
'visible' => [
':input[name="ding_event_unpublishing_schedule"]' => ['checked' => TRUE],
],
],
];
}
}
/**
* Implements hook_node_presave().
*
* @throws \Exception
*/
function ding_event_unpublishing_node_presave($node) {
if ($node->type !== 'ding_event') {
return;
}
$event_date = $node->field_ding_event_date[LANGUAGE_NONE][0];
$scheduled_unpublish = variable_get('ding_event_unpublishing_schedule', FALSE);
$scheduled_unpublish_buffer = variable_get('ding_event_unpublishing_schedule_buffer', 0);
if ($scheduled_unpublish) {
$tz_db = new DateTimeZone($event_date['timezone_db']);
$tz = new DateTimeZone($event_date['timezone']);
$end_date = DateTime::createFromFormat('Y-m-d H:i:s', $event_date['value2'], $tz_db);
$buffer_in_seconds = (string) $scheduled_unpublish_buffer * 60 * 60;
$interval = new DateInterval('PT' . $buffer_in_seconds . 'S');
if ($event_date['all_day'] == '1') {
$interval = new DateInterval('P1DT' . $buffer_in_seconds . 'S');
}
$node->unpublish_on = $end_date->setTimezone($tz)
->add($interval)
->getTimestamp();
}
}