chado_feature__md5checksum.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. class chado_feature__md5checksum extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Sequence MD5 checksum'),
  9. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  10. 'default_widget' => 'chado_feature__md5checksum_widget',
  11. 'default_formatter' => 'chado_feature__md5checksum_formatter',
  12. 'storage' => array(
  13. 'type' => 'field_chado_storage',
  14. 'module' => 'tripal_chado',
  15. 'active' => TRUE
  16. ),
  17. );
  18. }
  19. /**
  20. * @see TripalField::can_attach()
  21. */
  22. protected function can_attach($entity_type, $bundle, $details) {
  23. $table_name = $details['chado_table'];
  24. $type_table = $details['chado_type_table'];
  25. $type_field = $details['chado_type_column'];
  26. $cv_id = $details['chado_cv_id'];
  27. $cvterm_id = $details['chado_cvterm_id'];
  28. if ($table_name == 'feature') {
  29. return TRUE;
  30. }
  31. return FALSE;
  32. }
  33. /**
  34. * @see TripalField::create_info()
  35. */
  36. function create_info($entity_type, $bundle, $details) {
  37. if (!$this->can_attach($entity_type, $bundle, $details)) {
  38. return;
  39. }
  40. $table_name = $details['chado_table'];
  41. $type_table = $details['chado_type_table'];
  42. $type_field = $details['chado_type_column'];
  43. $cv_id = $details['chado_cv_id'];
  44. $cvterm_id = $details['chado_cvterm_id'];
  45. return array(
  46. 'field_name' => $table_name . '__md5checksum',
  47. 'type' => 'chado_feature__md5checksum',
  48. 'cardinality' => 1,
  49. 'locked' => FALSE,
  50. 'storage' => array(
  51. 'type' => 'field_chado_storage',
  52. ),
  53. 'settings' => array(
  54. 'chado_table' => $table_name,
  55. 'chado_column' => 'md5checksum',
  56. 'semantic_web' => 'data:2190',
  57. ),
  58. );
  59. }
  60. /**
  61. * @see TripalField::create_instance_info()
  62. */
  63. function create_instance_info($entity_type, $bundle, $details) {
  64. if (!$this->can_attach($entity_type, $bundle, $details)) {
  65. return;
  66. }
  67. $table_name = $details['chado_table'];
  68. $type_table = $details['chado_type_table'];
  69. $type_field = $details['chado_type_column'];
  70. $cv_id = $details['chado_cv_id'];
  71. $cvterm_id = $details['chado_cvterm_id'];
  72. return array(
  73. 'field_name' => $table_name . '__md5checksum',
  74. 'entity_type' => $entity_type,
  75. 'bundle' => $bundle->name,
  76. 'label' => 'Sequence Checksum',
  77. 'description' => 'The MD5 checksum for the sequence. The checksum here
  78. will always be unique for the raw unformatted sequence. To verify that the
  79. sequence has not been corrupted, download the raw sequence and use an MD5 tool
  80. to calculate the value. If the value calculated is identical the one shown
  81. here, then the downloaded sequence is uncorrupted.',
  82. 'required' => FALSE,
  83. 'settings' => array(
  84. 'auto_attach' => FALSE,
  85. ),
  86. 'widget' => array(
  87. 'type' => 'chado_feature__md5checksum_widget',
  88. 'settings' => array(
  89. 'display_label' => 1,
  90. 'md5_fieldname' => 'feature__md5checksum',
  91. ),
  92. ),
  93. 'display' => array(
  94. 'deafult' => array(
  95. 'label' => 'above',
  96. 'type' => 'chado_feature__md5checksum_formatter',
  97. 'settings' => array(),
  98. ),
  99. ),
  100. );
  101. }
  102. /**
  103. * @see TripalField::widget_info()
  104. */
  105. function widget_info() {
  106. return array(
  107. 'label' => t('MD5 Checksum'),
  108. 'field types' => array('chado_feature__md5checksum'),
  109. );
  110. }
  111. /**
  112. * @see TripalField::formatter_info()
  113. */
  114. function formatter_info() {
  115. return array(
  116. 'label' => t('MD5 Checksum'),
  117. 'field types' => array('chado_feature__md5checksum'),
  118. 'settings' => array(
  119. ),
  120. );
  121. }
  122. /**
  123. * @see TripalField::formatter_view()
  124. */
  125. function formatter_view(&$element, $entity_type, $entity, $field,
  126. $instance, $langcode, $items, $display) {
  127. foreach ($items as $delta => $item) {
  128. $content = key_exists('value', $item) ? $item['value'] : '';
  129. $element[$delta] = array(
  130. // We create a render array to produce the desired markup,
  131. '#type' => 'markup',
  132. '#markup' => $content,
  133. );
  134. }
  135. }
  136. /**
  137. * @see TripalField::widget_form()
  138. */
  139. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  140. $langcode, $items, $delta, $element) {
  141. $settings = $field['settings'];
  142. $field_name = $field['field_name'];
  143. $field_type = $field['type'];
  144. $field_table = $field['settings']['chado_table'];
  145. $field_column = $field['settings']['chado_column'];
  146. // Get the field defaults.
  147. $md5checksum = '';
  148. if (count($items) > 0 and array_key_exists('feature__md5checksum', $items[0])) {
  149. $md5checksum = $items[0]['feature__md5checksum'];
  150. }
  151. if (array_key_exists('values', $form_state)) {
  152. $md5checksum = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'feature__md5checksum');
  153. }
  154. $widget['value'] = array(
  155. '#type' => 'value',
  156. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  157. );
  158. $widget['feature__md5checksum'] = array(
  159. '#type' => 'value',
  160. '#value' => $md5checksum,
  161. '#element_validate' => array('chado_feature__md5checksum_widget_validate'),
  162. );
  163. }
  164. }
  165. /**
  166. * Callback function for validating the chado_feature__md5checksum_widget.
  167. */
  168. function chado_feature__md5checksum_widget_validate($element, &$form_state) {
  169. $field_name = $element['#parents'][0];
  170. $field = $form_state['field'][$field_name]['und']['field'];
  171. $settings = $field['settings'];
  172. $field_name = $field['field_name'];
  173. $field_type = $field['type'];
  174. $field_table = $field['settings']['chado_table'];
  175. $field_column = $field['settings']['chado_column'];
  176. // Calculate the md5 checksum for the sequence only if md5 box is checked and
  177. // the residues exist.
  178. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state, 0, 'feature__residues');
  179. if ($residues) {
  180. $residues = preg_replace('/\s/', '', $residues);
  181. tripal_chado_set_field_form_values($field_name, $form_state, md5($residues), 0, 'feature__md5checksum');
  182. }
  183. else {
  184. // Otherwise, remove the md5 value
  185. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_table . '__md5checksum');
  186. }
  187. }