chado_base__dbxref_id.inc 15 KB

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