1 |
85ad3d82
|
Assos Assos
|
<?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("Taxonomy: vocabulary"),
|
14 |
|
|
'description' => t('Control access by vocabulary.'),
|
15 |
|
|
'callback' => 'ctools_term_vocabulary_ctools_access_check',
|
16 |
|
|
'default' => array('vids' => array()),
|
17 |
|
|
'settings form' => 'ctools_term_vocabulary_ctools_access_settings',
|
18 |
|
|
'settings form submit' => 'ctools_term_vocabulary_ctools_access_settings_submit',
|
19 |
|
|
'summary' => 'ctools_term_vocabulary_ctools_access_summary',
|
20 |
3753f249
|
Assos Assos
|
'required context' => new ctools_context_required(t('Vocabulary'), array(
|
21 |
|
|
'taxonomy_term',
|
22 |
|
|
'terms',
|
23 |
|
|
'taxonomy_vocabulary'
|
24 |
|
|
)),
|
25 |
85ad3d82
|
Assos Assos
|
);
|
26 |
|
|
|
27 |
|
|
/**
|
28 |
|
|
* Settings form for the 'by term_vocabulary' access plugin
|
29 |
|
|
*/
|
30 |
|
|
function ctools_term_vocabulary_ctools_access_settings($form, &$form_state, $conf) {
|
31 |
|
|
$options = array();
|
32 |
|
|
$vocabularies = taxonomy_get_vocabularies();
|
33 |
|
|
foreach ($vocabularies as $voc) {
|
34 |
3753f249
|
Assos Assos
|
$options[$voc->machine_name] = check_plain($voc->name);
|
35 |
85ad3d82
|
Assos Assos
|
}
|
36 |
|
|
|
37 |
3753f249
|
Assos Assos
|
_ctools_term_vocabulary_ctools_access_map_vids($conf);
|
38 |
|
|
|
39 |
|
|
$form['settings']['machine_name'] = array(
|
40 |
85ad3d82
|
Assos Assos
|
'#type' => 'checkboxes',
|
41 |
|
|
'#title' => t('Vocabularies'),
|
42 |
|
|
'#options' => $options,
|
43 |
|
|
'#description' => t('Only the checked vocabularies will be valid.'),
|
44 |
3753f249
|
Assos Assos
|
'#default_value' => $conf['machine_name'],
|
45 |
85ad3d82
|
Assos Assos
|
);
|
46 |
|
|
return $form;
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* Compress the term_vocabularys allowed to the minimum.
|
51 |
|
|
*/
|
52 |
|
|
function ctools_term_vocabulary_ctools_access_settings_submit($form, &$form_state) {
|
53 |
3753f249
|
Assos Assos
|
$form_state['values']['settings']['machine_name'] = array_filter($form_state['values']['settings']['machine_name']);
|
54 |
85ad3d82
|
Assos Assos
|
}
|
55 |
|
|
|
56 |
|
|
/**
|
57 |
|
|
* Check for access.
|
58 |
|
|
*/
|
59 |
|
|
function ctools_term_vocabulary_ctools_access_check($conf, $context) {
|
60 |
|
|
// As far as I know there should always be a context at this point, but this
|
61 |
|
|
// is safe.
|
62 |
3753f249
|
Assos Assos
|
if (empty($context) || empty($context->data) || empty($context->data->vocabulary_machine_name)) {
|
63 |
85ad3d82
|
Assos Assos
|
return FALSE;
|
64 |
|
|
}
|
65 |
|
|
|
66 |
3753f249
|
Assos Assos
|
_ctools_term_vocabulary_ctools_access_map_vids($conf);
|
67 |
|
|
|
68 |
|
|
if (array_filter($conf['machine_name']) && empty($conf['machine_name'][$context->data->vocabulary_machine_name])) {
|
69 |
85ad3d82
|
Assos Assos
|
return FALSE;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
return TRUE;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Provide a summary description based upon the checked term_vocabularys.
|
77 |
|
|
*/
|
78 |
|
|
function ctools_term_vocabulary_ctools_access_summary($conf, $context) {
|
79 |
|
|
if (!isset($conf['type'])) {
|
80 |
|
|
$conf['type'] = array();
|
81 |
|
|
}
|
82 |
|
|
$vocabularies = taxonomy_get_vocabularies();
|
83 |
|
|
|
84 |
3753f249
|
Assos Assos
|
_ctools_term_vocabulary_ctools_access_map_vids($conf);
|
85 |
|
|
|
86 |
85ad3d82
|
Assos Assos
|
$names = array();
|
87 |
3753f249
|
Assos Assos
|
if (!empty($conf['machine_name'])) {
|
88 |
|
|
foreach (array_filter($conf['machine_name']) as $machine_name) {
|
89 |
|
|
foreach ($vocabularies as $vocabulary) {
|
90 |
|
|
if ($vocabulary->machine_name === $machine_name) {
|
91 |
|
|
$names[] = check_plain($vocabulary->name);
|
92 |
|
|
continue;
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
85ad3d82
|
Assos Assos
|
}
|
97 |
|
|
|
98 |
|
|
if (empty($names)) {
|
99 |
|
|
return t('@identifier is any vocabulary', array('@identifier' => $context->identifier));
|
100 |
|
|
}
|
101 |
|
|
|
102 |
3753f249
|
Assos Assos
|
return format_plural(count($names), '@identifier vocabulary is "@machine_names"', '@identifier vocabulary is one of "@machine_names"', array(
|
103 |
|
|
'@machine_names' => implode(', ', $names),
|
104 |
|
|
'@identifier' => $context->identifier
|
105 |
|
|
));
|
106 |
85ad3d82
|
Assos Assos
|
}
|
107 |
|
|
|
108 |
3753f249
|
Assos Assos
|
/**
|
109 |
|
|
* Helper function to map the vids from old features to the new machine_name.
|
110 |
|
|
*
|
111 |
|
|
* Add the machine_name key to $conf if the vids key exist.
|
112 |
|
|
*
|
113 |
|
|
* @param array $conf
|
114 |
|
|
* The configuration of this plugin.
|
115 |
|
|
*/
|
116 |
|
|
function _ctools_term_vocabulary_ctools_access_map_vids(&$conf) {
|
117 |
|
|
if (!empty($conf['vids'])) {
|
118 |
|
|
$conf['machine_name'] = array();
|
119 |
|
|
$vocabularies = taxonomy_get_vocabularies();
|
120 |
|
|
foreach ($conf['vids'] as $vid) {
|
121 |
|
|
$machine_name = $vocabularies[$vid]->machine_name;
|
122 |
|
|
$conf['machine_name'][$machine_name] = $vocabularies[$vid]->machine_name;
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
|