chado_feature__seqlen.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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' => array(
  47. 'name' => 'Sequence length',
  48. 'accession' => '1249',
  49. 'ns' => 'data',
  50. 'nsurl' => 'http://edamontology.org/',
  51. ),
  52. ),
  53. );
  54. }
  55. return $field_info;
  56. }
  57. /**
  58. * @see TripalField::widget_info()
  59. */
  60. function widget_info() {
  61. return array(
  62. 'label' => t('Sequence Length'),
  63. 'field types' => array('chado_feature__seqlen'),
  64. );
  65. }
  66. /**
  67. * @see TripalField::formatter_info()
  68. */
  69. function formatter_info() {
  70. return array(
  71. 'label' => t('Residues Length'),
  72. 'field types' => array('chado_feature__seqlen'),
  73. 'settings' => array(
  74. ),
  75. );
  76. }
  77. /**
  78. * @see TripalField::formatter_view()
  79. */
  80. function formatter_view(&$element, $entity_type, $entity, $field,
  81. $instance, $langcode, $items, $display) {
  82. foreach ($items as $delta => $item) {
  83. $element[$delta] = array(
  84. '#type' => 'markup',
  85. '#markup' => $item['value'],
  86. );
  87. }
  88. }
  89. /**
  90. * @see TripalField::widget_form()
  91. */
  92. function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  93. $settings = $field['settings'];
  94. $field_name = $field['field_name'];
  95. $field_type = $field['type'];
  96. $field_table = $field['settings']['chado_table'];
  97. $field_column = $field['settings']['chado_column'];
  98. $widget['value'] = array(
  99. '#type' => 'value',
  100. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  101. );
  102. $widget['feature__seqlen'] = array(
  103. '#type' => 'value',
  104. '#value' => 0,
  105. '#title' => $element['#title'],
  106. '#description' => $element['#description'],
  107. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  108. '#delta' => $delta,
  109. '#element_validate' => array('chado_feature__seqlen_widget_validate'),
  110. );
  111. }
  112. }
  113. /**
  114. * Callback function for validating the chado_feature__seqlen_widget.
  115. */
  116. function chado_feature__seqlen_widget_validate($element, &$form_state) {
  117. $field_name = $element['#parents'][0];
  118. // Get the residues so we can calculate teh length.
  119. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state, 0, 'feature__residues');
  120. // Remove any white spaces.
  121. if ($residues) {
  122. $residues = preg_replace('/\s/', '', $residues);
  123. tripal_chado_set_field_form_values($field_name, $form_state, strlen($residues), 0, 'feature__seqlen');
  124. }
  125. else {
  126. // Otherwise, remove the seqlen value
  127. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, 'feature_seqlen');
  128. }
  129. }