chado_linker__prop.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. class chado_linker__prop extends ChadoField {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. // The default lable for this field.
  11. public static $default_label = 'Chado Property';
  12. // The default description for this field.
  13. public static $description = 'Add details about this property.';
  14. // Provide a list of instance specific settings. These can be access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = array(
  22. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'local',
  24. // The name of the term.
  25. 'term_name' => 'property',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => 'property',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. // The table in Chado that the instance maps to.
  33. 'chado_table' => '',
  34. // The primary key column of hte table in Dhado.
  35. 'chado_column' => '',
  36. // The base table.
  37. 'base_table' => '',
  38. );
  39. // The default widget for this field.
  40. public static $default_widget = 'chado_linker__prop_widget';
  41. // The default formatter for this field.
  42. public static $default_formatter = 'chado_linker__prop_formatter';
  43. // A boolean specifying that users should not be allowed to create
  44. // fields and instances of this field type through the UI. Such
  45. // fields can only be created programmatically with field_create_field()
  46. // and field_create_instance().
  47. public static $no_ui = FALSE;
  48. /**
  49. *
  50. * @see TripalField::load()
  51. */
  52. public function load($entity, $details = array()) {
  53. $field_name = $this->field['field_name'];
  54. $field_type = $this->field['type'];
  55. $field_table = $this->instance['settings']['chado_table'];
  56. $field_column = $this->instance['settings']['chado_column'];
  57. $base_table = $this->instance['settings']['base_table'];
  58. $vocabulary = $this->instance['settings']['term_vocabulary'];
  59. $accession = $this->instance['settings']['term_accession'];
  60. $cvterm = tripal_get_cvterm(array(
  61. 'dbxref_id' => array(
  62. 'db_id' => array(
  63. 'name' => $vocabulary,
  64. ),
  65. 'accession' => $accession,
  66. ),
  67. ));
  68. $cvterm_id = $cvterm->cvterm_id;
  69. // Get the FK that links to the base record.
  70. $schema = chado_get_schema($field_table);
  71. $pkey = $schema['primary key'][0];
  72. if (!isset($schema['foreign keys'][$base_table]['columns'])) {
  73. return;
  74. }
  75. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  76. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  77. // Set some defaults for the empty record.
  78. $chado_record = $entity->chado_record;
  79. $entity->{$field_name}['und'][0] = array(
  80. 'value' => '',
  81. 'chado-' . $field_table . '__' . $pkey => '',
  82. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->$fkey_lcolumn,
  83. 'chado-' . $field_table . '__value' => '',
  84. 'chado-' . $field_table . '__type_id' => $cvterm_id,
  85. 'chado-' . $field_table . '__rank' => '',
  86. );
  87. // Get the properties associated with this record for the given type.
  88. $columns = array('*');
  89. $match = array(
  90. $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
  91. 'type_id' => $cvterm_id,
  92. );
  93. $options = array(
  94. 'return_array' => TRUE,
  95. 'order_by' => array('rank' => 'ASC')
  96. );
  97. $properties = chado_select_record($field_table, $columns, $match, $options);
  98. for ($i = 0; $i < count($properties); $i++) {
  99. $property = $properties[$i];
  100. foreach ($schema['fields'] as $fname => $details) {
  101. $entity->{$field_name}['und'][$i] = array(
  102. 'value' => $property->value,
  103. 'chado-' . $field_table . '__' . $pkey => $property->$pkey,
  104. 'chado-' . $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  105. 'chado-' . $field_table . '__value' => $property->value,
  106. 'chado-' . $field_table . '__type_id' => $property->type_id,
  107. 'chado-' . $field_table . '__rank' => $property->rank,
  108. );
  109. }
  110. }
  111. }
  112. /**
  113. * @see ChadoField::query()
  114. */
  115. public function query($query, $condition) {
  116. $prop_linker = $this->instance['settings']['chado_table'];
  117. $base_table = $this->instance['settings']['base_table'];
  118. $bschema = chado_get_schema($base_table);
  119. $bpkey = $bschema['primary key'][0];
  120. $alias = $this->field['field_name'];
  121. $operator = $condition['operator'];
  122. $vocab = $this->instance['settings']['term_vocabulary'];
  123. $accession = $this->instance['settings']['term_accession'];
  124. $cvterm = tripal_get_cvterm(array('id' => $vocab . ':' . $accession));
  125. $query->join($prop_linker, $alias, "base.$bpkey = $alias.$bpkey");
  126. $query->condition("$alias.type_id", $cvterm->cvterm_id);
  127. $query->condition("$alias.value", $condition['value'], $operator);
  128. }
  129. }