views_handler_join_chado_through_linking.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. module_load_include('inc','views','includes/base');
  3. module_load_include('inc','views','includes/handlers');
  4. /**
  5. * Handler to allow joins between records via a linking table
  6. *
  7. * Example Usage:
  8. * To join the analysis table to the feature table through the analysisfeature table,
  9. * (ie: get analysis fields to show up in a feature view)
  10. * Use the following code in the analysis hook_views_data:
  11. * @code
  12. $data['analysis']['table']['join']['feature'] = array(
  13. 'linking' => array(
  14. 'table' => 'analysisfeature',
  15. 'left_field' => 'feature_id',
  16. 'field' => 'analysis_id',
  17. ),
  18. 'left_field' => 'feature_id',
  19. 'field' => 'analysis_id',
  20. 'handler' => 'views_handler_join_chado_through_linking'
  21. );
  22. * @endcode
  23. *
  24. * NOTE: If the right table is in the drupal schema rather then the chado schema
  25. * (ie: node, chado_feature) then add the following to the above join description:
  26. * @code
  27. 'table_is_drupal' => TRUE
  28. * @endcode
  29. * This will ensure the drupal table is surrounded by { } and as such any database
  30. * prefixes are added correctly. If the left table is in the drupal schema it should already
  31. * be defined by a previous join (or the From clause).
  32. */
  33. class views_handler_join_chado_through_linking extends views_join {
  34. // PHP 4 doesn't call constructors of the base class automatically from a
  35. // constructor of a derived class. It is your responsibility to propagate
  36. // the call to constructors upstream where appropriate.
  37. function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT', $added = NULL) {
  38. parent::construct($table, $left_table, $left_field, $field, $extra, $type);
  39. }
  40. /**
  41. * Creates SQL for both joins table => linking_table and linking_table => left_table
  42. * NOTE: Uses fields in definition as passed in from hook_views_data join definition
  43. */
  44. function join($table, &$query) {
  45. $output = '';
  46. $joins = array();
  47. $joins[] = $this->create_single_join(
  48. $query,
  49. array(
  50. 'table' => $this->definition['linking']['table'],
  51. 'field' => $this->definition['linking']['left_field'],
  52. 'is_drupal' => FALSE,
  53. ),
  54. array(
  55. 'table' => $this->definition['left_table'],
  56. 'field' => $this->definition['left_field'],
  57. ),
  58. 'LEFT'
  59. );
  60. $joins[] = $this->create_single_join(
  61. $query,
  62. array(
  63. 'table' => $this->definition['table'],
  64. 'field' => $this->definition['field'],
  65. 'is_drupal' => $this->definition['table_is_drupal'],
  66. ),
  67. array(
  68. 'table' => $this->definition['linking']['table'],
  69. 'field' => $this->definition['linking']['field'],
  70. ),
  71. 'LEFT'
  72. );
  73. $output .= implode("\n",$joins);
  74. return $output;
  75. }
  76. /**
  77. * Creates SQL for a single join based on parameters
  78. */
  79. function create_single_join (&$query, $right_spec, $left_spec, $join_type) {
  80. if ($right_spec['table']) {
  81. $right = $query->get_table_info($right_spec['table']);
  82. if (!$right['alias']) { $right['alias'] = $right_spec['table']; }
  83. $right_field = "$right[alias].$right_spec[field]";
  84. if ($right_spec['is_drupal']) {
  85. $right_table = '{'.$right_spec['table'].'}';
  86. } else {
  87. $right_table = $right_spec['table'];
  88. }
  89. }
  90. if ($left_spec['table']) {
  91. $left = $query->get_table_info($left_spec['table']);
  92. if (!$left['alias']) { $left['alias'] = $left_spec['table']; }
  93. $left_field = "$left[alias].$left_spec[field]";
  94. } else {
  95. // This can be used if left_field is a formula or something. It should be used only *very* rarely.
  96. $left_field = $this->left_spec['field'];
  97. }
  98. $output = " $join_type JOIN $right_table $right[alias] ON $left_field = $right_field";
  99. return $output;
  100. }
  101. }