organism_sync.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_organism_sync () {
  6. $form = array();
  7. get_tripal_organism_admin_form_sync_set($form);
  8. get_tripal_organism_admin_form_cleanup_set($form);
  9. return $form;
  10. }
  11. /**
  12. *
  13. *
  14. * @ingroup tripal_organism
  15. */
  16. function get_tripal_organism_admin_form_cleanup_set(&$form) {
  17. $form['cleanup'] = array(
  18. '#type' => 'fieldset',
  19. '#title' => t('Clean Up'),
  20. '#description' => t("With Drupal and chado residing in different databases or database schemas " .
  21. "it is possible that nodes in Drupal and organisms in Chado become " .
  22. "\"orphaned\". This can occur if an organism node in Drupal is " .
  23. "deleted but the corresponding chado organism is not and/or vice " .
  24. "versa. Click the button below to resolve these discrepancies."),
  25. );
  26. $form['cleanup']['button'] = array(
  27. '#type' => 'submit',
  28. '#value' => t('Clean up orphaned organisms'),
  29. );
  30. }
  31. /**
  32. *
  33. * @ingroup tripal_organism
  34. */
  35. function get_tripal_organism_admin_form_sync_set(&$form) {
  36. // define the fieldsets
  37. $form['sync'] = array(
  38. '#type' => 'fieldset',
  39. '#title' => t('Sync Organisms')
  40. );
  41. // get the list of organisms
  42. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  43. $org_rset = chado_query($sql);
  44. // if we've added any organisms to the list that can be synced
  45. // then we want to build the form components to allow the user
  46. // to select one or all of them. Otherwise, just present
  47. // a message stating that all organisms are currently synced.
  48. $org_boxes = array();
  49. $added = 0;
  50. foreach ($org_rset as $organism) {
  51. // check to see if the organism is already present as a node in drupal.
  52. // if so, then skip it.
  53. $sql = "SELECT * FROM {chado_organism} WHERE organism_id = :organism_id";
  54. if (!db_query($sql, array(':organism_id' => $organism->organism_id))->fetchObject()) {
  55. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  56. $added++;
  57. }
  58. }
  59. // if we have organisms we need to add to the checkbox then
  60. // build that form element
  61. if ($added > 0) {
  62. $org_boxes['all'] = "All Organisms";
  63. $form['sync']['organisms'] = array(
  64. '#title' => t('Available Organisms'),
  65. '#type' => t('checkboxes'),
  66. '#description' => t("Check the organisms you want to sync. Drupal content will be created for each of the organisms listed above. Select 'All Organisms' to sync all of them."),
  67. '#required' => FALSE,
  68. '#prefix' => '<div id="org_boxes">',
  69. '#suffix' => '</div>',
  70. '#options' => $org_boxes,
  71. );
  72. $form['sync']['button'] = array(
  73. '#type' => 'submit',
  74. '#value' => t('Submit Sync Job')
  75. );
  76. }
  77. // we don't have any organisms to select from
  78. else {
  79. $form['sync']['value'] = array(
  80. '#markup' => t('All organisms in Chado are currently synced with Drupal.')
  81. );
  82. }
  83. }
  84. /**
  85. *
  86. * @ingroup tripal_organism
  87. */
  88. function tripal_organism_sync_submit($form, &$form_state) {
  89. global $user; // we need access to the user info
  90. $job_args = array();
  91. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  92. // check to see if the user wants to sync chado and drupal. If
  93. // so then we need to register a job to do so with tripal
  94. $organisms = $form_state['values']['organisms'];
  95. $do_all = FALSE;
  96. $to_sync = array();
  97. foreach ($organisms as $organism_id) {
  98. if (preg_match("/^all$/i" , $organism_id)) {
  99. $do_all = TRUE;
  100. }
  101. if ($organism_id and preg_match("/^\d+$/i" , $organism_id)) {
  102. // get the list of organisms
  103. $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
  104. $organism = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  105. $to_sync[$organism_id] = "$organism->genus $organism->species";
  106. }
  107. }
  108. // submit the job the tripal job manager
  109. if ($do_all) {
  110. tripal_add_job('Sync all organisms' , 'tripal_organism',
  111. 'tripal_organism_sync_organisms' , $job_args , $user->uid);
  112. }
  113. else{
  114. foreach ($to_sync as $organism_id => $name) {
  115. $job_args[0] = $organism_id;
  116. tripal_add_job("Sync organism: $name" , 'tripal_organism',
  117. 'tripal_organism_sync_organisms' , $job_args , $user->uid);
  118. }
  119. }
  120. }
  121. // -------------------------------------
  122. // Submit the Cleanup Job if selected
  123. if ($form_state['values']['op'] == t('Clean up orphaned organisms')) {
  124. tripal_add_job('Cleanup orphaned organisms', 'tripal_organism',
  125. 'tripal_organism_cleanup', $job_args, $user->uid);
  126. }
  127. }
  128. /**
  129. * Synchronize organisms from chado to drupal
  130. *
  131. * @ingroup tripal_organism
  132. */
  133. function tripal_organism_sync_organisms($organism_id = NULL, $job_id = NULL) {
  134. global $user;
  135. $page_content = '';
  136. if (!$organism_id) {
  137. $sql = "SELECT * FROM {organism} O";
  138. $results = chado_query($sql);
  139. }
  140. else {
  141. $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
  142. $results = chado_query($sql, array(':organism_id' => $organism_id));
  143. }
  144. // We'll use the following SQL statement for checking if the organism
  145. // already exists as a drupal node.
  146. $sql = "SELECT * FROM {chado_organism} WHERE organism_id = :organism_id";
  147. foreach ($results as $organism) {
  148. // check if this organism already exists in the drupal database. if it
  149. // does then skip this organism and go to the next one.
  150. if (!db_query($sql, array(':organism_id' => $organism->organism_id))->fetchObject()) {
  151. $new_node = new stdClass();
  152. $new_node->type = 'chado_organism';
  153. $new_node->uid = $user->uid;
  154. $new_node->title = "$organism->genus $organism->species";
  155. $new_node->organism_id = $organism->organism_id;
  156. $new_node->genus = $organism->genus;
  157. $new_node->species = $organism->species;
  158. $new_node->description = '';
  159. $form = array(); // dummy variable
  160. $form_state = array(); // dummy variable
  161. node_validate($new_node, $form, $form_state);
  162. if (!form_get_errors()) {
  163. $node = node_submit($new_node);
  164. node_save($node);
  165. if ($node->nid) {
  166. print "Added $organism->common_name\n";
  167. }
  168. }
  169. else {
  170. watchdog('torg_sync', "Unable to create organism node. ID: %org_id, Name: %name.",
  171. array('%org_id' => $organism->organism_id, '%name' => $organism->genus . ' ' . $organism->species),
  172. WATCHDOG_WARNING);
  173. }
  174. }
  175. }
  176. return $page_content;
  177. }
  178. /**
  179. * Remove orphaned drupal nodes
  180. *
  181. * @param $dummy
  182. * Not Used -kept for backwards compatibility
  183. * @param $job_id
  184. * The id of the tripal job executing this function
  185. *
  186. * @ingroup tripal_organism
  187. */
  188. function tripal_organism_cleanup($dummy = NULL, $job_id = NULL) {
  189. return tripal_core_chado_node_cleanup_orphaned('organism', $job_id);
  190. }