chado_feature__md5checksum.inc 6.8 KB

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