tripal_natural_diversity.admin.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. function tripal_natural_diversity_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 nd_geolocation
  11. $active_jobs = FALSE;
  12. if (tripal_get_module_active_jobs('tripal_nd_geolocation')) {
  13. $active_jobs = TRUE;
  14. }
  15. // add the field set for syncing libraries
  16. if (!$active_jobs) {
  17. get_tripal_natural_diversity_admin_form_sync_set($form);
  18. get_tripal_natural_diversity_admin_form_cleanup_set($form);
  19. }
  20. else {
  21. $form['notice'] = array(
  22. '#type' => 'fieldset',
  23. '#title' => t('Natural Diversity Management Temporarily Unavailable')
  24. );
  25. $form['notice']['message'] = array(
  26. '#value' => t('Currently, natural_diversity 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_natural_diversity
  35. */
  36. function get_tripal_natural_diversity_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 nd_geolocation in Chado become ".
  45. "\"orphaned\". This can occur if a nd_geolocation node in Drupal is ".
  46. "deleted but the corresponding chado natural_diversity 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 nd_geolocation'),
  53. '#weight' => 2,
  54. );
  55. }
  56. /**
  57. *
  58. * @ingroup tripal_natural_diversity
  59. */
  60. function get_tripal_natural_diversity_admin_form_sync_set(&$form) {
  61. // define the fieldsets
  62. $form['sync'] = array(
  63. '#type' => 'fieldset',
  64. '#title' => t('Sync ND Geolocation')
  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_nd_geolocation')) {
  71. $active_jobs = TRUE;
  72. }
  73. if (!$active_jobs) {
  74. // get the list of nd_geolocation
  75. $sql = "SELECT * FROM {nd_geolocation} ORDER BY description";
  76. $org_rset = chado_query($sql);
  77. // if we've added any nd_geolocation to the list that can be synced
  78. // then we want to build the form components to allow the user
  79. // to select one or all of them. Otherwise, just present
  80. // a message stating that all nd_geolocation are currently synced.
  81. $nd_geo_boxes = array();
  82. $added = 0;
  83. while ($nd_geolocation = db_fetch_object($org_rset)) {
  84. // check to see if the nd_geolocation is already present as a node in drupal.
  85. // if so, then skip it.
  86. $sql = "SELECT * FROM {chado_nd_geolocation} WHERE nd_geolocation_id = %d";
  87. if (!db_fetch_object(db_query($sql, $nd_geolocation->nd_geolocation_id))) {
  88. $nd_geo_boxes[$nd_geolocation->nd_geolocation_id] = $nd_geolocation->description;
  89. $added++;
  90. }
  91. }
  92. // if we have nd_geolocation we need to add to the checkbox then
  93. // build that form element
  94. if ($added > 0) {
  95. $nd_geo_boxes['all'] = "All ND Geolocation";
  96. $form['sync']['nd_geolocation'] = array(
  97. '#title' => t('Available ND Geolocation'),
  98. '#type' => t('checkboxes'),
  99. '#description' => t("Check the nd_geolocation you want to sync. Drupal content will be created for each of the nd_geolocation listed above. Select 'All Natural Diversity' to sync all of them."),
  100. '#required' => FALSE,
  101. '#prefix' => '<div id="org_boxes">',
  102. '#suffix' => '</div>',
  103. '#options' => $nd_geo_boxes,
  104. );
  105. $form['sync']['button'] = array(
  106. '#type' => 'submit',
  107. '#value' => t('Submit Sync Job')
  108. );
  109. }
  110. // we don't have any nd_geolocation to select from
  111. else {
  112. $form['sync']['value'] = array(
  113. '#value' => t('All nd_geolocation in Chado are currently synced with Drupal.')
  114. );
  115. }
  116. }
  117. // we don't want to present a form since we have an active job running
  118. else {
  119. $form['sync']['value'] = array(
  120. '#value' => t('Currently, jobs exist related to chado nd_geolocation. Please check back later for nd_geolocation that can by synced once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.')
  121. );
  122. }
  123. }
  124. /**
  125. *
  126. * @ingroup tripal_natural_diversity
  127. */
  128. function tripal_natural_diversity_admin_validate($form, &$form_state) {
  129. global $user; // we need access to the user info
  130. $job_args = array();
  131. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  132. // check to see if the user wants to sync chado and drupal. If
  133. // so then we need to register a job to do so with tripal
  134. $nd_geolocation = $form_state['values']['nd_geolocation'];
  135. $do_all = FALSE;
  136. $to_sync = array();
  137. foreach ($nd_geolocation as $nd_geolocation_id) {
  138. if (preg_match("/^all$/i" , $nd_geolocation_id)) {
  139. $do_all = TRUE;
  140. }
  141. if ($nd_geolocation_id and preg_match("/^\d+$/i" , $nd_geolocation_id)) {
  142. // get the list of nd_geolocation
  143. $sql = "SELECT * FROM {nd_geolocation} WHERE nd_geolocation_id = %d";
  144. $nd_geolocation = db_fetch_object(chado_query($sql, $nd_geolocation_id));
  145. $to_sync[$nd_geolocation_id] = "$nd_geolocation->description";
  146. }
  147. }
  148. // submit the job the tripal job manager
  149. if ($do_all) {
  150. tripal_add_job('Sync all nd_geolocation' , 'tripal_natural_diversity',
  151. 'tripal_natural_diversity_sync_nd_geolocation' , $job_args , $user->uid);
  152. }
  153. else{
  154. foreach ($to_sync as $nd_geolocation_id => $name) {
  155. $job_args[0] = $nd_geolocation_id;
  156. tripal_add_job("Sync nd_geolocation: $name" , 'tripal_natural_diversity',
  157. 'tripal_natural_diversity_sync_nd_geolocation' , $job_args , $user->uid);
  158. }
  159. }
  160. }
  161. // -------------------------------------
  162. // Submit the Cleanup Job if selected
  163. if ($form_state['values']['op'] == t('Clean up orphaned nd_geolocation')) {
  164. tripal_add_job('Cleanup orphaned nd_geolocation', 'tripal_natural_diversity',
  165. 'tripal_natural_diversity_cleanup', $job_args, $user->uid);
  166. }
  167. }
  168. /**
  169. * Synchronize natural_diversity from chado to drupal
  170. *
  171. * @ingroup tripal_natural_diversity
  172. */
  173. function tripal_natural_diversity_sync_nd_geolocation($nd_geolocation_id = NULL, $job_id = NULL) {
  174. global $user;
  175. $page_content = '';
  176. if (!$nd_geolocation_id) {
  177. $sql = "SELECT * FROM {nd_geolocation} P";
  178. $results = chado_query($sql);
  179. }
  180. else {
  181. $sql = "SELECT * FROM {nd_geolocation} P WHERE nd_geolocation_id = %d";
  182. $results = chado_query($sql, $nd_geolocation_id);
  183. }
  184. // We'll use the following SQL statement for checking if the nd_geolocation
  185. // already exists as a drupal node.
  186. $sql = "SELECT * FROM {chado_nd_geolocation} ".
  187. "WHERE nd_geolocation_id = %d";
  188. while ($nd_geolocation = db_fetch_object($results)) {
  189. // check if this nd_geolocation already exists in the drupal database. if it
  190. // does then skip this nd_geolocation and go to the next one.
  191. if (!db_fetch_object(db_query($sql, $nd_geolocation->nd_geolocation_id))) {
  192. $new_node = new stdClass();
  193. $new_node->type = 'chado_nd_geolocation';
  194. $new_node->uid = $user->uid;
  195. $new_node->nd_geolocation_id = $nd_geolocation->nd_geolocation_id;
  196. $new_node->title = $nd_geolocation->description;
  197. $new_node->latitude = $nd_geolocation->latitude;
  198. $new_node->longitude = $nd_geolocation->longitude;
  199. $new_node->altitude = $nd_geolocation->altitude;
  200. $new_node->geodetic_datum = $nd_geolocation->geodetic_datum;
  201. node_validate($new_node);
  202. if (!form_get_errors()) {
  203. $node = node_submit($new_node);
  204. node_save($node);
  205. db_query("UPDATE node SET title = '%s' WHERE nid = %d", $nd_geolocation->description, $node->nid); // Use description as node title
  206. if ($node->nid) {
  207. print "Added $nd_geolocation->description\n";
  208. }
  209. }
  210. else {
  211. print "Failed to insert nd_geolocation $nd_geolocation->description\n";
  212. }
  213. }
  214. else {
  215. print "Skipped $nd_geolocation->description\n";
  216. }
  217. }
  218. return $page_content;
  219. }
  220. /**
  221. * Remove orphaned drupal nodes
  222. *
  223. * @param $dummy
  224. * Not Used -kept for backwards compatibility
  225. * @param $job_id
  226. * The id of the tripal job executing this function
  227. *
  228. * @ingroup tripal_natural_diversity
  229. */
  230. function tripal_natural_diversity_cleanup($dummy = NULL, $job_id = NULL) {
  231. return tripal_core_clean_orphaned_nodes('nd_geolocation', $job_id);
  232. }