data__sequence_checksum_widget.inc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. class data__sequence_checksum_widget extends TripalFieldWidget {
  3. // The default lable for this field.
  4. public static $label = 'Sequence MD5 checksum';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('no_widget');
  7. /**
  8. *
  9. * @param unknown $field
  10. * @param unknown $instance
  11. */
  12. public function __construct($field, $instance = NULL) {
  13. $this->field = $field;
  14. $this->instance = $instance;
  15. }
  16. /**
  17. *
  18. * @see TripalFieldWidget::form()
  19. */
  20. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  21. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  22. $settings = $this->field['settings'];
  23. $field_name = $this->field['field_name'];
  24. $field_type = $this->field['type'];
  25. $field_table = $this->field['settings']['chado_table'];
  26. $field_column = $this->field['settings']['chado_column'];
  27. // Get the field defaults.
  28. $md5checksum = '';
  29. if (count($items) > 0 and array_key_exists('chado-feature__md5checksum', $items[0])) {
  30. $md5checksum = $items[0]['chado-feature__md5checksum'];
  31. }
  32. $widget['value'] = array(
  33. '#type' => 'value',
  34. '#value' => $md5checksum,
  35. );
  36. $widget['chado-feature__md5checksum'] = array(
  37. '#type' => 'value',
  38. '#value' => $md5checksum,
  39. );
  40. }
  41. /**
  42. * Performs validation of the widgetForm.
  43. *
  44. * Use this validate to ensure that form values are entered correctly. Note
  45. * this is different from the validate() function which ensures that the
  46. * field data meets expectations.
  47. *
  48. * @param $form
  49. * @param $form_state
  50. */
  51. public function validate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  52. }
  53. /**
  54. *
  55. * @see TripalFieldWidget::submit()
  56. */
  57. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  58. $field = $this->field;
  59. $settings = $field['settings'];
  60. $field_name = $field['field_name'];
  61. $field_type = $field['type'];
  62. $field_table = $field['settings']['chado_table'];
  63. $field_column = $field['settings']['chado_column'];
  64. // Get the residues so we can calculate teh length.
  65. $residues = isset($form_state['values']['feature__residues'][$langcode][0]['chado-feature__residues']) ? $form_state['values']['feature__residues'][$langcode][0]['chado-feature__residues'] : '';
  66. if ($residues) {
  67. // Remove spaces and new lines from the residues string.
  68. $residues = preg_replace('/\s/', '', $residues);
  69. $form_state['values']['feature__residues'][$langcode][0]['chado-feature__residues'] = $residues;
  70. $form_state['values'][$field_name][$langcode][$delta]['chado-feature__md5checksum'] = md5($residues);
  71. }
  72. else {
  73. // Otherwise, remove the md5 value
  74. $form_state['values'][$field_name][$langcode][$delta]['chado-feature__md5checksum'] = '__NULL__';
  75. }
  76. }
  77. }