chado_linker__prop.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. class chado_linker__prop 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 = 'Chado Property';
  12. // The default description for this field.
  13. public static $description = 'Add details about this property.';
  14. // Provide a list of instance specific settings. These can be access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = array(
  22. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'local',
  24. // The name of the term.
  25. 'term_name' => 'property',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => 'property',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. );
  33. // The default widget for this field.
  34. public static $default_widget = 'chado_linker__prop_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'chado_linker__prop_formatter';
  37. // A boolean specifying that users should not be allowed to create
  38. // fields and instances of this field type through the UI. Such
  39. // fields can only be created programmatically with field_create_field()
  40. // and field_create_instance().
  41. public static $no_ui = FALSE;
  42. /**
  43. *
  44. * @see TripalField::load()
  45. */
  46. public function load($entity, $details = array()) {
  47. $field_name = $this->field['field_name'];
  48. $field_type = $this->field['type'];
  49. $field_table = $this->instance['settings']['chado_table'];
  50. $field_column = $this->instance['settings']['chado_column'];
  51. $base_table = $this->instance['settings']['base_table'];
  52. $vocabulary = $this->instance['settings']['term_vocabulary'];
  53. $accession = $this->instance['settings']['term_accession'];
  54. $cvterm = tripal_get_cvterm(array(
  55. 'dbxref_id' => array(
  56. 'db_id' => array(
  57. 'name' => $vocabulary,
  58. ),
  59. 'accession' => $accession,
  60. ),
  61. ));
  62. $cvterm_id = $cvterm->cvterm_id;
  63. // Get the FK that links to the base record.
  64. $schema = chado_get_schema($field_table);
  65. $pkey = $schema['primary key'][0];
  66. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  67. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  68. // Set some defaults for the empty record.
  69. $chado_record = $entity->chado_record;
  70. $entity->{$field_name}['und'][0] = array(
  71. 'value' => '',
  72. 'chado-' . $field_table . '__' . $pkey => '',
  73. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  74. 'chado-' . $field_table . '__value' => '',
  75. 'chado-' . $field_table . '__type_id' => '',
  76. 'chado-' . $field_table . '__rank' => '',
  77. );
  78. // Get the properties associated with this record for the given type.
  79. $columns = array('*');
  80. $match = array(
  81. $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
  82. 'type_id' => $cvterm_id,
  83. );
  84. $options = array(
  85. 'return_array' => TRUE,
  86. 'order_by' => array('rank' => 'ASC')
  87. );
  88. $properties = chado_select_record($field_table, $columns, $match, $options);
  89. for ($i = 0; $i < count($properties); $i++) {
  90. $property = $properties[$i];
  91. foreach ($schema['fields'] as $fname => $details) {
  92. $entity->{$field_name}['und'][$i] = array(
  93. 'value' => $property->value,
  94. 'chado-' . $field_table . '__' . $pkey => $property->$pkey,
  95. 'chado-' . $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  96. 'chado-' . $field_table . '__value' => $property->value,
  97. 'chado-' . $field_table . '__type_id' => $property->type_id,
  98. 'chado-' . $field_table . '__rank' => $property->rank,
  99. );
  100. }
  101. }
  102. }
  103. }