chado_base__md5checksum.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. class chado_base__md5checksum extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('MD5 checksum'),
  9. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  10. 'default_widget' => 'chado_base__md5checksum_widget',
  11. 'default_formatter' => 'chado_base__md5checksum_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. $md5_fieldname = '';
  31. $schema = chado_get_schema($table_name);
  32. // Only attach to tables that have md5checksum columns. Currently for
  33. // Chado v1.3 this is only the feature and expression tables.
  34. if (array_key_exists('md5checksum', $schema['fields'])) {
  35. if ($table_name == 'feature') {
  36. $md5_fieldname = 'residues';
  37. }
  38. else if ($table_name == 'expression') {
  39. $md5_fieldname = 'uniquename';
  40. }
  41. else {
  42. return $field_info;
  43. }
  44. }
  45. else {
  46. return $field_info;
  47. }
  48. // Create an array with information about this field.
  49. $field_info = array(
  50. 'field_name' => 'base__md5checksum',
  51. 'field_type' => 'chado_base__md5checksum',
  52. 'widget_type' => 'chado_base__md5checksum_widget',
  53. 'description' => 'The MD5 checksum for the sequence.',
  54. 'label' => 'MD5 Checksum',
  55. 'is_required' => 0,
  56. 'storage' => 'field_chado_storage',
  57. 'widget_settings' => array(
  58. 'display_label' => 1,
  59. 'md5_fieldname' => $md5_fieldname,
  60. ),
  61. 'field_settings' => array(
  62. 'chado_table' => $table_name,
  63. 'chado_column' => 'md5checksum',
  64. 'semantic_web' => array(
  65. 'name' => 'md5_checksum',
  66. 'accession' => 'md5_checksum',
  67. 'ns' => 'local',
  68. 'nsurl' => '',
  69. ),
  70. ),
  71. );
  72. return $field_info;
  73. }
  74. /**
  75. * @see TripalField::widget_info()
  76. */
  77. function widget_info() {
  78. return array(
  79. 'label' => t('MD5 Checksum Checkbox'),
  80. 'field types' => array('chado_base__md5checksum'),
  81. );
  82. }
  83. /**
  84. * @see TripalField::formatter_info()
  85. */
  86. function formatter_info() {
  87. return array(
  88. 'label' => t('MD5 Checksum'),
  89. 'field types' => array('chado_base__md5checksum'),
  90. 'settings' => array(
  91. ),
  92. );
  93. }
  94. /**
  95. * @see TripalField::formatter_view()
  96. */
  97. function formatter_view(&$element, $entity_type, $entity, $field,
  98. $instance, $langcode, $items, $display) {
  99. foreach ($items as $delta => $item) {
  100. $content = key_exists('value', $item) ? $item['value'] : '';
  101. $element[$delta] = array(
  102. // We create a render array to produce the desired markup,
  103. '#type' => 'markup',
  104. '#markup' => $content,
  105. );
  106. }
  107. }
  108. /**
  109. * @see TripalField::widget_form()
  110. */
  111. function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  112. $settings = $field['settings'];
  113. $field_name = $field['field_name'];
  114. $field_type = $field['type'];
  115. $field_table = $field['settings']['chado_table'];
  116. $field_column = $field['settings']['chado_column'];
  117. $form['value'] = array(
  118. '#type' => 'value',
  119. '#value' => $items[0]['value'],
  120. );
  121. $widget[$field_table . '__md5checksum'] = array(
  122. '#type' => 'checkbox',
  123. '#title' => $element['#title'],
  124. '#description' => $element['#description'],
  125. '#options' => array(0, 1),
  126. '#default_value' => '',
  127. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  128. '#delta' => $delta,
  129. '#element_validate' => array('chado_base__md5checksum_widget_validate'),
  130. );
  131. }
  132. /**
  133. * @see TripalField::load()
  134. */
  135. function load($field, $entity, $details) {
  136. $record = $details['record'];
  137. $settings = $field['settings'];
  138. $field_name = $field['field_name'];
  139. $field_type = $field['type'];
  140. $field_table = $field['settings']['chado_table'];
  141. $field_column = $field['settings']['chado_column'];
  142. $entity->{$field_name}['und'][0]['value'] = $record->md5checksum;
  143. $entity->{$field_name}['und'][0][$field_table . '__md5checksum'] = $record->md5checksum;
  144. }
  145. }
  146. /**
  147. * Callback function for validating the chado_base__md5checksum_widget.
  148. */
  149. function chado_base__md5checksum_widget_validate($element, &$form_state) {
  150. $field_name = $element['#parents'][0];
  151. $field = $form_state['field'][$field_name]['und']['field'];
  152. $settings = $field['settings'];
  153. $field_name = $field['field_name'];
  154. $field_type = $field['type'];
  155. $field_table = $field['settings']['chado_table'];
  156. $field_column = $field['settings']['chado_column'];
  157. // Calculate the md5 checksum for the sequence only if md5 box is checked and
  158. // the residues exist.
  159. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state, 0, 'feature__residues');
  160. if ($residues) {
  161. $residues = preg_replace('/\s/', '', $residues);
  162. tripal_chado_set_field_form_values($field_name, $form_state, md5($residues), 0, $field_table . '__md5checksum');
  163. }
  164. else {
  165. // Otherwise, remove the md5 value
  166. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_table . '__md5checksum');
  167. }
  168. }