summaryrefslogtreecommitdiff
path: root/sites/all/modules/ctools/plugins/access/term_vocabulary.inc
blob: b003138dffdd3dd46bd680d2fd096e6e92f7e8c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php

/**
 * @file
 * Plugin to provide access control based upon term vocabulary
 */

/**
 * Plugins are described by creating a $plugin array which will be used
 * by the system that includes this file.
 */
$plugin = array(
  'title' => t("Taxonomy: vocabulary"),
  'description' => t('Control access by vocabulary.'),
  'callback' => 'ctools_term_vocabulary_ctools_access_check',
  'default' => array('vids' => array()),
  'settings form' => 'ctools_term_vocabulary_ctools_access_settings',
  'settings form submit' => 'ctools_term_vocabulary_ctools_access_settings_submit',
  'summary' => 'ctools_term_vocabulary_ctools_access_summary',
  'required context' => new ctools_context_required(t('Vocabulary'), array(
    'taxonomy_term',
    'terms',
    'taxonomy_vocabulary'
  )),
);

/**
 * Settings form for the 'by term_vocabulary' access plugin
 */
function ctools_term_vocabulary_ctools_access_settings($form, &$form_state, $conf) {
  $options = array();
  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $voc) {
    $options[$voc->machine_name] = check_plain($voc->name);
  }

  _ctools_term_vocabulary_ctools_access_map_vids($conf);

  $form['settings']['machine_name'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Vocabularies'),
    '#options' => $options,
    '#description' => t('Only the checked vocabularies will be valid.'),
    '#default_value' => $conf['machine_name'],
  );
  return $form;
}

/**
 * Compress the term_vocabularys allowed to the minimum.
 */
function ctools_term_vocabulary_ctools_access_settings_submit($form, &$form_state) {
  $form_state['values']['settings']['machine_name'] = array_filter($form_state['values']['settings']['machine_name']);
}

/**
 * Check for access.
 */
function ctools_term_vocabulary_ctools_access_check($conf, $context) {
  // As far as I know there should always be a context at this point, but this
  // is safe.
  if (empty($context) || empty($context->data) || empty($context->data->vocabulary_machine_name)) {
    return FALSE;
  }

  _ctools_term_vocabulary_ctools_access_map_vids($conf);

  if (array_filter($conf['machine_name']) && empty($conf['machine_name'][$context->data->vocabulary_machine_name])) {
    return FALSE;
  }

  return TRUE;
}

/**
 * Provide a summary description based upon the checked term_vocabularys.
 */
function ctools_term_vocabulary_ctools_access_summary($conf, $context) {
  if (!isset($conf['type'])) {
    $conf['type'] = array();
  }
  $vocabularies = taxonomy_get_vocabularies();

  _ctools_term_vocabulary_ctools_access_map_vids($conf);

  $names = array();
  if (!empty($conf['machine_name'])) {
    foreach (array_filter($conf['machine_name']) as $machine_name) {
      foreach ($vocabularies as $vocabulary) {
        if ($vocabulary->machine_name === $machine_name) {
          $names[] = check_plain($vocabulary->name);
          continue;
        }
      }
    }
  }

  if (empty($names)) {
    return t('@identifier is any vocabulary', array('@identifier' => $context->identifier));
  }

  return format_plural(count($names), '@identifier vocabulary is "@machine_names"', '@identifier vocabulary is one of "@machine_names"', array(
    '@machine_names' => implode(', ', $names),
    '@identifier' => $context->identifier
  ));
}

/**
 * Helper function to map the vids from old features to the new machine_name.
 *
 * Add the machine_name key to $conf if the vids key exist.
 *
 * @param array $conf
 *   The configuration of this plugin.
 */
function _ctools_term_vocabulary_ctools_access_map_vids(&$conf) {
  if (!empty($conf['vids'])) {
    $conf['machine_name'] = array();
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($conf['vids'] as $vid) {
      $machine_name = $vocabularies[$vid]->machine_name;
      $conf['machine_name'][$machine_name] = $vocabularies[$vid]->machine_name;
    }
  }
}