comparison sites/all/modules/ctools/plugins/access/term_vocabulary.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 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('taxonomy_term', 'terms', 'taxonomy_vocabulary')),
21 );
22
23 /**
24 * Settings form for the 'by term_vocabulary' access plugin
25 */
26 function ctools_term_vocabulary_ctools_access_settings($form, &$form_state, $conf) {
27 $options = array();
28 $vocabularies = taxonomy_get_vocabularies();
29 foreach ($vocabularies as $voc) {
30 $options[$voc->vid] = check_plain($voc->name);
31 }
32
33 $form['settings']['vids'] = array(
34 '#type' => 'checkboxes',
35 '#title' => t('Vocabularies'),
36 '#options' => $options,
37 '#description' => t('Only the checked vocabularies will be valid.'),
38 '#default_value' => $conf['vids'],
39 );
40 return $form;
41 }
42
43 /**
44 * Compress the term_vocabularys allowed to the minimum.
45 */
46 function ctools_term_vocabulary_ctools_access_settings_submit($form, &$form_state) {
47 $form_state['values']['settings']['vids'] = array_filter($form_state['values']['settings']['vids']);
48 }
49
50 /**
51 * Check for access.
52 */
53 function ctools_term_vocabulary_ctools_access_check($conf, $context) {
54 // As far as I know there should always be a context at this point, but this
55 // is safe.
56 if (empty($context) || empty($context->data) || empty($context->data->vid)) {
57 return FALSE;
58 }
59
60 if (array_filter($conf['vids']) && empty($conf['vids'][$context->data->vid])) {
61 return FALSE;
62 }
63
64 return TRUE;
65 }
66
67 /**
68 * Provide a summary description based upon the checked term_vocabularys.
69 */
70 function ctools_term_vocabulary_ctools_access_summary($conf, $context) {
71 if (!isset($conf['type'])) {
72 $conf['type'] = array();
73 }
74 $vocabularies = taxonomy_get_vocabularies();
75
76 $names = array();
77 foreach (array_filter($conf['vids']) as $vid) {
78 $names[] = check_plain($vocabularies[$vid]->name);
79 }
80
81 if (empty($names)) {
82 return t('@identifier is any vocabulary', array('@identifier' => $context->identifier));
83 }
84
85 return format_plural(count($names), '@identifier vocabulary is "@vids"', '@identifier vocabulary is one of "@vids"', array('@vids' => implode(', ', $names), '@identifier' => $context->identifier));
86 }
87