1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide access control based upon term vocabulary
|
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("Forum: forum"),
|
14
|
'description' => t('Control access by which forum is in use.'),
|
15
|
'callback' => 'advanced_forum_forum_id_ctools_access_check',
|
16
|
'default' => array('tids' => array()),
|
17
|
'settings form' => 'advanced_forum_forum_id_ctools_access_settings',
|
18
|
'settings form submit' => 'advanced_forum_forum_id_ctools_access_settings_submit',
|
19
|
'summary' => 'advanced_forum_forum_id_ctools_acesss_summary',
|
20
|
'required context' => new ctools_context_required(t('Forum'), array('forum')),
|
21
|
);
|
22
|
|
23
|
/**
|
24
|
* Settings form for the 'by term_vocabulary' access plugin.
|
25
|
*/
|
26
|
function advanced_forum_forum_id_ctools_access_settings($form, &$form_state, $conf) {
|
27
|
$options = array();
|
28
|
$vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', ''));
|
29
|
$options[0] = $vocabulary->name;
|
30
|
|
31
|
$tree = taxonomy_get_tree($vocabulary->vid);
|
32
|
if ($tree) {
|
33
|
foreach ($tree as $term) {
|
34
|
$choice = new stdClass();
|
35
|
$choice->option = array($term->tid => str_repeat('-', $term->depth + 1) . $term->name);
|
36
|
$options[] = $choice;
|
37
|
}
|
38
|
}
|
39
|
|
40
|
$form['settings']['tids'] = array(
|
41
|
'#type' => 'select',
|
42
|
'#title' => t('Forums'),
|
43
|
'#default_value' => $conf['tids'],
|
44
|
'#options' => $options,
|
45
|
'#multiple' => TRUE,
|
46
|
'#size' => $multiple ? min(9, count($options)) : 0,
|
47
|
);
|
48
|
|
49
|
return $form;
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Compress the term_vocabularys allowed to the minimum.
|
54
|
*/
|
55
|
function advanced_forum_forum_id_ctools_access_settings_submit($form, &$form_state) {
|
56
|
$form_state['values']['settings']['rids'] = array_keys(array_filter($form_state['values']['settings']['rids']));
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Check for access.
|
61
|
*/
|
62
|
function advanced_forum_forum_id_ctools_access_check($conf, $context) {
|
63
|
// As far as I know there should always be a context at this point, but this
|
64
|
// is safe.
|
65
|
if (empty($context) || empty($context->data) || !isset($context->data->tid)) {
|
66
|
return FALSE;
|
67
|
}
|
68
|
|
69
|
if (!empty($conf['tids']) && !isset($conf['tids'][$context->data->tid])) {
|
70
|
return FALSE;
|
71
|
}
|
72
|
|
73
|
return TRUE;
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Provide a summary description based upon the checked term_vocabularys.
|
78
|
*/
|
79
|
function advanced_forum_forum_id_ctools_acesss_summary($conf, $context) {
|
80
|
$vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', ''));
|
81
|
if (empty($conf['tids'])) {
|
82
|
return t('@identifier is any forum', array('@identifier' => $context->identifier));
|
83
|
}
|
84
|
|
85
|
$names = array();
|
86
|
if (in_array(0, $conf['tids'])) {
|
87
|
$names[] = check_plain($vocabulary->name);
|
88
|
}
|
89
|
$result = db_query("SELECT name FROM {taxonomy_term_data} WHERE tid IN (" . implode(',', $conf['tids']) . ")");
|
90
|
|
91
|
while ($term = $result->fetchObject()) {
|
92
|
$names[] = check_plain($term->name);
|
93
|
}
|
94
|
|
95
|
if (empty($names)) {
|
96
|
return t('@identifier is any forum', array('@identifier' => $context->identifier));
|
97
|
}
|
98
|
|
99
|
return format_plural(count($names), '@identifier is "@tids"', '@identifier is one of "@tids"', array(
|
100
|
'@tids' => implode(', ', $names),
|
101
|
'@identifier' => $context->identifier,
|
102
|
)
|
103
|
);
|
104
|
}
|