chado_feature__md5checksum.inc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. class chado_feature__md5checksum extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Sequence MD5 checksum';
  5. // The default description for this field.
  6. public static $default_description = 'A field for generating MD5 checksum for a sequence.';
  7. // Add any default settings elements. If you override the globalSettingsForm()
  8. // or the instanceSettingsForm() functions then you need to be sure that
  9. // any settings you want those functions to manage are listed in this
  10. // array.
  11. public static $default_settings = array(
  12. 'chado_table' => '',
  13. 'chado_column' => '',
  14. 'base_table' => '',
  15. );
  16. // Set this to the name of the storage backend that by default will support
  17. // this field.
  18. public static $default_storage = 'field_chado_storage';
  19. /**
  20. * @see TripalField::formatterView()
  21. */
  22. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  23. foreach ($items as $delta => $item) {
  24. $content = key_exists('value', $item) ? $item['value'] : '';
  25. $element[$delta] = array(
  26. // We create a render array to produce the desired markup,
  27. '#type' => 'markup',
  28. '#markup' => $content,
  29. );
  30. }
  31. }
  32. /**
  33. * @see TripalField::widgetForm()
  34. */
  35. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  36. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  37. $settings = $this->field['settings'];
  38. $field_name = $this->field['field_name'];
  39. $field_type = $this->field['type'];
  40. $field_table = $this->instance['settings']['chado_table'];
  41. $field_column = $this->instance['settings']['chado_column'];
  42. // Get the field defaults.
  43. $md5checksum = '';
  44. if (count($items) > 0 and array_key_exists('chado-feature__md5checksum', $items[0])) {
  45. $md5checksum = $items[0]['chado-feature__md5checksum'];
  46. }
  47. $widget['value'] = array(
  48. '#type' => 'value',
  49. '#value' => $md5checksum,
  50. );
  51. $widget['chado-feature__md5checksum'] = array(
  52. '#type' => 'value',
  53. '#value' => $md5checksum,
  54. );
  55. }
  56. /**
  57. * @see TripalField::widgetFormSubmit()
  58. */
  59. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  60. $field = $this->field;
  61. $settings = $field['settings'];
  62. $field_name = $field['field_name'];
  63. $field_type = $field['type'];
  64. $field_table = $field['settings']['chado_table'];
  65. $field_column = $field['settings']['chado_column'];
  66. // Get the residues so we can calculate teh length.
  67. $residues = isset($form_state['values']['feature__residues'][$langcode][0]['chado-feature__residues']) ? $form_state['values']['feature__residues'][$langcode][0]['chado-feature__residues'] : '';
  68. if ($residues) {
  69. // Remove spaces and new lines from the residues string.
  70. $residues = preg_replace('/\s/', '', $residues);
  71. $form_state['values']['feature__residues'][$langcode][0]['chado-feature__residues'] = $residues;
  72. $form_state['values'][$field_name][$langcode][$delta]['chado-feature__md5checksum'] = md5($residues);
  73. }
  74. else {
  75. // Otherwise, remove the md5 value
  76. $form_state['values'][$field_name][$langcode][$delta]['chado-feature__md5checksum'] = '__NULL__';
  77. }
  78. }
  79. }