md5checksum.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. *
  4. * @param unknown $entity_type
  5. * @param unknown $entity
  6. * @param unknown $field
  7. * @param unknown $instance
  8. * @param unknown $langcode
  9. * @param unknown $items
  10. * @param unknown $display
  11. */
  12. function tripal_chado_md5checksum_checkbox_formatter(&$element, $entity_type, $entity, $field,
  13. $instance, $langcode, $items, $display) {
  14. foreach ($items as $delta => $item) {
  15. $content = key_exists('value', $item) ? $item['value'] : '';
  16. $element[$delta] = array(
  17. // We create a render array to produce the desired markup,
  18. '#type' => 'markup',
  19. '#markup' => $content,
  20. );
  21. }
  22. }
  23. /**
  24. *
  25. * @param unknown $field_name
  26. * @param unknown $widget
  27. * @param unknown $form
  28. * @param unknown $form_state
  29. * @param unknown $field
  30. * @param unknown $instance
  31. * @param unknown $langcode
  32. * @param unknown $items
  33. * @param unknown $delta
  34. * @param unknown $element
  35. */
  36. function tripal_chado_md5checksum_checkbox_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  37. $widget['value'] = array(
  38. '#type' => 'checkbox',
  39. '#title' => $element['#title'],
  40. '#description' => $element['#description'],
  41. '#options' => array(0, 1),
  42. '#default_value' => 1,
  43. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  44. '#delta' => $delta,
  45. '#element_validate' => array('tripal_chado_md5checksum_checkbox_widget_validate'),
  46. );
  47. }
  48. /**
  49. * Callback function for validating the tripal_chado_md5checksum_checkbox_widget.
  50. */
  51. function tripal_chado_md5checksum_checkbox_widget_validate($element, &$form_state) {
  52. $field_name = $element['#parents'][0];
  53. // Calculate the md5 checksum for the sequence only if md5 box is checked and
  54. // the residues exist.
  55. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state);
  56. if ($residues) {
  57. $residues = preg_replace('/\s/', '', $residues);
  58. tripal_chado_set_field_form_values($field_name, $form_state, md5($residues));
  59. }
  60. else {
  61. // Otherwise, remove the md5 value
  62. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
  63. }
  64. }
  65. /**
  66. * Loads the field values with appropriate data.
  67. *
  68. * This function is called by the tripal_chado_field_storage_load() for
  69. * each property managed by the field_chado_storage storage type.
  70. *
  71. * @param $field
  72. * @param $entity
  73. * @param $record
  74. */
  75. function tripal_chado_md5checksum_field_load($field, $entity, $record) {
  76. $field_name = $field['field_name'];
  77. if ($record and property_exists($record, 'md5checksum')) {
  78. $entity->{$field_name}['und'][] = array('value' => $record->md5checksum);
  79. }
  80. }