schema__alternate_name.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. class schema__alternate_name 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 = 'Synonyms';
  12. // The default description for this field.
  13. public static $description = 'An alternative name (synonym or alias) to this record.';
  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' => 'schema',
  24. // The name of the term.
  25. 'term_name' => 'alternateName',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => 'alternateName',
  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 = 'schema__alternate_name_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'schema__alternate_name_formatter';
  37. /**
  38. *
  39. * @see TripalField::load()
  40. */
  41. public function load($entity) {
  42. $record = $entity->chado_record;
  43. $base_table = $this->instance['settings']['base_table'];
  44. $field_name = $this->field['field_name'];
  45. $field_type = $this->field['type'];
  46. $field_table = $this->instance['settings']['chado_table'];
  47. $field_column = $this->instance['settings']['chado_column'];
  48. // Get the PKey for this table
  49. $schema = chado_get_schema($field_table);
  50. $pkey = $schema['primary key'][0];
  51. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  52. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  53. // Set some defaults for the empty record.
  54. $entity->{$field_name}['und'][0] = array(
  55. 'value' => '',
  56. 'chado-' . $field_table . '__' . $pkey => '',
  57. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  58. 'chado-' . $field_table . '__' . 'synonym_id' => '',
  59. 'chado-' . $field_table . '__' . 'pub_id' => '',
  60. 'chado-' . $field_table . '__' . 'is_current' => '',
  61. 'chado-' . $field_table . '__' . 'is_internal' => '',
  62. 'name' => '',
  63. 'type_id' => '',
  64. // Ignore the synonym_sgml column for now.
  65. );
  66. $linker_table = $base_table . '_synonym';
  67. $options = array('return_array' => 1);
  68. $record = chado_expand_var($record, 'table', $linker_table, $options);
  69. $synonyms_linker = $record->$linker_table;
  70. if (is_array($synonyms_linker) and count($synonyms_linker) > 0) {
  71. $i = 0;
  72. foreach ($synonyms_linker as $index => $linker) {
  73. $synonym = $linker->synonym_id;
  74. $entity->{$field_name}['und'][$i] = array(
  75. 'value' => $synonym->name,
  76. 'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
  77. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
  78. 'chado-' . $field_table . '__' . 'synonym_id' => $synonym->synonym_id,
  79. 'chado-' . $field_table . '__' . 'pub_id' => $linker->pub_id->pub_id,
  80. 'chado-' . $field_table . '__' . 'is_current' => $linker->is_current,
  81. 'chado-' . $field_table . '__' . 'is_internal' => $linker->is_internal,
  82. 'name' => $synonym->name,
  83. 'type_id' => $synonym->type_id->cvterm_id,
  84. );
  85. $i++;
  86. }
  87. }
  88. }
  89. /**
  90. * @see ChadoField::query()
  91. */
  92. public function query($query, $condition) {
  93. $alias = $this->field['field_name'];
  94. $operator = $condition['operator'];
  95. $field_name = $this->field['field_name'];
  96. $field_type = $this->field['type'];
  97. $field_table = $this->instance['settings']['chado_table'];
  98. $field_column = $this->instance['settings']['chado_column'];
  99. $base_table = $this->instance['settings']['base_table'];
  100. $schema = chado_get_schema($field_table);
  101. $pkey = $schema['primary key'][0];
  102. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  103. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  104. $this->queryJoinOnce($query, $field_table, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
  105. $this->queryJoinOnce($query, 'synonym', $alias . '_SYN', $alias . "_SYN.synonym_id = $alias.synonym_id");
  106. $query->condition($alias . "_SYN.name", $condition['value']);
  107. }
  108. /**
  109. * @see ChadoField::query()
  110. */
  111. public function queryOrder($query, $order) {
  112. $alias = $this->field['field_name'];
  113. $field_name = $this->field['field_name'];
  114. $field_type = $this->field['type'];
  115. $field_table = $this->instance['settings']['chado_table'];
  116. $field_column = $this->instance['settings']['chado_column'];
  117. $base_table = $this->instance['settings']['base_table'];
  118. $schema = chado_get_schema($field_table);
  119. $pkey = $schema['primary key'][0];
  120. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  121. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  122. $this->queryJoinOnce($query, $field_table, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn", "LEFT OUTER");
  123. $this->queryJoinOnce($query, 'synonym', $alias . '_SYN', $alias . "_SYN.synonym_id = $alias.synonym_id", "LEFT OUTER");
  124. $query->orderBy($alias . "_SYN.name", $order['direction']);
  125. }
  126. }