-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbb.module
278 lines (247 loc) · 9.48 KB
/
cbb.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
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
<?php
/**
* @file
* Add support for Cookie Based Blocks.
*
* Cookie Based Blocks (cbb for short) allows users to add cookie based display
* to any block through the block's configuration interface. This implementation
* is based on an alteration of the Core block database table to leverage the
* Core Block API functions, objects and structure.
*/
/**
* Implements hook_permission().
*/
function cbb_permission() {
return array(
'administer cookie based blocks' => array(
'title' => t('Administer cookie based blocks'),
'description' => t('Set Cookie basedparameters for blocks.'),
),
);
}
/**
* Implements hook_block_view_alter().
*/
function cbb_block_view_alter(&$data, $block) {
if (isset($block->cbb)) {
_cbb_unserialize($block);
}
if (isset($block->cbb) && $block->cbb) {
if (isset($data['content']) && is_string($data['content'])) {
if (empty($data['content'])) {
return;
}
$data['content'] = array(
'#markup' => $data['content'],
);
}
$data['content']['#attached']['css'][] = array(
'data' => backdrop_get_path('module', 'cbb') . '/cbb.css',
);
$data['content']['#attached']['library'][] = array('system','jquery.cookie');
$data['content']['#attached']['js'][] = array(
'data' => backdrop_get_path('module', 'cbb') . '/cbb.js',
);
$data['content']['#attached']['js'][] = array(
'data' => array(
'cbb' => array(
'block-' . $block->module . '-' . $block->delta => array(
'cbb_expose_after' => $block->cbb_expose_after,
'cbb_expose_delay' => $block->cbb_expose_delay,
'cbb_use_colorbox' => $block->cbb_use_colorbox,
),
),
),
'type' => 'setting',
);
}
}
/**
* Implements theme_preprocess_block().
*
* Extend block's classes with any user defined classes.
*/
function cbb_preprocess_block(&$vars) {
$block = $vars['block'];
if (!empty($block->cbb_use) && $block->cbb_use) {
_cbb_alter_block_vars($block, $vars);
}
}
/**
* Implements hook_preprocess_HOOK().
*
* Extend panel block's with Cookie Based exposure.
*/
function cbb_preprocess_panels_pane(&$vars) {
if ($vars['pane']->type != 'block') {
return;
}
// Infer the block's $module and $delta from the pane subtype.
$block_parts = explode('-', $vars['pane']->subtype);
// Load the block based on the block parts.
$block = block_load($block_parts[0], $block_parts[1]);
// Add a generic 'module type' pane class.
$vars['classes_array'][] = backdrop_html_class('pane-' . $block->module);
// Add $css_class to the $classes_array.
if (!empty($block->cbb_use)) {
_cbb_alter_block_vars($block, $vars);
}
}
function _cbb_alter_block_vars($block, &$vars) {
// TODO: change this to JS inclusion, backdrop variables
$classes_array = ['block-cbb'];
foreach ($classes_array as $class) {
$vars['classes_array'][] = backdrop_clean_css_identifier($class, array());
}
}
function _cbb_fields() {
return array(
'cbb_use' => FALSE,
'cbb_expose_after' => "1 day",
'cbb_expose_delay' => "0",
'cbb_use_colorbox' => FALSE,
);
}
function _cbb_unserialize($block) {
// Why do I have to unserialize
if ($block->cbb) {
$block->cbb = unserialize($block->cbb);
}
else {
$block->cbb = array();
}
// Map values to block variables
foreach (_cbb_fields() as $key => $default) {
$block->$key = isset($block->cbb[$key]) ? $block->cbb[$key] : $default;
}
}
/**
* Implements hook_form_alter().
*
* Alter block edit form to add configuration field.
*/
function cbb_form_layout_block_configure_form_alter(&$form, &$form_state) {
if (user_access('administer cookie based blocks') ) {
$layout = $form_state['layout'];
$block = $form_state['block'];
/* _cbb_unserialize($block); */
if(!property_exists($block, "cbb_use")) {
$block->cbb_use = 0;
}
dpm($block);
$form['settings']['cbb_use'] = array(
'#type' => 'checkbox',
'#title' => t('Use Cookie Based Block.'),
'#default_value' => isset($block->cbb_use) ? $block->cbb_use : FALSE,
);
$form['settings']['cbb_expose_after'] = array(
'#type' => 'textfield',
'#title' => t('Show block again after previous exposure.'),
'#description' => t('Use PHP style duration like "7 days", "1 week". See PHP strtotime function for more info.'),
'#default_value' => isset($block->cbb_expose_after) ? $block->cbb_expose_after : "1 day",
'#states' => array(
'invisible' => array(
':input[name="cbb_use"]' => array('checked' => FALSE),
),
),
);
$form['settings']['cbb_expose_delay'] = array(
'#type' => 'textfield',
'#title' => t('Delay block display for n seconds.'),
'#default_value' => isset($block->cbb_expose_delay) ? $block->cbb_expose_delay : "0",
'#states' => array(
'invisible' => array(
':input[name="cbb_use"]' => array('checked' => FALSE),
),
),
);
$form['settings']['cbb_use_colorbox'] = array(
'#type' => 'checkbox',
'#title' => t('Use the jQuery.colobox() function if exists.'),
'#description' => t('When jQuery.colorbox() function is available we use it.'),
'#default_value' => isset($block->cbb_use_colorbox) ? $block->cbb_use_colorbox : FALSE,
'#states' => array(
'invisible' => array(
':input[name="cbb_use"]' => array('checked' => FALSE),
),
),
);
$form['#validate'][] = 'cbb_form_validate';
$form['#submit'][] = 'cbb_layout_block_configure_form_submit';
}
}
/**
* Implements hook_form_validate
* Vallidate the values entered into block settings
*/
function cbb_form_validate($form, &$form_state) {
if ($form_state['values']['form_id'] == 'block_admin_configure' || $form_state['values']['form_id'] == 'block_add_block_form') {
if ($form_state['values']['cbb_use'] && user_access('administer blocks')) {
if (isset($form_state['values']['cbb_use']) && $form_state['values']['cbb_use']) {
$cbb_expose_after = $form_state['values']['cbb_expose_after'];
$cbb_expose_after = preg_replace('!\s+!', ' ', $cbb_expose_after);
$cbb_expose_after = trim($cbb_expose_after);
$values = explode(" ", $cbb_expose_after);
if (count($values) != 2) {
form_set_error('cbb_expose_after', "This needs a number separated by a string.");
}
else {
$steps = $values[0];
$period = $values[1];
if ('' . intval($steps) != $steps || intval($steps) <= 0) {
form_set_error('cbb_expose_after', t("First value should be a positive integer"));
}
$lookup = array('minutes', 'hours', 'days', 'weeks');
if (!in_array($period, $lookup) && !in_array($period . 's', $lookup)) {
form_set_error('cbb_expose_after', t("Second value should be a duration: %periods", array('%periods' => join(', ', $lookup))));
}
}
form_set_value($form['settings']['cbb_expose_after'], $cbb_expose_after, $form_state);
// We cannot make field required so it could be empty so we MUST validate
$cbb_expose_delay = $form_state['values']['cbb_expose_delay'];
if ('' . intval($cbb_expose_delay) != $cbb_expose_delay || intval($cbb_expose_delay) < 0) {
form_set_error('cbb_expose_delay', t("The delay value '%cbb_expose_delay' should be a non negative integer", array('%cbb_expose_delay' => $cbb_expose_delay)));
}
form_set_value($form['settings']['cbb_expose_delay'], $cbb_expose_delay, $form_state);
}
}
}
}
/**
* Helper function: additional submit callback for block configuration pages.
*
* Save supplied Cookie Based Block settings.
*/
function cbb_layout_block_configure_form_submit($form, &$form_state) {
// Only save if value has changed.
$changed = $form['settings']['cbb_use']['#default_value'] != $form_state['values']['cbb_use'];
$changed = $changed || $form['settings']['cbb_expose_after']['#default_value'] != $form_state['values']['cbb_expose_after'];
$changed = $changed || $form['settings']['cbb_expose_delay']['#default_value'] != $form_state['values']['cbb_expose_delay'];
$changed = $changed || $form['settings']['cbb_use_colorbox']['#default_value'] != $form_state['values']['cbb_use_colorbox'];
if ( $changed && user_access('administer blocks')) {
dpm("Saving!");
// if (isset($form_state['values']['cbb_use']) && $form_state['values']['cbb_use']) {
$block = $form_state['block'];
// dpm($form_state['values']);
$block->settings['block_settings']['cbb_use'] = $form_state['values']['settings']['cbb_use'];
$block->settings['block_settings']['cbb_expose_after'] = $form_state['values']['settings']['cbb_expose_after'];
$block->settings['block_settings']['cbb_expose_delay'] = $form_state['values']['settings']['cbb_expose_delay'];
$block->settings['block_settings']['cbb_use_colorbox'] = $form_state['values']['settings']['cbb_use_colorbox'];
$form_state['block'] = $block;
dpm($block);
// Save the block settings.
$block->formSubmit($form, $form_state);
// }
/*
db_update('block')
->fields(array('cbb' => $cbb))
->condition('module', $form_state['values']['module'])
->condition('delta', $form_state['values']['delta'])
->execute();
// Flush all context module cache to use the updated css_class.
if (module_exists('context')) {
cache_clear_all('context', 'cache', TRUE);
}
*/
}
}