chado_feature__seqlen.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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::can_attach()
  22. */
  23. protected function can_attach($entity_type, $bundle, $details) {
  24. $table_name = $details['chado_table'];
  25. $type_table = $details['chado_type_table'];
  26. $type_field = $details['chado_type_column'];
  27. $cv_id = $details['chado_cv_id'];
  28. $cvterm_id = $details['chado_cvterm_id'];
  29. if ($table_name == 'feature') {
  30. return TRUE;
  31. }
  32. return FALSE;
  33. }
  34. /**
  35. * @see TripalField::create_info()
  36. */
  37. function create_info($entity_type, $bundle, $details) {
  38. if (!$this->can_attach($entity_type, $bundle, $details)) {
  39. return;
  40. }
  41. $table_name = $details['chado_table'];
  42. $type_table = $details['chado_type_table'];
  43. $type_field = $details['chado_type_column'];
  44. $cv_id = $details['chado_cv_id'];
  45. $cvterm_id = $details['chado_cvterm_id'];
  46. return array(
  47. 'field_name' => 'feature__seqlen',
  48. 'type' => 'chado_feature__seqlen',
  49. 'cardinality' => 1,
  50. 'locked' => FALSE,
  51. 'storage' => array(
  52. 'type' => 'field_chado_storage',
  53. ),
  54. 'settings' => array(
  55. 'chado_table' => $table_name,
  56. 'chado_column' => 'seqlen',
  57. 'semantic_web' => 'data:1249',
  58. ),
  59. );
  60. }
  61. /**
  62. * @see TripalField::create_instance_info()
  63. */
  64. function create_instance_info($entity_type, $bundle, $details) {
  65. if (!$this->can_attach($entity_type, $bundle, $details)) {
  66. return;
  67. }
  68. $table_name = $details['chado_table'];
  69. $type_table = $details['chado_type_table'];
  70. $type_field = $details['chado_type_column'];
  71. $cv_id = $details['chado_cv_id'];
  72. $cvterm_id = $details['chado_cvterm_id'];
  73. return array(
  74. 'field_name' => 'feature__seqlen',
  75. 'entity_type' => $entity_type,
  76. 'bundle' => $bundle->name,
  77. 'label' => 'Raw Sequence Length',
  78. 'description' => 'The number of residues in the raw sequence. This length
  79. is only for the assigned raw sequence and does not represent the length of any
  80. sequences derived from alignments. If this value is zero but aligned sequences
  81. are present then this record has no official assigned sequence.',
  82. 'required' => FALSE,
  83. 'settings' => array(
  84. 'auto_attach' => FALSE,
  85. ),
  86. 'widget' => array(
  87. 'type' => 'chado_feature__seqlen_widget',
  88. 'settings' => array(
  89. 'display_label' => 1,
  90. ),
  91. ),
  92. 'display' => array(
  93. 'deafult' => array(
  94. 'label' => 'above',
  95. 'type' => 'chado_feature__seqlen_formatter',
  96. 'settings' => array(),
  97. ),
  98. ),
  99. );
  100. }
  101. /**
  102. * @see TripalField::widget_info()
  103. */
  104. function widget_info() {
  105. return array(
  106. 'label' => t('Sequence Length'),
  107. 'field types' => array('chado_feature__seqlen'),
  108. );
  109. }
  110. /**
  111. * @see TripalField::formatter_info()
  112. */
  113. function formatter_info() {
  114. return array(
  115. 'label' => t('Residues Length'),
  116. 'field types' => array('chado_feature__seqlen'),
  117. 'settings' => array(
  118. ),
  119. );
  120. }
  121. /**
  122. * @see TripalField::formatter_view()
  123. */
  124. function formatter_view(&$element, $entity_type, $entity, $field,
  125. $instance, $langcode, $items, $display) {
  126. foreach ($items as $delta => $item) {
  127. $element[$delta] = array(
  128. '#type' => 'markup',
  129. '#markup' => $item['value'],
  130. );
  131. }
  132. }
  133. /**
  134. * @see TripalField::widget_form()
  135. */
  136. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  137. $langcode, $items, $delta, $element) {
  138. $settings = $field['settings'];
  139. $field_name = $field['field_name'];
  140. $field_type = $field['type'];
  141. $field_table = $field['settings']['chado_table'];
  142. $field_column = $field['settings']['chado_column'];
  143. $widget['value'] = array(
  144. '#type' => 'value',
  145. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  146. );
  147. $widget['feature__seqlen'] = array(
  148. '#type' => 'value',
  149. '#value' => 0,
  150. '#title' => $element['#title'],
  151. '#description' => $element['#description'],
  152. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  153. '#delta' => $delta,
  154. '#element_validate' => array('chado_feature__seqlen_widget_validate'),
  155. );
  156. }
  157. }
  158. /**
  159. * Callback function for validating the chado_feature__seqlen_widget.
  160. */
  161. function chado_feature__seqlen_widget_validate($element, &$form_state) {
  162. $field_name = $element['#parents'][0];
  163. // Get the residues so we can calculate teh length.
  164. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state, 0, 'feature__residues');
  165. // Remove any white spaces.
  166. if ($residues) {
  167. $residues = preg_replace('/\s/', '', $residues);
  168. tripal_chado_set_field_form_values($field_name, $form_state, strlen($residues), 0, 'feature__seqlen');
  169. }
  170. else {
  171. // Otherwise, remove the seqlen value
  172. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, 'feature_seqlen');
  173. }
  174. }