Mercurial > hg > rr-repo
comparison sites/all/modules/ctools/plugins/access/term.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 /** | |
4 * @file | |
5 * Plugin to provide access control based upon specific terms. | |
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"), | |
14 'description' => t('Control access by a specific term.'), | |
15 'callback' => 'ctools_term_ctools_access_check', | |
16 'default' => array('vids' => array()), | |
17 'settings form' => 'ctools_term_ctools_access_settings', | |
18 'settings form validation' => 'ctools_term_ctools_access_settings_validate', | |
19 'settings form submit' => 'ctools_term_ctools_access_settings_submit', | |
20 'summary' => 'ctools_term_ctools_access_summary', | |
21 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')), | |
22 ); | |
23 | |
24 /** | |
25 * Settings form for the 'by term' access plugin | |
26 */ | |
27 function ctools_term_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 $form['settings']['vid'] = array( | |
39 '#title' => t('Vocabulary'), | |
40 '#type' => 'select', | |
41 '#options' => array(), | |
42 '#description' => t('Select the vocabulary for this form.'), | |
43 '#id' => 'ctools-select-vid', | |
44 '#default_value' => $conf['vid'], | |
45 '#required' => TRUE, | |
46 ); | |
47 | |
48 ctools_include('dependent'); | |
49 $options = array(); | |
50 | |
51 // A note: Dependency works strangely on these forms as they have never been | |
52 // updated to a more modern system so they are not individual forms of their | |
53 // own like the content types. | |
54 | |
55 $form['settings']['#tree'] = TRUE; | |
56 | |
57 // Loop over each of the configured vocabularies. | |
58 foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) { | |
59 $options[$vid] = $vocabulary->name; | |
60 $form['settings'][$vocabulary->vid] = array( | |
61 '#title' => t('Terms'), | |
62 '#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)), //. $description, | |
63 '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)), | |
64 '#default_value' => !empty($conf[$vid]) ? $conf[$vid] : '', | |
65 '#multiple' => TRUE, | |
66 ); | |
67 | |
68 $terms = array(); | |
69 foreach (taxonomy_get_tree($vocabulary->vid) as $tid => $term) { | |
70 $terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name; | |
71 } | |
72 $form['settings'][$vocabulary->vid]['#type'] = 'select'; | |
73 $form['settings'][$vocabulary->vid]['#options'] = $terms; | |
74 unset($terms); | |
75 } | |
76 $form['settings']['vid']['#options'] = $options; | |
77 return $form; | |
78 } | |
79 | |
80 /** | |
81 * Check for access. | |
82 */ | |
83 function ctools_term_ctools_access_check($conf, $context) { | |
84 // As far as I know there should always be a context at this point, but this | |
85 // is safe. | |
86 if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) { | |
87 return FALSE; | |
88 } | |
89 | |
90 // Get the $vid. | |
91 if (!isset($conf['vid'])) { | |
92 return FALSE; | |
93 } | |
94 $vid = $conf['vid']; | |
95 | |
96 // Get the terms. | |
97 if (!isset($conf[$vid])) { | |
98 return FALSE; | |
99 } | |
100 | |
101 $return = FALSE; | |
102 | |
103 $terms = array_filter($conf[$vid]); | |
104 // For multi-term if any terms coincide, let's call that good enough: | |
105 if (isset($context->tids)) { | |
106 return (bool) array_intersect($terms, $context->tids); | |
107 } | |
108 else { | |
109 return in_array($context->data->tid, $terms); | |
110 } | |
111 } | |
112 | |
113 /** | |
114 * Provide a summary description based upon the checked terms. | |
115 */ | |
116 function ctools_term_ctools_access_summary($conf, $context) { | |
117 $vid = $conf['vid']; | |
118 $terms = array(); | |
119 foreach ($conf[$vid] as $tid) { | |
120 $term = taxonomy_term_load($tid); | |
121 $terms[] = $term->name; | |
122 } | |
123 | |
124 return format_plural(count($terms), | |
125 '@term can be the term "@terms"', | |
126 '@term can be one of these terms: @terms', | |
127 array('@terms' => implode(', ', $terms), | |
128 '@term' => $context->identifier)); | |
129 } |