Projet

Général

Profil

Paste
Télécharger (4,23 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / term_depth / plugins / access / term_depth.inc @ 219d19c4

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide access control based upon a parent term.
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 depth"),
14
  'description' => t('Control access by the depth of a term.'),
15
  'callback' => 'term_depth_term_depth_ctools_access_check',
16
  'default' => array('vid' => array(), 'depth' => 0),
17
  'settings form' => 'term_depth_term_depth_ctools_access_settings',
18
  'settings form validate' => 'term_depth_term_depth_ctools_access_settings_validate',
19
  'settings form submit' => 'term_depth_term_depth_ctools_access_settings_submit',
20
  'summary' => 'term_depth_term_depth_ctools_access_summary',
21
  'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
22
);
23

    
24
/**
25
 * Settings form for the 'term depth' access plugin.
26
 */
27
function term_depth_term_depth_ctools_access_settings($form, &$form_state, $conf) {
28
  $vocabularies = taxonomy_get_vocabularies();
29
  $options = array();
30
  // Loop over each of the configured vocabularies.
31
  foreach ($vocabularies as $vid => $vocab) {
32
    $options[$vocab->machine_name] = $vocab->name;
33
  }
34

    
35
  _term_depth_convert_config_vid_to_vocabulary_name($conf);
36

    
37
  $form['settings']['vocabulary'] = array(
38
    '#title' => t('Vocabulary'),
39
    '#type' => 'select',
40
    '#options' => $options,
41
    '#description' => t('Select the vocabulary for this form. If there exists a parent term in that vocabulary, this access check will succeed.'),
42
    '#id' => 'ctools-select-vocabulary',
43
    '#default_value' => !empty($conf['vocabulary']) ? $conf['vocabulary'] : array(),
44
    '#required' => TRUE,
45
  );
46

    
47
  $form['settings']['depth'] = array(
48
    '#title' => t('Depth'),
49
    '#type' => 'textfield',
50
    '#description' => t('Set the required depth of the term. If the term exists at the correct depth, this access check will succeed.'),
51
    '#default_value' => $conf['depth'],
52
    '#required' => TRUE,
53
  );
54

    
55
  return $form;
56
}
57

    
58
/**
59
 * @param $conf
60
 */
61
function _term_depth_convert_config_vid_to_vocabulary_name(&$conf) {
62
  // Fallback on legacy 'vid', when no vocabularies are available.
63
  if (empty($conf['vocabulary']) && !empty($conf['vid'])) {
64
    $vocabulary = _ctools_term_vocabulary_machine_name_convert(array($conf['vid']));
65
    $conf['vocabulary'] = reset($vocabulary);
66
    unset($conf['vid'], $vocabulary);
67
  }
68
}
69

    
70
/**
71
 * Submit function for the access plugins settings.
72
 */
73
function term_depth_term_depth_ctools_access_settings_submit($form, $form_state) {
74
  $form_state['conf']['depth'] = (integer) $form_state['values']['settings']['depth'];
75
  $form_state['conf']['vocabulary'] = array_filter($form_state['conf']['vocabulary']);
76
}
77

    
78
/**
79
 * Check for access.
80
 */
81
function term_depth_term_depth_ctools_access_check($conf, $context) {
82
  // As far as I know there should always be a context at this point, but this
83
  // is safe.
84
  if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
85
    return FALSE;
86
  }
87

    
88
  _term_depth_convert_config_vid_to_vocabulary_name($conf);
89

    
90
  // Get the $vocabulary.
91
  if (!isset($conf['vocabulary'])) {
92
    return FALSE;
93
  }
94
  $vocab = taxonomy_vocabulary_machine_name_load($conf['vocabulary']);
95
  if ($vocab->vid != $context->data->vid) {
96
    return FALSE;
97
  }
98

    
99
  $depth = _term_depth($context->data->tid);
100

    
101
  return ($depth == $conf['depth']);
102
}
103

    
104
/**
105
 * Provide a summary description based upon the checked terms.
106
 */
107
function term_depth_term_depth_ctools_access_summary($conf, $context) {
108
  _term_depth_convert_config_vid_to_vocabulary_name($conf);
109
  $vocab = taxonomy_vocabulary_machine_name_load($conf['vocabulary']);
110
  return t('"@term" is in vocabulary "@vocab" at depth @depth', array(
111
    '@term' => $context->identifier,
112
    '@vocab' => $vocab->name,
113
    '@depth' => $conf['depth'],
114
  ));
115
}
116

    
117
/**
118
 * Find the depth of a term.
119
 */
120
function _term_depth($tid) {
121
  static $depths = array();
122

    
123
  if (!isset($depths[$tid])) {
124
    $parent = db_select('taxonomy_term_hierarchy', 'th')
125
      ->fields('th', array('parent'))
126
      ->condition('tid', $tid)
127
      ->execute()->fetchField();
128

    
129
    if ($parent == 0) {
130
      $depths[$tid] = 1;
131
    }
132
    else {
133
      $depths[$tid] = 1 + _term_depth($parent);
134
    }
135
  }
136

    
137
  return $depths[$tid];
138
}