obi__organism.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. // In order for this field to integrate with Drupal Views, a set of
  29. // handlers must be specififed. These include handlers for
  30. // the field, for the filter, and the sort. Within this variable,
  31. // the key must be one of: field, filter, sort; and the value
  32. // is the settings for those handlers as would be provided by
  33. // a hook_views_data(). The following defaults make a field visible
  34. // using the default formatter of the field, allow for filtering using
  35. // a string value and sortable. in order for filters to work you
  36. // must implement the query() function.
  37. public static $default_view_handlers = array(
  38. 'field' => array(
  39. 'handler' => 'tripal_views_handler_field',
  40. 'click sortable' => TRUE,
  41. ),
  42. 'filter' => array(
  43. 'handler' => 'tripal_views_handler_filter_string',
  44. ),
  45. 'sort' => array(
  46. 'handler' => 'views_handler_sort',
  47. ),
  48. );
  49. // The default widget for this field.
  50. public static $default_widget = 'OBI__organism_widget';
  51. // The default formatter for this field.
  52. public static $default_formatter = 'OBI__organism_formatter';
  53. /**
  54. * @see TripalField::validate()
  55. */
  56. public function validate($entity_type, $entity, $field, $items, &$errors) {
  57. $settings = $this->field['settings'];
  58. $field_name = $this->field['field_name'];
  59. $field_type = $this->field['type'];
  60. $field_table = $this->instance['settings']['chado_table'];
  61. $field_column = $this->instance['settings']['chado_column'];
  62. // Get the field values.
  63. foreach ($items as $delta => $values) {
  64. // Get the field values.
  65. $organism_id = $values['chado-' . $field_table . '__organism_id'];
  66. if (!$organism_id or $organism_id == 0) {
  67. $errors[$field_name]['und'][0][] = array(
  68. 'message' => t("Please specify an organism."),
  69. 'error' => 'chado_base__organism_id'
  70. );
  71. }
  72. }
  73. }
  74. /**
  75. * @see TripalField::load()
  76. */
  77. public function load($entity) {
  78. $record = $entity->chado_record;
  79. $settings = $this->instance['settings'];
  80. $field_name = $this->field['field_name'];
  81. $field_type = $this->field['type'];
  82. $field_table = $this->instance['settings']['chado_table'];
  83. $field_column = $this->instance['settings']['chado_column'];
  84. // Get the terms for each of the keys for the 'values' property.
  85. $label_term = 'rdfs:label';
  86. $genus_term = tripal_get_chado_semweb_term('organism', 'genus');
  87. $species_term = tripal_get_chado_semweb_term('organism', 'species');
  88. $infraspecific_name_term = tripal_get_chado_semweb_term('organism', 'infraspecific_name');
  89. $infraspecific_type_term = tripal_get_chado_semweb_term('organism', 'type_id');
  90. // Set some defaults for the empty record.
  91. $entity->{$field_name}['und'][0] = array(
  92. 'value' => array(
  93. /*
  94. // Types of elements that will appear in the value array.
  95. $label_term => '',
  96. $genus_term => '',
  97. $species_term => '',
  98. */
  99. ),
  100. );
  101. if ($record) {
  102. $organism = $record->organism_id;
  103. $string = $settings['field_display_string'];
  104. $label = tripal_replace_chado_tokens($string, $organism);
  105. $entity->{$field_name}['und'][0]['value'] = array(
  106. $label_term => $label,
  107. $genus_term => $organism->genus,
  108. $species_term => $organism->species,
  109. );
  110. // The infraspecific fiels were introdcued in Chado v1.3.
  111. if (property_exists($organism, 'infraspecific_name')) {
  112. $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = NULL;
  113. $entity->{$field_name}['und'][0]['value'][$infraspecific_name_term] = $organism->infraspecific_name;
  114. if ($organism->type_id) {
  115. $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = $organism->type_id->name;
  116. }
  117. }
  118. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__organism_id'] = $organism->organism_id;
  119. // Is there a published entity for this organism?
  120. if (property_exists($record->{$field_column}, 'entity_id')) {
  121. $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $record->{$field_column}->entity_id;
  122. }
  123. }
  124. }
  125. /**
  126. * @see TripalField::globalSettingsForm()
  127. */
  128. public function settingsForm($has_data) {
  129. $element = parent::globalSettingsForm($has_data);
  130. $settings = $this->field['settings'];
  131. $element['instructions'] = array(
  132. '#type' => 'item',
  133. '#markup' => 'You may rewrite the way this field is presented to the end-user.
  134. The Rewrite Value field allows you to use tokens to indicate how the
  135. value should be displayed. Tokens will be substituted with appriorate
  136. data from the database. See the Available tokens list for the
  137. tokens you may use.'
  138. );
  139. $element['field_display_string'] = array(
  140. '#type' => 'textfield',
  141. '#title' => 'Rewrite Value',
  142. '#description' => t('Provide a mixture of text and/or tokens for the format.
  143. For example: [organism.genus] [organism.species]. When displayed,
  144. the tokens will be replaced with the actual value.'),
  145. '#default_value' => $settings['field_display_string'],
  146. );
  147. $element['tokens'] = array(
  148. '#type' => 'fieldset',
  149. '#collapsed' => TRUE,
  150. '#collapsible' => TRUE,
  151. '#title' => 'Available Tokens'
  152. );
  153. $headers = array('Token', 'Description');
  154. $rows = array();
  155. // Here we use the tripal_get_chado_tokens rather than the
  156. // tripal_get_entity_tokens because we can't gurantee that all organisms
  157. // have entities.
  158. $tokens = tripal_get_chado_tokens('organism');
  159. foreach ($tokens as $token) {
  160. $rows[] = array(
  161. $token['token'],
  162. $token['description'],
  163. );
  164. }
  165. $table_vars = array(
  166. 'header' => $headers,
  167. 'rows' => $rows,
  168. 'attributes' => array(),
  169. 'sticky' => FALSE,
  170. 'caption' => '',
  171. 'colgroups' => array(),
  172. 'empty' => 'There are no tokens',
  173. );
  174. $element['tokens']['list'] = array(
  175. '#type' => 'item',
  176. '#markup' => theme_table($table_vars),
  177. );
  178. // Add in the semantic web fields.
  179. $parent_elements = parent::settings_form($field, $instance, $has_data);
  180. $element = array_merge($element, $parent_elements);
  181. return $element;
  182. }
  183. /**
  184. * @see TripalField::elementInfo()
  185. */
  186. public function elementInfo() {
  187. $field_term = $this->getFieldTermID();
  188. $genus_term = tripal_get_chado_semweb_term('organism', 'genus');
  189. $species_term = tripal_get_chado_semweb_term('organism', 'species');
  190. $infraspecific_name_term = tripal_get_chado_semweb_term('organism', 'infraspecific_name');
  191. $infraspecific_type_term = tripal_get_chado_semweb_term('organism', 'type_id');
  192. return array(
  193. $field_term => array(
  194. 'operations' => array('eq', 'contains', 'starts'),
  195. 'sortable' => TRUE,
  196. 'searchable' => TRUE,
  197. 'type' => 'string',
  198. 'elements' => array(
  199. 'rdfs:label' => array(
  200. 'searchable' => TRUE,
  201. 'name' => 'scientfic_name',
  202. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  203. 'sortable' => TRUE,
  204. ),
  205. $genus_term => array(
  206. 'searchable' => TRUE,
  207. 'name' => 'genus',
  208. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  209. 'sortable' => TRUE,
  210. ),
  211. $species_term => array(
  212. 'searchable' => TRUE,
  213. 'name' => 'species',
  214. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  215. 'sortable' => TRUE,
  216. ),
  217. $infraspecific_name_term => array(
  218. 'searchable' => TRUE,
  219. 'name' => 'infraspecies',
  220. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  221. 'sortable' => TRUE,
  222. ),
  223. $infraspecific_type_term => array(
  224. 'searchable' => TRUE,
  225. 'name' => 'infraspecific_type',
  226. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  227. 'sortable' => TRUE,
  228. ),
  229. 'entity' => array(
  230. 'searchable' => FALSE,
  231. ),
  232. ),
  233. )
  234. );
  235. }
  236. /**
  237. * @see ChadoField::query()
  238. */
  239. public function query($query, $condition) {
  240. $alias = $this->field['field_name'];
  241. $operator = $condition['operator'];
  242. dpm($condition);
  243. $field_term_id = $this->getFieldTermID();
  244. $genus_term = $field_term_id . ',' . tripal_get_chado_semweb_term('organism', 'genus');
  245. $species_term = $field_term_id . ',' . tripal_get_chado_semweb_term('organism', 'species');
  246. $infraspecific_name_term = $field_term_id . ',' . tripal_get_chado_semweb_term('organism', 'infraspecific_name');
  247. $infraspecific_type_term = $field_term_id . ',' . tripal_get_chado_semweb_term('organism', 'type_id');
  248. $query->join('organism', $alias, "base.organism_id = $alias.organism_id");
  249. // If the column is the field name then we're during a search on the full
  250. // scientific name.
  251. if ($condition['column'] == $field_term_id or
  252. $condition['column'] == $field_term_id . ',rdfs:label') {
  253. if (chado_get_version() <= 1.3) {
  254. $query->where("CONCAT($alias.genus, ' ', $alias.species) $operator :full_name", array(':full_name' => $condition['value']));
  255. }
  256. else {
  257. $query->leftJoin('cvterm', $alias . '_cvterm', 'base.infraspecific_type = ' . $alias . '_cvterm.type_id');
  258. $query->where("CONCAT($alias.genus, ' ', $alias.species, ' ', " . $alias . "'_cvterm.name', ' ', $alias.infraspecific_name) $operator :full_name", array(':full_name' => $condition['value']));
  259. }
  260. }
  261. // If the column is a subfield.
  262. if ($condition['column'] == $species_term) {
  263. $query->condition("$alias.species", $condition['value'], $operator);
  264. }
  265. if ($condition['column'] == $genus_term) {
  266. $query->condition("$alias.genus", $condition['value'], $operator);
  267. }
  268. if ($condition['column'] == $infraspecific_name_term) {
  269. $query->condition("$alias.infraspecific_name", $condition['value'], $operator);
  270. }
  271. if ($condition['column'] == $infraspecific_type_term) {
  272. $query->join('cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  273. $query->condition("CVT.name", $condition['value'], $operator);
  274. }
  275. }
  276. /**
  277. * @see ChadoField::queryOrder()
  278. */
  279. public function queryOrder($query, $order) {
  280. // If the table hasn't yet been joined then add it.
  281. $joins = $query->getTables();
  282. if (!in_array($this->field['field_name'], $joins)) {
  283. $alias = $this->field['field_name'];
  284. $query->join('organism', $alias, "base.organism_id = $alias.organism_id");
  285. }
  286. // Now perform the sort.
  287. if ($order['column'] == 'organism.species') {
  288. $query->orderBy("$alias.genus", $order['direction']);
  289. }
  290. if ($order['column'] == 'organism.genus') {
  291. $query->orderBy("$alias.species", $order['direction']);
  292. }
  293. if ($order['column'] == 'organism.infraspecies') {
  294. $query->orderBy("$alias.infraspecific_name", $order['direction']);
  295. }
  296. if ($order['column'] == 'organism.infraspecies') {
  297. if (!in_array('CVT', $joins)) {
  298. $query->join('cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  299. }
  300. $query->orderBy("CVT.name", $order['direction']);
  301. }
  302. }
  303. }