tripal_project.admin.inc

  1. 2.x tripal_project/includes/tripal_project.admin.inc
  2. 3.x legacy/tripal_project/includes/tripal_project.admin.inc
  3. 1.x tripal_project/includes/tripal_project.admin.inc

@todo Add file header description

File

tripal_project/includes/tripal_project.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. function tripal_project_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_project')) {
  13. $active_jobs = TRUE;
  14. }
  15. // add the field set for syncing libraries
  16. if (!$active_jobs) {
  17. get_tripal_project_admin_form_sync_set($form);
  18. get_tripal_project_admin_form_cleanup_set($form);
  19. // get_tripal_project_admin_form_reindex_set($form);
  20. }
  21. else {
  22. $form['notice'] = array(
  23. '#type' => 'fieldset',
  24. '#title' => t('Project Management Temporarily Unavailable')
  25. );
  26. $form['notice']['message'] = array(
  27. '#value' => t('Currently, project 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.'),
  28. );
  29. }
  30. return system_settings_form($form);
  31. }
  32. /**
  33. *
  34. *
  35. * @ingroup tripal_project
  36. */
  37. function get_tripal_project_admin_form_cleanup_set(&$form) {
  38. $form['cleanup'] = array(
  39. '#type' => 'fieldset',
  40. '#title' => t('Clean Up')
  41. );
  42. $form['cleanup']['description'] = array(
  43. '#type' => 'item',
  44. '#value' => t("With Drupal and chado residing in different databases ".
  45. "it is possible that nodes in Drupal and projects in Chado become ".
  46. "\"orphaned\". This can occur if an project node in Drupal is ".
  47. "deleted but the corresponding chado project is not and/or vice ".
  48. "versa. Click the button below to resolve these discrepancies."),
  49. '#weight' => 1,
  50. );
  51. $form['cleanup']['button'] = array(
  52. '#type' => 'submit',
  53. '#value' => t('Clean up orphaned projects'),
  54. '#weight' => 2,
  55. );
  56. }
  57. /**
  58. *
  59. * @ingroup tripal_project
  60. */
  61. function get_tripal_project_admin_form_sync_set(&$form) {
  62. // define the fieldsets
  63. $form['sync'] = array(
  64. '#type' => 'fieldset',
  65. '#title' => t('Sync Projects')
  66. );
  67. // before proceeding check to see if we have any
  68. // currently processing jobs. If so, we don't want
  69. // to give the opportunity to sync libraries
  70. $active_jobs = FALSE;
  71. if (tripal_get_module_active_jobs('tripal_project')) {
  72. $active_jobs = TRUE;
  73. }
  74. if (!$active_jobs) {
  75. // get the list of projects
  76. $sql = "SELECT * FROM {project} ORDER BY name";
  77. $org_rset = chado_query($sql);
  78. // if we've added any projects 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 projects are currently synced.
  82. $proj_boxes = array();
  83. $added = 0;
  84. while ($project = db_fetch_object($org_rset)) {
  85. // check to see if the project is already present as a node in drupal.
  86. // if so, then skip it.
  87. $sql = "SELECT * FROM {chado_project} WHERE project_id = %d";
  88. if (!db_fetch_object(db_query($sql, $project->project_id))) {
  89. $proj_boxes[$project->project_id] = $project->name;
  90. $added++;
  91. }
  92. }
  93. // if we have projects we need to add to the checkbox then
  94. // build that form element
  95. if ($added > 0) {
  96. $proj_boxes['all'] = "All Projects";
  97. $form['sync']['projects'] = array(
  98. '#title' => t('Available Projects'),
  99. '#type' => t('checkboxes'),
  100. '#description' => t("Check the projects you want to sync. Drupal content will be created for each of the projects listed above. Select 'All Projects' to sync all of them."),
  101. '#required' => FALSE,
  102. '#prefix' => '<div id="org_boxes">',
  103. '#suffix' => '</div>',
  104. '#options' => $proj_boxes,
  105. );
  106. $form['sync']['button'] = array(
  107. '#type' => 'submit',
  108. '#value' => t('Submit Sync Job')
  109. );
  110. }
  111. // we don't have any projects to select from
  112. else {
  113. $form['sync']['value'] = array(
  114. '#value' => t('All projects in Chado are currently synced with Drupal.')
  115. );
  116. }
  117. }
  118. // we don't want to present a form since we have an active job running
  119. else {
  120. $form['sync']['value'] = array(
  121. '#value' => t('Currently, jobs exist related to chado projects. Please check back later for projects that can by synced once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.')
  122. );
  123. }
  124. }
  125. /**
  126. *
  127. * @ingroup tripal_project
  128. */
  129. function tripal_project_admin_validate($form, &$form_state) {
  130. global $user; // we need access to the user info
  131. $job_args = array();
  132. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  133. // check to see if the user wants to sync chado and drupal. If
  134. // so then we need to register a job to do so with tripal
  135. $projects = $form_state['values']['projects'];
  136. $do_all = FALSE;
  137. $to_sync = array();
  138. foreach ($projects as $project_id) {
  139. if (preg_match("/^all$/i" , $project_id)) {
  140. $do_all = TRUE;
  141. }
  142. if ($project_id and preg_match("/^\d+$/i" , $project_id)) {
  143. // get the list of projects
  144. $sql = "SELECT * FROM {Project} WHERE project_id = %d";
  145. $project = db_fetch_object(chado_query($sql, $project_id));
  146. $to_sync[$project_id] = "$project->genus $project->species";
  147. }
  148. }
  149. // submit the job the tripal job manager
  150. if ($do_all) {
  151. tripal_add_job('Sync all projects' , 'tripal_project',
  152. 'tripal_project_sync_projects' , $job_args , $user->uid);
  153. }
  154. else{
  155. foreach ($to_sync as $project_id => $name) {
  156. $job_args[0] = $project_id;
  157. tripal_add_job("Sync project: $name" , 'tripal_project',
  158. 'tripal_project_sync_projects' , $job_args , $user->uid);
  159. }
  160. }
  161. }
  162. // -------------------------------------
  163. // Submit the Reindex Job if selected
  164. if ($form_state['values']['op'] == t('Reindex Features')) {
  165. $projects = $form_state['values']['re-projects'];
  166. foreach ($projects as $project_id) {
  167. if ($project_id and preg_match("/^\d+$/i" , $project_id)) {
  168. // get the project info
  169. $sql = "SELECT * FROM {project} WHERE project_id = %d";
  170. $project = db_fetch_object(chado_query($sql , $project_id));
  171. $job_args[0] = $project_id;
  172. tripal_add_job("Reindex features for project: $project->genus ".
  173. "$project->species", 'tripal_project' ,
  174. 'tripal_project_reindex_features', $job_args, $user->uid);
  175. }
  176. }
  177. }
  178. // -------------------------------------
  179. // Submit the taxonomy Job if selected
  180. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  181. $projects = $form_state['values']['tx-projects'];
  182. foreach ($projects as $project_id) {
  183. if ($project_id and preg_match("/^\d+$/i", $project_id)) {
  184. // get the project info
  185. $sql = "SELECT * FROM {project} WHERE project_id = %d";
  186. $project = db_fetch_object(chado_query($sql , $project_id));
  187. $job_args[0] = $project_id;
  188. tripal_add_job("Set taxonomy for features in project: ".
  189. "$project->genus $project->species" , 'tripal_project',
  190. 'tripal_project_taxonify_features', $job_args, $user->uid);
  191. }
  192. }
  193. }
  194. // -------------------------------------
  195. // Submit the Cleanup Job if selected
  196. if ($form_state['values']['op'] == t('Clean up orphaned projects')) {
  197. tripal_add_job('Cleanup orphaned projects', 'tripal_project',
  198. 'tripal_project_cleanup', $job_args, $user->uid);
  199. }
  200. }
  201. /**
  202. * Synchronize projects from chado to drupal
  203. *
  204. * @ingroup tripal_project
  205. */
  206. function tripal_project_sync_projects($project_id = NULL, $job_id = NULL) {
  207. global $user;
  208. $page_content = '';
  209. if (!$project_id) {
  210. $sql = "SELECT * FROM {project} P";
  211. $results = chado_query($sql);
  212. }
  213. else {
  214. $sql = "SELECT * FROM {project} P WHERE project_id = %d";
  215. $results = chado_query($sql, $project_id);
  216. }
  217. // We'll use the following SQL statement for checking if the project
  218. // already exists as a drupal node.
  219. $sql = "SELECT * FROM {chado_project} ".
  220. "WHERE project_id = %d";
  221. while ($project = db_fetch_object($results)) {
  222. // check if this project already exists in the drupal database. if it
  223. // does then skip this project and go to the next one.
  224. if (!db_fetch_object(db_query($sql, $project->project_id))) {
  225. $new_node = new stdClass();
  226. $new_node->type = 'chado_project';
  227. $new_node->uid = $user->uid;
  228. $new_node->title = "$project->name";
  229. $new_node->project_id = $project->project_id;
  230. $new_node->name = $project->name;
  231. $new_node->description = $project->description;
  232. node_validate($new_node);
  233. if (!form_get_errors()) {
  234. $node = node_submit($new_node);
  235. node_save($node);
  236. if ($node->nid) {
  237. print "Added $project->name\n";
  238. }
  239. }
  240. else {
  241. print "Failed to insert project $project->name\n";
  242. }
  243. }
  244. else {
  245. print "Skipped $project->name\n";
  246. }
  247. }
  248. return $page_content;
  249. }
  250. /*
  251. *
  252. */
  253. function tripal_project_sync_projects_form_submit($form, &$form_state) {
  254. global $user;
  255. //sync'ing is done by a tripal_job that is added here
  256. $job_id = tripal_add_job('Sync Projects', 'tripal_project',
  257. 'tripal_project_sync_all_projects', array(), $user->uid);
  258. }
  259. /*
  260. *
  261. */
  262. function tripal_project_sync_all_projects() {
  263. //retrieve all projects in drupal
  264. $resource = db_query('SELECT project_id FROM {chado_project}');
  265. $drupal_projects = array();
  266. while ($r = db_fetch_object($resource)) {
  267. $drupal_projects[$r->project_id] = $r->project_id;
  268. }
  269. // retrieve all projects in chado
  270. $chado_projects = array();
  271. $resource = chado_query('SELECT project_id FROM {project}');
  272. while ($r = db_fetch_object($resource)) {
  273. // if not already in drupal add to list to be sync'd
  274. if (!isset($drupal_projects[$r->project_id])) {
  275. $chado_projects[$r->project_id] = $r->project_id;
  276. }
  277. }
  278. print 'Number of Projects to Sync: ' . sizeof($chado_projects) . "\n";
  279. foreach ($chado_projects as $project_id) {
  280. $project = tripal_core_chado_select('project', array('name', 'description'), array('project_id' => $project_id));
  281. // create node
  282. $new_node = new stdClass();
  283. $new_node->type = 'chado_project';
  284. $new_node->uid = $user->uid;
  285. $new_node->title = $project[0]->name;
  286. $new_node->project_id = $project_id;
  287. $new_node->description = $project[0]->description;
  288. node_validate($new_node);
  289. $errors = form_get_errors();
  290. if (!$errors) {
  291. $node = node_submit($new_node);
  292. node_save($node);
  293. if ($node->nid) {
  294. print "Added " . $project[0]->name . " (Node ID:" . $node->nid . ")\n";
  295. }
  296. }
  297. else {
  298. print "Failed to insert project: " . $project[0]->name . "\n";
  299. print "Errors: " . print_r($errors, TRUE) . "\n";
  300. }
  301. }
  302. }
  303. /**
  304. * Remove orphaned drupal nodes
  305. *
  306. * @param $dummy
  307. * Not Used -kept for backwards compatibility
  308. * @param $job_id
  309. * The id of the tripal job executing this function
  310. *
  311. * @ingroup tripal_project
  312. */
  313. function tripal_project_cleanup($dummy = NULL, $job_id = NULL) {
  314. return tripal_core_clean_orphaned_nodes('project', $job_id);
  315. }