-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_library.module
More file actions
428 lines (383 loc) · 13.2 KB
/
ding_library.module
File metadata and controls
428 lines (383 loc) · 13.2 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/**
* @file
* Code for the Ding library feature.
*/
/**
* Implements hook_page_build().
*
* @todo Remove this crap when Opening Hours has valid markup.
*/
function ding_library_page_build(&$page) {
if ($page['#type']=='page' && isset($page['content']['system_main']['content']['#markup'])) {
$page['content']['system_main']['content']['#markup'] .= '<div id="prev" style="display:none;">Helper Content Previous!</div>';
$page['content']['system_main']['content']['#markup'] .= '<div id="next" style="display:none;">Helper Content Next!</div>';
}
}
/**
* Implements hook_init().
*/
function ding_library_init() {
if ($_GET['q'] == 'libraries') {
drupal_add_js(drupal_get_path('module', 'ding_library') . '/js/ding_library.map.js', array(
'scope' => 'footer',
'weight' => 42,
));
}
}
/**
* Implements hook_enable().
*/
function ding_library_enable() {
$path = array(
'source' => 'libraries',
'language' => 'da',
);
// Create Danish alias for libraries if needed.
if (!path_load($path)) {
$path['alias'] = 'biblioteker';
path_save($path);
}
}
/**
* Implements hook_block_info().
*/
function ding_library_block_info() {
return array(
'library_address' => array(
'info' => 'Library address',
'cache' => DRUPAL_CACHE_GLOBAL,
),
'ding_menu' => array(
'info' => 'Ding main menu',
'cache' => DRUPAL_CACHE_PER_PAGE,
),
'ding_sidebar_menu' => array(
'info' => 'Ding sidebar menu',
'cache' => DRUPAL_CACHE_PER_PAGE,
),
);
}
/**
* Implements hook_block_configure().
*/
function ding_library_block_configure($delta = '') {
$form = array();
$libraries = array(
'' => t('None selected'),
);
if ($delta == 'library_address') {
$query = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('type', 'ding_library')
->orderBy('title', 'ASC');
foreach ($query->execute() as $row) {
$libraries[$row->nid] = $row->title;
}
$form['library_nid'] = array(
'#type' => 'select',
'#title' => t('Library'),
'#default_value' => variable_get('ding_library_address_nid', NULL),
'#options' => $libraries,
'#description' => t('The library whose address to display.'),
);
}
return $form;
}
/**
* Implements hook_block_save().
*/
function ding_library_block_save($delta = '', $edit = array()) {
if ($delta == 'library_address') {
variable_set('ding_library_address_nid', $edit['library_nid']);
}
}
/**
* Implements hook_block_view().
*/
function ding_library_block_view($delta = '') {
$block = array();
$node = NULL;
if ($delta == 'library_address') {
$block['subject'] = t('Address');
$node = NULL;
$nid = variable_get('ding_library_address_nid', '');
if (!empty($nid)) {
$node = node_load($nid);
}
$block['content'] = array(
'#theme' => 'ding_library_address',
'#node' => $node,
);
}
if ($delta == 'ding_menu') {
$block['subject'] = t('Main menu');
$l2_menu = $current_library = $level1_links = $level2_links = array();
$main_menu = menu_build_tree('main-menu');
// First level of main menu.
// We added the i18n_menu module as a dependency in
// ding_library_update_7003() but we check for the existence to ensure a
// clean upgrade path.
// TODO: translation generates notices in node_page_title (node.inc, 2075)
//if (module_exists('i18n_menu')) {
// $main_menu = i18n_menu_localize_tree($main_menu);
//}
$main_menu = menu_build_tree('main-menu');
$active_trail = menu_get_active_trail();
// Prepare L1 menu, skip hidden items
foreach ($main_menu as $index1 => $level1_item) {
if (!$level1_item['link']['hidden']) {
// Key is used as class when themeing.
$level1_links['menu-' . $level1_item['link']['mlid']] = array(
'href' => $level1_item['link']['href'],
'title' => $level1_item['link']['link_title'],
);
$l2_menu[$level1_item['link']['mlid']] = $level1_item['below'];
}
}
// Find and save the active submenu
$l2_ids = array_keys($l2_menu);
$parent_item = NULL;
foreach ($active_trail as $item) {
if (isset($item['plid']) && in_array($item['plid'], $l2_ids)) {
$parent_item = $item['plid'];
}
elseif (isset($item['plid']) && $item['plid'] == 0) { // && $item['module'] == 'ding_library_menu'
$parent_item = $item['mlid'];
}
if ($parent_item) {
$current_library['href'] = $level1_links['menu-' . $parent_item]['href'];
$current_library['title'] = $level1_links['menu-' . $parent_item]['title'];
// Prepare L2 menu, skip hidden items
if (is_array($l2_menu[$parent_item])) {
foreach ($l2_menu[$parent_item] as $level2_item) {
if (!$level2_item['link']['hidden']) {
$level2_links['menu-' . $level2_item['link']['mlid']] = array(
'href' => $level2_item['link']['href'],
'title' => $level2_item['link']['link_title'],
);
}
}
}
// Set parent as active by changing key to 'active'
$level1_keys = join('|', array_keys($level1_links));
$level1_keys = str_replace('menu-' . $parent_item, 'active', $level1_keys);
$level1_links = array_combine(explode('|', $level1_keys), array_values($level1_links));
break;
}
}
// Theme the lot as a list of links.
$block['content']['main-menu'] = array(
'#markup' => theme('links__library_menu', array(
'links' => $level1_links,
'heading' => array(
'text' => t('Main menu'),
'level' => 'h2',
'class' => array('element-invisible'),
),
)),
'#prefix' => '<div class="main-menu">',
'#suffix' => '</div>',
);
// And add the library children items.
if (count($level2_links)) {
// Add in the library as the first item of the list.
$level2_links['menu-0'] = array(
'href' => $current_library['href'],
'title' => t('Frontpage'),
);
ksort($level2_links);
$block['content']['library-menu'] = array(
'#markup' => theme('links__library_menu', array(
'links' => $level2_links,
'heading' => array(
'text' => t('@lib submenu', array('@lib' => $current_library['title'])),
'level' => 'h2',
),
)),
'#prefix' => '<div class="library-menu clearfix">',
'#suffix' => '</div>',
);
}
}
if ($delta == 'ding_sidebar_menu') {
$trail = menu_get_active_trail();
if (!empty($trail[1])) {
$tree = menu_tree_page_data($trail[1]['menu_name']);
foreach ($tree as $item) {
if ($item['link']['in_active_trail']) {
// If the item is in the active trail, we continue in the subtree.
$tree = empty($item['below']) ? array() : $item['below'];
// Library content type behavior.
// Check if the current page is a library and show only links below it.
if (!empty($tree)) {
foreach ($tree as $item) {
if ($item['link']['in_active_trail']) {
$node = menu_get_object();
if ($node && $node->type == 'ding_library') {
$tree = $item['below'];
break;
}
}
}
}
break;
}
}
// If we have a tree, render it.
if (!empty($tree)) {
$block['content'] = menu_tree_output($tree);
}
}
}
return $block;
}
/**
* Implements hook_theme().
*/
function ding_library_theme() {
return array(
'ding_library_address' => array(
'render element' => 'element',
'template' => 'ding-library-address',
),
);
}
/**
* Preprocessor.
*/
function template_preprocess_ding_library_address(&$vars) {
$node = $vars['element']['#node'];
// Ensure empty strings as default.
$vars += array(
'name' => '',
'address' => '',
'city' => '',
'phone' => '',
'fax' => '',
'mail' => '',
);
if ($node) {
$languages = field_language('node', $node);
$vars['name'] = check_plain($node->title);
if (!empty($node->field_address[$languages['field_address']][0]) && ($address = $node->field_address[$languages['field_address']][0])) {
$vars['address'] = check_plain($address['thoroughfare']);
if ($address['premise']) {
$vars['address'] .= ', ' . $address['premise'];
}
$vars['city'] = join(' ', array(check_plain($address['postal_code']), check_plain($address['locality'])));
}
if (!empty($node->field_phone[$languages['field_phone']][0]) &&
($phone = $node->field_phone[$languages['field_phone']][0]['safe_value'])) {
$vars['phone'] = t('Phone: @phone_number', array('@phone_number' => $phone));
}
if (!empty($node->field_email[$languages['field_email']][0]) &&
($mail = $node->field_email[$languages['field_email']][0])) {
$vars['mail'] = l($mail['email'], 'mailto:' . $mail['email']);
}
}
elseif (user_access('administer blocks')) {
$vars['name'] = t('Please select a library to display address information for at the <a href="!link">block configuration page</a>.', array('!link' => url('admin/structure/block/manage/ding_library/library_address/configure')));
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ding_library_form_ding_library_node_form_alter(&$form, &$form_state) {
if (isset($form['field_slug'][$form['#node']->language])) {
$form['field_slug'][$form['#node']->language][0]['#element_validate'][] = 'ding_library_slug_validate';
}
elseif (isset($form['field_slug']['und'])) {
$form['field_slug']['und'][0]['#element_validate'][] = 'ding_library_slug_validate';
}
}
/**
* Implements hook_ctools_plugin_directory -
*/
function ding_library_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/content_types';
}
}
/**
* Custom validate handler for the slug field.
*
* Don't allow charecters that aren't URL friendly, such as # , etc.
*/
function ding_library_slug_validate(&$form, &$form_state) {
// Try to get the slug value.
if (isset($form_state['values']['field_slug'][$form_state['node']->language])) {
$slug = $form_state['values']['field_slug'][$form_state['node']->language][0]['value'];
}
elseif (isset($form_state['values']['field_slug']['und'])) {
$slug = $form_state['values']['field_slug']['und'][0]['value'];
}
// If we found the slug do the actual validation.
if (!empty($slug)) {
if (!(preg_match('/^[a-z0-9_\-]+$/', $slug))) {
form_set_error(implode('][', $form['#parents']), t('Illeagal charecters detected, only small letters (a-z), numbers (0-9), dashes (-) and underscores (_) allowed'));
}
}
}
/**
* Implements hook_views_data_alter().
*/
function ding_library_views_data_alter(&$data) {
foreach (field_info_fields() as $name => $field) {
$name = 'field_data_' . $name;
if ($field['type'] == 'group') {
foreach ($data[$name] as $column => $views_data) {
if (strpos($column, 'gid') !== FALSE) {
$data[$name]['ding_library_nid'] = $views_data;
unset($data[$name]['ding_library_nid']['filter']);
unset($data[$name]['ding_library_nid']['sort']);
$data[$name]['ding_library_nid']['title'] = str_replace('gid', 'nid', $data[$name]['ding_library_nid']['title']);
$data[$name]['ding_library_nid']['title short'] = str_replace('gid', 'nid', $data[$name]['ding_library_nid']['title short']);
$data[$name]['ding_library_nid']['argument']['handler'] = 'ding_library_nid_to_gid_argument';
}
}
}
}
}
/**
* Implements hook_og_audience_options_alter().
* We need this to bypass any group membership checks for any users to allow
* them posting events/news to any library.
*/
function ding_library_og_audience_options_alter(&$options, &$opt_group, $account) {
// Expose all active groups to the user.
$options['content groups'] = og_get_all_group();
}
/**
* Implements hook_panels_post_render().
*/
function ding_library_panels_post_render($display) {
// Add custom js to Ding library node page.
$is_node_page = strpos($display->cache_key, 'panel_context:node_view:node_view_panel_') !== FALSE;
$is_ding_library = FALSE;
if (!empty($display->context['argument_entity_id:node_1']->data->type)) {
$is_ding_library = $display->context['argument_entity_id:node_1']->data->type == 'ding_library';
}
if ($is_node_page && $is_ding_library) {
drupal_add_js(
drupal_get_path('module', 'ding_library') . '/js/ding_library.node_map.js',
array('scope' => 'footer')
);
}
}
/**
* Implements hook_preprocess_HOOK().
* Extend panel block's classes.
*/
function ding_library_preprocess_panels_pane(&$vars) {
if ($vars['pane']->type != 'block') {
return;
}
if (in_array('pane-ding-library-ding-sidebar-menu', $vars['classes_array'])) {
$trail = menu_get_active_trail();
$tree = menu_tree_page_data($trail[1]['menu_name']);
$class = $trail[1]['menu_name'] . '-' . $trail[1]['mlid'];
$vars['classes_array'][] = $class;
}
}
include_once('ding_library.features.inc');