1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide access control based on whether a node belongs to a book.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Plugins are described by creating a $plugin array which will be used
|
10
|
* by the system that includes this file.
|
11
|
*/
|
12
|
if (module_exists('book')) {
|
13
|
$plugin = array(
|
14
|
'title' => t("Book: node is in a book"),
|
15
|
'description' => t('Control access based upon a node belonging to a book.'),
|
16
|
'callback' => 'ctools_book_node_ctools_access_check',
|
17
|
'default' => array('book' => array()),
|
18
|
'settings form' => 'ctools_book_node_ctools_access_settings',
|
19
|
'settings form submit' => 'ctools_book_node_ctools_access_settings_submit',
|
20
|
'summary' => 'ctools_book_node_ctools_access_summary',
|
21
|
'required context' => new ctools_context_required(t('Node'), 'node'),
|
22
|
);
|
23
|
}
|
24
|
|
25
|
/**
|
26
|
* Settings form for the 'by book_node' access plugin.
|
27
|
*/
|
28
|
function ctools_book_node_ctools_access_settings($form, &$form_state, $conf) {
|
29
|
$options = array(
|
30
|
'any' => t('In any book'),
|
31
|
);
|
32
|
$books = book_get_books();
|
33
|
foreach ($books as $bid => $book) {
|
34
|
$options[$bid] = $book['title'];
|
35
|
}
|
36
|
$form['settings']['book'] = array(
|
37
|
'#title' => t('Book'),
|
38
|
'#type' => 'checkboxes',
|
39
|
'#options' => $options,
|
40
|
'#description' => t('Pass only if the node belongs to one of the selected books'),
|
41
|
'#default_value' => $conf['book'],
|
42
|
'#required' => TRUE,
|
43
|
);
|
44
|
return $form;
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Check for access.
|
49
|
*/
|
50
|
function ctools_book_node_ctools_access_check($conf, $context) {
|
51
|
// As far as I know there should always be a context at this point, but this
|
52
|
// is safe.
|
53
|
if (empty($context) || empty($context->data) || empty($context->data->book)) {
|
54
|
return FALSE;
|
55
|
}
|
56
|
|
57
|
if ($conf['book']['any']) {
|
58
|
return !empty($context->data->book);
|
59
|
}
|
60
|
|
61
|
foreach ($conf['book'] as $bid => $value) {
|
62
|
if ($bid == 'any') {
|
63
|
continue;
|
64
|
}
|
65
|
if ($value && ($bid == $context->data->book['bid'])) {
|
66
|
return TRUE;
|
67
|
}
|
68
|
}
|
69
|
|
70
|
return FALSE;
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Provide a summary description based upon the checked node_languages.
|
75
|
*/
|
76
|
function ctools_book_node_ctools_access_summary($conf, $context) {
|
77
|
if ($conf['book']['any']) {
|
78
|
return t('@identifier belongs to a book', array('@identifier' => $context->identifier));
|
79
|
}
|
80
|
|
81
|
$books = array();
|
82
|
foreach ($conf['book'] as $bid => $value) {
|
83
|
if ($value) {
|
84
|
$node = node_load($bid);
|
85
|
$books[] = $node->title;
|
86
|
}
|
87
|
}
|
88
|
|
89
|
if (count($books) == 1) {
|
90
|
return t('@identifier belongs to the book "@book"', array('@book' => $books[0], '@identifier' => $context->identifier));
|
91
|
}
|
92
|
|
93
|
return t('@identifier belongs in multiple books', array('@identifier' => $context->identifier));
|
94
|
}
|