tripal_fields.fields.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <?php
  2. /**
  3. * Implements hook_field_info().
  4. */
  5. function tripal_fields_field_info() {
  6. $fields = array(
  7. 'organism_id' => array(
  8. 'label' => t('Organism'),
  9. 'description' => t('A field for specifying an organism.'),
  10. 'default_widget' => 'tripal_fields_organism_select_widget',
  11. 'default_formatter' => 'tripal_fields_organism_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_fields',
  16. 'active' => TRUE
  17. ),
  18. ),
  19. 'dbxref_id' => array(
  20. 'label' => t('Cross-reference'),
  21. 'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference. At a minimum, the database and accession must be provided.'),
  22. 'default_widget' => 'tripal_fields_primary_dbxref_widget',
  23. 'default_formatter' => 'tripal_fields_primary_dbxref_formatter',
  24. 'settings' => array(),
  25. 'storage' => array(
  26. 'type' => 'field_chado_storage',
  27. 'module' => 'tripal_fields',
  28. 'active' => TRUE
  29. ),
  30. ),
  31. 'residues' => array(
  32. 'label' => t('Residues'),
  33. 'description' => t('A field for managing nucleotide and protein residues.'),
  34. 'default_widget' => 'tripal_fields_residue_textarea_widget',
  35. 'default_formatter' => 'tripal_fields_residues_formatter',
  36. 'settings' => array(),
  37. 'storage' => array(
  38. 'type' => 'field_chado_storage',
  39. 'module' => 'tripal_fields',
  40. 'active' => TRUE
  41. ),
  42. ),
  43. 'md5checksum' => array(
  44. 'label' => t('MD5 checksum'),
  45. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  46. 'default_widget' => 'tripal_fields_md5checksum_checkbox_widget',
  47. 'default_formatter' => 'tripal_fields_md5checksum_formatter',
  48. 'settings' => array(),
  49. 'storage' => array(
  50. 'type' => 'field_chado_storage',
  51. 'module' => 'tripal_fields',
  52. 'active' => TRUE
  53. ),
  54. ),
  55. 'seqlen' => array(
  56. 'label' => t('Sequence length'),
  57. 'description' => t('A field for calculating the length of a sequence.'),
  58. 'default_widget' => 'tripal_fields_seqlen_hidden_widget',
  59. 'default_formatter' => 'tripal_fields_seqlen_formatter',
  60. 'settings' => array(),
  61. 'storage' => array(
  62. 'type' => 'field_chado_storage',
  63. 'module' => 'tripal_fields',
  64. 'active' => TRUE
  65. ),
  66. ),
  67. );
  68. return $fields;
  69. }
  70. /**
  71. * Implements hook_field_widget_info().
  72. */
  73. function tripal_fields_field_widget_info() {
  74. return array(
  75. 'tripal_fields_organism_select_widget' => array(
  76. 'label' => t('Organism Select'),
  77. 'field types' => array('organism_id')
  78. ),
  79. 'tripal_fields_primary_dbxref_widget' => array(
  80. 'label' => t('Cross-reference'),
  81. 'field types' => array('dbxref_id'),
  82. 'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference. At a minimum, the database and accession must be provided.'),
  83. ),
  84. 'tripal_fields_md5checksum_checkbox_widget' => array(
  85. 'label' => t('MD5 Checksum Checkbox'),
  86. 'field types' => array('md5checksum')
  87. ),
  88. 'tripal_fields_residues_textarea_widget' => array(
  89. 'label' => t('Residues'),
  90. 'field types' => array('residues')
  91. ),
  92. 'tripal_fields_seqlen_hidden_widget' => array(
  93. 'field types' => array('seqlen')
  94. ),
  95. );
  96. }
  97. /**
  98. * Implements hook_field_formatter_info().
  99. */
  100. function tripal_fields_field_formatter_info() {
  101. return array(
  102. 'tripal_fields_organism_formatter' => array(
  103. 'label' => t('Organism'),
  104. 'field types' => array('organism_id')
  105. ),
  106. 'tripal_fields_primary_dbxref_formatter' => array(
  107. 'label' => t('Cross-reference'),
  108. 'field types' => array('dbxref_id')
  109. ),
  110. 'tripal_fields_md5checksum_formatter' => array(
  111. 'label' => t('MD5 checksum'),
  112. 'field types' => array('md5checksum')
  113. ),
  114. 'tripal_fields_residues_formatter' => array(
  115. 'label' => t('Residues'),
  116. 'field types' => array('residues')
  117. ),
  118. 'tripal_fields_seqlen_formatter' => array(
  119. 'label' => t('Sequence length'),
  120. 'field types' => array('seqlen')
  121. ),
  122. );
  123. }
  124. /**
  125. * Implements hook_field_formatter_view().
  126. *
  127. * Two formatters are implemented.
  128. * - field_example_simple_text just outputs markup indicating the color that
  129. * was entered and uses an inline style to set the text color to that value.
  130. * - field_example_color_background does the same but also changes the
  131. * background color of div.region-content.
  132. *
  133. * @see field_example_field_formatter_info()
  134. */
  135. function tripal_fields_field_formatter_view($entity_type, $entity, $field,
  136. $instance, $langcode, $items, $display) {
  137. $element = array();
  138. switch ($display['type']) {
  139. case 'tripal_fields_organism_formatter':
  140. foreach ($items as $delta => $item) {
  141. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
  142. // TODO: add hook here to allow other modules to change this display
  143. // if they want.
  144. $element[$delta] = array(
  145. // We create a render array to produce the desired markup,
  146. // "<p>Genus Species</p>".
  147. // See theme_html_tag().
  148. '#type' => 'markup',
  149. '#markup' => '<i>' . $organism[0]->genus .' ' . $organism[0]->species . '</i>',
  150. );
  151. }
  152. break;
  153. case 'tripal_fields_primary_dbxref_formatter':
  154. foreach ($items as $delta => $item) {
  155. $accession = '';
  156. if ($item['value']) {
  157. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $item['value']));
  158. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  159. if ($dbxref->db_id->urlprefix) {
  160. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  161. }
  162. }
  163. // TODO: add hook here to allow other modules to change this display
  164. // if they want.
  165. $element[$delta] = array(
  166. // We create a render array to produce the desired markup,
  167. '#type' => 'markup',
  168. '#markup' => $accession,
  169. );
  170. }
  171. break;
  172. case 'tripal_fields_md5checksum_formatter':
  173. foreach ($items as $delta => $item) {
  174. // TODO: add hook here to allow other modules to change this display
  175. // if they want.
  176. $element[$delta] = array(
  177. // We create a render array to produce the desired markup,
  178. '#type' => 'markup',
  179. '#markup' => key_exists('value', $item) ? $item['value'] : '',
  180. );
  181. }
  182. break;
  183. case 'tripal_fields_residues_formatter':
  184. foreach ($items as $delta => $item) {
  185. // TODO: add hook here to allow other modules to change this display
  186. // if they want.
  187. $residues = key_exists('value', $item) ? $item['value'] : '';
  188. $element[$delta] = array(
  189. // We create a render array to produce the desired markup,
  190. '#type' => 'markup',
  191. '#markup' => '<pre>' . $residues . '</pre>',
  192. );
  193. }
  194. break;
  195. case 'tripal_fields_seqlen_formatter':
  196. foreach ($items as $delta => $item) {
  197. // TODO: add hook here to allow other modules to change this display
  198. // if they want.
  199. $element[$delta] = array(
  200. // We create a render array to produce the desired markup,
  201. '#type' => 'markup',
  202. '#markup' => key_exists('value', $item) ? $item['value'] : '',
  203. );
  204. }
  205. break;
  206. }
  207. return $element;
  208. }
  209. /**
  210. * Implements hook_field_widget_form().
  211. */
  212. function tripal_fields_field_widget_form(&$form, &$form_state, $field,
  213. $instance, $langcode, $items, $delta, $element) {
  214. $widget = $element;
  215. $widget['#delta'] = $delta;
  216. $field_name = $field['field_name'];
  217. switch ($instance['widget']['type']) {
  218. case 'tripal_fields_organism_select_widget':
  219. $options = tripal_get_organism_select_options();
  220. $widget += array(
  221. '#type' => 'select',
  222. '#title' => $element['#title'],
  223. '#description' => $element['#description'],
  224. '#options' => $options,
  225. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  226. '#required' => $element['#required'],
  227. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  228. '#delta' => $delta,
  229. '#element_validate' => array('tripal_fields_organism_select_widget_validate'),
  230. );
  231. $element['value'] = $widget;
  232. break;
  233. case 'tripal_fields_primary_dbxref_widget':
  234. // Get the field defaults from the database if a record exists.
  235. $dbxref_id = '';
  236. $db_id = '';
  237. $accession = '';
  238. $version = '';
  239. $description = '';
  240. if (count($items) > 0 and $items[0]['value']) {
  241. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
  242. $dbxref_id = $dbxref->dbxref_id;
  243. $db_id = $dbxref->db_id->db_id;
  244. $accession = $dbxref->accession;
  245. $version = $dbxref->version;
  246. $description = $dbxref->description;
  247. }
  248. $temp = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__db_id');
  249. if (count($temp) > 0) {
  250. $db_id = $temp[0];
  251. }
  252. $schema = chado_get_schema('dbxref');
  253. $options = tripal_get_db_select_options();
  254. $widget += array(
  255. '#element_validate' => array('tripal_fields_primary_dbxref_widget_validate'),
  256. '#type' => 'fieldset',
  257. '#title' => $element['#title'],
  258. '#description' => $element['#description'],
  259. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  260. '#delta' => $delta,
  261. '#theme' => 'tripal_fields_primary_dbxref_widget',
  262. array(
  263. $element['#field_name'] => array(
  264. '#type' => 'hidden',
  265. '#default_value' => $dbxref_id,
  266. ),
  267. 'dbxref__db_id' => array(
  268. '#type' => 'select',
  269. '#title' => t('Database'),
  270. '#options' => $options,
  271. '#required' => $element['#required'],
  272. '#default_value' => $db_id,
  273. '#ajax' => array(
  274. 'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
  275. 'wrapper' => "$field_name-dbxref--db-id",
  276. 'effect' => 'fade',
  277. 'method' => 'replace'
  278. )
  279. ),
  280. 'dbxref__accession' => array(
  281. '#type' => 'textfield',
  282. '#title' => t('Accession'),
  283. '#default_value' => $accession,
  284. '#required' => $element['#required'],
  285. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  286. '#size' => 15,
  287. '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
  288. '#ajax' => array(
  289. 'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
  290. 'wrapper' => "$field_name-dbxref--db-id",
  291. 'effect' => 'fade',
  292. 'method' => 'replace'
  293. )
  294. ),
  295. 'dbxref__version' => array(
  296. '#type' => 'textfield',
  297. '#title' => t('Version'),
  298. '#default_value' => $version,
  299. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  300. '#size' => 5,
  301. ),
  302. 'dbxref__description' => array(
  303. '#type' => 'textfield',
  304. '#title' => t('Description'),
  305. '#default_value' => $description,
  306. '#size' => 20,
  307. ),
  308. ),
  309. '#prefix' => "<span id='$field_name-dbxref--db-id'>",
  310. '#suffix' => "</span>"
  311. );
  312. $element['value'] = $widget;
  313. break;
  314. case 'tripal_fields_md5checksum_checkbox_widget':
  315. $widget += array(
  316. '#type' => 'checkbox',
  317. '#title' => $element['#title'],
  318. '#description' => $element['#description'],
  319. '#options' => array(0, 1),
  320. '#default_value' => 1,
  321. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  322. '#delta' => $delta,
  323. '#element_validate' => array('tripal_fields_md5checksum_checkbox_widget_validate'),
  324. );
  325. $element['value'] = $widget;
  326. break;
  327. case 'tripal_fields_residues_textarea_widget':
  328. $widget += array(
  329. '#type' => 'textarea',
  330. '#title' => $element['#title'],
  331. '#description' => $element['#description'],
  332. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  333. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  334. '#delta' => $delta,
  335. '#element_validate' => array('tripal_fields_residues_textarea_widget_validate'),
  336. );
  337. $element['value'] = $widget;
  338. break;
  339. case 'tripal_fields_seqlen_hidden_widget':
  340. $widget += array(
  341. '#type' => 'hidden',
  342. '#title' => $element['#title'],
  343. '#description' => $element['#description'],
  344. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  345. '#delta' => $delta,
  346. '#element_validate' => array('tripal_fields_seqlen_hidden_widget_validate'),
  347. );
  348. $element['value'] = $widget;
  349. break;
  350. }
  351. return $element;
  352. }
  353. /**
  354. * Implements hook_field_is_empty().
  355. */
  356. function tripal_fields_field_is_empty($item, $field) {
  357. if (empty($item['value']) && (string) $item['value'] !== '0') {
  358. return TRUE;
  359. }
  360. return FALSE;
  361. }
  362. /**
  363. * Callback function for validating the tripal_fields_organism_select_widget.
  364. */
  365. function tripal_fields_organism_select_widget_validate($element, &$form_state) {
  366. $field_name = $element['#field_name'];
  367. // If the form ID is field_ui_field_edit_form, then the user is editing the
  368. // field's values in the manage fields form of Drupal. We don't want
  369. // to validate it as if it were being used in a data entry form.
  370. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  371. return;
  372. }
  373. $organism_id = tripal_fields_get_field_form_values($field_name, $form_state);
  374. if (count($organism_id) == 0) {
  375. form_error($element, t("Please specify an organism that already exists in the database."));
  376. }
  377. }
  378. /**
  379. * Callback function for validating the tripal_fields_organism_select_widget.
  380. */
  381. function tripal_fields_primary_dbxref_widget_validate($element, &$form_state) {
  382. $field_name = $element['#field_name'];
  383. // If the form ID is field_ui_field_edit_form, then the user is editing the
  384. // field's values in the manage fields form of Drupal. We don't want
  385. // to validate it as if it were being used in a data entry form.
  386. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  387. return;
  388. }
  389. // Get the field values.
  390. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__db_id");
  391. $accession = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__accession");
  392. $version = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__version");
  393. $description = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__description");
  394. // Make sure that if a database ID is provided that an accession is also
  395. // provided. Here we use the form_set_error function rather than the
  396. // form_error function because the form_error will add a red_highlight
  397. // around all of the fields in the fieldset which is confusing as it's not
  398. // clear to the user what field is required and which isn't. Therefore,
  399. // we borrow the code from the 'form_error' function and append the field
  400. // so that the proper field is highlighted on error.
  401. if (count($db_id) == 0 and count($accession) > 0) {
  402. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  403. }
  404. if (count($db_id) > 0 and count($accession) == 0) {
  405. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  406. }
  407. // If user did not select a database, we want to remove dbxref_id from the
  408. // field.
  409. if (count($db_id) == 0) {
  410. tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__', $field_name);
  411. }
  412. }
  413. /**
  414. * Callback function for validating the tripal_fields_md5checksum_checkbox_widget.
  415. */
  416. function tripal_fields_md5checksum_checkbox_widget_validate($element, &$form_state) {
  417. $field_name = $element['#field_name'];
  418. // Calculate the md5 checksum for the sequence only if md5 box is checked and the residues exist
  419. $residues = tripal_fields_get_field_form_values('feature__residues', $form_state);
  420. if (count($residues) > 0 && trim($residues[0]) != '') {
  421. tripal_fields_set_field_form_values ($field_name, $form_state, md5($residues[0]));
  422. }
  423. else {
  424. // Otherwise, remove the md5 value
  425. tripal_fields_set_field_form_values ($field_name, $form_state, '__NULL__');
  426. }
  427. }
  428. /**
  429. * Callback function for validating the tripal_fields_residues_textarea_widget.
  430. */
  431. function tripal_fields_residues_textarea_widget_validate($element, &$form_state) {
  432. $field_name = $element['#field_name'];
  433. // Remove any white spaces.
  434. $residues = tripal_fields_get_field_form_values($field_name, $form_state);
  435. if (count($residues) > 0) {
  436. $residues = preg_replace('/\s/', '', $residues[0]);
  437. tripal_fields_set_field_form_values($field_name, $form_state, $residues);
  438. }
  439. }
  440. /**
  441. * Callback function for validating the tripal_fields_seqlen_hidden_widget.
  442. */
  443. function tripal_fields_seqlen_hidden_widget_validate($element, &$form_state) {
  444. $field_name = $element['#field_name'];
  445. // Get the residues so we can calculate teh length.
  446. $residues = tripal_fields_get_field_form_values('feature__residues', $form_state);
  447. // Remove any white spaces.
  448. if (count($residues) > 0) {
  449. $residues = preg_replace('/\s/', '', $residues[0]);
  450. tripal_fields_set_field_form_values($field_name, $form_state, strlen($residues));
  451. }
  452. else {
  453. // Otherwise, remove the md5 value
  454. tripal_fields_set_field_form_values ($field_name, $form_state, '__NULL__');
  455. }
  456. }
  457. /**
  458. * Theme function for the primary_dbxref_widget.
  459. *
  460. * @param $variables
  461. */
  462. function theme_tripal_fields_primary_dbxref_widget($variables) {
  463. $element = $variables['element'];
  464. $layout = "
  465. <div class=\"primary-dbxref-widget\">
  466. <div class=\"primary-dbxref-widget-item\">" .
  467. drupal_render($element[0]['dbxref__db_id']) . "
  468. </div>
  469. <div class=\"primary-dbxref-widget-item\">" .
  470. drupal_render($element[0]['dbxref__accession']) . "
  471. </div>
  472. <div class=\"primary-dbxref-widget-item\">" .
  473. drupal_render($element[0]['dbxref__version']) . "
  474. </div>
  475. <div class=\"primary-dbxref-widget-item\">" .
  476. drupal_render($element[0]['dbxref__description']) . "
  477. </div>
  478. </div>
  479. ";
  480. return $layout;
  481. }
  482. /**
  483. * Returns the values of the field from the $form_state.
  484. */
  485. function tripal_fields_get_field_form_values($field_name, $form_state, $child = NULL) {
  486. $values = array();
  487. if (!array_key_exists('values', $form_state)) {
  488. return $values;
  489. }
  490. if (array_key_exists($field_name, $form_state['values'])) {
  491. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  492. foreach ($items as $delta => $value) {
  493. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  494. $values[] = $value['value'][0][$child];
  495. }
  496. else if (!$child and $value['value']) {
  497. $values[] = $value['value'];
  498. }
  499. }
  500. }
  501. }
  502. return $values;
  503. }
  504. /**
  505. * Returns the values of the field from the $form_state.
  506. */
  507. function tripal_fields_set_field_form_values($field_name, &$form_state, $newvalue, $child = NULL) {
  508. $values = array();
  509. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  510. foreach ($items as $delta => $value) {
  511. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  512. $form_state['values'][$field_name][$langcode][$delta]['value'][0][$child] = $newvalue;
  513. }
  514. else if (!$child) {
  515. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  516. }
  517. }
  518. }
  519. return $values;
  520. }
  521. /**
  522. * An Ajax callback for the tripal_fields_admin_publish_form..
  523. */
  524. function tripal_fields_primary_dbxref_widget_form_ajax_callback($form, $form_state) {
  525. $field_name = $form_state['triggering_element']['#parents'][0];
  526. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__db_id');
  527. $accession = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__accession');
  528. if (count($db_id) > 0 and count($accession) > 0) {
  529. $values = array(
  530. 'db_id' => $db_id[0],
  531. 'accession' => $accession[0],
  532. );
  533. $options = array('is_duplicate' => TRUE);
  534. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  535. if (!$has_duplicate) {
  536. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  537. }
  538. }
  539. return $form[$field_name];
  540. }