chado_base__dbxref_id.inc 18 KB

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