context_condition_node_taxonomy.inc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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;
  46. }
  47. }
  48. if ($this->condition_used() && !empty($check_fields)) {
  49. foreach ($check_fields as $field) {
  50. if ($terms = field_get_items('node', $node, $field)) {
  51. foreach ($terms as $term) {
  52. foreach ($this->get_contexts($term['tid']) as $context) {
  53. // Check the node form option.
  54. if ($op === 'form') {
  55. $options = $this->fetch_from_context($context, 'options');
  56. if (!empty($options['node_form'])) {
  57. $this->condition_met($context, $term['tid']);
  58. }
  59. }
  60. else {
  61. $this->condition_met($context, $term['tid']);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }