chado_base__dbxref_id.inc 19 KB

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