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 |
|
|
'settings form validation' => 'term_depth_term_depth_ctools_access_settings_validate',
|
19 |
|
|
'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 |
|
|
// 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 |
|
|
// Loop over each of the configured vocabularies.
|
39 |
|
|
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
|
40 |
|
|
$options[$vid] = $vocabulary->name;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
$form['settings']['vid'] = array(
|
44 |
|
|
'#title' => t('Vocabulary'),
|
45 |
|
|
'#type' => 'select',
|
46 |
|
|
'#options' => $options,
|
47 |
|
|
'#description' => t('Select the vocabulary for this form. If there exists a parent term in that vocabulary, this access check will succeed.'),
|
48 |
|
|
'#id' => 'ctools-select-vid',
|
49 |
|
|
'#default_value' => $conf['vid'],
|
50 |
|
|
'#required' => TRUE,
|
51 |
|
|
);
|
52 |
|
|
|
53 |
|
|
$form['settings']['depth'] = array(
|
54 |
|
|
'#title' => t('Depth'),
|
55 |
|
|
'#type' => 'textfield',
|
56 |
|
|
'#description' => t('Set the required depth of the term. If the term exists at the correct depth, this access check will succeed.'),
|
57 |
|
|
'#default_value' => $conf['depth'],
|
58 |
|
|
'#required' => TRUE,
|
59 |
|
|
);
|
60 |
|
|
|
61 |
|
|
return $form;
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
/**
|
65 |
|
|
* Submit function for the access plugins settings.
|
66 |
|
|
*
|
67 |
|
|
* We cast all settings to numbers to ensure they can be safely handled.
|
68 |
|
|
*/
|
69 |
|
|
function term_depth_term_depth_ctools_access_settings_submit($form, $form_state) {
|
70 |
|
|
foreach (array('depth', 'vid') as $key) {
|
71 |
|
|
$form_state['conf'][$key] = (integer) $form_state['values']['settings'][$key];
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Check for access.
|
77 |
|
|
*/
|
78 |
|
|
function term_depth_term_depth_ctools_access_check($conf, $context) {
|
79 |
|
|
// As far as I know there should always be a context at this point, but this
|
80 |
|
|
// is safe.
|
81 |
|
|
if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
|
82 |
|
|
return FALSE;
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
// Get the $vid.
|
86 |
|
|
if (!isset($conf['vid'])) {
|
87 |
|
|
return FALSE;
|
88 |
|
|
}
|
89 |
|
|
$depth = _term_depth($context->data->tid);
|
90 |
|
|
|
91 |
|
|
return ($depth == $conf['depth']);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
|
|
* Provide a summary description based upon the checked terms.
|
96 |
|
|
*/
|
97 |
|
|
function term_depth_term_depth_ctools_access_summary($conf, $context) {
|
98 |
|
|
$vocab = taxonomy_vocabulary_load($conf['vid']);
|
99 |
|
|
|
100 |
|
|
return t('"@term" has parent in vocabulary "@vocab" at @depth', array(
|
101 |
|
|
'@term' => $context->identifier,
|
102 |
|
|
'@vocab' => $vocab->name,
|
103 |
|
|
'@depth' => $conf['depth'],
|
104 |
|
|
));
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
/**
|
108 |
|
|
* Find the depth of a term.
|
109 |
|
|
*/
|
110 |
|
|
function _term_depth($tid) {
|
111 |
|
|
static $depths = array();
|
112 |
|
|
|
113 |
|
|
if (!isset($depths[$tid])) {
|
114 |
|
|
$parent = db_select('taxonomy_term_hierarchy', 'th')
|
115 |
|
|
->fields('th', array('parent'))
|
116 |
|
|
->condition('tid', $tid)
|
117 |
|
|
->execute()->fetchField();
|
118 |
|
|
|
119 |
|
|
if ($parent == 0) {
|
120 |
|
|
$depths[$tid] = 1;
|
121 |
|
|
}
|
122 |
|
|
else {
|
123 |
|
|
$depths[$tid] = 1 + _term_depth($parent);
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
return $depths[$tid];
|
128 |
|
|
} |