chado_feature__seqlen.inc 5.4 KB

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