so__genotype.inc

File

tripal_chado/includes/TripalFields/so__genotype/so__genotype.inc
View source
  1. <?php
  2. class so__genotype 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 = 'Genotype';
  12. // The default description for this field.
  13. public static $description = 'Associates an indviddual or organization with
  14. this record.';
  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' => 'SO',
  25. // The name of the term.
  26. 'term_name' => 'Genotype',
  27. // The unique ID (i.e. accession) of the term.
  28. 'term_accession' => '0001027',
  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 = 'so__genotype_widget';
  36. // The default formatter for this field.
  37. public static $default_formatter = 'so__genotype_formatter';
  38. // --------------------------------------------------------------------------
  39. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  40. // --------------------------------------------------------------------------
  41. // An array containing details about the field. The format of this array
  42. // is the same as that returned by field_info_fields()
  43. protected $field;
  44. // An array containing details about an instance of the field. A field does
  45. // not have to have an instance. But if dealing with an instance (such as
  46. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  47. protected $instance;
  48. /**
  49. * @see TripalField::elementInfo()
  50. */
  51. public function elementInfo() {
  52. $field_term = $this->getFieldTermID();
  53. return array(
  54. $field_term => array(
  55. 'sortable' => FALSE,
  56. 'searchable' => FALSE,
  57. 'type' => 'xs:complexType',
  58. 'readonly' => TRUE,
  59. 'elements' => array(
  60. 'rdfs:type' => array(
  61. 'searchable' => FALSE,
  62. 'label' => 'Genotype Type',
  63. 'help' => 'The type of genotype.',
  64. 'sortable' => FALSE,
  65. 'type' => 'xs:string',
  66. 'readonly' => TRUE,
  67. 'required' => FALSE,
  68. ),
  69. 'schema:name' => array(
  70. 'label' => 'Genotype Name',
  71. 'help' => 'The name of the genotype.',
  72. 'searchable' => FALSE,
  73. 'sortable' => FALSE,
  74. 'type' => 'xs:string',
  75. 'readonly' => TRUE,
  76. 'required' => FALSE,
  77. ),
  78. 'schema:description' => array(
  79. 'label' => 'Genotype Description',
  80. 'help' => 'A description of the genotype.',
  81. 'searchable' => FALSE,
  82. 'sortable' => FALSE,
  83. 'type' => 'xs:string',
  84. 'readonly' => TRUE,
  85. 'required' => FALSE,
  86. ),
  87. )
  88. ),
  89. );
  90. }
  91. /**
  92. *
  93. * @see TripalField::load()
  94. */
  95. public function load($entity) {
  96. $record = $entity->chado_record;
  97. $field_name = $this->field['field_name'];
  98. $field_type = $this->field['type'];
  99. $field_table = $this->instance['settings']['chado_table'];
  100. $field_column = $this->instance['settings']['chado_column'];
  101. // Get the FK that links to the base record.
  102. $schema = chado_get_schema($field_table);
  103. $base_table = $entity->chado_table;
  104. $pkey = $schema['primary key'][0];
  105. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  106. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  107. // Set some defaults for the empty record.
  108. $entity->{$field_name}['und'][0] = array(
  109. 'value' => array(),
  110. $field_table . '__' . $pkey => '',
  111. $field_table . '__' . $fkey_lcolumn => '',
  112. $field_table . '__' . 'genotype_id' => '',
  113. // Ignore the synonym_sgml column for now.
  114. );
  115. $linker_table = $base_table . '_genotype';
  116. $options = array(
  117. 'return_array' => 1,
  118. 'include_fk' => array(
  119. 'genotype_id' => array(
  120. 'type_id' => array(
  121. 'dbxref_id' => array(
  122. 'db_id' => TRUE,
  123. ),
  124. ),
  125. ),
  126. $fkey_lcolumn => TRUE,
  127. ),
  128. );
  129. $record = chado_expand_var($record, 'table', $linker_table, $options);
  130. $genotype_linkers = isset($record->$linker_table->$fkey_rcolumn) ? $record->$linker_table->$fkey_rcolumn : '';
  131. if ($genotype_linkers) {
  132. foreach ($genotype_linkers as $i => $genotype_linker) {
  133. $genotype = $genotype_linker->genotype_id;
  134. $entity->{$field_name}['und'][$i] = array(
  135. 'value' => array(
  136. 'rdfs:type' => $genotype->type_id->name,
  137. 'schema:name' => $genotype->name,
  138. 'schema:description' => $genotype->description,
  139. ),
  140. $field_table . '__' . $pkey => $genotype_linker->$pkey,
  141. $field_table . '__' . $fkey_lcolumn => $genotype_linker->$fkey_lcolumn->$fkey_lcolumn,
  142. $field_table . '__' . 'genotype_id' => $genotype->genotype_id
  143. );
  144. if ($genotype && property_exists($genotype, 'entity_id')) {
  145. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $genotype->entity_id;
  146. }
  147. }
  148. }
  149. }
  150. }