tripal_chado.api.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Publishes content in Chado as a new TripalEntity entity.
  4. *
  5. * @param $values
  6. * A key/value associative array that supports the following keys:
  7. * - bundle_name: The name of the the TripalBundle (e.g. bio_data-12345).
  8. * @param $job_id
  9. * (Optional) The numeric job ID as provided by the Tripal jobs system. There
  10. * is no need to specify this argument if this function is being called
  11. * outside of the jobs systems.
  12. *
  13. * @return boolean
  14. * TRUE if all of the records of the given bundle type were published, and
  15. * FALSE if a failure occured.
  16. */
  17. function tripal_chado_publish_records($values, $job_id = NULL) {
  18. $bundle_name = $values['bundle_name'];
  19. $sync_node = array_key_exists('sync_node', $values) ? $values['sync_node'] : '';
  20. if (!array_key_exists('bundle_name', $values) or !$values['bundle_name']) {
  21. tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => 'The bundle name was not provided'));
  22. return FALSE;
  23. }
  24. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  25. $bundle_id = $bundle->id;
  26. $table = tripal_get_bundle_variable('chado_table', $bundle_id);
  27. $column = tripal_get_bundle_variable('chado_column', $bundle_id);
  28. $cvterm_id = tripal_get_bundle_variable('chado_cvterm_id', $bundle_id);
  29. // Get the table information
  30. $table_schema = chado_get_schema($table);
  31. $pkey_field = $table_schema['primary key'][0];
  32. $select = "SELECT $pkey_field as record_id ";
  33. $from = "FROM {" . $table . "} T
  34. LEFT JOIN public.chado_entity CE on CE.record_id = T.$pkey_field
  35. AND CE.data_table = '$table'
  36. ";
  37. if ($sync_node && db_table_exists('chado_' . $table)) {
  38. $select = "SELECT T.$pkey_field as record_id, CT.nid ";
  39. $from .= "INNER JOIN public.chado_$table CT ON CT.$pkey_field = T.$pkey_field";
  40. }
  41. $where = " WHERE CE.record_id IS NULL ";
  42. if ($table != 'analysis' and $table != 'organism') {
  43. $where .= "AND $column = $cvterm_id";
  44. }
  45. $sql = $select . $from . $where;
  46. $records = chado_query($sql);
  47. $num_published = 0;
  48. try {
  49. while($record = $records->fetchObject()) {
  50. // First save the tripal_entity record.
  51. $record_id = $record->record_id;
  52. $ec = entity_get_controller('TripalEntity');
  53. $entity = $ec->create(array(
  54. 'bundle' => $bundle_name,
  55. 'term_id' => $bundle->term_id,
  56. ));
  57. $entity->save();
  58. // Next save the chado_entity record.
  59. $entity_record = array(
  60. 'entity_id' => $entity->id,
  61. 'record_id' => $record_id,
  62. 'data_table' => $table,
  63. 'type_table' => $table,
  64. 'field' => $column,
  65. );
  66. // For the Tv2 to Tv3 migration we want to add the nid to the
  67. // entity so we can associate the node with the entity.
  68. if (property_exists($record, 'nid')) {
  69. $entity_record['nid'] = $record->nid;
  70. }
  71. $success = drupal_write_record('chado_entity', $entity_record);
  72. $entity = entity_load('TripalEntity', array($entity->id));
  73. $entity = reset($entity);
  74. $title_format = tripal_get_title_format($bundle);
  75. $title = tripal_replace_tokens($title_format, $entity, $bundle);
  76. $ec->setTitle($entity, $title);
  77. $num_published++;
  78. }
  79. }
  80. catch (Exception $e) {
  81. $error = $e->getMessage();
  82. tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => $error));
  83. drupal_set_message('Failed publishing record. See recent logs for more details.', 'error');
  84. return FALSE;
  85. }
  86. drupal_set_message("Succesfully published $num_published " . $bundle->label . " record(s).");
  87. return TRUE;
  88. }