data__accession.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. class data__accession extends TripalField {
  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 term that this field maps to. The format for the term should be:
  11. // [vocab]:[accession] where [vocab] is the short name of the vocabulary
  12. // and [acession] is the unique accession number for the term. This term
  13. // must already exist in the vocabulary storage backend. This
  14. // value should never be changed once fields exist for this type.
  15. public static $term = 'data:2091';
  16. // The default lable for this field.
  17. public static $label = 'Accession';
  18. // The default description for this field.
  19. public static $description = 'The unique stable accession (ID) for
  20. this record on this site.';
  21. // Provide a list of global settings. These can be accessed witihn the
  22. // globalSettingsForm. When the globalSettingsForm is submitted then
  23. // Drupal will automatically change these settings for all fields.
  24. public static $settings = array(
  25. 'chado_table' => '',
  26. 'chado_column' => '',
  27. 'base_table' => '',
  28. );
  29. // Provide a list of instance specific settings. These can be access within
  30. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  31. // then Drupal with automatically change these settings for the instnace.
  32. // It is recommended to put settings at the instance level whenever possible.
  33. public static $instance_settings = array();
  34. // Set this to the name of the storage backend that by default will support
  35. // this field.
  36. public static $storage = 'field_chado_storage';
  37. // The default widget for this field.
  38. public static $default_widget = 'data__accession_widget';
  39. // The default formatter for this field.
  40. public static $default_formatter = 'data__accession_formatter';
  41. /**
  42. * @see TripalField::load()
  43. */
  44. public function load($entity, $details = array()) {
  45. $record = $details['record'];
  46. $field_name = $this->field['field_name'];
  47. $field_type = $this->field['type'];
  48. $field_table = $this->field['settings']['chado_table'];
  49. $field_column = $this->field['settings']['chado_column'];
  50. // Set some defauls for the empty record
  51. $entity->{$field_name}['und'][0] = array(
  52. 'value' => array(
  53. 'vocabulary' => '',
  54. 'accession' => '',
  55. 'URL' => '',
  56. ),
  57. 'chado-' . $field_table . '__' . $field_column => '',
  58. 'db_id' => '',
  59. 'accession' => '',
  60. 'version' => '',
  61. 'description' => '',
  62. );
  63. // Get the primary dbxref record (if it's not NULL). Because we have a
  64. // dbxref_id passed in by the base record, we will only have one record.
  65. if ($record->$field_column) {
  66. $dbxref = $record->$field_column;
  67. $value = $dbxref->db_id->name . ':' . $dbxref->accession;
  68. $entity->{$field_name}['und'][0] = array(
  69. 'value' => array(
  70. 'vocabulary' => $dbxref->db_id->name,
  71. 'accession' => $dbxref->accession,
  72. 'URL' => tripal_get_dbxref_url($dbxref),
  73. ),
  74. 'chado-' . $field_table . '__' . $field_column => $record->$field_column->$field_column,
  75. 'db_id' => $dbxref->db_id->db_id,
  76. 'accession' => $dbxref->accession,
  77. 'version' => $dbxref->version,
  78. 'description' => $dbxref->description,
  79. );
  80. }
  81. }
  82. /**
  83. * @see TripalField::validate()
  84. */
  85. public function validate($entity_type, $entity, $field, $items, &$errors) {
  86. $field_name = $this->field['field_name'];
  87. $settings = $this->field['settings'];
  88. $field_name = $this->field['field_name'];
  89. $field_type = $this->field['type'];
  90. $field_table = $this->field['settings']['chado_table'];
  91. $field_column = $this->field['settings']['chado_column'];
  92. // Get the field values.
  93. foreach ($items as $delta => $values) {
  94. $fk_val = $values['chado-' . $field_table . '__' . $field_column];
  95. $db_id = $values['db_id'];
  96. $accession = $values['accession'];
  97. $version = $values['version'];
  98. $description = $values['description'];
  99. // Make sure that if a database ID is provided that an accession is also
  100. // provided. Here we use the form_set_error function rather than the
  101. // form_error function because the form_error will add a red_highlight
  102. // around all of the fields in the fieldset which is confusing as it's not
  103. // clear to the user what field is required and which isn't. Therefore,
  104. // we borrow the code from the 'form_error' function and append the field
  105. // so that the proper field is highlighted on error.
  106. if (!$db_id and $accession) {
  107. $errors[$field_name][$delta]['und'][] = array(
  108. 'message' => t("A database and the accession must both be provided for the primary cross reference."),
  109. 'error' => 'chado_base__dbxref',
  110. );
  111. }
  112. if ($db_id and !$accession) {
  113. $errors[$field_name][$delta]['und'][] = array(
  114. 'message' => t("A database and the accession must both be provided for the primary cross reference."),
  115. 'error' => 'chado_base__dbxref',
  116. );
  117. }
  118. if (!$db_id and !$accession and ($version or $description)) {
  119. $errors[$field_name][$delta]['und'][] = array(
  120. 'message' => t("A database and the accession must both be provided for the primary cross reference."),
  121. 'error' => 'chado_base__dbxref',
  122. );
  123. }
  124. }
  125. }
  126. }