tripal_chado.api.inc 3.4 KB

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