tripal_chado.migrate.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. function tripal_chado_migrate_form($form, &$form_state) {
  3. $chado_content = 'all';
  4. if (array_key_exists('values', $form_state)) {
  5. $chado_content = $form_state['values']['chado_content'];
  6. }
  7. // Get all available Tripal v2 chado tables
  8. $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name LIKE 'chado_%'";
  9. $result = db_query($sql);
  10. $tables = array();
  11. while ($field = $result->fetchField()) {
  12. array_push($tables, $field);
  13. }
  14. // List all available Tripal v2 content types
  15. $result = db_select('node_type', 'nt')
  16. ->fields('nt', array('type', 'name', 'description'))
  17. ->condition('type', 'chado_%', 'LIKE')
  18. ->execute();
  19. $options = array ('all' => 'All');
  20. while ($obj = $result->fetchObject()) {
  21. if (in_array($obj->type, $tables)) {
  22. $options[$obj->type] = $obj->name;
  23. }
  24. }
  25. $form['chado_content'] = array(
  26. '#type' => 'select',
  27. '#title' => 'Chado Content',
  28. '#description' => t('Select the chado content to migrate.'),
  29. '#options' => $options,
  30. '#default_value' => $chado_content,
  31. '#ajax' => array(
  32. 'callback' => "tripal_chado_migrate_form_ajax_callback",
  33. 'wrapper' => "tripal-chado-migrate-form",
  34. 'effect' => 'fade',
  35. 'method' => 'replace'
  36. ),
  37. );
  38. // Add a review button that allows reviewing migratable content types
  39. if ($chado_content != 'all') {
  40. $table = str_replace('chado_', '', $chado_content);
  41. $schema = chado_get_schema($table);
  42. $pkey = $schema['primary key'][0];
  43. $fkeys = $schema['foreign keys'];
  44. if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  45. $form['review'] = array(
  46. '#type' => 'fieldset',
  47. '#title' => 'Review Content Type',
  48. '#description' => 'By clicking on the Review button, Tripal will gather information from
  49. existing database and list content types which can be mapped to a Tripal v3 content
  50. type. (may take a while depending on the size of your database.). This allows you
  51. to migrate only the types of your selection.'
  52. );
  53. $form['review']['review_btn'] = array(
  54. '#type' => 'button',
  55. '#name' => 'review_btn',
  56. '#value' => "Review",
  57. '#ajax' => array(
  58. 'callback' => "tripal_chado_migrate_form_ajax_callback",
  59. 'wrapper' => "tripal-chado-migrate-form",
  60. 'effect' => 'fade',
  61. 'method' => 'replace'
  62. ),
  63. );
  64. $counter = 0;
  65. if ($form_state['clicked_button']['#name'] == 'review_btn') {
  66. // Get all Tripal v2 node types from the chad_* linking table
  67. $sql =
  68. "SELECT DISTINCT V.name AS type, X.accession, db.name AS namespace
  69. FROM chado.$table T
  70. INNER JOIN $chado_content CT ON T.$pkey = CT.$pkey
  71. INNER JOIN chado.cvterm V ON V.cvterm_id = T.type_id
  72. INNER JOIN chado.dbxref X ON X.dbxref_id = V.dbxref_id
  73. INNER JOIN chado.db ON db.db_id = X.db_id";
  74. $subtypes = db_query($sql);
  75. while($subtype = $subtypes->fetchObject()) {
  76. $form['review']['chado_content_type--' . $subtype->namespace . '--' . $subtype->type] = array(
  77. '#type' => 'checkbox',
  78. '#title' => $subtype->type,
  79. );
  80. $counter ++;
  81. }
  82. // No subtype exists, migrate all
  83. if ($counter == 0) {
  84. $form['review']['nosubtype'] = array(
  85. '#markup' => t("<br>Type not found. Migrate all $options[$chado_content]."),
  86. );
  87. }
  88. }
  89. }
  90. }
  91. // Submit button
  92. $form['migrate_btn'] = array(
  93. '#type' => 'submit',
  94. '#name' => 'migrate_btn',
  95. '#value' => "Migrate $options[$chado_content]",
  96. );
  97. $form['#prefix'] = '<div id="tripal-chado-migrate-form">';
  98. $form['#suffix'] = '</div>';
  99. return $form;
  100. }
  101. function tripal_chado_migrate_form_validate($form, &$form_state) {
  102. }
  103. function tripal_chado_migrate_form_submit($form, &$form_state) {
  104. if ($form_state['clicked_button']['#name'] == 'migrate_btn') {
  105. global $user;
  106. $values = $form_state['values'];
  107. $chado_contents = $form_state['values']['chado_content'];
  108. $subtypes = array();
  109. foreach ($values AS $key => $value) {
  110. if ($chado_contents != 'all') {
  111. if (preg_match('/^chado_content_type--(.+)--(.+)/', $key, $matches) && $value == 1) {
  112. $namespace = $matches[1];
  113. $accession = $matches[2];
  114. $subtypes [$namespace] = $accession;
  115. }
  116. }
  117. }
  118. // Submit a job to migrate content
  119. global $user;
  120. $args = array(
  121. array(
  122. 'chado_content' => $chado_contents,
  123. 'subtypes' => $subtypes
  124. ),
  125. );
  126. $includes = array();
  127. return tripal_add_job("Migrate $chado_contents.", 'tripal_chado', 'tripal_chado_migrate_records', $args, $user->uid, 10, $includes);
  128. }
  129. }
  130. /**
  131. *
  132. */
  133. function tripal_chado_migrate_form_ajax_callback($form, $form_state) {
  134. return $form;
  135. }
  136. /**
  137. *
  138. */
  139. function tripal_chado_migrate_records($bundle_name, $job_id = NULL) {
  140. }