1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide access control based upon specific terms.
|
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
|
$plugin = array(
|
13
|
'title' => t("Taxonomy: term"),
|
14
|
'description' => t('Control access by a specific term.'),
|
15
|
'callback' => 'ctools_term_ctools_access_check',
|
16
|
'default' => array('vids' => array()),
|
17
|
'settings form' => 'ctools_term_ctools_access_settings',
|
18
|
'settings form validate' => 'ctools_term_ctools_access_settings_validate',
|
19
|
'settings form submit' => 'ctools_term_ctools_access_settings_submit',
|
20
|
'summary' => 'ctools_term_ctools_access_summary',
|
21
|
'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
|
22
|
);
|
23
|
|
24
|
/**
|
25
|
* Settings form for the 'by term' access plugin.
|
26
|
*/
|
27
|
function ctools_term_ctools_access_settings($form, &$form_state, $conf) {
|
28
|
// If no configuration was saved before, set some defaults.
|
29
|
if (empty($conf)) {
|
30
|
$conf = array(
|
31
|
'vid' => 0,
|
32
|
);
|
33
|
}
|
34
|
if (!isset($conf['vid'])) {
|
35
|
$conf['vid'] = 0;
|
36
|
}
|
37
|
|
38
|
$form['settings']['vid'] = array(
|
39
|
'#title' => t('Vocabulary'),
|
40
|
'#type' => 'select',
|
41
|
'#options' => array(),
|
42
|
'#description' => t('Select the vocabulary for this form.'),
|
43
|
'#id' => 'ctools-select-vid',
|
44
|
'#default_value' => $conf['vid'],
|
45
|
'#required' => TRUE,
|
46
|
);
|
47
|
|
48
|
ctools_include('dependent');
|
49
|
$options = array();
|
50
|
|
51
|
// A note: Dependency works strangely on these forms as they have never been
|
52
|
// updated to a more modern system so they are not individual forms of their
|
53
|
// own like the content types.
|
54
|
$form['settings']['#tree'] = TRUE;
|
55
|
|
56
|
// Loop over each of the configured vocabularies.
|
57
|
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
|
58
|
$options[$vid] = $vocabulary->name;
|
59
|
$form['settings'][$vocabulary->vid] = array(
|
60
|
'#title' => t('Terms'),
|
61
|
'#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)), //. $description,
|
62
|
'#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
|
63
|
'#default_value' => !empty($conf[$vid]) ? $conf[$vid] : '',
|
64
|
'#multiple' => TRUE,
|
65
|
);
|
66
|
|
67
|
$terms = array();
|
68
|
foreach (taxonomy_get_tree($vocabulary->vid) as $tid => $term) {
|
69
|
$terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name;
|
70
|
}
|
71
|
$form['settings'][$vocabulary->vid]['#type'] = 'select';
|
72
|
$form['settings'][$vocabulary->vid]['#options'] = $terms;
|
73
|
unset($terms);
|
74
|
}
|
75
|
$form['settings']['vid']['#options'] = $options;
|
76
|
return $form;
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Check for access.
|
81
|
*/
|
82
|
function ctools_term_ctools_access_check($conf, $context) {
|
83
|
// As far as I know there should always be a context at this point, but this
|
84
|
// is safe.
|
85
|
if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
|
86
|
return FALSE;
|
87
|
}
|
88
|
|
89
|
// Get the $vid.
|
90
|
if (!isset($conf['vid'])) {
|
91
|
return FALSE;
|
92
|
}
|
93
|
$vid = $conf['vid'];
|
94
|
|
95
|
// Get the terms.
|
96
|
if (!isset($conf[$vid])) {
|
97
|
return FALSE;
|
98
|
}
|
99
|
|
100
|
$return = FALSE;
|
101
|
|
102
|
$terms = array_filter($conf[$vid]);
|
103
|
// For multi-term if any terms coincide, let's call that good enough:
|
104
|
if (isset($context->tids)) {
|
105
|
return (bool) array_intersect($terms, $context->tids);
|
106
|
}
|
107
|
else {
|
108
|
return in_array($context->data->tid, $terms);
|
109
|
}
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Provide a summary description based upon the checked terms.
|
114
|
*/
|
115
|
function ctools_term_ctools_access_summary($conf, $context) {
|
116
|
$vid = $conf['vid'];
|
117
|
$terms = array();
|
118
|
foreach ($conf[$vid] as $tid) {
|
119
|
$term = taxonomy_term_load($tid);
|
120
|
$terms[] = $term->name;
|
121
|
}
|
122
|
|
123
|
return format_plural(count($terms),
|
124
|
'@term can be the term "@terms"',
|
125
|
'@term can be one of these terms: @terms',
|
126
|
array(
|
127
|
'@terms' => implode(', ', $terms),
|
128
|
'@term' => $context->identifier,
|
129
|
));
|
130
|
}
|