data__sequence_checksum_widget.inc

File

tripal_chado/includes/TripalFields/data__sequence_checksum/data__sequence_checksum_widget.inc
View source
  1. <?php
  2. class data__sequence_checksum_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_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->instance['settings']['chado_table'];
  17. $field_column = $this->instance['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-' . $field_table . '__md5checksum'] = array(
  28. '#type' => 'value',
  29. '#value' => $md5checksum,
  30. );
  31. }
  32. /**
  33. *
  34. * @see TripalFieldWidget::submit()
  35. */
  36. public function validate($element, $form, &$form_state, $langcode, $delta) {
  37. $field = $this->field;
  38. $settings = $field['settings'];
  39. $field_name = $field['field_name'];
  40. $field_type = $field['type'];
  41. $field_table = $this->instance['settings']['chado_table'];
  42. $field_column = $this->instance['settings']['chado_column'];
  43. // Get the residues so we can calculate the length.
  44. if ($form_state['values']['data__sequence']['und'][0]['chado-feature__residues']){
  45. // Remove spaces and new lines from the residues string.
  46. $residues = $form_state['values']['data__sequence']['und'][0]['chado-feature__residues'];
  47. $residues = preg_replace('/\s/', '', $residues);
  48. $checksum = md5($residues);
  49. $form_state['values'][$field_name]['und'][0]['chado-feature__md5checksum'] = $checksum;
  50. $form_state['values'][$field_name]['und'][0]['value'] = $checksum;
  51. }
  52. else {
  53. // Otherwise, remove the md5 value
  54. $form_state['values'][$field_name]['und'][0]['chado-feature__md5checksum'] = '__NULL__';
  55. }
  56. }
  57. }