chado_feature__md5checksum.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. 'semantic_web' => '',
  16. );
  17. // Set this to the name of the storage backend that by default will support
  18. // this field.
  19. public static $default_storage = 'field_chado_storage';
  20. /**
  21. * @see TripalField::formatterView()
  22. */
  23. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  24. foreach ($items as $delta => $item) {
  25. $content = key_exists('value', $item) ? $item['value'] : '';
  26. $element[$delta] = array(
  27. // We create a render array to produce the desired markup,
  28. '#type' => 'markup',
  29. '#markup' => $content,
  30. );
  31. }
  32. }
  33. /**
  34. * @see TripalField::widgetForm()
  35. */
  36. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  37. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  38. $settings = $this->field['settings'];
  39. $field_name = $this->field['field_name'];
  40. $field_type = $this->field['type'];
  41. $field_table = $this->field['settings']['chado_table'];
  42. $field_column = $this->field['settings']['chado_column'];
  43. // Get the field defaults.
  44. $md5checksum = '';
  45. if (count($items) > 0 and array_key_exists('chado-feature__md5checksum', $items[0])) {
  46. $md5checksum = $items[0]['chado-feature__md5checksum'];
  47. }
  48. $widget['value'] = array(
  49. '#type' => 'value',
  50. '#value' => $md5checksum,
  51. );
  52. $widget['chado-feature__md5checksum'] = array(
  53. '#type' => 'value',
  54. '#value' => $md5checksum,
  55. );
  56. }
  57. /**
  58. * @see TripalField::widgetFormSubmit()
  59. */
  60. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  61. $field = $this->field;
  62. $settings = $field['settings'];
  63. $field_name = $field['field_name'];
  64. $field_type = $field['type'];
  65. $field_table = $field['settings']['chado_table'];
  66. $field_column = $field['settings']['chado_column'];
  67. // Get the residues so we can calculate teh length.
  68. $residues = $form_state['values']['feature__residues']['und'][0]['chado-feature__residues'];
  69. if ($residues) {
  70. $residues = preg_replace('/\s/', '', $residues);
  71. $form_state['values']['feature__residues']['und'][0]['chado-feature__residues'] = $residues;
  72. $items[0]['chado-feature__md5checksum'] = md5($residues);
  73. }
  74. else {
  75. // Otherwise, remove the md5 value
  76. $items[0]['chado-feature__md5checksum'] = '__NULL__';
  77. }
  78. }
  79. }