term_has_parent.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon a parent term.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t("Taxonomy: term has parent(s)"),
  12. 'description' => t('Control access if a term belongs to a specific parent term.'),
  13. 'callback' => 'ctools_term_has_parent_ctools_access_check',
  14. 'default' => array('vid' => array(), 'negate' => 0),
  15. 'settings form' => 'ctools_term_has_parent_ctools_access_settings',
  16. 'settings form submit' => 'ctools_term_has_parent_ctools_access_settings_submit',
  17. 'summary' => 'ctools_term_has_parent_ctools_access_summary',
  18. 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
  19. );
  20. /**
  21. * Settings form for the 'by parent term' access plugin.
  22. */
  23. function ctools_term_has_parent_ctools_access_settings($form, &$form_state, $conf) {
  24. // If no configuration was saved before, set some defaults.
  25. if (empty($conf)) {
  26. $conf = array(
  27. 'vid' => 0,
  28. );
  29. }
  30. if (!isset($conf['vid'])) {
  31. $conf['vid'] = 0;
  32. }
  33. $form['settings']['vid'] = array(
  34. '#title' => t('Vocabulary'),
  35. '#type' => 'select',
  36. '#options' => array(),
  37. '#description' => t('Select the vocabulary for this form.'),
  38. '#id' => 'ctools-select-vid',
  39. '#default_value' => $conf['vid'],
  40. '#required' => TRUE,
  41. );
  42. ctools_include('dependent');
  43. $options = array();
  44. // A note: Dependency works strangely on these forms as they have never been
  45. // updated to a more modern system so they are not individual forms of their
  46. // own like the content types.
  47. $form['settings']['#tree'] = TRUE;
  48. // Loop over each of the configured vocabularies.
  49. foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  50. $options[$vid] = $vocabulary->name;
  51. $form['settings']['vid_' . $vid] = array(
  52. '#title' => t('Terms'),
  53. '#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)),
  54. '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
  55. '#default_value' => !empty($conf['vid_' . $vid]) ? $conf['vid_' . $vid] : '',
  56. '#size' => 10,
  57. '#multiple' => TRUE,
  58. // @todo: Remove the following workaround when the following patch is in core. {@see:http://drupal.org/node/1117526}
  59. '#name' => sprintf("settings[%u][]", $vid),
  60. '#attributes' => array('multiple' => 'multiple'),
  61. );
  62. $terms = array();
  63. foreach (taxonomy_get_tree($vocabulary->vid) as $term) {
  64. $terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name;
  65. }
  66. $form['settings']['vid_' . $vid]['#type'] = 'checkboxes';
  67. $form['settings']['vid_' . $vid]['#options'] = $terms;
  68. unset($terms);
  69. }
  70. $form['settings']['vid']['#options'] = $options;
  71. $form['settings']['include_self'] = array(
  72. '#title' => t('Include these term(s) as candidates?'),
  73. '#description' => t('When this rule is evaluated, should the term(s) you select be included as candidates for access?'),
  74. '#default_value' => !empty($conf['include_self']) ? $conf['include_self'] : FALSE,
  75. '#type' => 'checkbox',
  76. );
  77. return $form;
  78. }
  79. /**
  80. * Filters values to store less.
  81. */
  82. function ctools_term_has_parent_ctools_access_settings_submit($form, &$form_state) {
  83. foreach ($form_state['values']['settings'] as $key => $value) {
  84. if (strpos($key, 'vid_') === 0) {
  85. $form_state['values']['settings'][$key] = array_filter($form_state['values']['settings'][$key]);
  86. }
  87. }
  88. }
  89. /**
  90. * Check for access.
  91. */
  92. function ctools_term_has_parent_ctools_access_check($conf, $context) {
  93. // As far as I know there should always be a context at this point, but this
  94. // is safe.
  95. if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
  96. return FALSE;
  97. }
  98. // Get the $vid.
  99. if (!isset($conf['vid'])) {
  100. return FALSE;
  101. }
  102. $vid = $conf['vid'];
  103. // we'll start looking up the hierarchy from our context term id.
  104. $current_term = $context->data->tid;
  105. $term = '';
  106. // Scan up the tree.
  107. while (TRUE) {
  108. // Select parent as term_parent to avoid PHP5 complications with the parent keyword.
  109. // @todo: Find a way to reduce the number of queries required for really deep hierarchies.
  110. $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();
  111. // If no term is found, get out of the loop.
  112. if (!$term || empty($term->tid)) {
  113. break;
  114. }
  115. // Check the term selected, if the user asked it to.
  116. if (!empty($conf['include_self']) && isset($conf['vid_' . $vid][$term->tid])) {
  117. return TRUE;
  118. }
  119. // Did we find the parent TID we were looking for?
  120. if (isset($conf['vid_' . $vid][$term->tid])) {
  121. // YES, we're done!
  122. return TRUE;
  123. }
  124. // Nope, we didn't find it.
  125. // If this is the top of the hierarchy, stop scanning.
  126. if ($term->term_parent == 0) {
  127. break;
  128. }
  129. // Update the parent, and keep scanning.
  130. $current_term = $term->term_parent;
  131. }
  132. return FALSE;
  133. }
  134. /**
  135. * Provide a summary description based upon the checked terms.
  136. */
  137. function ctools_term_has_parent_ctools_access_summary($conf, $context) {
  138. $vid = (int) $conf['vid'];
  139. $terms = array();
  140. foreach ($conf['vid_' . $vid] as $tid) {
  141. $term = taxonomy_term_load($tid);
  142. $terms[] = $term->name;
  143. }
  144. return format_plural(count($terms),
  145. '@term can have the parent "@terms"',
  146. '@term can have one of these parents: @terms',
  147. array(
  148. '@terms' => implode(', ', $terms),
  149. '@term' => $context->identifier,
  150. ));
  151. }