tripal_project.admin.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $previous_db = tripal_db_set_active('chado');
  49. $resource = db_query('SELECT project_id FROM {project}');
  50. tripal_db_set_active($previous_db);
  51. while ($r = db_fetch_object($resource)) {
  52. // if not already in drupal add to list to be sync'd
  53. if (!isset($drupal_projects[$r->project_id])) {
  54. $chado_projects[$r->project_id] = $r->project_id;
  55. }
  56. }
  57. print 'Number of Projects to Sync: ' . sizeof($chado_projects) . "\n";
  58. foreach ($chado_projects as $project_id) {
  59. $project = tripal_core_chado_select('project', array('name', 'description'), array('project_id' => $project_id));
  60. // create node
  61. $new_node = new stdClass();
  62. $new_node->type = 'chado_project';
  63. $new_node->uid = $user->uid;
  64. $new_node->title = $project[0]->name;
  65. $new_node->project_id = $project_id;
  66. $new_node->description = $project[0]->description;
  67. node_validate($new_node);
  68. $errors = form_get_errors();
  69. if (!$errors) {
  70. $node = node_submit($new_node);
  71. node_save($node);
  72. if ($node->nid) {
  73. print "Added " . $project[0]->name . " (Node ID:" . $node->nid . ")\n";
  74. }
  75. }
  76. else {
  77. print "Failed to insert project: " . $project[0]->name . "\n";
  78. print "Errors: " . print_r($errors, TRUE) . "\n";
  79. }
  80. }
  81. }