context_condition_node_taxonomy.inc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Expose node taxonomy terms as a context condition.
  4. */
  5. class context_condition_node_taxonomy extends context_condition_node {
  6. function condition_values() {
  7. $values = array();
  8. if (module_exists('taxonomy')) {
  9. foreach (taxonomy_get_vocabularies() as $vocab) {
  10. if (empty($vocab->tags)) {
  11. foreach (taxonomy_get_tree($vocab->vid) as $term) {
  12. $values[$term->tid] = check_plain($term->name);
  13. }
  14. }
  15. }
  16. }
  17. return $values;
  18. }
  19. function condition_form($context) {
  20. $form = parent::condition_form($context);
  21. $form['#type'] = 'select';
  22. $form['#size'] = 12;
  23. $form['#multiple'] = TRUE;
  24. $vocabularies = taxonomy_get_vocabularies();
  25. $options = array();
  26. foreach ($vocabularies as $vid => $vocabulary) {
  27. $tree = taxonomy_get_tree($vid);
  28. if ($tree && (count($tree) > 0)) {
  29. $options[$vocabulary->name] = array();
  30. foreach ($tree as $term) {
  31. $options[$vocabulary->name][$term->tid] = str_repeat('-', $term->depth) . $term->name;
  32. }
  33. }
  34. }
  35. $form['#options'] = $options;
  36. return $form;
  37. }
  38. function execute($node, $op) {
  39. // build a list of each taxonomy reference field belonging to the bundle for the current node
  40. $fields = field_info_fields();
  41. $instance_fields = field_info_instances('node', $node->type);
  42. $check_fields = array();
  43. foreach ($instance_fields as $key => $field_info) {
  44. if ($fields[$key]['type'] == 'taxonomy_term_reference') {
  45. $check_fields[$key] = 'tid';
  46. }
  47. else if ($fields[$key]['type'] == 'entityreference' &&
  48. $fields[$key]['settings']['target_type'] == 'taxonomy_term') {
  49. $check_fields[$key] = 'target_id';
  50. }
  51. }
  52. if ($this->condition_used() && !empty($check_fields)) {
  53. foreach ($check_fields as $field => $term_id_key) {
  54. if ($terms = field_get_items('node', $node, $field)) {
  55. foreach ($terms as $term) {
  56. foreach ($this->get_contexts($term[$term_id_key]) as $context) {
  57. // Check the node form option.
  58. if ($op === 'form') {
  59. $options = $this->fetch_from_context($context, 'options');
  60. if (!empty($options['node_form'])) {
  61. $this->condition_met($context, $term[$term_id_key]);
  62. }
  63. }
  64. else {
  65. $this->condition_met($context, $term[$term_id_key]);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }