chado_feature__seqlen.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. class chado_feature__seqlen extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Sequence length'),
  9. 'description' => t('A field for calculating the length of a sequence.'),
  10. 'default_widget' => 'chado_feature__seqlen_widget',
  11. 'default_formatter' => 'chado_feature__seqlen_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_chado',
  16. 'active' => TRUE
  17. ),
  18. );
  19. }
  20. /**
  21. * @see TripalField::attach_info()
  22. */
  23. function attach_info($entity_type, $bundle, $settings) {
  24. $field_info = array();
  25. $table_name = $settings['data_table'];
  26. $type_table = $settings['type_table'];
  27. $type_field = $settings['field'];
  28. $cv_id = $settings['cv_id'];
  29. $cvterm_id = $settings['cvterm_id'];
  30. // If this is not the feature table then we don't want to attach.
  31. if ($table_name == 'feature') {
  32. $field_info = array(
  33. 'field_name' => 'feature__seqlen',
  34. 'field_type' => 'chado_feature__seqlen',
  35. 'widget_type' => 'chado_feature__seqlen_widget',
  36. 'description' => 'The length of the sequence (residues).',
  37. 'label' => 'Sequence Length',
  38. 'is_required' => 0,
  39. 'storage' => 'field_chado_storage',
  40. 'widget_settings' => array(
  41. 'display_label' => 1
  42. ),
  43. 'field_settings' => array(
  44. 'chado_table' => $table_name,
  45. 'chado_column' => 'seqlen',
  46. 'semantic_web' => 'data:1249',
  47. ),
  48. );
  49. }
  50. return $field_info;
  51. }
  52. /**
  53. * @see TripalField::widget_info()
  54. */
  55. function widget_info() {
  56. return array(
  57. 'label' => t('Sequence Length'),
  58. 'field types' => array('chado_feature__seqlen'),
  59. );
  60. }
  61. /**
  62. * @see TripalField::formatter_info()
  63. */
  64. function formatter_info() {
  65. return array(
  66. 'label' => t('Residues Length'),
  67. 'field types' => array('chado_feature__seqlen'),
  68. 'settings' => array(
  69. ),
  70. );
  71. }
  72. /**
  73. * @see TripalField::formatter_view()
  74. */
  75. function formatter_view(&$element, $entity_type, $entity, $field,
  76. $instance, $langcode, $items, $display) {
  77. foreach ($items as $delta => $item) {
  78. $element[$delta] = array(
  79. '#type' => 'markup',
  80. '#markup' => $item['value'],
  81. );
  82. }
  83. }
  84. /**
  85. * @see TripalField::widget_form()
  86. */
  87. function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  88. $settings = $field['settings'];
  89. $field_name = $field['field_name'];
  90. $field_type = $field['type'];
  91. $field_table = $field['settings']['chado_table'];
  92. $field_column = $field['settings']['chado_column'];
  93. $widget['value'] = array(
  94. '#type' => 'value',
  95. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  96. );
  97. $widget['feature__seqlen'] = array(
  98. '#type' => 'value',
  99. '#value' => 0,
  100. '#title' => $element['#title'],
  101. '#description' => $element['#description'],
  102. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  103. '#delta' => $delta,
  104. '#element_validate' => array('chado_feature__seqlen_widget_validate'),
  105. );
  106. }
  107. }
  108. /**
  109. * Callback function for validating the chado_feature__seqlen_widget.
  110. */
  111. function chado_feature__seqlen_widget_validate($element, &$form_state) {
  112. $field_name = $element['#parents'][0];
  113. // Get the residues so we can calculate teh length.
  114. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state, 0, 'feature__residues');
  115. // Remove any white spaces.
  116. if ($residues) {
  117. $residues = preg_replace('/\s/', '', $residues);
  118. tripal_chado_set_field_form_values($field_name, $form_state, strlen($residues), 0, 'feature__seqlen');
  119. }
  120. else {
  121. // Otherwise, remove the seqlen value
  122. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, 'feature_seqlen');
  123. }
  124. }