tripal_analysis.sync.inc 7.3 KB

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