chado_base__dbxref_id.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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' => 'data:2091',
  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. 'deafult' => array(
  112. 'label' => 'above',
  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. if ($item['value']['URL']) {
  153. $content = l($item['value']['URL'], $item['value']['URL']);
  154. }
  155. $element[$delta] = array(
  156. '#type' => 'markup',
  157. '#markup' => $content,
  158. );
  159. }
  160. }
  161. }
  162. /**
  163. * @see TripalField::widgetForm()
  164. */
  165. static function widgetForm(&$widget, &$form, &$form_state, $field, $instance,
  166. $langcode, $items, $delta, $element) {
  167. $field_name = $field['field_name'];
  168. $field_type = $field['type'];
  169. $field_table = $field['settings']['chado_table'];
  170. $field_column = $field['settings']['chado_column'];
  171. // Get the field defaults.
  172. $fk_val = '';
  173. $dbxref_id = '';
  174. $db_id = '';
  175. $accession = '';
  176. $version = '';
  177. $description = '';
  178. // If the field already has a value then it will come through the $items
  179. // array. This happens when editing an existing record.
  180. if (count($items) > 0 and array_key_exists($delta, $items)) {
  181. $fk_val = $items[$delta][$field_table . '__' . $field_column];
  182. $dbxref_id = $items[$delta][$field_table . '__' . $field_column . '--dbxref_id'];
  183. $db_id = $items[$delta][$field_table . '__' . $field_column . '--db_id'];
  184. $accession = $items[$delta][$field_table . '__' . $field_column . '--accession'];
  185. $version = $items[$delta][$field_table . '__' . $field_column . '--version'];
  186. $description = $items[$delta][$field_table . '__' . $field_column . '--description'];
  187. }
  188. // Check $form_state['values'] to see if an AJAX call set the values.
  189. if (array_key_exists('values', $form_state)) {
  190. $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column);
  191. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--dbxref_id');
  192. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--db_id');
  193. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--accession');
  194. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--version');
  195. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--description');
  196. }
  197. // If we are here because our parent was triggered in a form submit
  198. // then that means an ajax call was made and we don't want the fieldset to
  199. // be closed when it returns from the ajax call.
  200. // $collapsed = TRUE;
  201. // if (array_key_exists('triggering_element', $form_state) and
  202. // $form_state['triggering_element']['#parents'][0] == $field_name) {
  203. // $collapsed = FALSE;
  204. // }
  205. // if ($dbxref_id) {
  206. // $collapsed = FALSE;
  207. // }
  208. $schema = chado_get_schema('dbxref');
  209. $options = tripal_get_db_select_options();
  210. $widget['#element_validate'] = array('chado_base__dbxref_id_widget_validate');
  211. $widget['#theme'] = 'chado_base__dbxref_id_widget';
  212. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  213. $widget['#suffix'] = "</span>";
  214. // A temporary element used for theming the fieldset.
  215. $widget['#theme_settings'] = array(
  216. '#title' => $element['#title'],
  217. '#description' => $element['#description'],
  218. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  219. '#theme' => 'chado_base__dbxref_id_widget',
  220. //'#collapsible' => TRUE,
  221. //'#collapsed' => $collapsed,
  222. );
  223. $widget['value'] = array(
  224. '#type' => 'value',
  225. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  226. );
  227. $widget[$field_table . '__' . $field_column] = array(
  228. '#type' => 'value',
  229. '#default_value' => $fk_val,
  230. );
  231. $widget[$field_table . '__' . $field_column . '--dbxref_id'] = array(
  232. '#type' => 'value',
  233. '#default_value' => $dbxref_id,
  234. );
  235. $widget[$field_table . '__' . $field_column . '--db_id'] = array(
  236. '#type' => 'select',
  237. '#title' => t('Database'),
  238. '#options' => $options,
  239. '#required' => $element['#required'],
  240. '#default_value' => $db_id,
  241. '#ajax' => array(
  242. 'callback' => "chado_base__dbxref_id_widget_form_ajax_callback",
  243. 'wrapper' => "$field_name-dbxref--db-id",
  244. 'effect' => 'fade',
  245. 'method' => 'replace'
  246. ),
  247. );
  248. $widget[$field_table . '__' . $field_column . '--accession'] = array(
  249. '#type' => 'textfield',
  250. '#title' => t('Accession'),
  251. '#default_value' => $accession,
  252. '#required' => $element['#required'],
  253. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  254. '#size' => 15,
  255. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  256. '#ajax' => array(
  257. 'callback' => "tripal_chado_dbxref_widget_form_ajax_callback",
  258. 'wrapper' => "$field_name-dbxref--db-id",
  259. 'effect' => 'fade',
  260. 'method' => 'replace'
  261. ),
  262. '#disabled' => $db_id ? FALSE : TRUE,
  263. );
  264. $widget[$field_table . '__' . $field_column . '--version'] = array(
  265. '#type' => 'textfield',
  266. '#title' => t('Version'),
  267. '#default_value' => $version,
  268. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  269. '#size' => 5,
  270. '#disabled' => $db_id ? FALSE : TRUE,
  271. );
  272. $widget[$field_table . '__' . $field_column . '--description'] = array(
  273. '#type' => 'textfield',
  274. '#title' => t('Description'),
  275. '#default_value' => $description,
  276. '#size' => 20,
  277. '#disabled' => $db_id ? FALSE : TRUE,
  278. );
  279. $widget['links'] = array(
  280. '#type' => 'item',
  281. '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  282. );
  283. }
  284. /**
  285. * @see TripalField::load()
  286. */
  287. static function load($field, $entity, $details = array()) {
  288. $record = $details['record'];
  289. $field_name = $field['field_name'];
  290. $field_type = $field['type'];
  291. $field_table = $field['settings']['chado_table'];
  292. $field_column = $field['settings']['chado_column'];
  293. // Set some defauls for the empty record
  294. $entity->{$field_name}['und'][0] = array(
  295. 'value' => array(),
  296. $field_table . '__' . $field_column => '',
  297. $field_table . '__dbxref_id--dbxref_id' => '',
  298. $field_table . '__dbxref_id--db_id' => '',
  299. $field_table . '__dbxref_id--accession' => '',
  300. $field_table . '__dbxref_id--version' => '',
  301. $field_table . '__dbxref_id--description' => '',
  302. );
  303. // Get the primary dbxref record (if it's not NULL). Because we have a
  304. // dbxref_id passed in by the base record, we will only have one record.
  305. if ($record->$field_column) {
  306. $dbxref = $record->$field_column;
  307. $value = $dbxref->db_id->name . ':' . $dbxref->accession;
  308. $URL = '';
  309. if ($dbxref->db_id->urlprefix) {
  310. $URL = l($value, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  311. }
  312. $entity->{$field_name}['und'][0] = array(
  313. 'value' => array(
  314. 'vocabulary' => $dbxref->db_id->name,
  315. 'accession' => $dbxref->accession,
  316. 'URL' => $URL,
  317. ),
  318. $field_table . '__' . $field_column => $record->$field_column->$field_column,
  319. $field_table . '__' . $field_column . '--dbxref_id' => $dbxref->dbxref_id,
  320. $field_table . '__' . $field_column . '--db_id' => $dbxref->db_id->db_id,
  321. $field_table . '__' . $field_column . '--accession' => $dbxref->accession,
  322. $field_table . '__' . $field_column . '--version' => $dbxref->version,
  323. $field_table . '__' . $field_column . '--description' => $dbxref->description,
  324. );
  325. }
  326. }
  327. }
  328. /**
  329. * Callback function for validating the tripal_chado_dbxref_select_widget.
  330. */
  331. function chado_base__dbxref_id_widget_validate($element, &$form_state) {
  332. $field_name = $element['#parents'][0];
  333. $field = $form_state['field'][$field_name]['und']['field'];
  334. $settings = $field['settings'];
  335. $field_name = $field['field_name'];
  336. $field_type = $field['type'];
  337. $field_table = $field['settings']['chado_table'];
  338. $field_column = $field['settings']['chado_column'];
  339. $field_prefix = $field_table . '__' . $field_column;
  340. // If the form ID is field_ui_field_edit_form, then the user is editing the
  341. // field's values in the manage fields form of Drupal. We don't want
  342. // to validate it as if it were being used in a data entry form.
  343. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  344. return;
  345. }
  346. // Get the field values.
  347. $fk_value = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column);
  348. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--dbxref_id');
  349. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--db_id');
  350. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--accession');
  351. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--version');
  352. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--description');
  353. // Make sure that if a database ID is provided that an accession is also
  354. // provided. Here we use the form_set_error function rather than the
  355. // form_error function because the form_error will add a red_highlight
  356. // around all of the fields in the fieldset which is confusing as it's not
  357. // clear to the user what field is required and which isn't. Therefore,
  358. // we borrow the code from the 'form_error' function and append the field
  359. // so that the proper field is highlighted on error.
  360. if (!$db_id and $accession) {
  361. 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."));
  362. }
  363. if ($db_id and !$accession) {
  364. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--accession', t("A database and the accession must both be provided for the primary cross reference."));
  365. }
  366. if (!$db_id and !$accession and ($version or $description)) {
  367. 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."));
  368. }
  369. // If user did not select a database, we want to remove dbxref_id from the
  370. // field.
  371. if (!$db_id) {
  372. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_prefix . '--dbxref_id');
  373. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_table . '__' . $field_column);
  374. }
  375. // If the dbxref_id does not match the db_id + accession then the user
  376. // has selected a new dbxref record and we need to update the hidden
  377. // value accordingly.
  378. if ($db_id and $accession) {
  379. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  380. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  381. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, 0, $field_table . '__' . $field_column);
  382. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, 0, $field_prefix . '--dbxref_id');
  383. }
  384. }
  385. }
  386. /**
  387. * An Ajax callback for the tripal_chado_admin_publish_form..
  388. */
  389. function chado_base__dbxref_id_widget_form_ajax_callback($form, $form_state) {
  390. $field_name = $form_state['triggering_element']['#parents'][0];
  391. $field = field_info_field($field_name);
  392. $field_type = $field['type'];
  393. $field_table = $field['settings']['chado_table'];
  394. $field_column = $field['settings']['chado_column'];
  395. $field_prefix = $field_table . '__' . $field_column;
  396. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--db_id');
  397. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--accession');
  398. if ($db_id and $accession) {
  399. $values = array(
  400. 'db_id' => $db_id,
  401. 'accession' => $accession,
  402. );
  403. $options = array('is_duplicate' => TRUE);
  404. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  405. if (!$has_duplicate) {
  406. drupal_set_message('The selected cross reference is new and will be added for future auto completions.', 'warning');
  407. }
  408. }
  409. return $form[$field_name];
  410. }
  411. function theme_chado_base__dbxref_id_widget($variables) {
  412. $element = $variables['element'];
  413. $field_name = $element['#field_name'];
  414. $field = field_info_field($field_name);
  415. $field_type = $field['type'];
  416. $field_table = $field['settings']['chado_table'];
  417. $field_column = $field['settings']['chado_column'];
  418. $field_prefix = $field_table . '__' . $field_column;
  419. $layout = "
  420. <div class=\"primary-dbxref-widget\">
  421. <div class=\"primary-dbxref-widget-item\">" .
  422. drupal_render($element[$field_prefix . '--db_id']) . "
  423. </div>
  424. <div class=\"primary-dbxref-widget-item\">" .
  425. drupal_render($element[$field_prefix . '--accession']) . "
  426. </div>
  427. <div class=\"primary-dbxref-widget-item\">" .
  428. drupal_render($element[$field_prefix . '--version']) . "
  429. </div>
  430. <div class=\"primary-dbxref-widget-item\">" .
  431. drupal_render($element[$field_prefix . '--description']) . "
  432. </div>
  433. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  434. </div>
  435. ";
  436. // $classes = array();
  437. // $classes[] = 'collapsible';
  438. // $theme_settings = $element['#theme_settings'];
  439. // if ($theme_settings['#collapsed'] == FALSE) {
  440. // $classes[] = 'collapsed';
  441. // }
  442. $fieldset = array(
  443. '#title' => $element['#title'],
  444. '#value' => '',
  445. '#description' => $element['#description'],
  446. '#children' => $layout,
  447. // '#attributes' => array('class' => $classes),
  448. );
  449. return theme('fieldset', array('element' => $fieldset));
  450. }