1 |
3753f249
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Plugin to provide access control based upon a parent term.
|
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 depth"),
|
14 |
|
|
'description' => t('Control access by the depth of a term.'),
|
15 |
|
|
'callback' => 'term_depth_term_depth_ctools_access_check',
|
16 |
|
|
'default' => array('vid' => array(), 'depth' => 0),
|
17 |
|
|
'settings form' => 'term_depth_term_depth_ctools_access_settings',
|
18 |
219d19c4
|
Assos Assos
|
'settings form validate' => 'term_depth_term_depth_ctools_access_settings_validate',
|
19 |
3753f249
|
Assos Assos
|
'settings form submit' => 'term_depth_term_depth_ctools_access_settings_submit',
|
20 |
|
|
'summary' => 'term_depth_term_depth_ctools_access_summary',
|
21 |
|
|
'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
|
22 |
|
|
);
|
23 |
|
|
|
24 |
|
|
/**
|
25 |
|
|
* Settings form for the 'term depth' access plugin.
|
26 |
|
|
*/
|
27 |
|
|
function term_depth_term_depth_ctools_access_settings($form, &$form_state, $conf) {
|
28 |
136a805a
|
Assos Assos
|
$vocabularies = taxonomy_get_vocabularies();
|
29 |
|
|
$options = array();
|
30 |
3753f249
|
Assos Assos
|
// Loop over each of the configured vocabularies.
|
31 |
136a805a
|
Assos Assos
|
foreach ($vocabularies as $vid => $vocab) {
|
32 |
|
|
$options[$vocab->machine_name] = $vocab->name;
|
33 |
3753f249
|
Assos Assos
|
}
|
34 |
|
|
|
35 |
136a805a
|
Assos Assos
|
_term_depth_convert_config_vid_to_vocabulary_name($conf);
|
36 |
|
|
|
37 |
|
|
$form['settings']['vocabulary'] = array(
|
38 |
3753f249
|
Assos Assos
|
'#title' => t('Vocabulary'),
|
39 |
|
|
'#type' => 'select',
|
40 |
|
|
'#options' => $options,
|
41 |
|
|
'#description' => t('Select the vocabulary for this form. If there exists a parent term in that vocabulary, this access check will succeed.'),
|
42 |
136a805a
|
Assos Assos
|
'#id' => 'ctools-select-vocabulary',
|
43 |
|
|
'#default_value' => !empty($conf['vocabulary']) ? $conf['vocabulary'] : array(),
|
44 |
3753f249
|
Assos Assos
|
'#required' => TRUE,
|
45 |
|
|
);
|
46 |
|
|
|
47 |
|
|
$form['settings']['depth'] = array(
|
48 |
|
|
'#title' => t('Depth'),
|
49 |
|
|
'#type' => 'textfield',
|
50 |
|
|
'#description' => t('Set the required depth of the term. If the term exists at the correct depth, this access check will succeed.'),
|
51 |
|
|
'#default_value' => $conf['depth'],
|
52 |
|
|
'#required' => TRUE,
|
53 |
|
|
);
|
54 |
|
|
|
55 |
|
|
return $form;
|
56 |
|
|
}
|
57 |
|
|
|
58 |
136a805a
|
Assos Assos
|
/**
|
59 |
|
|
* @param $conf
|
60 |
|
|
*/
|
61 |
|
|
function _term_depth_convert_config_vid_to_vocabulary_name(&$conf) {
|
62 |
|
|
// Fallback on legacy 'vid', when no vocabularies are available.
|
63 |
|
|
if (empty($conf['vocabulary']) && !empty($conf['vid'])) {
|
64 |
7e72b748
|
Assos Assos
|
$vocabulary = _ctools_term_vocabulary_machine_name_convert(array($conf['vid']));
|
65 |
|
|
$conf['vocabulary'] = reset($vocabulary);
|
66 |
|
|
unset($conf['vid'], $vocabulary);
|
67 |
136a805a
|
Assos Assos
|
}
|
68 |
|
|
}
|
69 |
|
|
|
70 |
3753f249
|
Assos Assos
|
/**
|
71 |
|
|
* Submit function for the access plugins settings.
|
72 |
|
|
*/
|
73 |
|
|
function term_depth_term_depth_ctools_access_settings_submit($form, $form_state) {
|
74 |
136a805a
|
Assos Assos
|
$form_state['conf']['depth'] = (integer) $form_state['values']['settings']['depth'];
|
75 |
|
|
$form_state['conf']['vocabulary'] = array_filter($form_state['conf']['vocabulary']);
|
76 |
3753f249
|
Assos Assos
|
}
|
77 |
|
|
|
78 |
|
|
/**
|
79 |
|
|
* Check for access.
|
80 |
|
|
*/
|
81 |
|
|
function term_depth_term_depth_ctools_access_check($conf, $context) {
|
82 |
|
|
// As far as I know there should always be a context at this point, but this
|
83 |
|
|
// is safe.
|
84 |
|
|
if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
|
85 |
|
|
return FALSE;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
136a805a
|
Assos Assos
|
_term_depth_convert_config_vid_to_vocabulary_name($conf);
|
89 |
|
|
|
90 |
|
|
// Get the $vocabulary.
|
91 |
|
|
if (!isset($conf['vocabulary'])) {
|
92 |
3753f249
|
Assos Assos
|
return FALSE;
|
93 |
|
|
}
|
94 |
136a805a
|
Assos Assos
|
$vocab = taxonomy_vocabulary_machine_name_load($conf['vocabulary']);
|
95 |
|
|
if ($vocab->vid != $context->data->vid) {
|
96 |
|
|
return FALSE;
|
97 |
|
|
}
|
98 |
|
|
|
99 |
3753f249
|
Assos Assos
|
$depth = _term_depth($context->data->tid);
|
100 |
|
|
|
101 |
|
|
return ($depth == $conf['depth']);
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
|
|
* Provide a summary description based upon the checked terms.
|
106 |
|
|
*/
|
107 |
|
|
function term_depth_term_depth_ctools_access_summary($conf, $context) {
|
108 |
136a805a
|
Assos Assos
|
_term_depth_convert_config_vid_to_vocabulary_name($conf);
|
109 |
|
|
$vocab = taxonomy_vocabulary_machine_name_load($conf['vocabulary']);
|
110 |
|
|
return t('"@term" is in vocabulary "@vocab" at depth @depth', array(
|
111 |
3753f249
|
Assos Assos
|
'@term' => $context->identifier,
|
112 |
|
|
'@vocab' => $vocab->name,
|
113 |
|
|
'@depth' => $conf['depth'],
|
114 |
|
|
));
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
/**
|
118 |
|
|
* Find the depth of a term.
|
119 |
|
|
*/
|
120 |
|
|
function _term_depth($tid) {
|
121 |
|
|
static $depths = array();
|
122 |
|
|
|
123 |
|
|
if (!isset($depths[$tid])) {
|
124 |
|
|
$parent = db_select('taxonomy_term_hierarchy', 'th')
|
125 |
|
|
->fields('th', array('parent'))
|
126 |
|
|
->condition('tid', $tid)
|
127 |
|
|
->execute()->fetchField();
|
128 |
|
|
|
129 |
|
|
if ($parent == 0) {
|
130 |
|
|
$depths[$tid] = 1;
|
131 |
|
|
}
|
132 |
|
|
else {
|
133 |
|
|
$depths[$tid] = 1 + _term_depth($parent);
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
return $depths[$tid];
|
138 |
|
|
} |