Project

General

Profile

Paste
Download (2.74 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / ctools / plugins / access / term_parent.inc @ 6e3ce7c2

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: parent term"),
14
  'description' => t('Control access by existence of a parent term.'),
15
  'callback' => 'ctools_term_parent_ctools_access_check',
16
  'default' => array('vid' => array(), 'negate' => 0),
17
  'settings form' => 'ctools_term_parent_ctools_access_settings',
18
  'settings form validate' => 'ctools_term_parent_ctools_access_settings_validate',
19
  'settings form submit' => 'ctools_term_parent_ctools_access_settings_submit',
20
  'summary' => 'ctools_term_parent_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 parent term' access plugin.
26
 */
27
function ctools_term_parent_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. If there exists a parent term in that vocabulary, this access check will succeed.'),
43
    '#id' => 'ctools-select-vid',
44
    '#default_value' => $conf['vid'],
45
    '#required' => TRUE,
46
  );
47

    
48
  $options = array();
49

    
50
  // Loop over each of the configured vocabularies.
51
  foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
52
    $options[$vid] = $vocabulary->name;
53
  }
54
  $form['settings']['vid']['#options'] = $options;
55
  return $form;
56
}
57

    
58
/**
59
 * Check for access.
60
 */
61
function ctools_term_parent_ctools_access_check($conf, $context) {
62
  // As far as I know there should always be a context at this point, but this
63
  // is safe.
64
  if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
65
    return FALSE;
66
  }
67

    
68
  // Get the $vid.
69
  if (!isset($conf['vid'])) {
70
    return FALSE;
71
  }
72
  $vid = $conf['vid'];
73

    
74
  $count = db_query('SELECT COUNT(*) FROM {taxonomy_term_hierarchy} th INNER JOIN {taxonomy_term_data} td ON th.parent = td.tid WHERE th.tid = :tid AND td.vid = :vid', array(':tid' => $context->data->tid, ':vid' => $vid))->fetchField();
75

    
76
  return $count ? TRUE : FALSE;
77
}
78

    
79
/**
80
 * Provide a summary description based upon the checked terms.
81
 */
82
function ctools_term_parent_ctools_access_summary($conf, $context) {
83
  $vocab = taxonomy_vocabulary_load($conf['vid']);
84

    
85
  return t('"@term" has parent in vocabulary "@vocab"', array('@term' => $context->identifier, '@vocab' => $vocab->name));
86
}