data__accession.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. class data__accession 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 label for this field.
  11. public static $default_label = 'Site Accession';
  12. // The default description for this field.
  13. public static $description = 'The unique stable accession (ID) for this record on this site.';
  14. // Provide a list of instance specific settings. These can be accessed within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal will automatically change these settings for the instance.
  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. schema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'data',
  24. // The name of the term.
  25. 'term_name' => 'accession',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '2091',
  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. ];
  33. // The default widget for this field.
  34. public static $default_widget = 'data__accession_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'data__accession_formatter';
  37. /**
  38. * @see TripalField::load()
  39. */
  40. public function load($entity) {
  41. $record = $entity->chado_record;
  42. $field_name = $this->field['field_name'];
  43. $field_type = $this->field['type'];
  44. $field_table = $this->instance['settings']['chado_table'];
  45. $field_column = $this->instance['settings']['chado_column'];
  46. // Set some defauls for the empty record
  47. $entity->{$field_name}['und'][0] = [
  48. 'value' => '',
  49. 'chado-' . $field_table . '__' . $field_column => '',
  50. 'db_id' => '',
  51. 'accession' => '',
  52. ];
  53. // Get the primary dbxref record (if it's not NULL). Because we have a
  54. // dbxref_id passed in by the base record, we will only have one record.
  55. if ($record->$field_column) {
  56. $dbxref = $record->$field_column;
  57. $value = $dbxref->db_id->name . ':' . $dbxref->accession;
  58. // Skip the local:null accession as it is just a placeholder.
  59. if ($value != 'local:null') {
  60. $entity->{$field_name}['und'][0] = [
  61. 'value' => $dbxref->accession,
  62. 'chado-' . $field_table . '__' . $field_column => $record->$field_column->$field_column,
  63. 'db_id' => $dbxref->db_id->db_id,
  64. 'accession' => $dbxref->accession,
  65. ];
  66. }
  67. }
  68. }
  69. /**
  70. * @see ChadoField::query()
  71. */
  72. public function query($query, $condition) {
  73. $alias = $this->field['field_name'];
  74. $operator = $condition['operator'];
  75. $field_table = $this->instance['settings']['chado_table'];
  76. $field_column = $this->instance['settings']['chado_column'];
  77. $field_term_id = $this->getFieldTermID();
  78. $accession_term = chado_get_semweb_term($field_table, $field_column);
  79. // We don't offer any sub elements so the value coming in should
  80. // always be the field_name.
  81. if ($condition['column'] == $accession_term) {
  82. $this->queryJoinOnce($query, 'dbxref', 'DBX', "DBX.dbxref_id = base.dbxref_id");
  83. $query->condition("DBX.accession", $condition['value'], $operator);
  84. }
  85. }
  86. public function queryOrder($query, $order) {
  87. $alias = $this->field['field_name'];
  88. $operator = $condition['operator'];
  89. $field_table = $this->instance['settings']['chado_table'];
  90. $field_column = $this->instance['settings']['chado_column'];
  91. $field_term_id = $this->getFieldTermID();
  92. $accession_term = chado_get_semweb_term($field_table, $field_column);
  93. // We don't offer any sub elements so the value coming in should
  94. // always be the field_name.
  95. if ($order['column'] == $accession_term) {
  96. $this->queryJoinOnce($query, 'dbxref', 'DBX', "DBX.dbxref_id = base.dbxref_id", "LEFT OUTER");
  97. $query->orderBy("DBX.accession", $order['direction']);
  98. }
  99. }
  100. /**
  101. * @see TripalField::validate()
  102. */
  103. public function validate($entity_type, $entity, $langcode, $items, &$errors) {
  104. // If we don't have an entity then we don't want to validate. The case
  105. // where this could happen is when a user is editing the field settings
  106. // and trying to set a default value. In that case there's no entity and
  107. // we don't want to validate. There will always be an entity for creation
  108. // and update operations of a content type.
  109. if (!$entity) {
  110. return;
  111. }
  112. $field_name = $this->field['field_name'];
  113. $settings = $this->field['settings'];
  114. $field_type = $this->field['type'];
  115. $field_table = $this->instance['settings']['chado_table'];
  116. $field_column = $this->instance['settings']['chado_column'];
  117. // Get the field values.
  118. foreach ($items as $delta => $values) {
  119. $db_id = $values['db_id'];
  120. $accession = $values['accession'];
  121. // Make sure that if a database ID is provided that an accession is also
  122. // provided. Here we use the form_set_error function rather than the
  123. // form_error function because the form_error will add a red_highlight
  124. // around all of the fields in the fieldset which is confusing as it's not
  125. // clear to the user what field is required and which isn't. Therefore,
  126. // we borrow the code from the 'form_error' function and append the field
  127. // so that the proper field is highlighted on error.
  128. if (!$db_id and $accession) {
  129. $errors[$field_name][$delta]['und'][] = [
  130. 'message' => t("A database and the accession must both be provided for the primary cross reference."),
  131. 'error' => 'chado_base__dbxref',
  132. ];
  133. }
  134. if ($db_id and !$accession) {
  135. $errors[$field_name][$delta]['und'][] = [
  136. 'message' => t("A database and the accession must both be provided for the primary cross reference."),
  137. 'error' => 'chado_base__dbxref',
  138. ];
  139. }
  140. if (!$db_id and !$accession) {
  141. $errors[$field_name][$delta]['und'][] = [
  142. 'message' => t("A database and the accession must both be provided for the primary cross reference."),
  143. 'error' => 'chado_base__dbxref',
  144. ];
  145. }
  146. }
  147. }
  148. }