tripal_project.admin.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. function tripal_project_administration_description_page() {
  7. $output = '';
  8. return $output;
  9. }
  10. function tripal_project_configuration_page() {
  11. $output = '';
  12. $output .= drupal_get_form('tripal_project_sync_projects_form');
  13. return $output;
  14. }
  15. function tripal_project_sync_projects_form($form_state = NULL) {
  16. $form = array();
  17. $form['sync'] = array(
  18. '#type' => 'fieldset',
  19. '#title' => 'Sync Projects'
  20. );
  21. $form['sync']['description'] = array(
  22. '#type' => 'item',
  23. '#value' => 'Many of the details for projects are stored in chado. Often other tripal '
  24. .'modules may create projects as a means of grouping data together. Sync\'ing projects '
  25. .'in chado created drupal pages (known as nodes) which display the data to priviledged users.'
  26. );
  27. $form['sync']['submit'] = array(
  28. '#type' => 'submit',
  29. '#value' => 'Sync All Projects',
  30. );
  31. return $form;
  32. }
  33. function tripal_project_sync_projects_form_submit($form, &$form_state) {
  34. global $user;
  35. //sync'ing is done by a tripal_job that is added here
  36. $job_id = tripal_add_job('Sync Projects', 'tripal_project',
  37. 'tripal_project_sync_all_projects', array(), $user->uid);
  38. }
  39. function tripal_project_sync_all_projects() {
  40. //retrieve all projects in drupal
  41. $resource = db_query('SELECT project_id FROM {chado_project}');
  42. $drupal_projects = array();
  43. while ($r = db_fetch_object($resource)) {
  44. $drupal_projects[$r->project_id] = $r->project_id;
  45. }
  46. // retrieve all projects in chado
  47. $chado_projects = array();
  48. $resource = chado_query('SELECT project_id FROM {project}');
  49. while ($r = db_fetch_object($resource)) {
  50. // if not already in drupal add to list to be sync'd
  51. if (!isset($drupal_projects[$r->project_id])) {
  52. $chado_projects[$r->project_id] = $r->project_id;
  53. }
  54. }
  55. print 'Number of Projects to Sync: ' . sizeof($chado_projects) . "\n";
  56. foreach ($chado_projects as $project_id) {
  57. $project = tripal_core_chado_select('project', array('name', 'description'), array('project_id' => $project_id));
  58. // create node
  59. $new_node = new stdClass();
  60. $new_node->type = 'chado_project';
  61. $new_node->uid = $user->uid;
  62. $new_node->title = $project[0]->name;
  63. $new_node->project_id = $project_id;
  64. $new_node->description = $project[0]->description;
  65. node_validate($new_node);
  66. $errors = form_get_errors();
  67. if (!$errors) {
  68. $node = node_submit($new_node);
  69. node_save($node);
  70. if ($node->nid) {
  71. print "Added " . $project[0]->name . " (Node ID:" . $node->nid . ")\n";
  72. }
  73. }
  74. else {
  75. print "Failed to insert project: " . $project[0]->name . "\n";
  76. print "Errors: " . print_r($errors, TRUE) . "\n";
  77. }
  78. }
  79. }