term_vocabulary.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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('taxonomy_term', 'terms', 'taxonomy_vocabulary')),
  19. );
  20. /**
  21. * Settings form for the 'by term_vocabulary' access plugin
  22. */
  23. function ctools_term_vocabulary_ctools_access_settings($form, &$form_state, $conf) {
  24. $options = array();
  25. $vocabularies = taxonomy_get_vocabularies();
  26. foreach ($vocabularies as $voc) {
  27. $options[$voc->vid] = check_plain($voc->name);
  28. }
  29. $form['settings']['vids'] = array(
  30. '#type' => 'checkboxes',
  31. '#title' => t('Vocabularies'),
  32. '#options' => $options,
  33. '#description' => t('Only the checked vocabularies will be valid.'),
  34. '#default_value' => $conf['vids'],
  35. );
  36. return $form;
  37. }
  38. /**
  39. * Compress the term_vocabularys allowed to the minimum.
  40. */
  41. function ctools_term_vocabulary_ctools_access_settings_submit($form, &$form_state) {
  42. $form_state['values']['settings']['vids'] = array_filter($form_state['values']['settings']['vids']);
  43. }
  44. /**
  45. * Check for access.
  46. */
  47. function ctools_term_vocabulary_ctools_access_check($conf, $context) {
  48. // As far as I know there should always be a context at this point, but this
  49. // is safe.
  50. if (empty($context) || empty($context->data) || empty($context->data->vid)) {
  51. return FALSE;
  52. }
  53. if (array_filter($conf['vids']) && empty($conf['vids'][$context->data->vid])) {
  54. return FALSE;
  55. }
  56. return TRUE;
  57. }
  58. /**
  59. * Provide a summary description based upon the checked term_vocabularys.
  60. */
  61. function ctools_term_vocabulary_ctools_access_summary($conf, $context) {
  62. if (!isset($conf['type'])) {
  63. $conf['type'] = array();
  64. }
  65. $vocabularies = taxonomy_get_vocabularies();
  66. $names = array();
  67. foreach (array_filter($conf['vids']) as $vid) {
  68. $names[] = check_plain($vocabularies[$vid]->name);
  69. }
  70. if (empty($names)) {
  71. return t('@identifier is any vocabulary', array('@identifier' => $context->identifier));
  72. }
  73. return format_plural(count($names), '@identifier vocabulary is "@vids"', '@identifier vocabulary is one of "@vids"', array('@vids' => implode(', ', $names), '@identifier' => $context->identifier));
  74. }