chado_feature__md5checksum.inc 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $settings = $this->field['settings'];
  38. $field_name = $this->field['field_name'];
  39. $field_type = $this->field['type'];
  40. $field_table = $this->field['settings']['chado_table'];
  41. $field_column = $this->field['settings']['chado_column'];
  42. // Get the field defaults.
  43. $md5checksum = '';
  44. if (count($items) > 0 and array_key_exists('feature__md5checksum', $items[0])) {
  45. $md5checksum = $items[0]['feature__md5checksum'];
  46. }
  47. if (array_key_exists('values', $form_state)) {
  48. //$md5checksum = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'feature__md5checksum');
  49. }
  50. $widget['value'] = array(
  51. '#type' => 'value',
  52. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  53. );
  54. $widget['feature__md5checksum'] = array(
  55. '#type' => 'value',
  56. '#value' => $md5checksum,
  57. // '#element_validate' => array('chado_feature__md5checksum_widget_validate'),
  58. );
  59. }
  60. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  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. // Calculate the md5 checksum for the sequence only if md5 box is checked and
  68. // the residues exist.
  69. //$residues = tripal_chado_get_field_form_values('feature__residues', $form_state, 0, 'feature__residues');
  70. if ($residues) {
  71. $residues = preg_replace('/\s/', '', $residues);
  72. tripal_chado_set_field_form_values($field_name, $form_state, md5($residues), 0, 'feature__md5checksum');
  73. }
  74. else {
  75. // Otherwise, remove the md5 value
  76. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_table . '__md5checksum');
  77. }
  78. }
  79. }