term_has_parent.inc 5.7 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'] = 'select';
  67. $form['settings']['vid_' . $vid]['#type'] = 'checkboxes';
  68. $form['settings']['vid_' . $vid]['#options'] = $terms;
  69. unset($terms);
  70. }
  71. $form['settings']['vid']['#options'] = $options;
  72. $form['settings']['include_self'] = array(
  73. '#title' => t('Include these term(s) as candidates?'),
  74. '#description' => t('When this rule is evaluated, should the term(s) you select be included as candidates for access?'),
  75. '#default_value' => !empty($conf['include_self']) ? $conf['include_self'] : FALSE,
  76. '#type' => 'checkbox',
  77. );
  78. return $form;
  79. }
  80. /**
  81. * Filters values to store less.
  82. */
  83. function ctools_term_has_parent_ctools_access_settings_submit($form, &$form_state) {
  84. foreach ($form_state['values']['settings'] as $key => $value) {
  85. if (strpos($key, 'vid_') === 0) {
  86. $form_state['values']['settings'][$key] = array_filter($form_state['values']['settings'][$key]);
  87. }
  88. }
  89. }
  90. /**
  91. * Check for access.
  92. */
  93. function ctools_term_has_parent_ctools_access_check($conf, $context) {
  94. // As far as I know there should always be a context at this point, but this
  95. // is safe.
  96. if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
  97. return FALSE;
  98. }
  99. // Get the $vid.
  100. if (!isset($conf['vid'])) {
  101. return FALSE;
  102. }
  103. $vid = $conf['vid'];
  104. // we'll start looking up the hierarchy from our context term id.
  105. $current_term = $context->data->tid;
  106. $term='';
  107. // scan up the tree.
  108. while (true) {
  109. // select parent as term_parent to avoid PHP5 complications with the parent keyword
  110. //@todo: Find a way to reduce the number of queries required for really deep hierarchies.
  111. $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();
  112. // if no term is found, get out of the loop
  113. if (!$term || empty($term->tid)) {
  114. break;
  115. }
  116. // check the term selected, if the user asked it to.
  117. if (!empty($conf['include_self']) && isset($conf['vid_' . $vid][$term->tid])) {
  118. return TRUE;
  119. }
  120. // did we find the parent TID we were looking for?
  121. if (isset($conf['vid_' . $vid][$term->tid])) {
  122. // YES, we're done!
  123. return TRUE;
  124. }
  125. // Nope, we didn't find it.
  126. // If this is the top of the hierarchy, stop scanning.
  127. if ($term->term_parent==0) {
  128. break;
  129. }
  130. // update the parent, and keep scanning.
  131. $current_term = $term->term_parent;
  132. }
  133. return FALSE;
  134. }
  135. /**
  136. * Provide a summary description based upon the checked terms.
  137. */
  138. function ctools_term_has_parent_ctools_access_summary($conf, $context) {
  139. $vid = (int)$conf['vid'];
  140. $terms = array();
  141. foreach ($conf['vid_' . $vid] as $tid) {
  142. $term = taxonomy_term_load($tid);
  143. $terms[] = $term->name;
  144. }
  145. return format_plural(count($terms),
  146. '@term can have the parent "@terms"',
  147. '@term can have one of these parents: @terms',
  148. array('@terms' => implode(', ', $terms),
  149. '@term' => $context->identifier));
  150. }