tripal_analysis.sync.inc 8.6 KB

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