chado_linker__prop.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. class chado_linker__prop 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:property';
  16. // The default lable for this field.
  17. public static $label = 'Property';
  18. // The default description for this field.
  19. public static $description = 'Add details about this property.';
  20. // Provide a list of global settings. These can be accessed witihn the
  21. // globalSettingsForm. When the globalSettingsForm is submitted then
  22. // Drupal will automatically change these settings for all fields.
  23. public static $settings = array(
  24. 'chado_table' => '',
  25. 'chado_column' => '',
  26. 'base_table' => '',
  27. );
  28. // Provide a list of instance specific settings. These can be access within
  29. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  30. // then Drupal with automatically change these settings for the instnace.
  31. // It is recommended to put settings at the instance level whenever possible.
  32. public static $instance_settings = array();
  33. // Set this to the name of the storage backend that by default will support
  34. // this field.
  35. public static $storage = 'tripal_no_storage';
  36. // The default widget for this field.
  37. public static $default_widget = 'chado_linker__prop_widget';
  38. // The default formatter for this field.
  39. public static $default_formatter = 'chado_linker__prop_formatter';
  40. // --------------------------------------------------------------------------
  41. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  42. // --------------------------------------------------------------------------
  43. // An array containing details about the field. The format of this array
  44. // is the same as that returned by field_info_fields()
  45. protected $field;
  46. // An array containing details about an instance of the field. A field does
  47. // not have to have an instance. But if dealing with an instance (such as
  48. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  49. protected $instance;
  50. /**
  51. *
  52. * @see TripalField::validate()
  53. */
  54. public function validate($entity_type, $entity, $field, $items, &$errors) {
  55. }
  56. /**
  57. *
  58. * @see TripalField::load()
  59. */
  60. public function load($entity, $details = array()) {
  61. $field_name = $this->field['field_name'];
  62. $field_type = $this->field['type'];
  63. $field_table = $this->field['settings']['chado_table'];
  64. $field_column = $this->field['settings']['chado_column'];
  65. $base_table = $this->field['settings']['base_table'];
  66. $matches = array();
  67. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  68. $table_name = $matches[1];
  69. $cvterm_id = $matches[2];
  70. // Get the FK that links to the base record.
  71. $schema = chado_get_schema($field_table);
  72. $pkey = $schema['primary key'][0];
  73. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  74. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  75. // Set some defaults for the empty record.
  76. $chado_record = $entity->chado_record;
  77. $entity->{$field_name}['und'][0] = array(
  78. 'value' => '',
  79. 'chado-' . $field_table . '__' . $pkey => '',
  80. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
  81. 'chado-' . $field_table . '__value' => '',
  82. 'chado-' . $field_table . '__type_id' => '',
  83. 'chado-' . $field_table . '__rank' => '',
  84. );
  85. // Get the properties associated with this base record for this fields
  86. // given type.
  87. $columns = array('*');
  88. $match = array(
  89. $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
  90. 'type_id' => $cvterm_id,
  91. );
  92. $options = array(
  93. 'return_array' => TRUE,
  94. 'order_by' => array('rank' => 'ASC')
  95. );
  96. $properties = chado_select_record($field_table, $columns, $match, $options);
  97. for ($i = 0; $i < count($properties); $i++) {
  98. $property = $properties[$i];
  99. foreach ($schema['fields'] as $fname => $details) {
  100. $entity->{$field_name}['und'][$i] = array(
  101. 'value' => array(),
  102. 'chado-' . $field_table . '__' . $pkey => $property->$pkey,
  103. 'chado-' . $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  104. 'chado-' . $field_table . '__value' => $property->value,
  105. 'chado-' . $field_table . '__type_id' => $property->type_id,
  106. 'chado-' . $field_table . '__rank' => $property->rank,
  107. );
  108. }
  109. }
  110. }
  111. /**
  112. *
  113. * @see TripalField::settingsForm()
  114. */
  115. public function settingsForm($has_data) {
  116. }
  117. /**
  118. *
  119. * @param unknown $form
  120. * @param unknown $form_state
  121. */
  122. public function settingsFormValidate($form, &$form_state) {
  123. }
  124. /**
  125. *
  126. * @see TripalField::instanceSettingsForm()
  127. */
  128. public function instanceSettingsForm() {
  129. }
  130. /**
  131. *
  132. * @see TripalField::instanceSettingsFormValidate()
  133. */
  134. public function instanceSettingsFormValidate($form, &$form_state) {
  135. }
  136. }