data__accession.inc 5.9 KB

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