chado_base__dbxref_id.inc 15 KB

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