data__accession.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 lable 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
  14. this record on this site.';
  15. // Provide a list of instance specific settings. These can be access within
  16. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  17. // then Drupal with automatically change these settings for the instnace.
  18. // It is recommended to put settings at the instance level whenever possible.
  19. // If you override this variable in a child class be sure to replicate the
  20. // term_name, term_vocab, term_accession and term_fixed keys as these are
  21. // required for all TripalFields.
  22. public static $default_instance_settings = array(
  23. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  24. 'term_vocabulary' => 'data',
  25. // The name of the term.
  26. 'term_name' => 'accession',
  27. // The unique ID (i.e. accession) of the term.
  28. 'term_accession' => '2091',
  29. // Set to TRUE if the site admin is allowed to change the term
  30. // type. This will create form elements when editing the field instance
  31. // to allow the site admin to change the term settings above.
  32. 'term_fixed' => FALSE,
  33. );
  34. // The default widget for this field.
  35. public static $default_widget = 'data__accession_widget';
  36. // The default formatter for this field.
  37. public static $default_formatter = 'data__accession_formatter';
  38. /**
  39. * @see TripalField::load()
  40. */
  41. public function load($entity) {
  42. $record = $entity->chado_record;
  43. $field_name = $this->field['field_name'];
  44. $field_type = $this->field['type'];
  45. $field_table = $this->instance['settings']['chado_table'];
  46. $field_column = $this->instance['settings']['chado_column'];
  47. // Set some defauls for the empty record
  48. $entity->{$field_name}['und'][0] = array(
  49. 'value' => '',
  50. 'chado-' . $field_table . '__' . $field_column => '',
  51. 'db_id' => '',
  52. 'accession' => '',
  53. 'version' => '',
  54. 'description' => '',
  55. );
  56. // Get the primary dbxref record (if it's not NULL). Because we have a
  57. // dbxref_id passed in by the base record, we will only have one record.
  58. if ($record->$field_column) {
  59. $dbxref = $record->$field_column;
  60. $value = $dbxref->db_id->name . ':' . $dbxref->accession;
  61. $entity->{$field_name}['und'][0] = array(
  62. 'value' => $dbxref->accession,
  63. 'chado-' . $field_table . '__' . $field_column => $record->$field_column->$field_column,
  64. 'db_id' => $dbxref->db_id->db_id,
  65. 'accession' => $dbxref->accession,
  66. 'version' => $dbxref->version,
  67. 'description' => $dbxref->description,
  68. );
  69. }
  70. }
  71. /**
  72. * @see ChadoField::query()
  73. */
  74. public function query($query, $condition) {
  75. $chado_table = $this->instance['settings']['chado_table'];
  76. $base_table = $this->instance['settings']['base_table'];
  77. $bschema = chado_get_schema($base_table);
  78. $bpkey = $bschema['primary key'][0];
  79. $alias = 'dbx_linker';
  80. $operator = $condition['operator'];
  81. if ($condition['column'] == 'accession') {
  82. $query->join('dbxref', 'DBX', "DBX.dbxref_id = base.dbxref_id");
  83. $query->condition("DBX.accession", $condition['value'], $operator);
  84. }
  85. }
  86. /**
  87. * @see TripalField::validate()
  88. */
  89. public function validate($entity_type, $entity, $field, $items, &$errors) {
  90. $field_name = $this->field['field_name'];
  91. $settings = $this->field['settings'];
  92. $field_type = $this->field['type'];
  93. $field_table = $this->instance['settings']['chado_table'];
  94. $field_column = $this->instance['settings']['chado_column'];
  95. // Get the field values.
  96. foreach ($items as $delta => $values) {
  97. $fk_val = $values['chado-' . $field_table . '__' . $field_column];
  98. $db_id = $values['db_id'];
  99. $accession = $values['accession'];
  100. $version = $values['version'];
  101. $description = $values['description'];
  102. // Make sure that if a database ID is provided that an accession is also
  103. // provided. Here we use the form_set_error function rather than the
  104. // form_error function because the form_error will add a red_highlight
  105. // around all of the fields in the fieldset which is confusing as it's not
  106. // clear to the user what field is required and which isn't. Therefore,
  107. // we borrow the code from the 'form_error' function and append the field
  108. // so that the proper field is highlighted on error.
  109. if (!$db_id and $accession) {
  110. $errors[$field_name][$delta]['und'][] = array(
  111. 'message' => t("A database and the accession must both be provided for the primary cross reference."),
  112. 'error' => 'chado_base__dbxref',
  113. );
  114. }
  115. if ($db_id and !$accession) {
  116. $errors[$field_name][$delta]['und'][] = array(
  117. 'message' => t("A database and the accession must both be provided for the primary cross reference."),
  118. 'error' => 'chado_base__dbxref',
  119. );
  120. }
  121. if (!$db_id and !$accession and ($version or $description)) {
  122. $errors[$field_name][$delta]['und'][] = array(
  123. 'message' => t("A database and the accession must both be provided for the primary cross reference."),
  124. 'error' => 'chado_base__dbxref',
  125. );
  126. }
  127. }
  128. }
  129. }