forked from SU-SWS/stanford_subsites
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stanford_subsites.inc
549 lines (462 loc) · 15 KB
/
stanford_subsites.inc
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php
/**
* @file
* A collection of private helper functions for use with the stanford_subsites.module
*/
/**
* Gets the active subsite node based on node page and on path.
*
* @return object
* $node [subsite] or false
*/
function stanford_subsites_get_active_subsite() {
global $subsite;
$static_subsite = &drupal_static(__FUNCTION__);
// Grab from global object first.
if ($static_subsite) {
return $static_subsite;
}
// Not found or set yet. Carry on.
$subsite_node = FALSE;
// First try from node.
$a0 = arg(0);
$a1 = arg(1);
if ($a0 == "node" && is_numeric($a1)) {
$node = node_load($a1);
if (is_object($node)) {
$subsite_node = _lookup_subsite_reference_by_node($node);
}
}
// If the page being viewed is not a node then check the path.
if (!$subsite_node) {
$request_path = request_path();
$subsite_node = _lookup_subsite_reference_by_path($request_path);
}
// Assign the global subsite variable and return the result.
$subsite = $subsite_node;
$static_subsite = $subsite_node;
return $subsite_node;
}
/**
* Get all subsite nodes.
* $published = TRUE / FALSE / "ALL"
* @return array
* An array of subsite nodes
*/
function stanford_subsites_get_subsite_nodes($published = "all") {
$query = db_select("node", "n")
->fields("n", array("nid"))
->condition("type", SUBSITE_CONTENT_TYPE)
->orderBy('title');
if ($published !== "all") {
$query->condition("status", $published);
}
$results = $query->execute();
$nids = array();
while ($result = $results->fetchAssoc()) {
$nids[] = $result['nid'];
}
return node_load_multiple($nids);
}
/**
* Lookup function for nodes by entity reference.
*
* _lookup_subsite_reference_by_node()
*
* @param object $node
* The currently being viewed node object.
*
* @return bool
* True if this node has a subsite / False if none.
*/
function _lookup_subsite_reference_by_node($node) {
// If the node that is being viewed is the subsite node then just return it!
if ($node->type == SUBSITE_CONTENT_TYPE) {
return $node;
}
$term = FALSE;
// Check for the field.
if (isset($node->{SUBSITE_TAGS_FIELD})) {
$term = _get_subsite_term_from_node($node);
}
// Check for the term.
if (!$term) {
return FALSE;
}
return _get_subsite_node_by_term($term);
}
/**
* Lookup function for nodes by subsite path.
*
* _lookup_subsite_reference_by_path.
*
* @param string $path
* The currently viewed url path.
*
* @return mixed
* A node object of the subsite node or false if none.
*/
function _lookup_subsite_reference_by_path($path) {
// First get all of the subsite paths.
$subsite_paths = _get_subsite_paths();
// Check raw string for path... :( needs optimization.
foreach ($subsite_paths as $nid => $site_path) {
if (stripos($path, $site_path . "/") !== FALSE) {
return node_load($nid);
}
}
return FALSE;
}
/**
* Get the subsite base paths.
*
* @return array
* An array of subsite paths.
*/
function _get_subsite_paths() {
$paths = cache_get('stanford_subsites_subsite_paths');
// If cache isnt set then we need to generate it.
if (!$paths) {
$paths = array();
$query = db_select('node', 'n')
->fields('n', array('nid'))
->condition('type', SUBSITE_CONTENT_TYPE)
->execute();
while ($path_string = $query->fetchAssoc()) {
$paths[$path_string['nid']] = drupal_get_path_alias("node/" . $path_string['nid']);
}
cache_set('stanford_subsites_subsite_paths', $paths);
return $paths;
}
// Cached version.
return $paths->data;
}
/**
* Get taxonomy field instance.
*
* @param string $bundle
* The bundle this instance belongs to.
*
* @return array
* An array describing the instance of the reference field.
*/
function _get_tax_field_instance($bundle) {
if (!$bundle) {
return FALSE;
}
$ret = array();
$ret['entity_type'] = "node";
$ret['bundle'] = $bundle;
$ret['field_name'] = SUBSITE_TAGS_FIELD;
$ret['label'] = t('Subsite Tags');
$ret['description'] = t("Enter the value of the subsite.");
$ret['widget']['type'] = "taxonomy_select";
$ret['widget']['settings']['size'] = 60;
// $ret['widget']['settings']['autocomplete_path'] = "taxonomy/autocomplete";
$ret['display']['default']['type'] = "hidden";
$ret['display']['teaser']['type'] = "hidden";
return $ret;
}
/**
* Sets the active working space for subsites.
*
* Right now only sets the default subsite on node/add.
*
* @param int $nid
* The nid of the new active subsite for the working space.
*/
function stanford_subsites_set_subsite_workingspace($nid) {
// // Set it.
// stanford_subsites_clear_subsite_workingspace();
// // One week lifetime.
// $lifetime = time() + (60 * 60 * 24 * 7);
// setcookie('stanford_subsite_workspace', $nid, $lifetime, "/");
$_SESSION['active_subsite'] = $nid;
}
/**
* Removes the active subsite working space.
*/
function stanford_subsites_clear_subsite_workingspace() {
// setcookie('stanford_subsite_workspace', 0, 1, "/");
unset($_SESSION['active_subsite']);
}
/**
* Get the active working space.
*
* Gets the active working space for subsites. \
*
* @return int
* The nid of the active subsite for the working space or false.
*/
function stanford_subsites_get_subsite_workingspace() {
// If a cookie is available.
// if (isset($_COOKIE['stanford_subsite_workspace']) &&
// is_numeric($_COOKIE['stanford_subsite_workspace'])) {
// return check_plain($_COOKIE['stanford_subsite_workspace']);
// }
if (isset($_SESSION['active_subsite']) && is_numeric($_SESSION['active_subsite'])) {
return check_plain($_SESSION['active_subsite']);
}
// Not available.
return FALSE;
}
/**
* Creates and places a new menu and menu block.
*
* Creates and places a new menu and menu block specifically for this subsite
* In the new menu a 'home' link is created to the subsite
* menu block theme/region placement is controlled by the settings page
* Tasks:
* 1. Create menu
* 2. Add subsite node as a link
* 3. Create menu block from new menu
* 4. Place menu block via context into region
*
* @param object $node
* The newly created subsite node (node_insert hook)
*
* @return array
* menu => the menu,
* menu_block => the menu block object
*/
function _create_subsite_menu_and_menublock($node) {
// Create menu ---------------------------------------------------------------
$menu_name = _get_subsite_menu_name($node);
$menu = array(
'menu_name' => $menu_name,
'title' => t('Subsite Menu') . ": " . substr($node->{SUBSITE_NAME_FIELD}[$node->language][0]['value'], 0, 64),
'description' => t("Menu for all of this subsites links"),
);
// Save it.
menu_save($menu);
// Create menu link ----------------------------------------------------------
// Add a link to this subsite in the new menu.
$menu_link = array(
"link_path" => drupal_get_normal_path('node/' . $node->nid),
"link_title" => t("Home"),
"menu_name" => $menu_name,
);
// Save it!
menu_link_save($menu_link);
// Let the user know with a nice message.
drupal_set_message("A Menu for this subsite was successfully created. " . l(t("Edit the menu here »"), "admin/structure/menu/manage/" . $menu_name));
// Create menu block ---------------------------------------------------------
// Now create a menu block and place it.
$menu_block = array();
$block_ids = variable_get('menu_block_ids', array());
$delta = empty($block_ids) ? 1 : max($block_ids) + 1;
// Save the new array of blocks IDs.
$block_ids[] = $delta;
variable_set('menu_block_ids', $block_ids);
$menu_block['delta'] = $delta;
$menu_block['module'] = "menu_block";
$menu_block['delta'] = $delta;
$menu_block['title'] = "<none>";
$menu_block['display_options'] = "advanced";
$menu_block['title_link'] = 0;
$menu_block['admin_title'] = t('Subsite Menu Block: ') . $menu_name;
$menu_block['menu_name'] = $menu_name;
$menu_block['level'] = variable_get('stanford_subsite_menu_block_level', "1");
$menu_block['follow'] = 0;
$menu_block['follow_parent'] = "active";
$menu_block['depth'] = variable_get('stanford_subsite_menu_block_depth', "1");
$menu_block['expanded'] = 0;
$menu_block['sort'] = 0;
$menu_block['parent_mlid'] = $menu_name . ":0";
$menu_block['regions'] = array();
$menu_block['visibility'] = "0";
$menu_block['pages'] = "";
$menu_block['roles'] = array();
$menu_block['custom'] = "0";
$menu_block['types'] = array();
$menu_block['visibility__active_tab'] = "edit-path";
$menu_block['parent'] = $menu_name . ":0";
menu_block_block_save($delta, $menu_block);
// Run the normal new block submission
// (borrowed from block_add_block_form_submit).
$query = db_insert('block')->fields(array(
'visibility',
'pages',
'custom',
'title',
'module',
'theme',
'region',
'status',
'weight',
'delta',
'cache'
));
foreach (list_themes() as $key => $theme) {
if ($theme->status) {
$region = !empty($menu_block['regions'][$theme->name]) ? $menu_block['regions'][$theme->name] : BLOCK_REGION_NONE;
$query->values(array(
'visibility' => (int) $menu_block['visibility'],
'pages' => trim($menu_block['pages']),
'custom' => (int) $menu_block['custom'],
'title' => $menu_block['title'],
'module' => $menu_block['module'],
'theme' => $theme->name,
'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
'status' => 0,
'status' => (int) ($region != BLOCK_REGION_NONE),
'weight' => 0,
'delta' => $delta,
'cache' => DRUPAL_NO_CACHE,
));
}
}
$query->execute();
$query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
foreach (array_filter($menu_block['roles']) as $rid) {
$query->values(array(
'rid' => $rid,
'module' => $menu_block['module'],
'delta' => $delta,
));
}
$query->execute();
drupal_set_message(t('The Menu block has been created.'));
return array(
'menu' => $menu,
'menu_block' => $menu_block,
);
}
/**
* Enable the menu for use on all subsite enabled content types.
*
* @param object $menu
* A fully loaded menu object
*/
function _enable_menu_for_subsite_content_types($menu) {
// Get all enabled content types from the settings form array.
$enabled_types = variable_get('stanford_subsite_content_types', array());
// Loop through the enabled types and add the new menu to their settings array.
foreach ($enabled_types as $k => $type) {
$opts = variable_get('menu_options_' . $type, array('main-menu'));
$opts[] = $menu['menu_name'];
variable_set('menu_options_' . $type, $opts);
}
// Also do subsite content types by default...
$opts = variable_get('menu_options_' . SUBSITE_CONTENT_TYPE, array('main-menu'));
$opts[] = $menu['menu_name'];
variable_set('menu_options_' . SUBSITE_CONTENT_TYPE, $opts);
}
/**
* Create subsite context.
*
* @param object $node
* The fully saved and/or loaded subsite node object.
* @param array $args
* A misc array of arguments to pass in.
* menu => (optional) the menu associated with the subsite
* menu_block => (optional) the menu_block associated with the subsite
*/
function _create_subsite_context($node, &$args) {
$menu = isset($args['menu']) ? $args['menu'] : FALSE;
$menu_block = isset($args['menu_block']) ? $args['menu_block'] : FALSE;
$delta = isset($args['menu_block']) ? $menu_block['delta'] : 0;
// We can cheat here a bit and re-use the menu name function.
$context_name = _get_subsite_menu_name($node);
// Find out what region we are suspposed to be placing the menu block in by
// getting the theme from the subsite node and comparing it to the subsite
// configuration settings.
$the_theme = variable_get('theme_default');
$theme_region = FALSE;
if (isset($node->{SUBSITE_THEME_FIELD}[$node->language][0]['value']) && $node->{SUBSITE_THEME_FIELD}[$node->language][0]['value'] !== "default") {
$the_theme = $node->{SUBSITE_THEME_FIELD}[$node->language][0]['value'];
}
// Settings from admin/config/subsites/.
$settings = variable_get('stanford_subsite_mbtr', array());
if (isset($settings[$the_theme]) && $settings[$the_theme] !== "stanford_subsites_dnp") {
$theme_region = $settings[$the_theme];
}
$context = new stdClass();
$context->name = $context_name;
$context->description = "Subsite wide context for " . substr($node->{SUBSITE_NAME_FIELD}[$node->language][0]['value'], 0, 210);
$context->tag = drupal_clean_css_identifier(substr($node->{SUBSITE_NAME_FIELD}[$node->language][0]['value'], 0, 32));
$context->conditions = array(
'active_subsite' => array(
'values' => array(
$node->nid => $node->nid,
),
),
);
if ($theme_region) {
$context->reactions = array(
'block' => array(
'blocks' => array(
"menu_block-" . $delta => array(
'module' => "menu_block",
'delta' => $delta,
'region' => $theme_region,
'weight' => -10,
),
),
),
);
}
$context->condition_mode = 0;
$context->table = "context";
$context->type = "Normal";
$context->export_type = 1;
context_save($context);
cache_clear_all();
drupal_set_message("A subsite context was created for you. " . l(t("Edit it here") . " »", "admin/structure/context/list/" . $context_name . "/edit"));
$args['context'] = $context;
return TRUE;
}
/**
* Generates the machine name for the drupal menu for a subsite node.
*
* @param object $node
* A subsite node.
*
* @return string
* The machine name of the menu for the subsite
*/
function _get_subsite_menu_name($node) {
$filter = array(' ' => '-', '/' => '-', '[' => '-', ']' => '-');
$term = _get_subsite_term_from_node($node);
$menu_name = strtolower(drupal_clean_css_identifier($term->name, $filter));
// Max length of menu name is 32 chars.
$menu_name = substr($menu_name, 0, 32);
return $menu_name;
}
/**
* Returns a fully loaded subsite term.
* @param [type] $node [description]
* @return [type] [description]
*/
function _get_subsite_term_from_node($node) {
if (!is_object($node)) {
return FALSE;
}
$lang = $node->language;
$tid = @$node->{SUBSITE_TAGS_FIELD}[$lang][0]['tid'];
if (!$tid) {
return FALSE;
}
return taxonomy_term_load($tid);
}
/**
* [_get_subsite_node_by_term description]
* @param [type] $term [description]
* @return [type] [description]
*/
function _get_subsite_node_by_term($term) {
// Find a node by joining the taxonomy_index table to node and filtering on
// node type and tid.
$select = db_select("node", "n")
->fields("n", array("nid"))
->condition("ti.tid", $term->tid)
->condition("n.type", SUBSITE_CONTENT_TYPE)
->range(0, 1);
$select->join("taxonomy_index", "ti", "n.nid = ti.nid");
$nid = $select->execute()->fetchField();
// If through all that we get one node. Load and return it.
if (is_numeric($nid)) {
return node_load($nid);
}
return FALSE;
}