term.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon specific terms.
  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"),
  12. 'description' => t('Control access by a specific term.'),
  13. 'callback' => 'ctools_term_ctools_access_check',
  14. 'default' => array('vids' => array()),
  15. 'settings form' => 'ctools_term_ctools_access_settings',
  16. 'settings form validation' => 'ctools_term_ctools_access_settings_validate',
  17. 'settings form submit' => 'ctools_term_ctools_access_settings_submit',
  18. 'summary' => 'ctools_term_ctools_access_summary',
  19. 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
  20. );
  21. /**
  22. * Settings form for the 'by term' access plugin
  23. */
  24. function ctools_term_ctools_access_settings($form, &$form_state, $conf) {
  25. // If no configuration was saved before, set some defaults.
  26. if (empty($conf)) {
  27. $conf = array(
  28. 'vid' => 0,
  29. );
  30. }
  31. if (!isset($conf['vid'])) {
  32. $conf['vid'] = 0;
  33. }
  34. $form['settings']['vid'] = array(
  35. '#title' => t('Vocabulary'),
  36. '#type' => 'select',
  37. '#options' => array(),
  38. '#description' => t('Select the vocabulary for this form.'),
  39. '#id' => 'ctools-select-vid',
  40. '#default_value' => $conf['vid'],
  41. '#required' => TRUE,
  42. );
  43. ctools_include('dependent');
  44. $options = array();
  45. // A note: Dependency works strangely on these forms as they have never been
  46. // updated to a more modern system so they are not individual forms of their
  47. // own like the content types.
  48. $form['settings']['#tree'] = TRUE;
  49. // Loop over each of the configured vocabularies.
  50. foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  51. $options[$vid] = $vocabulary->name;
  52. $form['settings'][$vocabulary->vid] = array(
  53. '#title' => t('Terms'),
  54. '#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)), //. $description,
  55. '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
  56. '#default_value' => !empty($conf[$vid]) ? $conf[$vid] : '',
  57. '#multiple' => TRUE,
  58. );
  59. $terms = array();
  60. foreach (taxonomy_get_tree($vocabulary->vid) as $tid => $term) {
  61. $terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name;
  62. }
  63. $form['settings'][$vocabulary->vid]['#type'] = 'select';
  64. $form['settings'][$vocabulary->vid]['#options'] = $terms;
  65. unset($terms);
  66. }
  67. $form['settings']['vid']['#options'] = $options;
  68. return $form;
  69. }
  70. /**
  71. * Check for access.
  72. */
  73. function ctools_term_ctools_access_check($conf, $context) {
  74. // As far as I know there should always be a context at this point, but this
  75. // is safe.
  76. if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
  77. return FALSE;
  78. }
  79. // Get the $vid.
  80. if (!isset($conf['vid'])) {
  81. return FALSE;
  82. }
  83. $vid = $conf['vid'];
  84. // Get the terms.
  85. if (!isset($conf[$vid])) {
  86. return FALSE;
  87. }
  88. $return = FALSE;
  89. $terms = array_filter($conf[$vid]);
  90. // For multi-term if any terms coincide, let's call that good enough:
  91. if (isset($context->tids)) {
  92. return (bool) array_intersect($terms, $context->tids);
  93. }
  94. else {
  95. return in_array($context->data->tid, $terms);
  96. }
  97. }
  98. /**
  99. * Provide a summary description based upon the checked terms.
  100. */
  101. function ctools_term_ctools_access_summary($conf, $context) {
  102. $vid = $conf['vid'];
  103. $terms = array();
  104. foreach ($conf[$vid] as $tid) {
  105. $term = taxonomy_term_load($tid);
  106. $terms[] = $term->name;
  107. }
  108. return format_plural(count($terms),
  109. '@term can be the term "@terms"',
  110. '@term can be one of these terms: @terms',
  111. array('@terms' => implode(', ', $terms),
  112. '@term' => $context->identifier));
  113. }