data__sequence_checksum_widget.inc 2.7 KB

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