chado_linker__dbxref.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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($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($items) == 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. $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 = $element['#entity']->chado_record_id;
  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 (count($items) > 0 and array_key_exists($delta, $items)) {
  82. $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
  83. $fkey_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $fkey, $fkey_value);
  84. $dbxref_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__dbxref_id', $dbxref_id);
  85. $db_id = tripal_get_field_item_keyval($items, $delta, 'db_id', $db_id);
  86. $accession = tripal_get_field_item_keyval($items, $delta, 'accession', $accession);
  87. $version = tripal_get_field_item_keyval($items, $delta, 'version', $version);
  88. $description = tripal_get_field_item_keyval($items, $delta, 'description', $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 = $form_state['values'][$field_name]['und'][$delta][$field_table . '__' . $pkey];
  93. $fkey_value = $form_state['values'][$field_name]['und'][$delta][$field_table . '__' . $fkey];
  94. $dbxref_id = $form_state['values'][$field_name]['und'][$delta][$field_table . '__dbxref_id'];
  95. $db_id = $form_state['values'][$field_name]['und'][$delta][db_id];
  96. $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
  97. $version = $form_state['values'][$field_name]['und'][$delta]['version'];
  98. $description = $form_state['values'][$field_name]['und'][$delta]['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['chado-' . $field_table . '__' . $pkey] = array(
  113. '#type' => 'value',
  114. '#default_value' => $record_id,
  115. );
  116. $widget['chado-' . $field_table . '__' . $fkey] = array(
  117. '#type' => 'value',
  118. '#default_value' => $fkey_value,
  119. );
  120. $widget['chado-' . $field_table . '__dbxref_id'] = array(
  121. '#type' => 'value',
  122. '#default_value' => $dbxref_id,
  123. );
  124. $widget['dbxref_id'] = array(
  125. '#type' => 'value',
  126. '#default_value' => $dbxref_id,
  127. );
  128. $widget['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['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['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['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. /**
  180. * @see TripalField::widgetFormValidate()
  181. */
  182. public function widgetFormValidate($entity_type, $entity, $field, $items, &$errors) {
  183. $field_name = $this->field['field_name'];
  184. $field_type = $this->field['type'];
  185. $table_name = $this->field['settings']['chado_table'];
  186. $field_table = $this->field['settings']['chado_table'];
  187. $field_column = $this->field['settings']['chado_column'];
  188. $base_table = $this->field['settings']['base_table'];
  189. $schema = chado_get_schema($table_name);
  190. $pkey = $schema['primary key'][0];
  191. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  192. $fkey = $fkeys[0];
  193. // Get the field values.
  194. foreach ($items as $delta => $values) {
  195. // Get the field values.
  196. $dbxref_id = $values['chado-' . $field_table . '__dbxref_id'];
  197. $db_id = $values['db_id'];
  198. $accession = $values['accession'];
  199. $version = $values['version'];
  200. $description = $values['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. $errors[$field_name][$delta]['und'][$delta] = array(
  210. 'message' => t("A database and the accession must both be provided."),
  211. 'error' => 'chado_linker__dbxref',
  212. );
  213. }
  214. if ($db_id and !$accession) {
  215. $errors[$field_name][$delta]['und'][$delta] = array(
  216. 'message' => t("A database and the accession must both be provided."),
  217. 'error' => 'chado_linker__dbxref',
  218. );
  219. }
  220. if (!$db_id and !$accession and ($version or $description)) {
  221. $errors[$field_name][$delta]['und'][$delta] = array(
  222. 'message' => t("A database and the accession must both be provided."),
  223. 'error' => 'chado_linker__dbxref',
  224. );
  225. }
  226. }
  227. }
  228. /**
  229. * @see TripalField::widgetFormSubmit()
  230. */
  231. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  232. $field_name = $this->field['field_name'];
  233. $field_type = $this->field['type'];
  234. $table_name = $this->field['settings']['chado_table'];
  235. $field_table = $this->field['settings']['chado_table'];
  236. $field_column = $this->field['settings']['chado_column'];
  237. $base_table = $this->field['settings']['base_table'];
  238. $schema = chado_get_schema($table_name);
  239. $pkey = $schema['primary key'][0];
  240. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  241. $fkey = $fkeys[0];
  242. // Get the field values.
  243. foreach ($items as $delta => $values) {
  244. // Get the field values.
  245. $dbxref_id = $values['chado-' . $field_table . '__dbxref_id'];
  246. $db_id = $values['db_id'];
  247. $accession = $values['accession'];
  248. $version = $values['version'];
  249. $description = $values['description'];
  250. // If the dbxref_id does not match the db_id + accession then the user
  251. // has selected a new dbxref record and we need to update the hidden
  252. // value accordingly.
  253. if ($db_id and $accession) {
  254. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  255. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  256. $items[$delta]['chado-' . $table_name . '__dbxref_id'] = $dbxref->dbxref_id;
  257. $items[$delta]['dbxref_id'] = $dbxref->dbxref_id;
  258. }
  259. }
  260. // If the db_id and accession are not set, then remove the linker FK
  261. // value to the base table.
  262. else {
  263. $items[$delta]['chado-' . $table_name . '__' . $pkey] = '';
  264. $items[$delta]['chado-' . $table_name . '__' . $fkey] = '';
  265. $items[$delta]['chado-' . $table_name . '__dbxref_id'] = '';
  266. }
  267. }
  268. }
  269. /**
  270. * @see TripalField::load()
  271. */
  272. public function load($entity, $details = array()) {
  273. $record = $details['record'];
  274. $field_name = $this->field['field_name'];
  275. $field_type = $this->field['type'];
  276. $field_table = $this->field['settings']['chado_table'];
  277. $field_column = $this->field['settings']['chado_column'];
  278. $base_table = $record->tablename;
  279. $schema = chado_get_schema($field_table);
  280. $pkey = $schema['primary key'][0];
  281. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  282. $fkey = $fkeys[0];
  283. // Set some defaults for the empty record.
  284. $entity->{$field_name}['und'][0] = array(
  285. 'value' => array(),
  286. 'chado-' . $field_table . '__' . $pkey => '',
  287. 'chado-' . $field_table . '__' . $fkey => '',
  288. 'chado-' . $field_table . '__dbxref_id' => '',
  289. 'dbxref_id' => '',
  290. 'db_id' => '',
  291. 'accession' => '',
  292. 'version' => '',
  293. 'description' => '',
  294. );
  295. $linker_table = $base_table . '_dbxref';
  296. $options = array('return_array' => 1);
  297. $record = chado_expand_var($record, 'table', $linker_table, $options);
  298. if (count($record->$linker_table) > 0) {
  299. $i = 0;
  300. foreach ($record->$linker_table as $index => $linker) {
  301. $dbxref = $linker->dbxref_id;
  302. $URL = tripal_get_dbxref_url($dbxref);
  303. $entity->{$field_name}['und'][$i] = array(
  304. 'value' => array(
  305. 'vocabulary' => $dbxref->db_id->name,
  306. 'accession' => $dbxref->accession,
  307. 'URL' => $URL,
  308. ),
  309. 'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
  310. 'chado-' . $field_table . '__' . $fkey => $linker->$fkey->$fkey,
  311. 'chado-' . $field_table . '__dbxref_id' => $dbxref->dbxref_id,
  312. 'dbxref_id' => $dbxref->dbxref_id,
  313. 'db_id' => $dbxref->db_id->db_id,
  314. 'accession' => $dbxref->accession,
  315. 'version' => $dbxref->version,
  316. 'description' => $dbxref->description,
  317. );
  318. $i++;
  319. }
  320. }
  321. }
  322. }
  323. /**
  324. * Theme function for the dbxref_id_widget.
  325. *
  326. * @param $variables
  327. */
  328. function theme_chado_linker__dbxref_widget($variables) {
  329. $element = $variables['element'];
  330. // These two fields were added to the widget to help identify the fields
  331. // for layout.
  332. $table_name = $element['#table_name'];
  333. $fkey = $element['#fkey_field'];
  334. $layout = "
  335. <div class=\"secondary-dbxref-widget\">
  336. <div class=\"secondary-dbxref-widget-item\">" .
  337. drupal_render($element['db_id']) . "
  338. </div>
  339. <div class=\"secondary-dbxref-widget-item\">" .
  340. drupal_render($element['accession']) . "
  341. </div>
  342. <div class=\"secondary-dbxref-widget-item\">" .
  343. drupal_render($element['version']) . "
  344. </div>
  345. <div class=\"secondary-dbxref-widget-item\">" .
  346. drupal_render($element['description']) . "
  347. </div>
  348. <div class=\"secondary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  349. </div>
  350. ";
  351. return $layout;
  352. }
  353. /**
  354. * An Ajax callback for the dbxref widget.
  355. */
  356. function chado_linker__dbxref_widget_form_ajax_callback($form, $form_state) {
  357. $field_name = $form_state['triggering_element']['#parents'][0];
  358. $delta = $form_state['triggering_element']['#parents'][2];
  359. $field = field_info_field($field_name);
  360. $field_type = $field['type'];
  361. $field_table = $field['settings']['chado_table'];
  362. $field_column = $field['settings']['chado_column'];
  363. $field_prefix = 'chado-' . $field_table . '__dbxref_id';
  364. // Check to see if this dbxref already exists. If not then
  365. // give a notice to the user that the dbxref will be added.
  366. $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
  367. $db_id = $form_state['values'][$field_name]['und'][$delta]['accession'];
  368. if ($db_id and $accession) {
  369. $values = array(
  370. 'db_id' => $db_id,
  371. 'accession' => $accession,
  372. );
  373. $options = array('is_duplicate' => TRUE);
  374. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  375. if (!$has_duplicate) {
  376. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  377. }
  378. }
  379. return $form[$field_name]['und'][$delta];
  380. }