chado_base__dbxref_id.inc 15 KB

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