chado_linker__dbxref.inc 16 KB

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