chado_linker__expression.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. class chado_linker__expression 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 = 'local:expression';
  16. // The default lable for this field.
  17. public static $label = 'Expression';
  18. // The default description for this field.
  19. public static $description = 'Associates an expression with
  20. this record.';
  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 = 'tripal_no_storage';
  37. // The default widget for this field.
  38. public static $default_widget = 'chado_linker__expression_widget';
  39. // The default formatter for this field.
  40. public static $default_formatter = 'chado_linker__expression_formatter';
  41. // --------------------------------------------------------------------------
  42. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  43. // --------------------------------------------------------------------------
  44. // An array containing details about the field. The format of this array
  45. // is the same as that returned by field_info_fields()
  46. protected $field;
  47. // An array containing details about an instance of the field. A field does
  48. // not have to have an instance. But if dealing with an instance (such as
  49. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  50. protected $instance;
  51. /**
  52. *
  53. * @see TripalField::validate()
  54. */
  55. public function validate($entity_type, $entity, $field, $items, &$errors) {
  56. }
  57. /**
  58. *
  59. * @see TripalField::load()
  60. */
  61. public function load($entity, $details = array()) {
  62. $record = $details['record'];
  63. $field_name = $this->field['field_name'];
  64. $field_type = $this->field['type'];
  65. $field_table = $this->field['settings']['chado_table'];
  66. $field_column = $this->field['settings']['chado_column'];
  67. // Get the FK that links to the base record.
  68. $schema = chado_get_schema($field_table);
  69. $base_table = $details['record']->tablename;
  70. $pkey = $schema['primary key'][0];
  71. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  72. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  73. // Set some defaults for the empty record.
  74. $entity->{$field_name}['und'][0] = array(
  75. 'value' => array(),
  76. );
  77. $linker_table = $base_table . '_expression';
  78. $options = array(
  79. 'return_array' => 1,
  80. );
  81. $record = chado_expand_var($record, 'table', $linker_table, $options);
  82. $exp_linkers = $record->$linker_table;
  83. if ($exp_linkers) {
  84. foreach ($exp_linkers as $i => $exp_linker) {
  85. // Because the unqiuename is a text field we must expand it.
  86. $expression = $exp_linker->expression_id;
  87. $expression = chado_expand_var($expression, 'field', 'expression.uniquename', $options);
  88. // Set the values that will be seen by the user on the page and in
  89. // web services, or anwhere this field is viewed.
  90. $entity->{$field_name}['und'][$i]['value'] = array(
  91. 'name' => $expression->uniquename,
  92. 'description' => $expression->description,
  93. //'md5checksum' => $expression->md5checksum,
  94. );
  95. // Add the pub information if a real pub is associated with the record.
  96. $pub = $exp_linker->pub_id;
  97. if ($pub->uniquename != 'null') {
  98. $pub_details = tripal_get_minimal_pub_info($pub);
  99. $entity->{$field_name}['und'][$i]['value']['publication'] = $pub_details;
  100. $entity->{$field_name}['und'][$i]['value']['publication']['type'] = $pub->type_id->name;
  101. if (property_exists($pub, 'entity_id')) {
  102. $entity->{$field_name}['und'][$i]['publication'][0]['value']['entity'] = 'TripalEntity:' . $pub->entity_id;
  103. }
  104. }
  105. // Add the linker_expressionprop
  106. $linkerprop_table = $linker_table . 'prop';
  107. if (db_table_exists('chado.' . $linkerprop_table)) {
  108. $exp_linker = chado_expand_var($exp_linker, 'table', $linkerprop_table, $options);
  109. $exp_linkerprops = $exp_linker->feature_expressionprop;
  110. if ($exp_linkerprops) {
  111. foreach ($exp_linkerprops AS $linkerprop) {
  112. $entity->{$field_name}['und'][$i]['value'][$linkerprop->type_id->name] = $linkerprop->value;
  113. }
  114. }
  115. }
  116. // Add the fields for the widget form. The name requres the following
  117. // format if the fields should be used as values for insertint/updating
  118. // into the chado table: [table_name]__[field_name]
  119. $entity->{$field_name}['und'][$i][$linker_table . '__expression_id'] = $expression->expression_id;
  120. $entity->{$field_name}['und'][$i][$linker_table . '__uniquename'] = $expression->uniquename;
  121. //$entity->{$field_name}['und'][$i][$linker_table . '__md5checksum'] = $expression->md5checksum;
  122. $entity->{$field_name}['und'][$i][$linker_table . '__description'] = $expression->description;
  123. }
  124. }
  125. }
  126. /**
  127. *
  128. * @see TripalField::settingsForm()
  129. */
  130. public function settingsForm($has_data) {
  131. }
  132. /**
  133. *
  134. * @param unknown $form
  135. * @param unknown $form_state
  136. */
  137. public function settingsFormValidate($form, &$form_state) {
  138. }
  139. /**
  140. *
  141. * @see TripalField::instanceSettingsForm()
  142. */
  143. public function instanceSettingsForm() {
  144. }
  145. /**
  146. *
  147. * @see TripalField::instanceSettingsFormValidate()
  148. */
  149. public function instanceSettingsFormValidate($form, &$form_state) {
  150. }
  151. }