Mercurial > hg > rr-repo
comparison sites/all/modules/ctools/plugins/access/term_has_parent.inc @ 0:ff03f76ab3fe
initial version
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Wed, 21 Aug 2013 18:51:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ff03f76ab3fe |
---|---|
1 <?php | |
2 /** | |
3 * @file | |
4 * Plugin to provide access control based upon a parent term. | |
5 */ | |
6 | |
7 /** | |
8 * Plugins are described by creating a $plugin array which will be used | |
9 * by the system that includes this file. | |
10 */ | |
11 $plugin = array( | |
12 'title' => t("Taxonomy: term has parent(s)"), | |
13 'description' => t('Control access if a term belongs to a specific parent term.'), | |
14 'callback' => 'ctools_term_has_parent_ctools_access_check', | |
15 'default' => array('vid' => array(), 'negate' => 0), | |
16 'settings form' => 'ctools_term_has_parent_ctools_access_settings', | |
17 'settings form submit' => 'ctools_term_has_parent_ctools_access_settings_submit', | |
18 'summary' => 'ctools_term_has_parent_ctools_access_summary', | |
19 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')), | |
20 ); | |
21 | |
22 /** | |
23 * Settings form for the 'by parent term' access plugin | |
24 */ | |
25 function ctools_term_has_parent_ctools_access_settings($form, &$form_state, $conf) { | |
26 // If no configuration was saved before, set some defaults. | |
27 if (empty($conf)) { | |
28 $conf = array( | |
29 'vid' => 0, | |
30 ); | |
31 } | |
32 if (!isset($conf['vid'])) { | |
33 $conf['vid'] = 0; | |
34 } | |
35 | |
36 $form['settings']['vid'] = array( | |
37 '#title' => t('Vocabulary'), | |
38 '#type' => 'select', | |
39 '#options' => array(), | |
40 '#description' => t('Select the vocabulary for this form.'), | |
41 '#id' => 'ctools-select-vid', | |
42 '#default_value' => $conf['vid'], | |
43 '#required' => TRUE, | |
44 ); | |
45 | |
46 ctools_include('dependent'); | |
47 $options = array(); | |
48 | |
49 // A note: Dependency works strangely on these forms as they have never been | |
50 // updated to a more modern system so they are not individual forms of their | |
51 // own like the content types. | |
52 | |
53 $form['settings']['#tree'] = TRUE; | |
54 | |
55 // Loop over each of the configured vocabularies. | |
56 foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) { | |
57 $options[$vid] = $vocabulary->name; | |
58 $form['settings']['vid_' . $vid] = array( | |
59 '#title' => t('Terms'), | |
60 '#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)), | |
61 '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)), | |
62 '#default_value' => !empty($conf['vid_' . $vid]) ? $conf['vid_' . $vid] : '', | |
63 '#size' => 10, | |
64 '#multiple' => TRUE, | |
65 //@todo: Remove the following workaround when the following patch is in core. {@see:http://drupal.org/node/1117526} | |
66 '#name' => sprintf("settings[%u][]", $vid), | |
67 '#attributes' => array('multiple' => 'multiple'), | |
68 ); | |
69 | |
70 $terms = array(); | |
71 foreach (taxonomy_get_tree($vocabulary->vid) as $term) { | |
72 $terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name; | |
73 } | |
74 //$form['settings']['vid_' . $vid]['#type'] = 'select'; | |
75 $form['settings']['vid_' . $vid]['#type'] = 'checkboxes'; | |
76 $form['settings']['vid_' . $vid]['#options'] = $terms; | |
77 unset($terms); | |
78 } | |
79 $form['settings']['vid']['#options'] = $options; | |
80 $form['settings']['include_self'] = array( | |
81 '#title' => t('Include these term(s) as candidates?'), | |
82 '#description' => t('When this rule is evaluated, should the term(s) you select be included as candidates for access?'), | |
83 '#default_value' => !empty($conf['include_self']) ? $conf['include_self'] : FALSE, | |
84 '#type' => 'checkbox', | |
85 ); | |
86 return $form; | |
87 } | |
88 | |
89 /** | |
90 * Filters values to store less. | |
91 */ | |
92 function ctools_term_has_parent_ctools_access_settings_submit($form, &$form_state) { | |
93 foreach ($form_state['values']['settings'] as $key => $value) { | |
94 if (strpos($key, 'vid_') === 0) { | |
95 $form_state['values']['settings'][$key] = array_filter($form_state['values']['settings'][$key]); | |
96 } | |
97 } | |
98 } | |
99 | |
100 /** | |
101 * Check for access. | |
102 */ | |
103 function ctools_term_has_parent_ctools_access_check($conf, $context) { | |
104 // As far as I know there should always be a context at this point, but this | |
105 // is safe. | |
106 if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) { | |
107 return FALSE; | |
108 } | |
109 | |
110 // Get the $vid. | |
111 if (!isset($conf['vid'])) { | |
112 return FALSE; | |
113 } | |
114 $vid = $conf['vid']; | |
115 | |
116 // we'll start looking up the hierarchy from our context term id. | |
117 $current_term = $context->data->tid; | |
118 | |
119 $term=''; | |
120 | |
121 // scan up the tree. | |
122 while (true) { | |
123 // select parent as term_parent to avoid PHP5 complications with the parent keyword | |
124 //@todo: Find a way to reduce the number of queries required for really deep hierarchies. | |
125 $term = db_query("SELECT parent AS term_parent, tid AS tid FROM {taxonomy_term_hierarchy} th WHERE th.tid = :tid", array(':tid'=>$current_term))->fetchObject(); | |
126 | |
127 // if no term is found, get out of the loop | |
128 if (!$term || empty($term->tid)) { | |
129 break; | |
130 } | |
131 | |
132 // check the term selected, if the user asked it to. | |
133 if (!empty($conf['include_self']) && isset($conf['vid_' . $vid][$term->tid])) { | |
134 return TRUE; | |
135 } | |
136 | |
137 // did we find the parent TID we were looking for? | |
138 if (isset($conf['vid_' . $vid][$term->tid])) { | |
139 // YES, we're done! | |
140 return TRUE; | |
141 } | |
142 // Nope, we didn't find it. | |
143 | |
144 // If this is the top of the hierarchy, stop scanning. | |
145 if ($term->term_parent==0) { | |
146 break; | |
147 } | |
148 | |
149 // update the parent, and keep scanning. | |
150 $current_term = $term->term_parent; | |
151 } | |
152 | |
153 return FALSE; | |
154 } | |
155 | |
156 /** | |
157 * Provide a summary description based upon the checked terms. | |
158 */ | |
159 function ctools_term_has_parent_ctools_access_summary($conf, $context) { | |
160 $vid = (int)$conf['vid']; | |
161 $terms = array(); | |
162 foreach ($conf['vid_' . $vid] as $tid) { | |
163 $term = taxonomy_term_load($tid); | |
164 $terms[] = $term->name; | |
165 } | |
166 | |
167 return format_plural(count($terms), | |
168 '@term can have the parent "@terms"', | |
169 '@term can have one of these parents: @terms', | |
170 array('@terms' => implode(', ', $terms), | |
171 '@term' => $context->identifier)); | |
172 } |