chado_linker__prop.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 = [
  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. // The number of default rows to show. The default is 1.
  39. 'rows' => 1,
  40. ];
  41. // The default widget for this field.
  42. public static $default_widget = 'chado_linker__prop_widget';
  43. // The default formatter for this field.
  44. public static $default_formatter = 'chado_linker__prop_formatter';
  45. // A boolean specifying that users should not be allowed to create
  46. // fields and instances of this field type through the UI. Such
  47. // fields can only be created programmatically with field_create_field()
  48. // and field_create_instance().
  49. public static $no_ui = FALSE;
  50. /**
  51. *
  52. * @see TripalField::load()
  53. */
  54. public function load($entity) {
  55. $field_name = $this->field['field_name'];
  56. $field_type = $this->field['type'];
  57. $field_table = $this->instance['settings']['chado_table'];
  58. $field_column = $this->instance['settings']['chado_column'];
  59. $base_table = $this->instance['settings']['base_table'];
  60. $vocabulary = $this->instance['settings']['term_vocabulary'];
  61. $accession = $this->instance['settings']['term_accession'];
  62. $cvterm = chado_get_cvterm([
  63. 'dbxref_id' => [
  64. 'db_id' => [
  65. 'name' => $vocabulary,
  66. ],
  67. 'accession' => $accession,
  68. ],
  69. ]);
  70. $cvterm_id = $cvterm->cvterm_id;
  71. // Get the FK that links to the base record.
  72. $schema = chado_get_schema($field_table);
  73. $pkey = $schema['primary key'][0];
  74. if (!isset($schema['foreign keys'][$base_table]['columns'])) {
  75. return;
  76. }
  77. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  78. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  79. // Set some defaults for the empty record.
  80. $chado_record = $entity->chado_record;
  81. $entity->{$field_name}['und'][0] = [
  82. 'value' => '',
  83. 'chado-' . $field_table . '__' . $pkey => '',
  84. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->$fkey_lcolumn,
  85. 'chado-' . $field_table . '__value' => '',
  86. 'chado-' . $field_table . '__type_id' => $cvterm_id,
  87. 'chado-' . $field_table . '__rank' => '',
  88. ];
  89. // Get the properties associated with this record for the given type.
  90. $columns = ['*'];
  91. $match = [
  92. $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
  93. 'type_id' => $cvterm_id,
  94. ];
  95. $options = [
  96. 'return_array' => TRUE,
  97. 'order_by' => ['rank' => 'ASC'],
  98. ];
  99. $properties = chado_select_record($field_table, $columns, $match, $options);
  100. for ($i = 0; $i < count($properties); $i++) {
  101. $property = $properties[$i];
  102. foreach ($schema['fields'] as $fname => $details) {
  103. $entity->{$field_name}['und'][$i] = [
  104. 'value' => $property->value,
  105. 'chado-' . $field_table . '__' . $pkey => $property->$pkey,
  106. 'chado-' . $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  107. 'chado-' . $field_table . '__value' => $property->value,
  108. 'chado-' . $field_table . '__type_id' => $property->type_id,
  109. 'chado-' . $field_table . '__rank' => $property->rank,
  110. ];
  111. }
  112. }
  113. // Ensure there are no values if there are no properties.
  114. // This is necessary to make sure the field is not rendered when there are no properies.
  115. // @todo: We should revisit this in the future as none of the other fields do this.
  116. // It was added here to make it easier to detect when the field was empty
  117. // but in hindsight, we would just check $entity->{$field_name}['und'][$i]['value']
  118. // in the formatter.
  119. if (empty($properties)) {
  120. unset($entity->{$field_name});
  121. }
  122. }
  123. /**
  124. * @see ChadoField::query()
  125. */
  126. public function query($query, $condition) {
  127. $prop_linker = $this->instance['settings']['chado_table'];
  128. $base_table = $this->instance['settings']['base_table'];
  129. $bschema = chado_get_schema($base_table);
  130. $bpkey = $bschema['primary key'][0];
  131. $alias = $this->field['field_name'];
  132. $operator = $condition['operator'];
  133. $vocab = $this->instance['settings']['term_vocabulary'];
  134. $accession = $this->instance['settings']['term_accession'];
  135. $cvterm = chado_get_cvterm(['id' => $vocab . ':' . $accession]);
  136. $this->queryJoinOnce($query, $prop_linker, $alias, "base.$bpkey = $alias.$bpkey");
  137. $query->condition("$alias.type_id", $cvterm->cvterm_id);
  138. $query->condition("$alias.value", $condition['value'], $operator);
  139. }
  140. /**
  141. * @see ChadoField::query()
  142. */
  143. public function queryOrder($query, $order) {
  144. $prop_linker = $this->instance['settings']['chado_table'];
  145. $base_table = $this->instance['settings']['base_table'];
  146. $bschema = chado_get_schema($base_table);
  147. $bpkey = $bschema['primary key'][0];
  148. $alias = $this->field['field_name'];
  149. $vocab = $this->instance['settings']['term_vocabulary'];
  150. $accession = $this->instance['settings']['term_accession'];
  151. $cvterm = chado_get_cvterm(['id' => $vocab . ':' . $accession]);
  152. $this->queryJoinOnce($query, $prop_linker, $alias, "base.$bpkey = $alias.$bpkey AND $alias.type_id = $cvterm->cvterm_id", "LEFT OUTER");
  153. $query->orderBy("$alias.value", $order['direction']);
  154. }
  155. }