obi__organism.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. class obi__organism extends ChadoField {
  3. // The default lable for this field.
  4. public static $default_label = 'Organism';
  5. // The default description for this field.
  6. public static $description = 'The organism to which this resource is associated.';
  7. // Provide a list of instance specific settings. These can be access within
  8. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  9. // then Drupal with automatically change these settings for the instnace.
  10. // It is recommended to put settings at the instance level whenever possible.
  11. // If you override this variable in a child class be sure to replicate the
  12. // term_name, term_vocab, term_accession and term_fixed keys as these are
  13. // required for all TripalFields.
  14. public static $default_instance_settings = array(
  15. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  16. 'term_vocabulary' => 'OBI',
  17. // The name of the term.
  18. 'term_name' => 'organism',
  19. // The unique ID (i.e. accession) of the term.
  20. 'term_accession' => '0100026',
  21. // Set to TRUE if the site admin is allowed to change the term
  22. // type. This will create form elements when editing the field instance
  23. // to allow the site admin to change the term settings above.
  24. 'term_fixed' => FALSE,
  25. // The format for display of the organism.
  26. 'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
  27. );
  28. // The default widget for this field.
  29. public static $default_widget = 'OBI__organism_widget';
  30. // The default formatter for this field.
  31. public static $default_formatter = 'OBI__organism_formatter';
  32. /**
  33. * @see TripalField::validate()
  34. */
  35. public function validate($entity_type, $entity, $langcode, $items, &$errors) {
  36. // If we don't have an entity then we don't want to validate. The case
  37. // where this could happen is when a user is editing the field settings
  38. // and trying to set a default value. In that case there's no entity and
  39. // we don't want to validate. There will always be an entity for creation
  40. // and update operations of a content type.
  41. if (!$entity) {
  42. return;
  43. }
  44. $settings = $this->field['settings'];
  45. $field_name = $this->field['field_name'];
  46. $field_type = $this->field['type'];
  47. $field_table = $this->instance['settings']['chado_table'];
  48. $field_column = $this->instance['settings']['chado_column'];
  49. // Set the linker field appropriately.
  50. if ($field_table == 'biomaterial') {
  51. $linker_field = 'chado-biomaterial__taxon_id';
  52. }
  53. else {
  54. $linker_field = 'chado-' . $field_table . '__organism_id';
  55. }
  56. // Get the field values.
  57. foreach ($items as $delta => $values) {
  58. // Get the field values.
  59. $organism_id = $values[$linker_field];
  60. if (!$organism_id or $organism_id == 0) {
  61. $errors[$field_name]['und'][0][] = array(
  62. 'message' => t("Please specify an organism."),
  63. 'error' => 'obi__organism_id'
  64. );
  65. }
  66. }
  67. }
  68. /**
  69. * @see TripalField::load()
  70. */
  71. public function load($entity) {
  72. $record = $entity->chado_record;
  73. $settings = $this->instance['settings'];
  74. $field_name = $this->field['field_name'];
  75. $field_type = $this->field['type'];
  76. $field_table = $this->instance['settings']['chado_table'];
  77. $field_column = $this->instance['settings']['chado_column'];
  78. // Get the terms for each of the keys for the 'values' property.
  79. $label_term = 'rdfs:label';
  80. $genus_term = chado_get_semweb_term('organism', 'genus');
  81. $species_term = chado_get_semweb_term('organism', 'species');
  82. $infraspecific_name_term = chado_get_semweb_term('organism', 'infraspecific_name');
  83. $infraspecific_type_term = chado_get_semweb_term('organism', 'type_id');
  84. // Set the linker field appropriately.
  85. if ($field_table == 'biomaterial') {
  86. $linker_field = 'chado-biomaterial__taxon_id';
  87. }
  88. else {
  89. $linker_field = 'chado-' . $field_table . '__organism_id';
  90. }
  91. // Set some defaults for the empty record.
  92. $entity->{$field_name}['und'][0] = array(
  93. 'value' => array(),
  94. );
  95. if ($record) {
  96. if ($field_table == 'biomaterial') {
  97. $organism = $record->taxon_id;
  98. }
  99. else {
  100. $organism = $record->organism_id;
  101. }
  102. $string = $settings['field_display_string'];
  103. $label = chado_replace_tokens($string, $organism);
  104. $entity->{$field_name}['und'][0]['value'] = array(
  105. $label_term => $label,
  106. $genus_term => $organism->genus,
  107. $species_term => $organism->species,
  108. );
  109. // The infraspecific fields were introduced in Chado v1.3.
  110. if (property_exists($organism, 'infraspecific_name')) {
  111. $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = NULL;
  112. $entity->{$field_name}['und'][0]['value'][$infraspecific_name_term] = $organism->infraspecific_name;
  113. if ($organism->type_id) {
  114. $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = $organism->type_id->name;
  115. }
  116. }
  117. $entity->{$field_name}['und'][0][$linker_field] = $organism->organism_id;
  118. // Is there a published entity for this organism?
  119. if (property_exists($record->{$field_column}, 'entity_id')) {
  120. $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $record->{$field_column}->entity_id;
  121. }
  122. }
  123. }
  124. /**
  125. * @see TripalField::globalSettingsForm()
  126. */
  127. public function instanceSettingsForm() {
  128. $element = parent::instanceSettingsForm();
  129. $settings = $this->instance['settings'];
  130. $element['instructions'] = array(
  131. '#type' => 'item',
  132. '#markup' => 'You may rewrite the way this field is presented to the end-user.
  133. The Rewrite Value field allows you to use tokens to indicate how the
  134. value should be displayed. Tokens will be substituted with appriorate
  135. data from the database. See the Available tokens list for the
  136. tokens you may use.'
  137. );
  138. $element['field_display_string'] = array(
  139. '#type' => 'textfield',
  140. '#title' => 'Rewrite Value',
  141. '#description' => t('Provide a mixture of text and/or tokens for the format.
  142. For example: [organism.genus] [organism.species]. When displayed,
  143. the tokens will be replaced with the actual value.'),
  144. '#default_value' => $settings['field_display_string'],
  145. );
  146. $element['tokens'] = array(
  147. '#type' => 'fieldset',
  148. '#collapsed' => TRUE,
  149. '#collapsible' => TRUE,
  150. '#title' => 'Available Tokens'
  151. );
  152. $headers = array('Token', 'Description');
  153. $rows = array();
  154. // Here we use the chado_get_tokens rather than the
  155. // tripal_get_entity_tokens because we can't gurantee that all organisms
  156. // have entities.
  157. $tokens = chado_get_tokens('organism');
  158. foreach ($tokens as $token) {
  159. $rows[] = array(
  160. $token['token'],
  161. $token['description'],
  162. );
  163. }
  164. $table_vars = array(
  165. 'header' => $headers,
  166. 'rows' => $rows,
  167. 'attributes' => array(),
  168. 'sticky' => FALSE,
  169. 'caption' => '',
  170. 'colgroups' => array(),
  171. 'empty' => 'There are no tokens',
  172. );
  173. $element['tokens']['list'] = array(
  174. '#type' => 'item',
  175. '#markup' => theme_table($table_vars),
  176. );
  177. return $element;
  178. }
  179. /**
  180. * @see TripalField::elementInfo()
  181. */
  182. public function elementInfo() {
  183. $field_term = $this->getFieldTermID();
  184. $genus_term = chado_get_semweb_term('organism', 'genus');
  185. $species_term = chado_get_semweb_term('organism', 'species');
  186. $infraspecific_name_term = chado_get_semweb_term('organism', 'infraspecific_name');
  187. $infraspecific_type_term = chado_get_semweb_term('organism', 'type_id');
  188. return array(
  189. $field_term => array(
  190. 'operations' => array('eq', 'contains', 'starts'),
  191. 'sortable' => TRUE,
  192. 'searchable' => TRUE,
  193. 'readonly' => FALSE,
  194. 'type' => 'xs:complexType',
  195. 'elements' => array(
  196. 'rdfs:label' => array(
  197. 'searchable' => TRUE,
  198. 'name' => 'scientific_name',
  199. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  200. 'sortable' => FALSE,
  201. 'type' => 'xs:string',
  202. 'readonly' => TRUE,
  203. 'required' => FALSE,
  204. ),
  205. $genus_term => array(
  206. 'searchable' => TRUE,
  207. 'name' => 'genus',
  208. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  209. 'sortable' => TRUE,
  210. 'readonly' => FALSE,
  211. 'type' => 'xs:string',
  212. 'required' => TRUE,
  213. ),
  214. $species_term => array(
  215. 'searchable' => TRUE,
  216. 'name' => 'species',
  217. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  218. 'sortable' => TRUE,
  219. 'readonly' => FALSE,
  220. 'type' => 'xs:string',
  221. 'required' => TRUE,
  222. ),
  223. $infraspecific_name_term => array(
  224. 'searchable' => TRUE,
  225. 'name' => 'infraspecies',
  226. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  227. 'sortable' => TRUE,
  228. 'readonly' => FALSE,
  229. 'type' => 'xs:string',
  230. 'required' => FALSE,
  231. ),
  232. $infraspecific_type_term => array(
  233. 'searchable' => TRUE,
  234. 'name' => 'infraspecific_type',
  235. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  236. 'sortable' => TRUE,
  237. 'readonly' => FALSE,
  238. 'type' => 'xs:integer',
  239. 'required' => FALSE,
  240. ),
  241. 'entity' => array(
  242. 'searchable' => FALSE,
  243. ),
  244. ),
  245. ),
  246. );
  247. }
  248. /**
  249. * @see ChadoField::query()
  250. */
  251. public function query($query, $condition) {
  252. $alias = $this->field['field_name'];
  253. $operator = $condition['operator'];
  254. $field_term_id = $this->getFieldTermID();
  255. $genus_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'genus');
  256. $species_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'species');
  257. $infraspecific_name_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'infraspecific_name');
  258. $infraspecific_type_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'type_id');
  259. // Join to the organism table for this field.
  260. $this->queryJoinOnce($query, 'organism', $alias, "base.organism_id = $alias.organism_id");
  261. // If the column is the field name then we're during a search on the full
  262. // scientific name.
  263. if ($condition['column'] == $field_term_id or
  264. $condition['column'] == $field_term_id . ',rdfs:label') {
  265. if (chado_get_version() <= 1.3) {
  266. $query->where("CONCAT($alias.genus, ' ', $alias.species) $operator :full_name", array(':full_name' => $condition['value']));
  267. }
  268. else {
  269. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', 'base.infraspecific_type = ' . $alias . '_cvterm.type_id', 'LEFT OUTER');
  270. $query->where("CONCAT($alias.genus, ' ', $alias.species, ' ', " . $alias . "'_cvterm.name', ' ', $alias.infraspecific_name) $operator :full_name", array(':full_name' => $condition['value']));
  271. }
  272. }
  273. // If the column is a subfield.
  274. if ($condition['column'] == $species_term) {
  275. $query->condition("$alias.species", $condition['value'], $operator);
  276. }
  277. if ($condition['column'] == $genus_term) {
  278. $query->condition("$alias.genus", $condition['value'], $operator);
  279. }
  280. if ($condition['column'] == $infraspecific_name_term) {
  281. $query->condition("$alias.infraspecific_name", $condition['value'], $operator);
  282. }
  283. if ($condition['column'] == $infraspecific_type_term) {
  284. $this->queryJoinOnce($query, 'cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  285. $query->condition("CVT.name", $condition['value'], $operator);
  286. }
  287. }
  288. /**
  289. * @see ChadoField::queryOrder()
  290. */
  291. public function queryOrder($query, $order) {
  292. $alias = $this->field['field_name'];
  293. $field_term_id = $this->getFieldTermID();
  294. $genus_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'genus');
  295. $species_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'species');
  296. $infraspecific_name_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'infraspecific_name');
  297. $infraspecific_type_term = $field_term_id . ',' . chado_get_semweb_term('organism', 'type_id');
  298. // Join to the organism table for this field.
  299. $this->queryJoinOnce($query, 'organism', $alias, "base.organism_id = $alias.organism_id");
  300. // Now perform the sort.
  301. if ($order['column'] == $species_term) {
  302. $query->orderBy("$alias.species", $order['direction']);
  303. }
  304. if ($order['column'] == $genus_term) {
  305. $query->orderBy("$alias.genus", $order['direction']);
  306. }
  307. if ($order['column'] == $infraspecific_name_term) {
  308. $query->orderBy("$alias.infraspecific_name", $order['direction']);
  309. }
  310. if ($order['column'] == $infraspecific_type_term) {
  311. if (!in_array('CVT', $joins)) {
  312. $this->queryJoinOnce($query, 'cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  313. }
  314. $query->orderBy("CVT.name", $order['direction']);
  315. }
  316. }
  317. }