term_vocabulary.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon term vocabulary
  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: vocabulary"),
  12. 'description' => t('Control access by vocabulary.'),
  13. 'callback' => 'ctools_term_vocabulary_ctools_access_check',
  14. 'default' => array('vids' => array()),
  15. 'settings form' => 'ctools_term_vocabulary_ctools_access_settings',
  16. 'settings form submit' => 'ctools_term_vocabulary_ctools_access_settings_submit',
  17. 'summary' => 'ctools_term_vocabulary_ctools_access_summary',
  18. 'required context' => new ctools_context_required(t('Vocabulary'), array(
  19. 'taxonomy_term',
  20. 'terms',
  21. 'taxonomy_vocabulary'
  22. )),
  23. );
  24. /**
  25. * Settings form for the 'by term_vocabulary' access plugin
  26. */
  27. function ctools_term_vocabulary_ctools_access_settings($form, &$form_state, $conf) {
  28. $options = array();
  29. $vocabularies = taxonomy_get_vocabularies();
  30. foreach ($vocabularies as $voc) {
  31. $options[$voc->machine_name] = check_plain($voc->name);
  32. }
  33. _ctools_term_vocabulary_ctools_access_map_vids($conf);
  34. $form['settings']['machine_name'] = array(
  35. '#type' => 'checkboxes',
  36. '#title' => t('Vocabularies'),
  37. '#options' => $options,
  38. '#description' => t('Only the checked vocabularies will be valid.'),
  39. '#default_value' => $conf['machine_name'],
  40. );
  41. return $form;
  42. }
  43. /**
  44. * Compress the term_vocabularys allowed to the minimum.
  45. */
  46. function ctools_term_vocabulary_ctools_access_settings_submit($form, &$form_state) {
  47. $form_state['values']['settings']['machine_name'] = array_filter($form_state['values']['settings']['machine_name']);
  48. }
  49. /**
  50. * Check for access.
  51. */
  52. function ctools_term_vocabulary_ctools_access_check($conf, $context) {
  53. // As far as I know there should always be a context at this point, but this
  54. // is safe.
  55. if (empty($context) || empty($context->data) || empty($context->data->vocabulary_machine_name)) {
  56. return FALSE;
  57. }
  58. _ctools_term_vocabulary_ctools_access_map_vids($conf);
  59. if (array_filter($conf['machine_name']) && empty($conf['machine_name'][$context->data->vocabulary_machine_name])) {
  60. return FALSE;
  61. }
  62. return TRUE;
  63. }
  64. /**
  65. * Provide a summary description based upon the checked term_vocabularys.
  66. */
  67. function ctools_term_vocabulary_ctools_access_summary($conf, $context) {
  68. if (!isset($conf['type'])) {
  69. $conf['type'] = array();
  70. }
  71. $vocabularies = taxonomy_get_vocabularies();
  72. _ctools_term_vocabulary_ctools_access_map_vids($conf);
  73. $names = array();
  74. if (!empty($conf['machine_name'])) {
  75. foreach (array_filter($conf['machine_name']) as $machine_name) {
  76. foreach ($vocabularies as $vocabulary) {
  77. if ($vocabulary->machine_name === $machine_name) {
  78. $names[] = check_plain($vocabulary->name);
  79. continue;
  80. }
  81. }
  82. }
  83. }
  84. if (empty($names)) {
  85. return t('@identifier is any vocabulary', array('@identifier' => $context->identifier));
  86. }
  87. return format_plural(count($names), '@identifier vocabulary is "@machine_names"', '@identifier vocabulary is one of "@machine_names"', array(
  88. '@machine_names' => implode(', ', $names),
  89. '@identifier' => $context->identifier
  90. ));
  91. }
  92. /**
  93. * Helper function to map the vids from old features to the new machine_name.
  94. *
  95. * Add the machine_name key to $conf if the vids key exist.
  96. *
  97. * @param array $conf
  98. * The configuration of this plugin.
  99. */
  100. function _ctools_term_vocabulary_ctools_access_map_vids(&$conf) {
  101. if (!empty($conf['vids'])) {
  102. $conf['machine_name'] = array();
  103. $vocabularies = taxonomy_get_vocabularies();
  104. foreach ($conf['vids'] as $vid) {
  105. $machine_name = $vocabularies[$vid]->machine_name;
  106. $conf['machine_name'][$machine_name] = $vocabularies[$vid]->machine_name;
  107. }
  108. }
  109. }