chado_feature__md5checksum.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. class chado_feature__md5checksum extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('MD5 checksum'),
  9. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  10. 'default_widget' => 'chado_feature__md5checksum_widget',
  11. 'default_formatter' => 'chado_feature__md5checksum_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_chado',
  16. 'active' => TRUE
  17. ),
  18. );
  19. }
  20. /**
  21. * @see TripalField::attach_info()
  22. */
  23. function attach_info($entity_type, $bundle, $settings) {
  24. $field_info = array();
  25. $table_name = $settings['data_table'];
  26. $type_table = $settings['type_table'];
  27. $type_field = $settings['field'];
  28. $cv_id = $settings['cv_id'];
  29. $cvterm_id = $settings['cvterm_id'];
  30. // If this is not the feature table then we don't want to attach.
  31. if ($table_name != 'feature') {
  32. return $field_info;
  33. }
  34. // Create an array with information about this field.
  35. $field_info = array(
  36. 'field_name' => 'feature__md5checksum',
  37. 'field_type' => 'chado_feature__md5checksum',
  38. 'widget_type' => 'chado_feature__md5checksum_widget',
  39. 'description' => 'The MD5 checksum for the sequence.',
  40. 'label' => 'MD5 Checksum',
  41. 'is_required' => 0,
  42. 'storage' => 'field_chado_storage',
  43. 'widget_settings' => array(
  44. 'display_label' => 1
  45. ),
  46. 'field_settings' => array(
  47. 'chado_table' => $table_name,
  48. 'chado_column' => 'md5checksum',
  49. 'semantic_web' => array(
  50. 'name' => 'md5_checksum',
  51. 'accession' => 'md5_checksum',
  52. 'ns' => 'local',
  53. 'nsurl' => '',
  54. ),
  55. ),
  56. );
  57. return $field_info;
  58. }
  59. /**
  60. * @see TripalField::widget_info()
  61. */
  62. function widget_info() {
  63. return array(
  64. 'label' => t('MD5 Checksum Checkbox'),
  65. 'field types' => array('chado_feature__md5checksum'),
  66. );
  67. }
  68. /**
  69. * @see TripalField::formatter_info()
  70. */
  71. function formatter_info() {
  72. return array(
  73. 'label' => t('MD5 Checksum'),
  74. 'field types' => array('chado_feature__md5checksum'),
  75. 'settings' => array(
  76. ),
  77. );
  78. }
  79. /**
  80. * @see TripalField::formatter_view()
  81. */
  82. function formatter_view(&$element, $entity_type, $entity, $field,
  83. $instance, $langcode, $items, $display) {
  84. foreach ($items as $delta => $item) {
  85. $content = key_exists('value', $item) ? $item['value'] : '';
  86. $element[$delta] = array(
  87. // We create a render array to produce the desired markup,
  88. '#type' => 'markup',
  89. '#markup' => $content,
  90. );
  91. }
  92. }
  93. /**
  94. * @see TripalField::widget_form()
  95. */
  96. function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  97. $widget['value'] = array(
  98. '#type' => 'checkbox',
  99. '#title' => $element['#title'],
  100. '#description' => $element['#description'],
  101. '#options' => array(0, 1),
  102. '#default_value' => 1,
  103. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  104. '#delta' => $delta,
  105. '#element_validate' => array('chado_feature__md5checksum_widget_validate'),
  106. );
  107. }
  108. /**
  109. * @see TripalField::load()
  110. */
  111. function load($field, $entity, $details) {
  112. $record = $details['record'];
  113. $field_name = $field['field_name'];
  114. if ($record and property_exists($record, 'md5checksum')) {
  115. $entity->{$field_name}['und'][] = array('value' => $record->md5checksum);
  116. }
  117. }
  118. }
  119. /**
  120. * Callback function for validating the chado_feature__md5checksum_widget.
  121. */
  122. function chado_feature__md5checksum_widget_validate($element, &$form_state) {
  123. $field_name = $element['#parents'][0];
  124. // Calculate the md5 checksum for the sequence only if md5 box is checked and
  125. // the residues exist.
  126. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state);
  127. if ($residues) {
  128. $residues = preg_replace('/\s/', '', $residues);
  129. tripal_chado_set_field_form_values($field_name, $form_state, md5($residues));
  130. }
  131. else {
  132. // Otherwise, remove the md5 value
  133. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
  134. }
  135. }