term_depth.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 depth"),
  12. 'description' => t('Control access by the depth of a term.'),
  13. 'callback' => 'term_depth_term_depth_ctools_access_check',
  14. 'default' => array('vid' => array(), 'depth' => 0),
  15. 'settings form' => 'term_depth_term_depth_ctools_access_settings',
  16. 'settings form validation' => 'term_depth_term_depth_ctools_access_settings_validate',
  17. 'settings form submit' => 'term_depth_term_depth_ctools_access_settings_submit',
  18. 'summary' => 'term_depth_term_depth_ctools_access_summary',
  19. 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
  20. );
  21. /**
  22. * Settings form for the 'term depth' access plugin.
  23. */
  24. function term_depth_term_depth_ctools_access_settings($form, &$form_state, $conf) {
  25. $vocabularies = taxonomy_get_vocabularies();
  26. $options = array();
  27. // Loop over each of the configured vocabularies.
  28. foreach ($vocabularies as $vid => $vocab) {
  29. $options[$vocab->machine_name] = $vocab->name;
  30. }
  31. _term_depth_convert_config_vid_to_vocabulary_name($conf);
  32. $form['settings']['vocabulary'] = array(
  33. '#title' => t('Vocabulary'),
  34. '#type' => 'select',
  35. '#options' => $options,
  36. '#description' => t('Select the vocabulary for this form. If there exists a parent term in that vocabulary, this access check will succeed.'),
  37. '#id' => 'ctools-select-vocabulary',
  38. '#default_value' => !empty($conf['vocabulary']) ? $conf['vocabulary'] : array(),
  39. '#required' => TRUE,
  40. );
  41. $form['settings']['depth'] = array(
  42. '#title' => t('Depth'),
  43. '#type' => 'textfield',
  44. '#description' => t('Set the required depth of the term. If the term exists at the correct depth, this access check will succeed.'),
  45. '#default_value' => $conf['depth'],
  46. '#required' => TRUE,
  47. );
  48. return $form;
  49. }
  50. /**
  51. * @param $conf
  52. */
  53. function _term_depth_convert_config_vid_to_vocabulary_name(&$conf) {
  54. // Fallback on legacy 'vid', when no vocabularies are available.
  55. if (empty($conf['vocabulary']) && !empty($conf['vid'])) {
  56. $conf['vocabulary'] = reset(_ctools_term_vocabulary_machine_name_convert(array($conf['vid'])));
  57. unset($conf['vid']);
  58. }
  59. }
  60. /**
  61. * Submit function for the access plugins settings.
  62. *
  63. */
  64. function term_depth_term_depth_ctools_access_settings_submit($form, $form_state) {
  65. $form_state['conf']['depth'] = (integer) $form_state['values']['settings']['depth'];
  66. $form_state['conf']['vocabulary'] = array_filter($form_state['conf']['vocabulary']);
  67. }
  68. /**
  69. * Check for access.
  70. */
  71. function term_depth_term_depth_ctools_access_check($conf, $context) {
  72. // As far as I know there should always be a context at this point, but this
  73. // is safe.
  74. if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
  75. return FALSE;
  76. }
  77. _term_depth_convert_config_vid_to_vocabulary_name($conf);
  78. // Get the $vocabulary.
  79. if (!isset($conf['vocabulary'])) {
  80. return FALSE;
  81. }
  82. $vocab = taxonomy_vocabulary_machine_name_load($conf['vocabulary']);
  83. if ($vocab->vid != $context->data->vid) {
  84. return FALSE;
  85. }
  86. $depth = _term_depth($context->data->tid);
  87. return ($depth == $conf['depth']);
  88. }
  89. /**
  90. * Provide a summary description based upon the checked terms.
  91. */
  92. function term_depth_term_depth_ctools_access_summary($conf, $context) {
  93. _term_depth_convert_config_vid_to_vocabulary_name($conf);
  94. $vocab = taxonomy_vocabulary_machine_name_load($conf['vocabulary']);
  95. return t('"@term" is in vocabulary "@vocab" at depth @depth', array(
  96. '@term' => $context->identifier,
  97. '@vocab' => $vocab->name,
  98. '@depth' => $conf['depth'],
  99. ));
  100. }
  101. /**
  102. * Find the depth of a term.
  103. */
  104. function _term_depth($tid) {
  105. static $depths = array();
  106. if (!isset($depths[$tid])) {
  107. $parent = db_select('taxonomy_term_hierarchy', 'th')
  108. ->fields('th', array('parent'))
  109. ->condition('tid', $tid)
  110. ->execute()->fetchField();
  111. if ($parent == 0) {
  112. $depths[$tid] = 1;
  113. }
  114. else {
  115. $depths[$tid] = 1 + _term_depth($parent);
  116. }
  117. }
  118. return $depths[$tid];
  119. }