tripal_chado.publish.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. function tripal_chado_publish_form($form, &$form_state) {
  3. $term_id = '';
  4. if (array_key_exists('values', $form_state)) {
  5. $term_id = $form_state['values']['term_id'];
  6. }
  7. $bundles = db_select('tripal_bundle', 'tb')
  8. ->fields('tb')
  9. ->orderBy('label', 'ASC')
  10. ->execute();
  11. $term_ids = array();
  12. $term_ids[] = 'Select a Content Type';
  13. while ($bundle = $bundles->fetchObject()) {
  14. $term_ids[$bundle->term_id] = $bundle->label;
  15. }
  16. $form['term_id'] = array(
  17. '#type' => 'select',
  18. '#title' => 'Content Type',
  19. '#description' => t('Select a content type to publish. Only data that
  20. is mapped to the selected vocabulary term will be published.'),
  21. '#options' => $term_ids,
  22. '#default_value' => $term_id,
  23. '#ajax' => array(
  24. 'callback' => "tripal_chado_publish_form_ajax_callback",
  25. 'wrapper' => "tripal-chado-publish-form",
  26. 'effect' => 'fade',
  27. 'method' => 'replace'
  28. ),
  29. );
  30. // If the user has selected a content type, then we need to
  31. // show some filters.
  32. if ($term_id) {
  33. $form['filters'] = array(
  34. '#type' => 'fieldset',
  35. '#title' => 'Filters',
  36. '#description' => t('Please provide any filters for limiting
  37. the records. Only those that match the filters specified
  38. below will be published. To publish all records of this
  39. type, leave all filters blank.'),
  40. '#collapsed' => TRUE,
  41. '#collapsible' => TRUE,
  42. );
  43. $form['publish_btn'] = array(
  44. '#type' => 'submit',
  45. '#name' => 'publish_btn',
  46. '#value' => 'Publish',
  47. );
  48. }
  49. $form['#prefix'] = '<div id="tripal-chado-publish-form">';
  50. $form['#suffix'] = '</div>';
  51. return $form;
  52. }
  53. function tripal_chado_publish_form_validate($form, &$form_state) {
  54. }
  55. function tripal_chado_publish_form_submit($form, &$form_state) {
  56. if ($form_state['clicked_button']['#name'] == 'publish_btn') {
  57. global $user;
  58. $term_id = $form_state['values']['term_id'];
  59. $bundle_name = 'bio-data_' . $term_id;
  60. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  61. $args = array($bundle_name);
  62. $includes = array(
  63. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.publish'),
  64. );
  65. return tripal_add_job("Publish " . $bundle->label . " records.",
  66. 'tripal_chado', 'tripal_chado_publish_records', $args,
  67. $user->uid, 10, $includes);
  68. }
  69. }
  70. /**
  71. *
  72. */
  73. function tripal_chado_publish_form_ajax_callback($form, $form_state) {
  74. return $form;
  75. }
  76. /**
  77. *
  78. */
  79. function tripal_chado_publish_records($bundle_name, $job_id = NULL) {
  80. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  81. $bundle_id = $bundle->id;
  82. $table = tripal_get_bundle_variable('chado_table', $bundle_id);
  83. $column = tripal_get_bundle_variable('chado_column', $bundle_id);
  84. $cvterm_id = tripal_get_bundle_variable('chado_cvterm_id', $bundle_id);
  85. // Get the table information
  86. $table_schema = chado_get_schema($table);
  87. $pkey_field = $table_schema['primary key'][0];
  88. $where = '';
  89. if ($table != 'analysis' and $table != 'organism') {
  90. $where .= "AND $column = $cvterm_id";
  91. }
  92. $sql = "
  93. SELECT $pkey_field as record_id
  94. FROM {" . $table . "} T
  95. LEFT JOIN public.chado_entity CE on CE.record_id = T.$pkey_field
  96. AND CE.data_table = '$table'
  97. WHERE CE.record_id IS NUll $where
  98. ";
  99. $records = chado_query($sql);
  100. $num_published = 0;
  101. try {
  102. while($record = $records->fetchObject()) {
  103. $record_id = $record->record_id;
  104. $ec = entity_get_controller('TripalEntity');
  105. $entity = $ec->create(array(
  106. 'bundle' => $bundle_name,
  107. 'term_id' => $bundle->term_id,
  108. ));
  109. $entity->save();
  110. $record = array(
  111. 'entity_id' => $entity->id,
  112. 'record_id' => $record_id,
  113. 'data_table' => $table,
  114. 'type_table' => $table,
  115. 'field' => $column,
  116. );
  117. $success = drupal_write_record('chado_entity', $record);
  118. $entity = entity_load('TripalEntity', array($entity->id));
  119. $entity = reset($entity);
  120. $title_format = tripal_get_title_format($bundle);
  121. $title = tripal_replace_tokens($title_format, $entity, $bundle);
  122. $ec->setTitle($entity, $title);
  123. $num_published++;
  124. }
  125. }
  126. catch (Exception $e) {
  127. $error = $e->getMessage();
  128. tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => $error));
  129. drupal_set_message('Failed publishing record. See recent logs for more details.', 'error');
  130. return FALSE;
  131. }
  132. drupal_set_message("Succesfully published $num_published " . $bundle->label . " record(s).");
  133. }