chado_feature__md5checksum.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. */
  8. function chado_feature__md5checksum_info() {
  9. return array(
  10. 'label' => t('MD5 checksum'),
  11. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  12. 'default_widget' => 'chado_feature__md5checksum_widget',
  13. 'default_formatter' => 'chado_feature__md5checksum_formatter',
  14. 'settings' => array(),
  15. 'storage' => array(
  16. 'type' => 'field_chado_storage',
  17. 'module' => 'tripal_chado',
  18. 'active' => TRUE
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements hook_widget_info.
  24. *
  25. * This is a hook provided by the tripal_chado module for offloading
  26. * the hook_field_widget_info() hook for each field to specify.
  27. */
  28. function chado_feature__md5checksum_widget_info() {
  29. return array(
  30. 'label' => t('MD5 Checksum Checkbox'),
  31. 'field types' => array('chado_feature__md5checksum'),
  32. );
  33. }
  34. /**
  35. * Implements hook_formatter_info.
  36. *
  37. * This is a hook provided by the tripal_chado module for
  38. * offloading the hook_field_formatter_info() for each field
  39. * to specify.
  40. *
  41. */
  42. function chado_feature__md5checksum_formatter_info() {
  43. return array(
  44. 'label' => t('MD5 Checksum'),
  45. 'field types' => array('chado_feature__md5checksum'),
  46. 'settings' => array(
  47. ),
  48. );
  49. }
  50. /**
  51. *
  52. * @param unknown $entity_type
  53. * @param unknown $entity
  54. * @param unknown $field
  55. * @param unknown $instance
  56. * @param unknown $langcode
  57. * @param unknown $items
  58. * @param unknown $display
  59. */
  60. function chado_feature__md5checksum_formatter(&$element, $entity_type, $entity, $field,
  61. $instance, $langcode, $items, $display) {
  62. foreach ($items as $delta => $item) {
  63. $content = key_exists('value', $item) ? $item['value'] : '';
  64. $element[$delta] = array(
  65. // We create a render array to produce the desired markup,
  66. '#type' => 'markup',
  67. '#markup' => $content,
  68. );
  69. }
  70. }
  71. /**
  72. *
  73. * @param unknown $field_name
  74. * @param unknown $widget
  75. * @param unknown $form
  76. * @param unknown $form_state
  77. * @param unknown $field
  78. * @param unknown $instance
  79. * @param unknown $langcode
  80. * @param unknown $items
  81. * @param unknown $delta
  82. * @param unknown $element
  83. */
  84. function chado_feature__md5checksum_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  85. $widget['value'] = array(
  86. '#type' => 'checkbox',
  87. '#title' => $element['#title'],
  88. '#description' => $element['#description'],
  89. '#options' => array(0, 1),
  90. '#default_value' => 1,
  91. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  92. '#delta' => $delta,
  93. '#element_validate' => array('chado_feature__md5checksum_widget_validate'),
  94. );
  95. }
  96. /**
  97. * Callback function for validating the chado_feature__md5checksum_widget.
  98. */
  99. function chado_feature__md5checksum_widget_validate($element, &$form_state) {
  100. $field_name = $element['#parents'][0];
  101. // Calculate the md5 checksum for the sequence only if md5 box is checked and
  102. // the residues exist.
  103. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state);
  104. if ($residues) {
  105. $residues = preg_replace('/\s/', '', $residues);
  106. tripal_chado_set_field_form_values($field_name, $form_state, md5($residues));
  107. }
  108. else {
  109. // Otherwise, remove the md5 value
  110. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
  111. }
  112. }
  113. /**
  114. * Loads the field values with appropriate data.
  115. *
  116. * This function is called by the tripal_chado_field_storage_load() for
  117. * each property managed by the field_chado_storage storage type.
  118. *
  119. * @param $field
  120. * @param $entity
  121. * @param $record
  122. */
  123. function chado_feature__md5checksum_load($field, $entity, $record) {
  124. $field_name = $field['field_name'];
  125. if ($record and property_exists($record, 'md5checksum')) {
  126. $entity->{$field_name}['und'][] = array('value' => $record->md5checksum);
  127. }
  128. }