tripal_eimage.admin.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. function tripal_eimage_admin($form_state = NULL) {
  7. $form = array();
  8. // before proceeding check to see if we have any
  9. // currently processing jobs. If so, we don't want
  10. // to give the opportunity to sync libraries
  11. $active_jobs = FALSE;
  12. if (tripal_get_module_active_jobs('tripal_eimage')) {
  13. $active_jobs = TRUE;
  14. }
  15. // add the field set for syncing libraries
  16. if (!$active_jobs) {
  17. get_tripal_eimage_admin_form_sync_set($form);
  18. get_tripal_eimage_admin_form_cleanup_set($form);
  19. }
  20. else {
  21. $form['notice'] = array(
  22. '#type' => 'fieldset',
  23. '#title' => t('Eimage Management Temporarily Unavailable')
  24. );
  25. $form['notice']['message'] = array(
  26. '#value' => t('Currently, eimage management jobs are waiting or are running. . Managemment features have been hidden until these jobs complete. Please check back later once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.'),
  27. );
  28. }
  29. return system_settings_form($form);
  30. }
  31. /**
  32. *
  33. *
  34. * @ingroup tripal_eimage
  35. */
  36. function get_tripal_eimage_admin_form_cleanup_set(&$form) {
  37. $form['cleanup'] = array(
  38. '#type' => 'fieldset',
  39. '#title' => t('Clean Up')
  40. );
  41. $form['cleanup']['description'] = array(
  42. '#type' => 'item',
  43. '#value' => t("With Drupal and chado residing in different databases ".
  44. "it is possible that nodes in Drupal and eimages in Chado become ".
  45. "\"orphaned\". This can occur if an eimage node in Drupal is ".
  46. "deleted but the corresponding chado eimage is not and/or vice ".
  47. "versa. Click the button below to resolve these discrepancies."),
  48. '#weight' => 1,
  49. );
  50. $form['cleanup']['button'] = array(
  51. '#type' => 'submit',
  52. '#value' => t('Clean up orphaned eimages'),
  53. '#weight' => 2,
  54. );
  55. }
  56. /**
  57. *
  58. * @ingroup tripal_eimage
  59. */
  60. function get_tripal_eimage_admin_form_sync_set(&$form) {
  61. // define the fieldsets
  62. $form['sync'] = array(
  63. '#type' => 'fieldset',
  64. '#title' => t('Sync Eimages')
  65. );
  66. // before proceeding check to see if we have any
  67. // currently processing jobs. If so, we don't want
  68. // to give the opportunity to sync libraries
  69. $active_jobs = FALSE;
  70. if (tripal_get_module_active_jobs('tripal_eimage')) {
  71. $active_jobs = TRUE;
  72. }
  73. if (!$active_jobs) {
  74. $eimage_boxes = array();
  75. // get the list of eimages
  76. $sql = "SELECT * FROM {eimage} E LEFT JOIN chado_eimage CE ON E.eimage_id = CE.eimage_id WHERE CE.eimage_id IS NULL LIMIT 100";
  77. $org_rset = chado_query($sql);
  78. // if we've added any eimages to the list that can be synced
  79. // then we want to build the form components to allow the user
  80. // to select one or all of them. Otherwise, just present
  81. // a message stating that all eimages are currently synced.
  82. $added = 0;
  83. $eimage_boxes = array();
  84. while ($eimage = db_fetch_object($org_rset)) {
  85. $eimage_boxes[$eimage->eimage_id] = "[" . $eimage->eimage_type . "] " . $eimage->image_uri;
  86. $added++;
  87. }
  88. // if we have eimages we need to add to the checkbox then
  89. // build that form element
  90. if ($added > 0) {
  91. $eimage_boxes['all'] = "All Eimages";
  92. $form['sync']['eimages'] = array(
  93. '#title' => t('Available Eimages (First 100 eimages listed if there are too many)'),
  94. '#type' => t('checkboxes'),
  95. '#description' => t("Check the eimages you want to sync. Drupal content will be created for each of the eimages listed above. Select 'All Eimages' to sync all of them."),
  96. '#required' => FALSE,
  97. '#prefix' => '<div id="org_boxes">',
  98. '#suffix' => '</div>',
  99. '#options' => $eimage_boxes,
  100. );
  101. $form['sync']['button'] = array(
  102. '#type' => 'submit',
  103. '#value' => t('Submit Sync Job')
  104. );
  105. }
  106. // we don't have any eimages to select from
  107. else {
  108. $form['sync']['value'] = array(
  109. '#value' => t('All eimages in Chado are currently synced with Drupal.')
  110. );
  111. }
  112. }
  113. // we don't want to present a form since we have an active job running
  114. else {
  115. $form['sync']['value'] = array(
  116. '#value' => t('Currently, jobs exist related to chado eimages. Please check back later for eimages that can by synced once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.')
  117. );
  118. }
  119. }
  120. /**
  121. *
  122. * @ingroup tripal_eimage
  123. */
  124. function tripal_eimage_admin_validate($form, &$form_state) {
  125. global $user; // we need access to the user info
  126. $job_args = array();
  127. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  128. // check to see if the user wants to sync chado and drupal. If
  129. // so then we need to register a job to do so with tripal
  130. $eimages = $form_state['values']['eimages'];
  131. $do_all = FALSE;
  132. $to_sync = array();
  133. foreach ($eimages as $eimage_id) {
  134. if (preg_match("/^all$/i" , $eimage_id)) {
  135. $do_all = TRUE;
  136. }
  137. if ($eimage_id and preg_match("/^\d+$/i" , $eimage_id)) {
  138. // get the list of eimages
  139. $sql = "SELECT * FROM {Eimage} WHERE eimage_id = %d";
  140. $eimage = db_fetch_object(chado_query($sql, $eimage_id));
  141. $to_sync[$eimage_id] = "[" . $eimage->eimage_type . "] $eimage->image_uri";
  142. }
  143. }
  144. // submit the job the tripal job manager
  145. if ($do_all) {
  146. tripal_add_job('Sync all eimages' , 'tripal_eimage',
  147. 'tripal_eimage_sync_eimages' , $job_args , $user->uid);
  148. }
  149. else{
  150. foreach ($to_sync as $eimage_id => $name) {
  151. $job_args[0] = $eimage_id;
  152. tripal_add_job("Sync eimage: $name" , 'tripal_eimage',
  153. 'tripal_eimage_sync_eimages' , $job_args , $user->uid);
  154. }
  155. }
  156. }
  157. // -------------------------------------
  158. // Submit the Cleanup Job if selected
  159. if ($form_state['values']['op'] == t('Clean up orphaned eimages')) {
  160. tripal_add_job('Cleanup orphaned eimages', 'tripal_eimage',
  161. 'tripal_eimage_cleanup', $job_args, $user->uid);
  162. }
  163. }
  164. /**
  165. * Synchronize eimages from chado to drupal
  166. *
  167. * @ingroup tripal_eimage
  168. */
  169. function tripal_eimage_sync_eimages($eimage_id = NULL, $job_id = NULL) {
  170. global $user;
  171. $page_content = '';
  172. if (!$eimage_id) {
  173. $sql = "SELECT * FROM {eimage} P";
  174. $results = chado_query($sql);
  175. }
  176. else {
  177. $sql = "SELECT * FROM {eimage} P WHERE eimage_id = %d";
  178. $results = chado_query($sql, $eimage_id);
  179. }
  180. // We'll use the following SQL statement for checking if the eimage
  181. // already exists as a drupal node.
  182. $sql = "SELECT * FROM {chado_eimage} ".
  183. "WHERE eimage_id = %d";
  184. while ($eimage = db_fetch_object($results)) {
  185. // check if this eimage already exists in the drupal database. if it
  186. // does then skip this eimage and go to the next one.
  187. if (!db_fetch_object(db_query($sql, $eimage->eimage_id))) {
  188. $new_node = new stdClass();
  189. $new_node->type = 'chado_eimage';
  190. $new_node->uid = $user->uid;
  191. $new_node->title = "$eimage->image_uri";
  192. $new_node->eimage_id = $eimage->eimage_id;
  193. $new_node->eimage_type = $eimage->eimage_type;
  194. $new_node->eimage_data = $eimage->eimage_data;
  195. node_validate($new_node);
  196. if (!form_get_errors()) {
  197. $node = node_submit($new_node);
  198. node_save($node);
  199. if ($node->nid) {
  200. print "Added [" . $eimage->eimage_type . "] $eimage->image_uri\n";
  201. }
  202. }
  203. else {
  204. print "Failed to insert eimage [" . $eimage->eimage_type . "] $eimage->image_uri\n";
  205. }
  206. }
  207. else {
  208. print "Skipped [" . $eimage->eimage_type . "] $eimage->image_uri\n";
  209. }
  210. }
  211. return $page_content;
  212. }
  213. /**
  214. * Remove orphaned drupal nodes
  215. *
  216. * @param $dummy
  217. * Not Used -kept for backwards compatibility
  218. * @param $job_id
  219. * The id of the tripal job executing this function
  220. *
  221. * @ingroup tripal_eimage
  222. */
  223. function tripal_eimage_cleanup($dummy = NULL, $job_id = NULL) {
  224. return tripal_core_clean_orphaned_nodes('eimage', $job_id);
  225. }