chado_feature__md5checksum.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. class chado_feature__md5checksum extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Sequence MD5 checksum';
  5. // The default description for this field.
  6. public static $default_description = 'A field for generating MD5 checksum for a sequence.';
  7. // Add any default settings elements. If you override the globalSettingsForm()
  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__md5checksum',
  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' => 'md5checksum',
  43. 'semantic_web' => tripal_get_chado_semweb_term($table_name, 'md5checksum'),
  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' => 'Sequence Checksum',
  64. 'description' => 'The MD5 checksum for the sequence. The checksum here
  65. will always be unique for the raw unformatted sequence. To verify that the
  66. sequence has not been corrupted, download the raw sequence and use an MD5 tool
  67. to calculate the value. If the value calculated is identical the one shown
  68. here, then the downloaded sequence is uncorrupted.',
  69. 'required' => FALSE,
  70. 'settings' => array(
  71. 'auto_attach' => TRUE,
  72. ),
  73. 'widget' => array(
  74. 'type' => 'chado_feature__md5checksum_widget',
  75. 'settings' => array(
  76. 'display_label' => 1,
  77. 'md5_fieldname' => 'feature__md5checksum',
  78. ),
  79. ),
  80. 'display' => array(
  81. 'default' => array(
  82. 'label' => 'inline',
  83. 'type' => 'chado_feature__md5checksum_formatter',
  84. 'settings' => array(),
  85. ),
  86. ),
  87. );
  88. }
  89. /**
  90. * @see TripalField::widgetInfo()
  91. */
  92. public static function widgetInfo() {
  93. return array(
  94. 'chado_feature__md5checksum_widget' => array(
  95. 'label' => t('MD5 Checksum'),
  96. 'field types' => array('chado_feature__md5checksum'),
  97. ),
  98. );
  99. }
  100. /**
  101. * @see TripalField::formatterInfo()
  102. */
  103. static function formatterInfo() {
  104. return array(
  105. 'chado_feature__md5checksum_formatter' => array(
  106. 'label' => t('MD5 Checksum'),
  107. 'field types' => array('chado_feature__md5checksum'),
  108. 'settings' => array(
  109. ),
  110. ),
  111. );
  112. }
  113. /**
  114. * @see TripalField::formatterView()
  115. */
  116. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  117. foreach ($items as $delta => $item) {
  118. $content = key_exists('value', $item) ? $item['value'] : '';
  119. $element[$delta] = array(
  120. // We create a render array to produce the desired markup,
  121. '#type' => 'markup',
  122. '#markup' => $content,
  123. );
  124. }
  125. }
  126. /**
  127. * @see TripalField::widgetForm()
  128. */
  129. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  130. $settings = $this->field['settings'];
  131. $field_name = $this->field['field_name'];
  132. $field_type = $this->field['type'];
  133. $field_table = $this->field['settings']['chado_table'];
  134. $field_column = $this->field['settings']['chado_column'];
  135. // Get the field defaults.
  136. $md5checksum = '';
  137. if (count($items) > 0 and array_key_exists('feature__md5checksum', $items[0])) {
  138. $md5checksum = $items[0]['feature__md5checksum'];
  139. }
  140. if (array_key_exists('values', $form_state)) {
  141. //$md5checksum = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'feature__md5checksum');
  142. }
  143. $widget['value'] = array(
  144. '#type' => 'value',
  145. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  146. );
  147. $widget['feature__md5checksum'] = array(
  148. '#type' => 'value',
  149. '#value' => $md5checksum,
  150. // '#element_validate' => array('chado_feature__md5checksum_widget_validate'),
  151. );
  152. }
  153. }
  154. /**
  155. * Callback function for validating the chado_feature__md5checksum_widget.
  156. */
  157. function chado_feature__md5checksum_widget_validate($element, &$form_state) {
  158. $field_name = $element['#parents'][0];
  159. $field = $form_state['field'][$field_name]['und']['field'];
  160. $settings = $field['settings'];
  161. $field_name = $field['field_name'];
  162. $field_type = $field['type'];
  163. $field_table = $field['settings']['chado_table'];
  164. $field_column = $field['settings']['chado_column'];
  165. // Calculate the md5 checksum for the sequence only if md5 box is checked and
  166. // the residues exist.
  167. //$residues = tripal_chado_get_field_form_values('feature__residues', $form_state, 0, 'feature__residues');
  168. if ($residues) {
  169. $residues = preg_replace('/\s/', '', $residues);
  170. tripal_chado_set_field_form_values($field_name, $form_state, md5($residues), 0, 'feature__md5checksum');
  171. }
  172. else {
  173. // Otherwise, remove the md5 value
  174. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_table . '__md5checksum');
  175. }
  176. }