chado_base__dbxref_id.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. class chado_base__dbxref_id extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Accession'),
  9. 'description' => t('This field specifies the unique stable accession (ID) for
  10. this record. It requires that this site have a database entry.'),
  11. 'default_widget' => 'chado_base__dbxref_id_widget',
  12. 'default_formatter' => 'chado_base__dbxref_id_formatter',
  13. 'settings' => array(),
  14. 'storage' => array(
  15. 'type' => 'field_chado_storage',
  16. 'module' => 'tripal_chado',
  17. 'active' => TRUE
  18. ),
  19. );
  20. }
  21. /**
  22. * @see TripalField::attach_info()
  23. */
  24. function attach_info($entity_type, $bundle, $settings) {
  25. $field_info = array();
  26. $table_name = $settings['data_table'];
  27. $type_table = $settings['type_table'];
  28. $type_field = $settings['field'];
  29. $cv_id = $settings['cv_id'];
  30. $cvterm_id = $settings['cvterm_id'];
  31. // Check the schema for the data table if it does not have
  32. // a 'dbxref_id' column then we don't want to attach this field.
  33. $schema = chado_get_schema($table_name);
  34. if (!array_key_exists('dbxref_id', $schema['fields'])) {
  35. return $field_info;
  36. }
  37. // There is an dbxref_id column so attach the field!
  38. $field_info = array(
  39. 'field_name' => $table_name . '__dbxref_id',
  40. 'field_type' => 'chado_base__dbxref_id',
  41. 'widget_type' => 'chado_base__dbxref_id_widget',
  42. 'description' => 'This field specifies the unique stable accession (ID) for
  43. this record. It requires that this site have a database entry.',
  44. 'label' => 'Accession',
  45. 'is_required' => 0,
  46. 'storage' => 'field_chado_storage',
  47. 'widget_settings' => array(
  48. 'display_label' => 1
  49. ),
  50. 'field_settings' => array(
  51. 'chado_table' => $table_name,
  52. 'chado_column' => 'dbxref_id',
  53. 'semantic_web' => array(
  54. 'name' => 'Accession',
  55. 'accession' => '2091',
  56. 'ns' => 'data',
  57. 'nsurl' => ' http://edamontology.org/',
  58. ),
  59. ),
  60. );
  61. return $field_info;
  62. }
  63. /**
  64. * @see TripalField::widget_info()
  65. */
  66. function widget_info() {
  67. return array(
  68. 'label' => t('Accession'),
  69. 'field types' => array('chado_base__dbxref_id'),
  70. 'description' => t('This field specifies the unique stable accession (ID) for
  71. this record. It requires that this site have a database entry.'),
  72. );
  73. }
  74. /**
  75. * @see TripalField::formatter_info()
  76. */
  77. function formatter_info() {
  78. return array(
  79. 'label' => t('Accession'),
  80. 'field types' => array('chado_base__dbxref_id'),
  81. 'settings' => array(
  82. ),
  83. );
  84. }
  85. /**
  86. * @see TripalField::formatter_view()
  87. */
  88. function formatter_view(&$element, $entity_type, $entity, $field,
  89. $instance, $langcode, $items, $display) {
  90. foreach ($items as $delta => $item) {
  91. if ($item['value']) {
  92. $content = $item['value']['vocabulary'] . ':' . $item['value']['accession'];
  93. if ($item['value']['URL']) {
  94. $content = l($item['value']['URL'], $item['value']['URL']);
  95. }
  96. $element[$delta] = array(
  97. '#type' => 'markup',
  98. '#markup' => $content,
  99. );
  100. }
  101. }
  102. }
  103. /**
  104. * @see TripalField::widget_form()
  105. */
  106. function widget_form(&$widget, $form, $form_state, $field, $instance,
  107. $langcode, $items, $delta, $element) {
  108. $field_name = $field['field_name'];
  109. $field_type = $field['type'];
  110. $field_table = $field['settings']['chado_table'];
  111. $field_column = $field['settings']['chado_column'];
  112. // Get the field defaults.
  113. $fk_val = '';
  114. $dbxref_id = '';
  115. $db_id = '';
  116. $accession = '';
  117. $version = '';
  118. $description = '';
  119. // If the field already has a value then it will come through the $items
  120. // array. This happens when editing an existing record.
  121. if (count($items) > 0 and array_key_exists($delta, $items)) {
  122. $fk_val = $items[$delta][$field_table . '__' . $field_column];
  123. $dbxref_id = $items[$delta][$field_table . '__' . $field_column . '--dbxref_id'];
  124. $db_id = $items[$delta][$field_table . '__' . $field_column . '--db_id'];
  125. $accession = $items[$delta][$field_table . '__' . $field_column . '--accession'];
  126. $version = $items[$delta][$field_table . '__' . $field_column . '--version'];
  127. $description = $items[$delta][$field_table . '__' . $field_column . '--description'];
  128. }
  129. // Check $form_state['values'] to see if an AJAX call set the values.
  130. if (array_key_exists('values', $form_state)) {
  131. $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column);
  132. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--dbxref_id');
  133. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--db_id');
  134. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--accession');
  135. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--version');
  136. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--description');
  137. }
  138. // If we are here because our parent was triggered in a form submit
  139. // then that means an ajax call was made and we don't want the fieldset to
  140. // be closed when it returns from the ajax call.
  141. // $collapsed = TRUE;
  142. // if (array_key_exists('triggering_element', $form_state) and
  143. // $form_state['triggering_element']['#parents'][0] == $field_name) {
  144. // $collapsed = FALSE;
  145. // }
  146. // if ($dbxref_id) {
  147. // $collapsed = FALSE;
  148. // }
  149. $schema = chado_get_schema('dbxref');
  150. $options = tripal_get_db_select_options();
  151. $widget['#element_validate'] = array('chado_base__dbxref_id_widget_validate');
  152. $widget['#theme'] = 'chado_base__dbxref_id_widget';
  153. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  154. $widget['#suffix'] = "</span>";
  155. // A temporary element used for theming the fieldset.
  156. $widget['#theme_settings'] = array(
  157. '#title' => $element['#title'],
  158. '#description' => $element['#description'],
  159. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  160. '#theme' => 'chado_base__dbxref_id_widget',
  161. //'#collapsible' => TRUE,
  162. //'#collapsed' => $collapsed,
  163. );
  164. $widget['value'] = array(
  165. '#type' => 'value',
  166. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  167. );
  168. $widget[$field_table . '__' . $field_column] = array(
  169. '#type' => 'value',
  170. '#default_value' => $fk_val,
  171. );
  172. $widget[$field_table . '__' . $field_column . '--dbxref_id'] = array(
  173. '#type' => 'value',
  174. '#default_value' => $dbxref_id,
  175. );
  176. $widget[$field_table . '__' . $field_column . '--db_id'] = array(
  177. '#type' => 'select',
  178. '#title' => t('Database'),
  179. '#options' => $options,
  180. '#required' => $element['#required'],
  181. '#default_value' => $db_id,
  182. '#ajax' => array(
  183. 'callback' => "chado_base__dbxref_id_widget_form_ajax_callback",
  184. 'wrapper' => "$field_name-dbxref--db-id",
  185. 'effect' => 'fade',
  186. 'method' => 'replace'
  187. ),
  188. );
  189. $widget[$field_table . '__' . $field_column . '--accession'] = array(
  190. '#type' => 'textfield',
  191. '#title' => t('Accession'),
  192. '#default_value' => $accession,
  193. '#required' => $element['#required'],
  194. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  195. '#size' => 15,
  196. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  197. '#ajax' => array(
  198. 'callback' => "tripal_chado_dbxref_widget_form_ajax_callback",
  199. 'wrapper' => "$field_name-dbxref--db-id",
  200. 'effect' => 'fade',
  201. 'method' => 'replace'
  202. ),
  203. '#disabled' => $db_id ? FALSE : TRUE,
  204. );
  205. $widget[$field_table . '__' . $field_column . '--version'] = array(
  206. '#type' => 'textfield',
  207. '#title' => t('Version'),
  208. '#default_value' => $version,
  209. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  210. '#size' => 5,
  211. '#disabled' => $db_id ? FALSE : TRUE,
  212. );
  213. $widget[$field_table . '__' . $field_column . '--description'] = array(
  214. '#type' => 'textfield',
  215. '#title' => t('Description'),
  216. '#default_value' => $description,
  217. '#size' => 20,
  218. '#disabled' => $db_id ? FALSE : TRUE,
  219. );
  220. $widget['links'] = array(
  221. '#type' => 'item',
  222. '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  223. );
  224. }
  225. /**
  226. * @see TripalField::load()
  227. */
  228. function load($field, $entity, $details) {
  229. $record = $details['record'];
  230. $field_name = $field['field_name'];
  231. $field_type = $field['type'];
  232. $field_table = $field['settings']['chado_table'];
  233. $field_column = $field['settings']['chado_column'];
  234. // Set some defauls for the empty record
  235. $entity->{$field_name}['und'][0] = array(
  236. 'value' => array(),
  237. $field_table . '__' . $field_column => '',
  238. 'dbxref__dbxref_id' => '',
  239. 'dbxref__db_id' => '',
  240. 'dbxref__accession' => '',
  241. 'dbxref__version' => '',
  242. 'dbxref__description' => '',
  243. );
  244. // Get the primary dbxref record (if it's not NULL). Because we have a
  245. // dbxref_id passed in by the base record, we will only have one record.
  246. if ($record->$field_column) {
  247. $dbxref = $record->$field_column;
  248. $value = $dbxref->db_id->name . ':' . $dbxref->accession;
  249. $URL = '';
  250. if ($dbxref->db_id->urlprefix) {
  251. $URL = l($value, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  252. }
  253. $entity->{$field_name}['und'][0] = array(
  254. 'value' => array(
  255. 'vocabulary' => $dbxref->db_id->name,
  256. 'accession' => $dbxref->accession,
  257. 'URL' => $URL,
  258. ),
  259. $field_table . '__' . $field_column => $record->$field_column->$field_column,
  260. $field_table . '__' . $field_column . '--dbxref_id' => $dbxref->dbxref_id,
  261. $field_table . '__' . $field_column . '--db_id' => $dbxref->db_id->db_id,
  262. $field_table . '__' . $field_column . '--accession' => $dbxref->accession,
  263. $field_table . '__' . $field_column . '--version' => $dbxref->version,
  264. $field_table . '__' . $field_column . '--description' => $dbxref->description,
  265. );
  266. }
  267. }
  268. }
  269. /**
  270. * Callback function for validating the tripal_chado_dbxref_select_widget.
  271. */
  272. function chado_base__dbxref_id_widget_validate($element, &$form_state) {
  273. $field_name = $element['#parents'][0];
  274. $field = $form_state['field'][$field_name]['und']['field'];
  275. $settings = $field['settings'];
  276. $field_name = $field['field_name'];
  277. $field_type = $field['type'];
  278. $field_table = $field['settings']['chado_table'];
  279. $field_column = $field['settings']['chado_column'];
  280. $field_prefix = $field_table . '__' . $field_column;
  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. $fk_value = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column);
  289. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--dbxref_id');
  290. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--db_id');
  291. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--accession');
  292. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--version');
  293. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--description');
  294. // Make sure that if a database ID is provided that an accession is also
  295. // provided. Here we use the form_set_error function rather than the
  296. // form_error function because the form_error will add a red_highlight
  297. // around all of the fields in the fieldset which is confusing as it's not
  298. // clear to the user what field is required and which isn't. Therefore,
  299. // we borrow the code from the 'form_error' function and append the field
  300. // so that the proper field is highlighted on error.
  301. if (!$db_id and $accession) {
  302. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided for the primary cross reference."));
  303. }
  304. if ($db_id and !$accession) {
  305. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--accession', t("A database and the accession must both be provided for the primary cross reference."));
  306. }
  307. if (!$db_id and !$accession and ($version or $description)) {
  308. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided for the primary cross reference."));
  309. }
  310. // If user did not select a database, we want to remove dbxref_id from the
  311. // field.
  312. if (!$db_id) {
  313. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_prefix . '--dbxref_id');
  314. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_table . '__' . $field_column);
  315. }
  316. // If the dbxref_id does not match the db_id + accession then the user
  317. // has selected a new dbxref record and we need to update the hidden
  318. // value accordingly.
  319. if ($db_id and $accession) {
  320. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  321. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  322. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, 0, $field_table . '__' . $field_column);
  323. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, 0, $field_prefix . '--dbxref_id');
  324. }
  325. }
  326. }
  327. /**
  328. * An Ajax callback for the tripal_chado_admin_publish_form..
  329. */
  330. function chado_base__dbxref_id_widget_form_ajax_callback($form, $form_state) {
  331. $field_name = $form_state['triggering_element']['#parents'][0];
  332. $field = field_info_field($field_name);
  333. $field_type = $field['type'];
  334. $field_table = $field['settings']['chado_table'];
  335. $field_column = $field['settings']['chado_column'];
  336. $field_prefix = $field_table . '__' . $field_column;
  337. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--db_id');
  338. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--accession');
  339. if ($db_id and $accession) {
  340. $values = array(
  341. 'db_id' => $db_id,
  342. 'accession' => $accession,
  343. );
  344. $options = array('is_duplicate' => TRUE);
  345. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  346. if (!$has_duplicate) {
  347. drupal_set_message('The selected cross reference is new and will be added for future auto completions.', 'warning');
  348. }
  349. }
  350. return $form[$field_name];
  351. }
  352. function theme_chado_base__dbxref_id_widget($variables) {
  353. $element = $variables['element'];
  354. $field_name = $element['#field_name'];
  355. $field = field_info_field($field_name);
  356. $field_type = $field['type'];
  357. $field_table = $field['settings']['chado_table'];
  358. $field_column = $field['settings']['chado_column'];
  359. $field_prefix = $field_table . '__' . $field_column;
  360. $layout = "
  361. <div class=\"primary-dbxref-widget\">
  362. <div class=\"primary-dbxref-widget-item\">" .
  363. drupal_render($element[$field_prefix . '--db_id']) . "
  364. </div>
  365. <div class=\"primary-dbxref-widget-item\">" .
  366. drupal_render($element[$field_prefix . '--accession']) . "
  367. </div>
  368. <div class=\"primary-dbxref-widget-item\">" .
  369. drupal_render($element[$field_prefix . '--version']) . "
  370. </div>
  371. <div class=\"primary-dbxref-widget-item\">" .
  372. drupal_render($element[$field_prefix . '--description']) . "
  373. </div>
  374. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  375. </div>
  376. ";
  377. // $classes = array();
  378. // $classes[] = 'collapsible';
  379. // $theme_settings = $element['#theme_settings'];
  380. // if ($theme_settings['#collapsed'] == FALSE) {
  381. // $classes[] = 'collapsed';
  382. // }
  383. $fieldset = array(
  384. '#title' => $element['#title'],
  385. '#value' => '',
  386. '#description' => $element['#description'],
  387. '#children' => $layout,
  388. // '#attributes' => array('class' => $classes),
  389. );
  390. return theme('fieldset', array('element' => $fieldset));
  391. }