chado_linker__dbxref.inc 16 KB

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