-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlzLogoSwap.module
187 lines (156 loc) · 5.09 KB
/
dlzLogoSwap.module
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
<?php
/**
* Implements hook_preprocess_page().
*/
function dlzLogoSwap_preprocess_page(&$variables) {
$default = $variables['logo'];
if (_dlzLogoSwap_checkIsLogoSwapped()) {
$variables['logo'] = variable_get("dlzLogoSwap_logo_path", $default); //"themes/bartik/logo2.png";
}
}
/**
* Implements hook_cron().
*/
function dlzLogoSwap_cron() {
$start_date = variable_get("dlzLogoSwap_start_date", -1);
$end_date = variable_get("dlzLogoSwap_end_date", -1);
$no_end_date = variable_get("dlzLogoSwap_no_end_date", FALSE);
$start = strtotime($start_date);
$end = strtotime($end_date);
// If the start time is now or in the past...
if ($start <= time()) {
// If there is no end time configured...
if ($no_end_date == TRUE) {
// Then we need to make sure the logo swap is enabled.
_dlzLogoSwap_enableLogoSwap();
return;
}
// If the end time is now or in the past...
if ($end <= time()) {
// Then we need to disable the logo swap.
_dlzLogoSwap_disableLogoSwap();
return;
}
else { // If the end time is in the future... ($end > time())
// Then we need to enable the logo swap.
_dlzLogoSwap_enableLogoSwap();
return;
}
}
else { // If the start time is not now, or in the past... (then it must be in the future...)
// Then we need to disable the logo swap.
_dlzLogoSwap_disableLogoSwap();
return;
}
}
/**
* Implements hook_menu()
*/
function dlzLogoSwap_menu() {
$items = array();
$items['admin/config/content/dlzlogoswap'] = array(
'title' => 'Subscribe',
'description' => 'Subscribe to this node',
'page callback' => 'drupal_get_form',
'page arguments' => array("dlzLogoSwap_schedule_form"),
'access arguments' => array('create dlzLogoSwap schedules'),
'type' => MENU_LOCAL_TASK
);
return $items;
}
/**
* Implements Form API
*/
function dlzLogoSwap_schedule_form($node, &$form_state) {
if (!is_array($node)) {
$node = array();
}
$theme_settings = variable_get("theme_settings", -1);
$default_logo = (is_array($theme_settings) && isset($theme_settings['logo_path'])) ? $theme_settings['logo_path'] : "";
$node['dlzLogoSwap_logo_path'] = array(
'#title' => t('Path to alternate logo'),
'#type' => 'textfield',
'#description' => t('Specify the path to the alternate logo to be displayed.'),
'#default_value' => variable_get("dlzLogoSwap_logo_path", $default_logo),
'#required' => TRUE,
);
$node['dlzLogoSwap_start_date'] = array(
'#title' => t('Schedule Logo Swap Start'),
'#type' => 'date_popup',
'#description' => t('Specify the date when the alternate logo should be displayed.'),
'#default_value' => variable_get("dlzLogoSwap_start_date", ""),
'#required' => TRUE,
);
$node['dlzLogoSwap_end_date'] = array(
'#title' => t('Schedule Logo Swap End'),
'#type' => 'date_popup',
'#description' => t('Specify the date when the alternate logo should no longer be displayed.'),
'#required' => TRUE,
'#default_value' => variable_get("dlzLogoSwap_end_date", ""),
);
$node['dlzLogoSwap_no_end_date'] = array(
'#title' => t('No End Date.'),
'#type' => 'checkbox',
'#description' => t('Specify that the alternate logo should not swap out.'),
'#default_value' => variable_get("dlzLogoSwap_no_end_date", FALSE),
);
return system_settings_form($node);
/*
* system_setting_form($form) automatically does this for us:
*
$node['#submit'][] = 'system_settings_form_submit';
$node['submit'] = array(
'#type' => 'submit',
'#value' => t("Save Schedule"),
);
return $node;
*
*/
}
/**
* Form Validation Handler
*/
function dlzLogoSwap_schedule_form_validate($form, &$form_state) {
$start = $form_state['values']['dlzLogoSwap_start_date'];
$end = $form_state['values']['dlzLogoSwap_end_date'];
$start_ts = strtotime($start);
$end_ts = strtotime($end);
if ($end_ts <= $start_ts) {
$map = array("%end_date" => $end, "%start_date" => $start);
$msg = t("The scheduled end date (%end_date) is on or before the schedule start date (%start_date).", $map);
form_set_error("dlzLogoSwap_end_date", $msg);
}
if ($start_ts < time()) {
$map = array("%start_date" => $start);
$msg = t("The scheduled start date (%start_date) has already passed.", $map);
form_set_error("dlzLogoSwap_start_date", $msg);
}
}
/**
* Implements hook_permission()
*/
function dlzLogoSwap_permission() {
return array(
'create dlzLogoSwap schedules' => array(
'title' => t('Create Logo Swap Schedules'),
'description' => t('Schedule swapping the default site logo for an alternative one.'),
),
);
}
/**
* Private Methods
*/
function _dlzLogoSwap_checkIsLogoSwapped() {
$is_enabled = variable_get("dlzLogoSwap_enabled", FALSE);
return (bool) ($is_enabled == TRUE);
}
function _dlzLogoSwap_disableLogoSwap() {
if (_dlzLogoSwap_checkIsLogoSwapped() == TRUE) {
variable_set("dlzLogoSwap_enabled", FALSE);
}
}
function _dlzLogoSwap_enableLogoSwap() {
if(_dlzLogoSwap_checkIsLogoSwapped() == FALSE){
variable_set("dlzLogoSwap_enabled", TRUE);
}
}