chado_base__dbxref_id.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. class chado_base__dbxref_id extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Cross reference'),
  9. 'description' => t('This record can be cross referenced with a record in
  10. another online database. This field is intended for the most prominent
  11. reference. At a minimum, the database and accession must be provided.'),
  12. 'default_widget' => 'chado_base__dbxref_id_widget',
  13. 'default_formatter' => 'chado_base__dbxref_id_formatter',
  14. 'settings' => array(),
  15. 'storage' => array(
  16. 'type' => 'field_chado_storage',
  17. 'module' => 'tripal_chado',
  18. 'active' => TRUE
  19. ),
  20. );
  21. }
  22. /**
  23. * @see TripalField::attach_info()
  24. */
  25. function attach_info($entity_type, $bundle, $settings) {
  26. $field_info = array();
  27. $table_name = $settings['data_table'];
  28. $type_table = $settings['type_table'];
  29. $type_field = $settings['field'];
  30. $cv_id = $settings['cv_id'];
  31. $cvterm_id = $settings['cvterm_id'];
  32. // Check the schema for the data table if it does not have
  33. // a 'dbxref_id' column then we don't want to attach this field.
  34. $schema = chado_get_schema($table_name);
  35. if (!array_key_exists('dbxref_id', $schema['fields'])) {
  36. return $field_info;
  37. }
  38. // There is an dbxref_id column so attach the field!
  39. $field_info = array(
  40. 'field_name' => $table_name . '__dbxref_id',
  41. 'field_type' => 'chado_base__dbxref_id',
  42. 'widget_type' => 'chado_base__dbxref_id_widget',
  43. 'description' => 'This record can be cross referenced with a ' .
  44. 'record in another online database. The primary reference is for the ' .
  45. 'most prominent reference. At a minimum, the database and accession ' .
  46. 'must be provided. To remove a set reference, change the database ' .
  47. 'field to "Select a Database".',
  48. 'label' => 'Cross Reference',
  49. 'is_required' => 0,
  50. 'storage' => 'field_chado_storage',
  51. 'widget_settings' => array(
  52. 'display_label' => 1
  53. ),
  54. 'field_settings' => array(
  55. 'chado_table' => $table_name,
  56. 'chado_column' => 'dbxref_id',
  57. 'semantic_web' => array(
  58. 'name' => 'database cross reference',
  59. 'accession' => '0000554',
  60. 'ns' => 'SBO',
  61. 'nsurl' => 'http://www.ebi.ac.uk/sbo/main/',
  62. ),
  63. ),
  64. );
  65. return $field_info;
  66. }
  67. /**
  68. * @see TripalField::widget_info()
  69. */
  70. function widget_info() {
  71. return array(
  72. 'label' => t('Cross reference'),
  73. 'field types' => array('chado_base__dbxref_id'),
  74. 'description' => t('This record can be cross referenced with a record in
  75. another online database. This field is intended for the most
  76. prominent reference. At a minimum, the database and accession
  77. must be provided.'),
  78. );
  79. }
  80. /**
  81. * @see TripalField::formatter_info()
  82. */
  83. function formatter_info() {
  84. return array(
  85. 'label' => t('Database Cross Reference'),
  86. 'field types' => array('chado_base__dbxref_id'),
  87. 'settings' => array(
  88. ),
  89. );
  90. }
  91. /**
  92. * @see TripalField::formatter_view()
  93. */
  94. function formatter_view(&$element, $entity_type, $entity, $field,
  95. $instance, $langcode, $items, $display) {
  96. foreach ($items as $delta => $item) {
  97. if ($item['value']) {
  98. $content = $item['value']['namespace'] . ':' . $item['value']['accession'];
  99. if ($item['value']['URL']) {
  100. $content = l($item['value']['URL'], $item['value']['URL']);
  101. }
  102. $element[$delta] = array(
  103. '#type' => 'markup',
  104. '#markup' => $content,
  105. );
  106. }
  107. }
  108. }
  109. /**
  110. * @see TripalField::widget_form()
  111. */
  112. function widget_form(&$widget, $form, $form_state, $field, $instance,
  113. $langcode, $items, $delta, $element) {
  114. $field_name = $field['field_name'];
  115. $field_type = $field['type'];
  116. $field_table = $field['settings']['chado_table'];
  117. $field_column = $field['settings']['chado_column'];
  118. // Get the field defaults.
  119. $fk_val = '';
  120. $dbxref_id = '';
  121. $db_id = '';
  122. $accession = '';
  123. $version = '';
  124. $description = '';
  125. // If the field already has a value then it will come through the $items
  126. // array. This happens when editing an existing record.
  127. if (array_key_exists($delta, $items)) {
  128. $fk_val = $items[$delta][$field_table . '__' . $field_column];
  129. $dbxref_id = $items[$delta]['dbxref__dbxref_id'];
  130. $db_id = $items[$delta]['dbxref__db_id'];
  131. $accession = $items[$delta]['dbxref__accession'];
  132. $version = $items[$delta]['dbxref__version'];
  133. $description = $items[$delta]['dbxref__description'];
  134. }
  135. // Check $form_state['values'] to see if an AJAX call set the values.
  136. if (array_key_exists('values', $form_state)) {
  137. $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column);
  138. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__dbxref_id');
  139. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
  140. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
  141. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
  142. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
  143. }
  144. // If we are here because our parent was triggered in a form submit
  145. // then that means an ajax call was made and we don't want the fieldset to
  146. // be closed when it returns from the ajax call.
  147. // $collapsed = TRUE;
  148. // if (array_key_exists('triggering_element', $form_state) and
  149. // $form_state['triggering_element']['#parents'][0] == $field_name) {
  150. // $collapsed = FALSE;
  151. // }
  152. // if ($dbxref_id) {
  153. // $collapsed = FALSE;
  154. // }
  155. $schema = chado_get_schema('dbxref');
  156. $options = tripal_get_db_select_options();
  157. $widget['#element_validate'] = array('chado_base__dbxref_id_widget_validate');
  158. $widget['#theme'] = 'chado_base__dbxref_id_widget';
  159. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  160. $widget['#suffix'] = "</span>";
  161. // A temporary element used for theming the fieldset.
  162. $widget['#theme_settings'] = array(
  163. '#title' => $element['#title'],
  164. '#description' => $element['#description'],
  165. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  166. '#theme' => 'chado_base__dbxref_id_widget',
  167. //'#collapsible' => TRUE,
  168. //'#collapsed' => $collapsed,
  169. );
  170. $widget['value'] = array(
  171. '#type' => 'value',
  172. '#value' => $items[0]['value'],
  173. );
  174. $widget[$field_table . '__' . $field_column] = array(
  175. '#type' => 'value',
  176. '#default_value' => $fk_val,
  177. );
  178. $widget['dbxref__dbxref_id'] = array(
  179. '#type' => 'value',
  180. '#default_value' => $dbxref_id,
  181. );
  182. $widget['dbxref__db_id'] = array(
  183. '#type' => 'select',
  184. '#title' => t('Database'),
  185. '#options' => $options,
  186. '#required' => $element['#required'],
  187. '#default_value' => $db_id,
  188. '#ajax' => array(
  189. 'callback' => "chado_base__dbxref_id_widget_form_ajax_callback",
  190. 'wrapper' => "$field_name-dbxref--db-id",
  191. 'effect' => 'fade',
  192. 'method' => 'replace'
  193. ),
  194. );
  195. $widget['dbxref__accession'] = array(
  196. '#type' => 'textfield',
  197. '#title' => t('Accession'),
  198. '#default_value' => $accession,
  199. '#required' => $element['#required'],
  200. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  201. '#size' => 15,
  202. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  203. '#ajax' => array(
  204. 'callback' => "tripal_chado_dbxref_widget_form_ajax_callback",
  205. 'wrapper' => "$field_name-dbxref--db-id",
  206. 'effect' => 'fade',
  207. 'method' => 'replace'
  208. ),
  209. '#disabled' => $db_id ? FALSE : TRUE,
  210. );
  211. $widget['dbxref__version'] = array(
  212. '#type' => 'textfield',
  213. '#title' => t('Version'),
  214. '#default_value' => $version,
  215. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  216. '#size' => 5,
  217. '#disabled' => $db_id ? FALSE : TRUE,
  218. );
  219. $widget['dbxref__description'] = array(
  220. '#type' => 'textfield',
  221. '#title' => t('Description'),
  222. '#default_value' => $description,
  223. '#size' => 20,
  224. '#disabled' => $db_id ? FALSE : TRUE,
  225. );
  226. // $widget['links'] = array(
  227. // '#type' => 'item',
  228. // '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  229. // );
  230. }
  231. /**
  232. * @see TripalField::load()
  233. */
  234. function load($field, $entity, $details) {
  235. $record = $details['record'];
  236. $field_name = $field['field_name'];
  237. $field_type = $field['type'];
  238. $field_table = $field['settings']['chado_table'];
  239. $field_column = $field['settings']['chado_column'];
  240. // Set some defauls for the empty record
  241. $entity->{$field_name}['und'][0] = array(
  242. 'value' => array(),
  243. $field_table . '__' . $field_column => '',
  244. 'dbxref__dbxref_id' => '',
  245. 'dbxref__db_id' => '',
  246. 'dbxref__accession' => '',
  247. 'dbxref__version' => '',
  248. 'dbxref__description' => '',
  249. );
  250. // Get the primary dbxref record (if it's not NULL). Because we have a
  251. // dbxref_id passed in by the base record, we will only have one record.
  252. if ($record->$field_column) {
  253. $dbxref = $record->$field_column;
  254. $value = $dbxref->db_id->name . ':' . $dbxref->accession;
  255. $URL = '';
  256. if ($dbxref->db_id->urlprefix) {
  257. $URL = l($value, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  258. }
  259. $entity->{$field_name}['und'][0] = array(
  260. 'value' => array(
  261. 'namespace' => $dbxref->db_id->name,
  262. 'accession' => $dbxref->accession,
  263. 'URL' => $URL,
  264. ),
  265. $field_table . '__' . $field_column => $record->$field_column->$field_column,
  266. 'dbxref__dbxref_id' => $dbxref->dbxref_id,
  267. 'dbxref__db_id' => $dbxref->db_id->db_id,
  268. 'dbxref__accession' => $dbxref->accession,
  269. 'dbxref__version' => $dbxref->version,
  270. 'dbxref__description' => $dbxref->description,
  271. );
  272. }
  273. }
  274. }
  275. /**
  276. * Callback function for validating the tripal_chado_dbxref_select_widget.
  277. */
  278. function chado_base__dbxref_id_widget_validate($element, &$form_state) {
  279. $field_name = $element['#parents'][0];
  280. $field = $form_state['field'][$field_name]['und']['field'];
  281. $settings = $field['settings'];
  282. $field_name = $field['field_name'];
  283. $field_type = $field['type'];
  284. $field_table = $field['settings']['chado_table'];
  285. $field_column = $field['settings']['chado_column'];
  286. // If the form ID is field_ui_field_edit_form, then the user is editing the
  287. // field's values in the manage fields form of Drupal. We don't want
  288. // to validate it as if it were being used in a data entry form.
  289. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  290. return;
  291. }
  292. // Get the field values.
  293. $fk_value = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column);
  294. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__dbxref_id');
  295. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
  296. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
  297. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
  298. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
  299. // Make sure that if a database ID is provided that an accession is also
  300. // provided. Here we use the form_set_error function rather than the
  301. // form_error function because the form_error will add a red_highlight
  302. // around all of the fields in the fieldset which is confusing as it's not
  303. // clear to the user what field is required and which isn't. Therefore,
  304. // we borrow the code from the 'form_error' function and append the field
  305. // so that the proper field is highlighted on error.
  306. if (!$db_id and $accession) {
  307. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  308. }
  309. if ($db_id and !$accession) {
  310. form_set_error(implode('][', $element ['#parents']) . '][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  311. }
  312. if (!$db_id and !$accession and ($version or $description)) {
  313. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  314. }
  315. // If user did not select a database, we want to remove dbxref_id from the
  316. // field.
  317. if (!$db_id) {
  318. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, 'dbxref__dbxref_id');
  319. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_table . '__' . $field_column);
  320. }
  321. // If the dbxref_id does not match the db_id + accession then the user
  322. // has selected a new dbxref record and we need to update the hidden
  323. // value accordingly.
  324. if ($db_id and $accession) {
  325. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  326. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  327. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, 0, $field_table . '__' . $field_column);
  328. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, 0, 'dbxref__dbxref_id');
  329. }
  330. }
  331. }
  332. /**
  333. * An Ajax callback for the tripal_chado_admin_publish_form..
  334. */
  335. function chado_base__dbxref_id_widget_form_ajax_callback($form, $form_state) {
  336. $field_name = $form_state['triggering_element']['#parents'][0];
  337. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
  338. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__accession');
  339. if ($db_id and $accession) {
  340. $values = array(
  341. 'db_id' => $db_id,
  342. 'accession' => $accession,
  343. );
  344. $options = array('is_duplicate' => TRUE);
  345. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  346. if (!$has_duplicate) {
  347. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  348. }
  349. }
  350. return $form[$field_name];
  351. }