chado_linker__dbxref.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * This class provides fields for adding database cross-references.
  4. *
  5. * It supports two types of cross-references. The primary cross reference
  6. * which is usually in the base table as an FK to the dbxref table, and
  7. * secondary cross references which are in a linking table that connects
  8. * the base table with the dbxref table.
  9. *
  10. */
  11. class chado_linker__dbxref extends TripalField {
  12. // The default lable for this field.
  13. public static $default_label = 'Cross references';
  14. // The default description for this field.
  15. public static $default_description = 'This record can be cross referenced with a record in
  16. another online database. This field is intended for one or more
  17. references. At a minimum, the database and accession must be provided.';
  18. // Add any default settings elements. If you override the globalSettingsForm()
  19. // or the instanceSettingsForm() functions then you need to be sure that
  20. // any settings you want those functions to manage are listed in this
  21. // array.
  22. public static $default_settings = array(
  23. 'chado_table' => '',
  24. 'chado_column' => '',
  25. 'base_table' => '',
  26. 'semantic_web' => '',
  27. );
  28. // Set this to the name of the storage backend that by default will support
  29. // this field.
  30. public static $default_storage = 'field_chado_storage';
  31. /**
  32. * @see TripalField::formatterView()
  33. */
  34. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  35. $chado_table = $this->field['settings']['chado_table'];
  36. foreach ($items as $delta => $item) {
  37. if (!$item['value']) {
  38. continue;
  39. }
  40. $content = $item['value']['vocabulary'] . ':' . $item['value']['accession'];
  41. if ($item['value']['URL']) {
  42. $content = l($item['value']['URL'], $item['value']['URL']);
  43. }
  44. $element[$delta] = array(
  45. '#type' => 'markup',
  46. '#markup' => $content,
  47. );
  48. }
  49. if (count($items) == 0) {
  50. $element[$delta] = array(
  51. '#type' => 'markup',
  52. '#markup' => '',
  53. );
  54. }
  55. }
  56. /**
  57. * @see TripalField::widgetForm()
  58. */
  59. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  60. $field_name = $this->field['field_name'];
  61. $field_type = $this->field['type'];
  62. $field_table = $this->field['settings']['chado_table'];
  63. $field_column = $this->field['settings']['chado_column'];
  64. // Get the FK column that links to the base table.
  65. $chado_table = $this->field['settings']['chado_table'];
  66. $base_table = $this->field['settings']['base_table'];
  67. $schema = chado_get_schema($chado_table);
  68. $pkey = $schema['primary key'][0];
  69. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  70. $fkey = $fkeys[0];
  71. // Get the field defaults.
  72. $record_id = '';
  73. $fkey_value = '';
  74. $dbxref_id = '';
  75. $db_id = '';
  76. $accession = '';
  77. $version = '';
  78. $description = '';
  79. // If the field already has a value then it will come through the $items
  80. // array. This happens when editing an existing record.
  81. if (array_key_exists($delta, $items)) {
  82. $record_id = $items[$delta][$field_table . '__' . $pkey];
  83. $fkey_value = $items[$delta][$field_table . '__' . $fkey];
  84. $dbxref_id = $items[$delta][$field_table . '__dbxref_id'];
  85. $db_id = $items[$delta][$field_table . '__dbxref_id--db_id'];
  86. $accession = $items[$delta][$field_table . '__dbxref_id--accession'];
  87. $version = $items[$delta][$field_table . '__dbxref_id--version'];
  88. $description = $items[$delta][$field_table . '__dbxref_id--description'];
  89. }
  90. // Check $form_state['values'] to see if an AJAX call set the values.
  91. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  92. // $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $pkey);
  93. // $fkey_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $fkey);
  94. // $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id');
  95. // $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--db_id');
  96. // $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--accession');
  97. // $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--version');
  98. // $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--description');
  99. }
  100. $schema = chado_get_schema('dbxref');
  101. $options = tripal_get_db_select_options();
  102. $widget['#table_name'] = $chado_table;
  103. $widget['#fkey_field'] = $fkey;
  104. //$widget['#element_validate'] = array('chado_linker__dbxref_widget_validate');
  105. $widget['#theme'] = 'chado_linker__dbxref_widget';
  106. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id-$delta'>";
  107. $widget['#suffix'] = "</span>";
  108. $widget['value'] = array(
  109. '#type' => 'value',
  110. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  111. );
  112. $widget[$field_table . '__' . $pkey] = array(
  113. '#type' => 'value',
  114. '#default_value' => $record_id,
  115. );
  116. $widget[$field_table . '__' . $fkey] = array(
  117. '#type' => 'value',
  118. '#default_value' => $fkey_value,
  119. );
  120. $widget[$field_table . '__dbxref_id'] = array(
  121. '#type' => 'value',
  122. '#default_value' => $dbxref_id,
  123. );
  124. $widget[$field_table . '__dbxref_id--dbxref_id'] = array(
  125. '#type' => 'value',
  126. '#default_value' => $dbxref_id,
  127. );
  128. $widget[$field_table . '__dbxref_id--db_id'] = array(
  129. '#type' => 'select',
  130. '#title' => t('Database'),
  131. '#options' => $options,
  132. '#required' => $element['#required'],
  133. '#default_value' => $db_id,
  134. '#ajax' => array(
  135. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  136. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  137. 'effect' => 'fade',
  138. 'method' => 'replace'
  139. ),
  140. );
  141. $widget[$field_table . '__dbxref_id--accession'] = array(
  142. '#type' => 'textfield',
  143. '#title' => t('Accession'),
  144. '#default_value' => $accession,
  145. '#required' => $element['#required'],
  146. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  147. '#size' => 15,
  148. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  149. '#ajax' => array(
  150. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  151. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  152. 'effect' => 'fade',
  153. 'method' => 'replace'
  154. ),
  155. '#disabled' => $db_id ? FALSE : TRUE,
  156. );
  157. $widget[$field_table . '__dbxref_id--version'] = array(
  158. '#type' => 'textfield',
  159. '#title' => t('Version'),
  160. '#default_value' => $version,
  161. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  162. '#size' => 5,
  163. '#disabled' => $db_id ? FALSE : TRUE,
  164. );
  165. $widget[$field_table . '__dbxref_id--description'] = array(
  166. '#type' => 'textfield',
  167. '#title' => t('Description'),
  168. '#default_value' => $description,
  169. '#size' => 20,
  170. '#disabled' => $db_id ? FALSE : TRUE,
  171. );
  172. if (!$db_id) {
  173. $widget['links'] = array(
  174. '#type' => 'item',
  175. '#markup' => l('Add a database', 'admin/tripal/legacy/tripal_db/add', array('attributes' => array('target' => '_blank')))
  176. );
  177. }
  178. }
  179. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  180. $field_name = $element['#field_name'];
  181. $delta = $element['#delta'];
  182. $table_name = $element['#table_name'];
  183. $fkey = $element['#fkey_field'];
  184. $field = field_info_field($field_name);
  185. $field_type = $field['type'];
  186. $field_table = $field['settings']['chado_table'];
  187. $field_column = $field['settings']['chado_column'];
  188. $field_prefix = $field_table . '__dbxref_id';
  189. // If the form ID is field_ui_field_edit_form, then the user is editing the
  190. // field's values in the manage fields form of Drupal. We don't want
  191. // to validate it as if it were being used in a data entry form.
  192. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  193. return;
  194. }
  195. // Get the field values.
  196. // $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id');
  197. // $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--db_id');
  198. // $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--accession');
  199. // $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--version');
  200. // $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--description');
  201. // Make sure that if a database ID is provided that an accession is also
  202. // provided. Here we use the form_set_error function rather than the
  203. // form_error function because the form_error will add a red_highlight
  204. // around all of the fields in the fieldset which is confusing as it's not
  205. // clear to the user what field is required and which isn't. Therefore,
  206. // we borrow the code from the 'form_error' function and append the field
  207. // so that the proper field is highlighted on error.
  208. if (!$db_id and $accession) {
  209. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided."));
  210. }
  211. if ($db_id and !$accession) {
  212. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--accession', t("A database and the accession must both be provided."));
  213. }
  214. if (!$db_id and !$accession and ($version or $description)) {
  215. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided."));
  216. }
  217. // If the dbxref_id does not match the db_id + accession then the user
  218. // has selected a new dbxref record and we need to update the hidden
  219. // value accordingly.
  220. if ($db_id and $accession) {
  221. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  222. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  223. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $table_name . '__dbxref_id');
  224. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $field_prefix . '--dbxref_id');
  225. }
  226. }
  227. else {
  228. // If the db_id and accession are not set, then remove the linker FK value to the base table.
  229. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  230. }
  231. }
  232. /**
  233. * @see TripalField::load()
  234. */
  235. public function load($entity, $details = array()) {
  236. $record = $details['record'];
  237. $field_name = $this->field['field_name'];
  238. $field_type = $this->field['type'];
  239. $field_table = $this->field['settings']['chado_table'];
  240. $field_column = $this->field['settings']['chado_column'];
  241. $base_table = $record->tablename;
  242. $schema = chado_get_schema($field_table);
  243. $pkey = $schema['primary key'][0];
  244. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  245. $fkey = $fkeys[0];
  246. // Set some defaults for the empty record.
  247. $entity->{$field_name}['und'][0] = array(
  248. 'value' => array(),
  249. $field_table . '__' . $pkey => '',
  250. $field_table . '__' . $fkey => '',
  251. $field_table . '__dbxref_id' => '',
  252. $field_table . '__dbxref_id--dbxref_id' => '',
  253. $field_table . '__dbxref_id--db_id' => '',
  254. $field_table . '__dbxref_id--accession' => '',
  255. $field_table . '__dbxref_id--version' => '',
  256. $field_table . '__dbxref_id--description' => '',
  257. );
  258. $linker_table = $base_table . '_dbxref';
  259. $options = array('return_array' => 1);
  260. $record = chado_expand_var($record, 'table', $linker_table, $options);
  261. if (count($record->$linker_table) > 0) {
  262. $i = 0;
  263. foreach ($record->$linker_table as $index => $linker) {
  264. $dbxref = $linker->dbxref_id;
  265. $URL = tripal_get_dbxref_url($dbxref);
  266. $entity->{$field_name}['und'][$i] = array(
  267. 'value' => array(
  268. 'vocabulary' => $dbxref->db_id->name,
  269. 'accession' => $dbxref->accession,
  270. 'URL' => $URL,
  271. ),
  272. $field_table . '__' . $pkey => $linker->$pkey,
  273. $field_table . '__' . $fkey => $linker->$fkey->$fkey,
  274. $field_table . '__dbxref_id' => $dbxref->dbxref_id,
  275. $field_table . '__dbxref_id--dbxref_id' => $dbxref->dbxref_id,
  276. $field_table . '__dbxref_id--db_id' => $dbxref->db_id->db_id,
  277. $field_table . '__dbxref_id--accession' => $dbxref->accession,
  278. $field_table . '__dbxref_id--version' => $dbxref->version,
  279. $field_table . '__dbxref_id--description' => $dbxref->description,
  280. );
  281. $i++;
  282. }
  283. }
  284. }
  285. }
  286. /**
  287. * Theme function for the dbxref_id_widget.
  288. *
  289. * @param $variables
  290. */
  291. function theme_chado_linker__dbxref_widget($variables) {
  292. $element = $variables['element'];
  293. // These two fields were added to the widget to help identify the fields
  294. // for layout.
  295. $table_name = $element['#table_name'];
  296. $fkey = $element['#fkey_field'];
  297. $layout = "
  298. <div class=\"secondary-dbxref-widget\">
  299. <div class=\"secondary-dbxref-widget-item\">" .
  300. drupal_render($element[$table_name . '__dbxref_id--db_id']) . "
  301. </div>
  302. <div class=\"secondary-dbxref-widget-item\">" .
  303. drupal_render($element[$table_name . '__dbxref_id--accession']) . "
  304. </div>
  305. <div class=\"secondary-dbxref-widget-item\">" .
  306. drupal_render($element[$table_name . '__dbxref_id--version']) . "
  307. </div>
  308. <div class=\"secondary-dbxref-widget-item\">" .
  309. drupal_render($element[$table_name . '__dbxref_id--description']) . "
  310. </div>
  311. <div class=\"secondary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  312. </div>
  313. ";
  314. return $layout;
  315. }
  316. /**
  317. * An Ajax callback for the dbxref widget.
  318. */
  319. function chado_linker__dbxref_widget_form_ajax_callback($form, $form_state) {
  320. $field_name = $form_state['triggering_element']['#parents'][0];
  321. $delta = $form_state['triggering_element']['#parents'][2];
  322. $field = field_info_field($field_name);
  323. $field_type = $field['type'];
  324. $field_table = $field['settings']['chado_table'];
  325. $field_column = $field['settings']['chado_column'];
  326. $field_prefix = $field_table . '__dbxref_id';
  327. // Check to see if this dbxref already exists. If not then
  328. // give a notice to the user that the dbxref will be added.
  329. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--db_id');
  330. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--accession');
  331. if ($db_id and $accession) {
  332. $values = array(
  333. 'db_id' => $db_id,
  334. 'accession' => $accession,
  335. );
  336. $options = array('is_duplicate' => TRUE);
  337. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  338. if (!$has_duplicate) {
  339. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  340. }
  341. }
  342. return $form[$field_name]['und'][$delta];
  343. }