term_parent.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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: parent term"),
  12. 'description' => t('Control access by existence of a parent term.'),
  13. 'callback' => 'ctools_term_parent_ctools_access_check',
  14. 'default' => array('vid' => array(), 'negate' => 0),
  15. 'settings form' => 'ctools_term_parent_ctools_access_settings',
  16. 'settings form validation' => 'ctools_term_parent_ctools_access_settings_validate',
  17. 'settings form submit' => 'ctools_term_parent_ctools_access_settings_submit',
  18. 'summary' => 'ctools_term_parent_ctools_access_summary',
  19. 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
  20. );
  21. /**
  22. * Settings form for the 'by parent term' access plugin
  23. */
  24. function ctools_term_parent_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. If there exists a parent term in that vocabulary, this access check will succeed.'),
  39. '#id' => 'ctools-select-vid',
  40. '#default_value' => $conf['vid'],
  41. '#required' => TRUE,
  42. );
  43. $options = array();
  44. // Loop over each of the configured vocabularies.
  45. foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  46. $options[$vid] = $vocabulary->name;
  47. }
  48. $form['settings']['vid']['#options'] = $options;
  49. return $form;
  50. }
  51. /**
  52. * Check for access.
  53. */
  54. function ctools_term_parent_ctools_access_check($conf, $context) {
  55. // As far as I know there should always be a context at this point, but this
  56. // is safe.
  57. if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
  58. return FALSE;
  59. }
  60. // Get the $vid.
  61. if (!isset($conf['vid'])) {
  62. return FALSE;
  63. }
  64. $vid = $conf['vid'];
  65. $count = db_query('SELECT COUNT(*) FROM {taxonomy_term_hierarchy} th INNER JOIN {taxonomy_term_data} td ON th.parent = td.tid WHERE th.tid = :tid AND td.vid = :vid', array(':tid' => $context->data->tid, ':vid' => $vid))->fetchField();
  66. return $count ? TRUE : FALSE;
  67. }
  68. /**
  69. * Provide a summary description based upon the checked terms.
  70. */
  71. function ctools_term_parent_ctools_access_summary($conf, $context) {
  72. $vocab = taxonomy_vocabulary_load($conf['vid']);
  73. return t('"@term" has parent in vocabulary "@vocab"', array('@term' => $context->identifier, '@vocab' => $vocab->name));
  74. }