commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / ctools / plugins / access / term_vocabulary.inc
1 <?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 'required context' => new ctools_context_required(t('Vocabulary'), array(
21 'taxonomy_term',
22 'terms',
23 'taxonomy_vocabulary'
24 )),
25 );
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 $options[$voc->machine_name] = check_plain($voc->name);
35 }
36
37 _ctools_term_vocabulary_ctools_access_map_vids($conf);
38
39 $form['settings']['machine_name'] = array(
40 '#type' => 'checkboxes',
41 '#title' => t('Vocabularies'),
42 '#options' => $options,
43 '#description' => t('Only the checked vocabularies will be valid.'),
44 '#default_value' => $conf['machine_name'],
45 );
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 $form_state['values']['settings']['machine_name'] = array_filter($form_state['values']['settings']['machine_name']);
54 }
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 if (empty($context) || empty($context->data) || empty($context->data->vocabulary_machine_name)) {
63 return FALSE;
64 }
65
66 _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 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 _ctools_term_vocabulary_ctools_access_map_vids($conf);
85
86 $names = array();
87 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 }
97
98 if (empty($names)) {
99 return t('@identifier is any vocabulary', array('@identifier' => $context->identifier));
100 }
101
102 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 }
107
108 /**
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
127