Projet

Général

Profil

Paste
Télécharger (5,64 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / access / term_has_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: term has parent(s)"),
14
  'description' => t('Control access if a term belongs to a specific parent term.'),
15
  'callback' => 'ctools_term_has_parent_ctools_access_check',
16
  'default' => array('vid' => array(), 'negate' => 0),
17
  'settings form' => 'ctools_term_has_parent_ctools_access_settings',
18
  'settings form submit' => 'ctools_term_has_parent_ctools_access_settings_submit',
19
  'summary' => 'ctools_term_has_parent_ctools_access_summary',
20
  'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
21
);
22

    
23
/**
24
 * Settings form for the 'by parent term' access plugin.
25
 */
26
function ctools_term_has_parent_ctools_access_settings($form, &$form_state, $conf) {
27
  // If no configuration was saved before, set some defaults.
28
  if (empty($conf)) {
29
    $conf = array(
30
      'vid' => 0,
31
    );
32
  }
33
  if (!isset($conf['vid'])) {
34
    $conf['vid'] = 0;
35
  }
36

    
37
  $form['settings']['vid'] = array(
38
    '#title' => t('Vocabulary'),
39
    '#type' => 'select',
40
    '#options' => array(),
41
    '#description' => t('Select the vocabulary your parent term belongs to.'),
42
    '#id' => 'ctools-select-vid',
43
    '#default_value' => $conf['vid'],
44
    '#required' => TRUE,
45
  );
46

    
47
  ctools_include('dependent');
48
  $options = array();
49

    
50
  // A note: Dependency works strangely on these forms as they have never been
51
  // updated to a more modern system so they are not individual forms of their
52
  // own like the content types.
53
  $form['settings']['#tree'] = TRUE;
54

    
55
  // Loop over each of the configured vocabularies.
56
  foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
57
    $options[$vid] = $vocabulary->name;
58
    $form['settings']['vid_' . $vid] = array(
59
      '#title' => t('Terms'),
60
      '#description' => t('Select a parent term (or terms) from the @vocabulary vocabulary.', array('@vocabulary' => $vocabulary->name)),
61
      '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
62
      '#default_value' => !empty($conf['vid_' . $vid]) ? $conf['vid_' . $vid] : '',
63
      '#size' => 10,
64
      '#multiple' => TRUE,
65
      // @todo: Remove the following workaround when the following patch is in core. {@see:http://drupal.org/node/1117526}
66
      '#name' => sprintf("settings[%u][]", $vid),
67
      '#attributes' => array('multiple' => 'multiple'),
68
    );
69

    
70
    $terms = array();
71
    foreach (taxonomy_get_tree($vocabulary->vid) as $term) {
72
      $terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name;
73
    }
74
    $form['settings']['vid_' . $vid]['#type'] = 'checkboxes';
75
    $form['settings']['vid_' . $vid]['#options'] = $terms;
76
    unset($terms);
77
  }
78
  $form['settings']['vid']['#options'] = $options;
79
  $form['settings']['include_self'] = array(
80
    '#title' => t('Include these parent term(s)?'),
81
    '#description' => t('Should the term(s) you selected above be included in addition to their children?'),
82
    '#default_value' => !empty($conf['include_self']) ? $conf['include_self'] : FALSE,
83
    '#type' => 'checkbox',
84
  );
85
  return $form;
86
}
87

    
88
/**
89
 * Filters values to store less.
90
 */
91
function ctools_term_has_parent_ctools_access_settings_submit($form, &$form_state) {
92
  foreach ($form_state['values']['settings'] as $key => $value) {
93
    if (strpos($key, 'vid_') === 0) {
94
      $form_state['values']['settings'][$key] = array_filter($form_state['values']['settings'][$key]);
95
    }
96
  }
97
}
98

    
99
/**
100
 * Check for access.
101
 */
102
function ctools_term_has_parent_ctools_access_check($conf, $context) {
103
  // As far as I know there should always be a context at this point, but this
104
  // is safe.
105
  if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
106
    return FALSE;
107
  }
108

    
109
  // Get the $vid.
110
  if (!isset($conf['vid'])) {
111
    return FALSE;
112
  }
113
  $vid = $conf['vid'];
114

    
115
  // we'll start looking up the hierarchy from our context term id.
116
  $current_term = $context->data->tid;
117

    
118
  $term = '';
119

    
120
  // Scan up the tree.
121
  while (TRUE) {
122
    // Select parent as term_parent to avoid PHP5 complications with the parent keyword.
123
    // @todo: Find a way to reduce the number of queries required for really deep hierarchies.
124
    $term = db_query("SELECT parent AS term_parent, tid AS tid FROM {taxonomy_term_hierarchy} th WHERE th.tid = :tid", array(':tid' => $current_term))->fetchObject();
125

    
126
    // If no term is found, get out of the loop.
127
    if (!$term || empty($term->tid)) {
128
      break;
129
    }
130

    
131
    // Check the term selected, if the user asked it to.
132
    if (!empty($conf['include_self']) && isset($conf['vid_' . $vid][$term->tid])) {
133
      return TRUE;
134
    }
135

    
136
    // Did we find the parent TID we were looking for?
137
    if (isset($conf['vid_' . $vid][$term->tid])) {
138
      // YES, we're done!
139
      return TRUE;
140
    }
141
    // Nope, we didn't find it.
142
    // If this is the top of the hierarchy, stop scanning.
143
    if ($term->term_parent == 0) {
144
      break;
145
    }
146

    
147
    // Update the parent, and keep scanning.
148
    $current_term = $term->term_parent;
149
  }
150

    
151
  return FALSE;
152
}
153

    
154
/**
155
 * Provide a summary description based upon the checked terms.
156
 */
157
function ctools_term_has_parent_ctools_access_summary($conf, $context) {
158
  $vid = (int) $conf['vid'];
159
  $terms = array();
160
  foreach ($conf['vid_' . $vid] as $tid) {
161
    $term = taxonomy_term_load($tid);
162
    $terms[] = $term->name;
163
  }
164

    
165
  return format_plural(count($terms),
166
    '@term can have the parent "@terms"',
167
    '@term can have one of these parents: @terms',
168
    array(
169
      '@terms' => implode(', ', $terms),
170
      '@term' => $context->identifier,
171
    ));
172
}