This repository has been archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsc_cl.module
336 lines (258 loc) · 8.11 KB
/
rsc_cl.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
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
<?php
/**
* Implements hook_perm().
*/
function rsc_cl_permission() {
$permissions = array();
$permissions['administer rsc_cl'] = array(
'title' => t('Configure RSC CL module')
);
return $permissions;
}
/**
* Implementation of hook_menu().
*/
function rsc_cl_menu() {
$items = array();
//administrative menu
$items['admin/config/development/rsc-cl'] = array(
'title' => 'RSC CL',
'description' => t("Custom administration, hacks and settings for RSC's Christian Library"),
'page callback' => 'drupal_get_form',
'page arguments' => array('rsc_cl_admin_form'),
'access arguments' => array('administer rsc_cl'),
);
// TODO: move this page to rsc_taxonomy or rsc_library module
// TODO: replace library taxonomy pages with this type of display?
$items['lib/browse'] = array(
'type' => MENU_NORMAL_ITEM,
'title' => 'Browse through all library categories',
'page callback' => 'rsc_cl_browse',
'access callback' => 'user_access',
'access arguments' => array('access content'),
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function rsc_cl_menu_alter(&$items) {
// disable the normal user registration:
$items['user/register']['access callback'] = false;
}
function rsc_cl_admin_form() {
$form = array();
$form['hello'] = array(
'#markup' => 'There is nothing to configure.'
);
return system_settings_form($form);
}
function rsc_cl_admin_form_submit($form, &$form_state) {
}
function rsc_cl_admin_form_validate($form, &$form_state) {
}
/**
* Implements hook_block_info().
* https://api.drupal.org/api/drupal/modules!block!block.api.php/function/hook_block_info/7
*
* Defines custom blocks
*/
function rsc_cl_block_info() {
$blocks = array();
$blocks['main_menu'] = array(
'info' => 'Conditional main menu (from rsc_cl.module)',
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view($delta = '').
* https://api.drupal.org/api/drupal/modules!block!block.api.php/function/hook_block_view/7
*
* Serves the content for custom blocks
*/
function rsc_cl_block_view($delta) {
$block = array();
switch($delta) {
case 'main_menu':
$links = array();
$items = array();
if (rsc_cl_preaching_library()) {
// Preaching library menu
if (user_is_logged_in()) {
$links['pl'] = 'Home';
$links['user'] = 'My account';
$links['preacher/logout'] = 'Log out';
// $links['pl/help'] = 'Help!'; // There is no help page
} else {
$links['preacher/login'] = 'Log in';
$links['preacher/register'] = 'Register';
}
} else {
// Christian library menus
if (drupal_is_front_page() || arg(0) == 'search') {
$links += array(
'about' => 'About us',
'contact' => 'Contact us',
'donate' => 'Donations',
);
} else {
$links[''] = 'Home';
}
$links['lib/browse'] = 'Browse';
if (user_is_logged_in()) {
$links['user/logout'] = 'Log out';
} else {
$links['user/login'] = 'Log in';
}
}
foreach($links as $path => $text) {
$items[] = l($text,$path);
}
$block['subject'] = NULL; // no title
$block['content'] = array(
'#theme' => 'item_list',
'#items' => $items,
'#title' => 'Main menu',
'#type' => 'ul',
'#attributes' => array(
'class' => 'menu',
),
);
break;
}
return $block;
}
/**
* Implements template_preprocess_block(&$variables)
* https://api.drupal.org/api/drupal/modules!block!block.module/function/template_preprocess_block/7
*/
function rsc_cl_preprocess_block(&$variables) {
if ($variables['block']->module == 'rsc_cl' && $variables['block']->delta == 'main_menu') {
$variables['classes_array'][] = 'block-menu';
}
}
/**
* Page callback.
* This function is temporary.
*/
function rsc_cl_browse() {
$cid = 'rsc_cl_browse';
$GLOBALS['rsc_library']['lid'] = 2;
$content = cache_get($cid);
if (empty($content)) { // if the data is not available from cache
// drupal_set_message("Missed browse cache. Rebuilding page.");
$content=array();
$vid=4; // TODO: make this a parameter
$terms = taxonomy_get_tree($vid); // TODO: compare performance to simple sql query (combine with query below)
$tree = array();
// sort the array by weight while it is still flat // TODO: compare performance to quicksort
function weightsort($a,$b) {
return ($a->weight > $b->weight);
}
usort($terms,'weightsort');
foreach($terms as &$term) {
// key all terms by tid
$tree[$term->tid] = &$term;
// count nodes that fall directly under each term
$term->nodes = db_query("SELECT count(nid) as num FROM taxonomy_index WHERE tid = :tid",array(':tid'=>$term->tid))->fetchField();
// create array to contain children of each term
$term->children = array();
}
// let each term be referenced by it's parent
foreach($tree as $tid => &$term) {
$parent_tid = $term->parents[0];
if ($parent_tid) {
$tree[$parent_tid]->children[$tid] = &$term;
}
}
// unset terms in top level of array that have parents
foreach($tree as $tid => &$term) {
$parent_tid = $term->parents[0];
if ($parent_tid) {
unset($tree[$tid]); // unset term in base of array
}
}
function get_items($tree) {
$items = array();
foreach($tree as $tid => &$term) {
$nodes = ($term->nodes) ? " ({$term->nodes})" : "" ;
$items[$tid] = array(
'data' => l($term->name.$nodes,"taxonomy/term/{$term->tid}"),
'children' => get_items($term->children),
);
}
return $items;
}
$content['list'] = array(
'#theme' => 'item_list',
'#prefix' => '<div class="browse-taxonomy">',
'#suffix' => '</div>',
'#items' => get_items($tree),
);
// cache the browse page content (TODO: cache the rendered content?)
cache_set($cid, $content, 'cache', REQUEST_TIME + 2*24*60*60);
} else { // data is available from cache
$content = $content->data;
// drupal_set_message("Hit browse cache :D");
}
return $content;
}
/**
* Implements hook_init().
*/
function rsc_cl_init() {
/*
* Add the bib.ly JS and CSS files to every page
* Mostly taken from bibly module at https://www.drupal.org/project/bibly
*/
// The styling CSS.
drupal_add_css(drupal_get_path('module', 'rsc_cl').'/bibly.min.css', array(
'type' => 'file',
'group' => CSS_SYSTEM,
'every_page' => TRUE,
'scope' => 'footer',
));
// The JS to make the magic happen.
drupal_add_js(drupal_get_path('module', 'rsc_cl').'/bibly.min.js', array(
'type' => 'file',
'group' => JS_LIBRARY,
'every_page' => TRUE,
'scope' => 'footer',
));
// The config options.
$data = <<<JS
bibly.enablePopups = true;
bibly.popupVersion = 'ESV';
JS;
drupal_add_js($data, array(
'type' => 'inline',
'group' => JS_LIBRARY,
'every_page' => TRUE,
'scope' => 'footer',
));
/*
* Enable the library blocks on the youth page
*/
if (request_path() == 'youth') {
$GLOBALS['rsc_library']['lid'] = 2;
}
}
/**
* Determine if the current page request has something to do with the Preaching
* Library. Since a theme and a module may not depend on one another, there is
* a copy of this function in rsc2014. Keep them in sync please!
*/
function rsc_cl_preaching_library() {
/*
* To make it work for drupal sites that live in a subfolder, don't use
* request_uri(). Rather use arg() and/or request_path(). Note that arg will
* return the internal argument of the page rather than the aliased URL seen
* by the browser. Sometimes useful, other times a pain.
*/
return
// URL starts with /pl or /preacher
in_array(arg(0), array('pl', 'preacher',)) ||
// A file is being accessed from the PL directory (needed to make 404 pages look like the PL theme)
substr(request_path(), 0, 15) == 'system/files/pl';
}