chado_feature__md5checksum.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. */
  8. function chado_feature__md5checksum_info() {
  9. return array(
  10. 'label' => t('MD5 checksum'),
  11. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  12. 'default_widget' => 'chado_feature__md5checksum_widget',
  13. 'default_formatter' => 'chado_feature__md5checksum_formatter',
  14. 'settings' => array(),
  15. 'storage' => array(
  16. 'type' => 'field_chado_storage',
  17. 'module' => 'tripal_chado',
  18. 'active' => TRUE
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements hook_attach_info().
  24. *
  25. * This is a hook provided by the tripal_chado module. It allows the field
  26. * to specify which bundles it will attach to and to specify the settings.
  27. *
  28. * @param $entity_type
  29. * @param $entity
  30. * @param $term
  31. *
  32. * @return
  33. * A field array
  34. */
  35. function chado_feature__md5checksum_attach_info($entity_type, $bundle, $target) {
  36. $field_info = array();
  37. $table_name = $target['data_table'];
  38. $type_table = $target['type_table'];
  39. $type_field = $target['field'];
  40. $cv_id = $target['cv_id'];
  41. $cvterm_id = $target['cvterm_id'];
  42. // If this is not the feature table then we don't want to attach.
  43. if ($table_name != 'feature') {
  44. return $field_info;
  45. }
  46. // Create an array with information about this field.
  47. $field_info = array(
  48. 'field_name' => 'feature__md5checksum',
  49. 'field_type' => 'chado_feature__md5checksum',
  50. 'widget_type' => 'chado_feature__md5checksum_widget',
  51. 'description' => 'The MD5 checksum for the sequence.',
  52. 'label' => 'MD5 Checksum',
  53. 'is_required' => 0,
  54. 'storage' => 'field_chado_storage',
  55. 'widget_settings' => array(
  56. 'display_label' => 1
  57. ),
  58. 'field_settings' => array(
  59. 'chado_table' => $table_name,
  60. 'chado_column' => 'md5checksum',
  61. 'semantic_web' => array(
  62. 'name' => '',
  63. 'accession' => '',
  64. 'ns' => '',
  65. 'nsurl' => '',
  66. ),
  67. ),
  68. );
  69. return $field_info;
  70. }
  71. /**
  72. * Implements hook_widget_info.
  73. *
  74. * This is a hook provided by the tripal_chado module for offloading
  75. * the hook_field_widget_info() hook for each field to specify.
  76. */
  77. function chado_feature__md5checksum_widget_info() {
  78. return array(
  79. 'label' => t('MD5 Checksum Checkbox'),
  80. 'field types' => array('chado_feature__md5checksum'),
  81. );
  82. }
  83. /**
  84. * Implements hook_formatter_info.
  85. *
  86. * This is a hook provided by the tripal_chado module for
  87. * offloading the hook_field_formatter_info() for each field
  88. * to specify.
  89. *
  90. */
  91. function chado_feature__md5checksum_formatter_info() {
  92. return array(
  93. 'label' => t('MD5 Checksum'),
  94. 'field types' => array('chado_feature__md5checksum'),
  95. 'settings' => array(
  96. ),
  97. );
  98. }
  99. /**
  100. *
  101. * @param unknown $entity_type
  102. * @param unknown $entity
  103. * @param unknown $field
  104. * @param unknown $instance
  105. * @param unknown $langcode
  106. * @param unknown $items
  107. * @param unknown $display
  108. */
  109. function chado_feature__md5checksum_formatter(&$element, $entity_type, $entity, $field,
  110. $instance, $langcode, $items, $display) {
  111. foreach ($items as $delta => $item) {
  112. $content = key_exists('value', $item) ? $item['value'] : '';
  113. $element[$delta] = array(
  114. // We create a render array to produce the desired markup,
  115. '#type' => 'markup',
  116. '#markup' => $content,
  117. );
  118. }
  119. }
  120. /**
  121. *
  122. * @param unknown $field_name
  123. * @param unknown $widget
  124. * @param unknown $form
  125. * @param unknown $form_state
  126. * @param unknown $field
  127. * @param unknown $instance
  128. * @param unknown $langcode
  129. * @param unknown $items
  130. * @param unknown $delta
  131. * @param unknown $element
  132. */
  133. function chado_feature__md5checksum_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  134. $widget['value'] = array(
  135. '#type' => 'checkbox',
  136. '#title' => $element['#title'],
  137. '#description' => $element['#description'],
  138. '#options' => array(0, 1),
  139. '#default_value' => 1,
  140. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  141. '#delta' => $delta,
  142. '#element_validate' => array('chado_feature__md5checksum_widget_validate'),
  143. );
  144. }
  145. /**
  146. * Callback function for validating the chado_feature__md5checksum_widget.
  147. */
  148. function chado_feature__md5checksum_widget_validate($element, &$form_state) {
  149. $field_name = $element['#parents'][0];
  150. // Calculate the md5 checksum for the sequence only if md5 box is checked and
  151. // the residues exist.
  152. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state);
  153. if ($residues) {
  154. $residues = preg_replace('/\s/', '', $residues);
  155. tripal_chado_set_field_form_values($field_name, $form_state, md5($residues));
  156. }
  157. else {
  158. // Otherwise, remove the md5 value
  159. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
  160. }
  161. }
  162. /**
  163. * Loads the field values with appropriate data.
  164. *
  165. * This function is called by the tripal_chado_field_storage_load() for
  166. * each property managed by the field_chado_storage storage type.
  167. *
  168. * @param $field
  169. * @param $entity
  170. * @param $record
  171. */
  172. function chado_feature__md5checksum_load($field, $entity, $record) {
  173. $field_name = $field['field_name'];
  174. if ($record and property_exists($record, 'md5checksum')) {
  175. $entity->{$field_name}['und'][] = array('value' => $record->md5checksum);
  176. }
  177. }