chado_base__dbxref_id.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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' => '',
  59. 'accession' => '',
  60. 'ns' => '',
  61. 'nsurl' => '',
  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. $record = $entity->chado_record;
  97. foreach ($items as $delta => $item) {
  98. $accession = '';
  99. if ($item['value']) {
  100. $dbxref = $record->dbxref_id;
  101. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  102. if ($dbxref->db_id->urlprefix) {
  103. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession,
  104. array('attributes' => array('target' => '_blank')));
  105. }
  106. }
  107. $element[$delta] = array(
  108. '#type' => 'markup',
  109. '#markup' => $accession,
  110. );
  111. }
  112. }
  113. /**
  114. * @see TripalField::widget_form()
  115. */
  116. function widget_form(&$widget, $form, $form_state, $field, $instance,
  117. $langcode, $items, $delta, $element) {
  118. $field_name = $field['field_name'];
  119. // Get the field defaults.
  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. $dbxref_id = $items[$delta]['value'];
  129. $db_id = $items[$delta]['dbxref__db_id'];
  130. $accession = $items[$delta]['dbxref__accession'];
  131. $version = $items[$delta]['dbxref__version'];
  132. $description = $items[$delta]['dbxref__description'];
  133. }
  134. // Check $form_state['values'] to see if an AJAX call set the values.
  135. if (array_key_exists('values', $form_state)) {
  136. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_name);
  137. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
  138. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
  139. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
  140. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
  141. }
  142. // If we are here because our parent was triggered in a form submit
  143. // then that means an ajax call was made and we don't want the fieldset to
  144. // be closed when it returns from the ajax call.
  145. // $collapsed = TRUE;
  146. // if (array_key_exists('triggering_element', $form_state) and
  147. // $form_state['triggering_element']['#parents'][0] == $field_name) {
  148. // $collapsed = FALSE;
  149. // }
  150. // if ($dbxref_id) {
  151. // $collapsed = FALSE;
  152. // }
  153. $schema = chado_get_schema('dbxref');
  154. $options = tripal_get_db_select_options();
  155. $widget['#element_validate'] = array('chado_base__dbxref_id_widget_validate');
  156. $widget['#theme'] = 'chado_base__dbxref_id_widget';
  157. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  158. $widget['#suffix'] = "</span>";
  159. // A temporary element used for theming the fieldset.
  160. $widget['#theme_settings'] = array(
  161. '#title' => $element['#title'],
  162. '#description' => $element['#description'],
  163. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  164. '#theme' => 'chado_base__dbxref_id_widget',
  165. //'#collapsible' => TRUE,
  166. //'#collapsed' => $collapsed,
  167. );
  168. $widget['value'] = array(
  169. '#type' => 'value',
  170. '#default_value' => $dbxref_id,
  171. );
  172. $widget['dbxref__db_id'] = array(
  173. '#type' => 'select',
  174. '#title' => t('Database'),
  175. '#options' => $options,
  176. '#required' => $element['#required'],
  177. '#default_value' => $db_id,
  178. '#ajax' => array(
  179. 'callback' => "chado_base__dbxref_id_widget_form_ajax_callback",
  180. 'wrapper' => "$field_name-dbxref--db-id",
  181. 'effect' => 'fade',
  182. 'method' => 'replace'
  183. ),
  184. );
  185. $widget['dbxref__accession'] = array(
  186. '#type' => 'textfield',
  187. '#title' => t('Accession'),
  188. '#default_value' => $accession,
  189. '#required' => $element['#required'],
  190. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  191. '#size' => 15,
  192. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  193. '#ajax' => array(
  194. 'callback' => "tripal_chado_dbxref_widget_form_ajax_callback",
  195. 'wrapper' => "$field_name-dbxref--db-id",
  196. 'effect' => 'fade',
  197. 'method' => 'replace'
  198. ),
  199. '#disabled' => $db_id ? FALSE : TRUE,
  200. );
  201. $widget['dbxref__version'] = array(
  202. '#type' => 'textfield',
  203. '#title' => t('Version'),
  204. '#default_value' => $version,
  205. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  206. '#size' => 5,
  207. '#disabled' => $db_id ? FALSE : TRUE,
  208. );
  209. $widget['dbxref__description'] = array(
  210. '#type' => 'textfield',
  211. '#title' => t('Description'),
  212. '#default_value' => $description,
  213. '#size' => 20,
  214. '#disabled' => $db_id ? FALSE : TRUE,
  215. );
  216. $widget['links'] = array(
  217. '#type' => 'item',
  218. '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  219. );
  220. }
  221. /**
  222. * @see TripalField::load()
  223. */
  224. function load($field, $entity, $details) {
  225. $record = $details['record'];
  226. $field_name = $field['field_name'];
  227. $field_type = $field['type'];
  228. $field_table = $field['settings']['chado_table'];
  229. $field_column = $field['settings']['chado_column'];
  230. // Set some defauls for the empty record
  231. $entity->{$field_name}['und'][0] = array(
  232. 'value' => '',
  233. 'dbxref__db_id' => '',
  234. 'dbxref__accession' => '',
  235. 'dbxref__version' => '',
  236. 'dbxref__description' => '',
  237. );
  238. // Get the primary dbxref record (if it's not NULL). Because we have a
  239. // dbxref_id passed in by the base record, we will only have one record.
  240. if ($record->$field_column) {
  241. $columns = array('*');
  242. $match = array('dbxref_id' => $record->$field_column->$field_column);
  243. $options = array('return_array' => TRUE);
  244. $dbxrefs = chado_select_record('dbxref', $columns, $match, $options);
  245. if (count($dbxrefs) > 0) {
  246. $dbxref = $dbxrefs[0];
  247. $entity->{$field_name}['und'][0] = array(
  248. 'value' => $dbxref->dbxref_id,
  249. 'dbxref__db_id' => $dbxref->db_id,
  250. 'dbxref__accession' => $dbxref->accession,
  251. 'dbxref__version' => $dbxref->version,
  252. 'dbxref__description' => $dbxref->description,
  253. );
  254. }
  255. }
  256. }
  257. }
  258. /**
  259. * Callback function for validating the tripal_chado_dbxref_select_widget.
  260. */
  261. function chado_base__dbxref_id_widget_validate($element, &$form_state) {
  262. $field_name = $element['#parents'][0];
  263. // If the form ID is field_ui_field_edit_form, then the user is editing the
  264. // field's values in the manage fields form of Drupal. We don't want
  265. // to validate it as if it were being used in a data entry form.
  266. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  267. return;
  268. }
  269. // Get the field values.
  270. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_name);
  271. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
  272. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
  273. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
  274. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
  275. // Make sure that if a database ID is provided that an accession is also
  276. // provided. Here we use the form_set_error function rather than the
  277. // form_error function because the form_error will add a red_highlight
  278. // around all of the fields in the fieldset which is confusing as it's not
  279. // clear to the user what field is required and which isn't. Therefore,
  280. // we borrow the code from the 'form_error' function and append the field
  281. // so that the proper field is highlighted on error.
  282. if (!$db_id and $accession) {
  283. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  284. }
  285. if ($db_id and !$accession) {
  286. form_set_error(implode('][', $element ['#parents']) . '][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  287. }
  288. if (!$db_id and !$accession and ($version or $description)) {
  289. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  290. }
  291. // If user did not select a database, we want to remove dbxref_id from the
  292. // field.
  293. if (!$db_id) {
  294. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
  295. }
  296. // If the dbxref_id does not match the db_id + accession then the user
  297. // has selected a new dbxref record and we need to update the hidden
  298. // value accordingly.
  299. if ($db_id and $accession) {
  300. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  301. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  302. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id);
  303. }
  304. }
  305. }
  306. /**
  307. * An Ajax callback for the tripal_chado_admin_publish_form..
  308. */
  309. function chado_base__dbxref_id_widget_form_ajax_callback($form, $form_state) {
  310. $field_name = $form_state['triggering_element']['#parents'][0];
  311. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
  312. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__accession');
  313. if ($db_id and $accession) {
  314. $values = array(
  315. 'db_id' => $db_id,
  316. 'accession' => $accession,
  317. );
  318. $options = array('is_duplicate' => TRUE);
  319. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  320. if (!$has_duplicate) {
  321. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  322. }
  323. }
  324. return $form[$field_name];
  325. }
  326. function theme_chado_base__dbxref_id_widget($variables) {
  327. $element = $variables['element'];
  328. $layout = "
  329. <div class=\"primary-dbxref-widget\">
  330. <div class=\"primary-dbxref-widget-item\">" .
  331. drupal_render($element['dbxref__db_id']) . "
  332. </div>
  333. <div class=\"primary-dbxref-widget-item\">" .
  334. drupal_render($element['dbxref__accession']) . "
  335. </div>
  336. <div class=\"primary-dbxref-widget-item\">" .
  337. drupal_render($element['dbxref__version']) . "
  338. </div>
  339. <div class=\"primary-dbxref-widget-item\">" .
  340. drupal_render($element['dbxref__description']) . "
  341. </div>
  342. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  343. </div>
  344. ";
  345. // $classes = array();
  346. // $classes[] = 'collapsible';
  347. // $theme_settings = $element['#theme_settings'];
  348. // if ($theme_settings['#collapsed'] == FALSE) {
  349. // $classes[] = 'collapsed';
  350. // }
  351. $fieldset = array(
  352. '#title' => $element['#title'],
  353. '#value' => '',
  354. '#description' => $element['#description'],
  355. '#children' => $layout,
  356. // '#attributes' => array('class' => $classes),
  357. );
  358. return theme('fieldset', array('element' => $fieldset));
  359. }