taxrank__infraspecific_taxon.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. class taxrank__infraspecific_taxon 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 = 'Infraspecific Taxon';
  12. // The default description for this field.
  13. public static $description = 'Specifies the infraspecific taxon of an organism.';
  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 = [
  22. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'TAXRANK',
  24. // The name of the term.
  25. 'term_name' => 'infraspecific_taxon',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '0000046',
  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 = 'HERE_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'HERE_formatter';
  37. // --------------------------------------------------------------------------
  38. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  39. // --------------------------------------------------------------------------
  40. // An array containing details about the field. The format of this array
  41. // is the same as that returned by field_info_fields()
  42. protected $field;
  43. // An array containing details about an instance of the field. A field does
  44. // not have to have an instance. But if dealing with an instance (such as
  45. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  46. protected $instance;
  47. /**
  48. * @see TripalField::elementInfo()
  49. */
  50. public function elementInfo() {
  51. $field_term = $this->getFieldTermID();
  52. $label_term = 'rdfs:label';
  53. $infraspecific_name_term = chado_get_semweb_term('organism', 'infraspecific_name');
  54. $infraspecific_type_term = chado_get_semweb_term('organism', 'type_id');
  55. return [
  56. $field_term => [
  57. 'sortable' => FALSE,
  58. 'searchable' => TRUE,
  59. 'readonly' => FALSE,
  60. 'type' => 'xs:complexType',
  61. 'elements' => [
  62. $label_term => [
  63. 'name' => 'label',
  64. 'sortable' => FALSE,
  65. 'searchable' => TRUE,
  66. 'label' => 'Infraspecific Full Name',
  67. 'help' => 'The full infraspecific name including the rank and name.',
  68. 'type' => 'xsstring',
  69. 'readonly' => TRUE,
  70. 'required' => FALSE,
  71. ],
  72. $infraspecific_name_term => [
  73. 'name' => 'infraspecific_name',
  74. 'sortable' => TRUE,
  75. 'searchable' => TRUE,
  76. 'label' => 'Infraspecies Name',
  77. 'help' => 'The infraspecific name of the organism below the rank of species.',
  78. 'type' => 'xs:string',
  79. 'readonly' => FALSE,
  80. 'required' => TRUE,
  81. ],
  82. $infraspecific_type_term => [
  83. 'name' => 'infraspecific_rank',
  84. 'sortable' => TRUE,
  85. 'searchable' => TRUE,
  86. 'label' => 'Infraspecific Rank',
  87. 'help' => 'The infraspecific rank of the organism below the rank of species.',
  88. 'type' => 'xs:string',
  89. 'readonly' => FALSE,
  90. 'required' => TRUE,
  91. ],
  92. ],
  93. ],
  94. ];
  95. }
  96. /**
  97. * @see ChadoField::query()
  98. */
  99. public function query($query, $condition) {
  100. $alias = $this->field['field_name'];
  101. $operator = $condition['operator'];
  102. $field_term_id = $this->getFieldTermID();
  103. $label_term = $field_term_id . ',' . 'rdfs:label';
  104. $infraspecific_name_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'infraspecific_name');
  105. $infraspecific_type_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'type_id');
  106. if ($condition['column'] == $label_term or $condition['column'] == $field_term_id) {
  107. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . "_cvterm.cvterm_id = base.type_id");
  108. $query->where("CONCAT(" . $alias . "_cvterm.name, ' ', base.infraspecific_name) $operator :full_name", [':full_name' => $condition['value']]);
  109. }
  110. if ($condition['column'] == $infraspecific_name_term) {
  111. $query->condition('base.infraspecific_name', $condition['value'], $operator);
  112. }
  113. if ($condition['column'] == $infraspecific_type_term) {
  114. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . "_cvterm.cvterm_id = base.type_id");
  115. $query->condition($alias . '_cvterm.name', $condition['value'], $operator);
  116. }
  117. }
  118. /**
  119. * @see ChadoField::queryOrder()
  120. */
  121. public function queryOrder($query, $order) {
  122. $alias = $this->field['field_name'];
  123. $field_term_id = $this->getFieldTermID();
  124. $label_term = $field_term_id . ',' . 'rdfs:label';
  125. $infraspecific_name_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'infraspecific_name');
  126. $infraspecific_type_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'type_id');
  127. if ($order['column'] == $infraspecific_name_term) {
  128. $query->orderBy('base.infraspecific_name', $order['direction']);
  129. }
  130. if ($order['column'] == $infraspecific_type_term) {
  131. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . "_cvterm.cvterm_id = base.type_id", "LEFT OUTER");
  132. $query->orderBy($alias . '_cvterm.name', $order['direction']);
  133. }
  134. }
  135. /**
  136. *
  137. * @see TripalField::load()
  138. */
  139. public function load($entity) {
  140. $record = $entity->chado_record;
  141. $settings = $this->field['settings'];
  142. $field_name = $this->field['field_name'];
  143. $field_type = $this->field['type'];
  144. $field_table = $this->instance['settings']['chado_table'];
  145. $field_column = $this->instance['settings']['chado_column'];
  146. $entity->{$field_name}['und'][0]['value'] = '';
  147. if (chado_get_version() < 1.3) {
  148. return;
  149. }
  150. $label_term = 'rdfs:label';
  151. $infraspecific_name_term = chado_get_semweb_term('organism', 'infraspecific_name');
  152. $infraspecific_type_term = chado_get_semweb_term('organism', 'type_id');
  153. // Set some defaults for the empty record.
  154. $entity->{$field_name}['und'][0] = [
  155. 'value' => [],
  156. 'chado-organism__infraspecific_name' => '',
  157. 'chado-organism__type_id' => '',
  158. ];
  159. if ($record->infraspecific_name) {
  160. $label = $record->type_id->name . ' ' . $record->infraspecific_name;
  161. $entity->{$field_name}['und'][0]['value'][$label_term] = $label;
  162. $entity->{$field_name}['und'][0]['value'][$infraspecific_name_term] = $record->infraspecific_name;
  163. $entity->{$field_name}['und'][0]['chado-organism__infraspecific_name'] = $record->infraspecific_name;
  164. }
  165. if ($record->type_id) {
  166. $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = $record->type_id->name;
  167. $entity->{$field_name}['und'][0]['chado-organism__type_id'] = $record->type_id->cvterm_id;
  168. }
  169. }
  170. }