tripal_chado.publish.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // Get the form for this bundle (content type). Then iterate through
  44. // the fields and include only those that are Chado fields.
  45. /* $bundle_name = 'bio-data_' . $term_id;
  46. $entity = entity_get_controller('TripalEntity')->create(array('bundle' => $bundle_name, 'term_id' => $term_id));
  47. field_attach_form('TripalEntity', $entity, $form['filters'], $form_state);
  48. foreach (element_children($form['filters']) as $field_name) {
  49. $field = field_info_field($field_name);
  50. if ($field and $field['storage']['type'] != 'field_chado_storage') {
  51. unset($form['filters'][$field_name]);
  52. }
  53. // Ignore the KVPoperty field as this is just a field to
  54. // allow the user to add new properties which isn't appropriate
  55. // here.
  56. if ($field['type'] == 'kvproperty_adder') {
  57. unset($form['filters'][$field_name]);
  58. }
  59. // None of the fields should be required.
  60. dpm($form['filters'][$field_name]);
  61. } */
  62. $form['publish_btn'] = array(
  63. '#type' => 'submit',
  64. '#name' => 'publish_btn',
  65. '#value' => 'Publish',
  66. );
  67. }
  68. $form['#prefix'] = '<div id="tripal-chado-publish-form">';
  69. $form['#suffix'] = '</div>';
  70. return $form;
  71. }
  72. function tripal_chado_publish_form_validate($form, &$form_state) {
  73. }
  74. function tripal_chado_publish_form_submit($form, &$form_state) {
  75. if ($form_state['clicked_button']['#name'] == 'publish_btn') {
  76. global $user;
  77. $term_id = $form_state['values']['term_id'];
  78. $bundle_name = 'bio-data_' . $term_id;
  79. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  80. $args = array($bundle_name);
  81. $includes = array(
  82. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.publish'),
  83. );
  84. return tripal_add_job("Publish " . $bundle->label . " records.",
  85. 'tripal_chado', 'tripal_chado_publish_records', $args,
  86. $user->uid, 10, $includes);
  87. }
  88. }
  89. /**
  90. *
  91. */
  92. function tripal_chado_publish_form_ajax_callback($form, $form_state) {
  93. return $form;
  94. }
  95. /**
  96. *
  97. */
  98. function tripal_chado_publish_records($bundle_name, $job_id = NULL) {
  99. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  100. $bundle_id = $bundle->id;
  101. $table = tripal_get_bundle_variable('chado_table', $bundle_id);
  102. $column = tripal_get_bundle_variable('chado_column', $bundle_id);
  103. $cvterm_id = tripal_get_bundle_variable('chado_cvterm_id', $bundle_id);
  104. // Get the table information
  105. $table_schema = chado_get_schema($table);
  106. $pkey_field = $table_schema['primary key'][0];
  107. $where = '';
  108. if ($table != 'analysis' and $table != 'organism') {
  109. $where .= "AND $column = $cvterm_id";
  110. }
  111. $sql = "
  112. SELECT $pkey_field as record_id
  113. FROM {" . $table . "} T
  114. LEFT JOIN public.chado_entity CE on CE.record_id = T.$pkey_field
  115. AND CE.data_table = '$table'
  116. WHERE CE.record_id IS NUll $where
  117. ";
  118. $records = chado_query($sql);
  119. $num_published = 0;
  120. try {
  121. while($record = $records->fetchObject()) {
  122. $record_id = $record->record_id;
  123. $ec = entity_get_controller('TripalEntity');
  124. $entity = $ec->create(array(
  125. 'bundle' => $bundle_name,
  126. 'term_id' => $bundle->term_id,
  127. ));
  128. $entity->save();
  129. $record = array(
  130. 'entity_id' => $entity->id,
  131. 'record_id' => $record_id,
  132. 'data_table' => $table,
  133. 'type_table' => $table,
  134. 'field' => $column,
  135. );
  136. $success = drupal_write_record('chado_entity', $record);
  137. $entity = entity_load('TripalEntity', array($entity->id));
  138. $entity = reset($entity);
  139. $title_format = tripal_get_title_format($bundle);
  140. $title = tripal_replace_tokens($title_format, $entity, $bundle);
  141. $ec->setTitle($entity, $title);
  142. $num_published++;
  143. }
  144. }
  145. catch (Exception $e) {
  146. $error = $e->getMessage();
  147. tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => $error));
  148. drupal_set_message('Failed publishing record. See recent logs for more details.', 'error');
  149. return FALSE;
  150. }
  151. drupal_set_message("Succesfully published $num_published " . $bundle->label . " record(s).");
  152. }