Tripal v1.0 (6.x-1.0)
tripal_project.admin.inc
Go to the documentation of this file.
00001 <?php
00007 function tripal_project_admin($form_state = NULL) {
00008   $form = array();
00009 
00010   // before proceeding check to see if we have any
00011   // currently processing jobs. If so, we don't want
00012   // to give the opportunity to sync libraries
00013   $active_jobs = FALSE;
00014   if (tripal_get_module_active_jobs('tripal_project')) {
00015     $active_jobs = TRUE;
00016   }
00017 
00018   // add the field set for syncing libraries
00019   if (!$active_jobs) {
00020     get_tripal_project_admin_form_sync_set($form);
00021     get_tripal_project_admin_form_cleanup_set($form);
00022 //    get_tripal_project_admin_form_reindex_set($form);
00023 
00024   }
00025   else {
00026     $form['notice'] = array(
00027     '#type' => 'fieldset',
00028     '#title' => t('Project Management Temporarily Unavailable')
00029     );
00030     $form['notice']['message'] = array(
00031       '#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.'),
00032     );
00033   }
00034 
00035   return system_settings_form($form);
00036 }
00042 function get_tripal_project_admin_form_cleanup_set(&$form) {
00043   $form['cleanup'] = array(
00044     '#type' => 'fieldset',
00045     '#title' => t('Clean Up')
00046   );
00047   $form['cleanup']['description'] = array(
00048     '#type' => 'item',
00049     '#value' => t("With Drupal and chado residing in different databases ".
00050     "it is possible that nodes in Drupal and projects in Chado become ".
00051     "\"orphaned\".  This can occur if an project node in Drupal is ".
00052     "deleted but the corresponding chado project is not and/or vice ".
00053     "versa. Click the button below to resolve these discrepancies."),
00054     '#weight' => 1,
00055   );
00056   $form['cleanup']['button'] = array(
00057     '#type' => 'submit',
00058     '#value' => t('Clean up orphaned projects'),
00059     '#weight' => 2,
00060   );
00061 }
00066 function get_tripal_project_admin_form_sync_set(&$form) {
00067   // define the fieldsets
00068   $form['sync'] = array(
00069     '#type' => 'fieldset',
00070     '#title' => t('Sync Projects')
00071   );
00072 
00073   // before proceeding check to see if we have any
00074   // currently processing jobs. If so, we don't want
00075   // to give the opportunity to sync libraries
00076   $active_jobs = FALSE;
00077   if (tripal_get_module_active_jobs('tripal_project')) {
00078     $active_jobs = TRUE;
00079   }
00080 
00081   if (!$active_jobs) {
00082 
00083     // get the list of projects
00084     $sql = "SELECT * FROM {project} ORDER BY name";
00085     $org_rset = chado_query($sql);
00086 
00087     // if we've added any projects to the list that can be synced
00088     // then we want to build the form components to allow the user
00089     // to select one or all of them.  Otherwise, just present
00090     // a message stating that all projects are currently synced.
00091     $proj_boxes = array();
00092     $added = 0;
00093     while ($project = db_fetch_object($org_rset)) {
00094       // check to see if the project is already present as a node in drupal.
00095       // if so, then skip it.
00096       $sql = "SELECT * FROM {chado_project} WHERE project_id = %d";
00097       if (!db_fetch_object(db_query($sql, $project->project_id))) {
00098         $proj_boxes[$project->project_id] = $project->name;
00099         $added++;
00100       }
00101     }
00102 
00103     // if we have projects we need to add to the checkbox then
00104     // build that form element
00105     if ($added > 0) {
00106       $proj_boxes['all'] = "All Projects";
00107 
00108       $form['sync']['projects'] = array(
00109         '#title'       => t('Available Projects'),
00110         '#type'        => t('checkboxes'),
00111         '#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."),
00112         '#required'    => FALSE,
00113         '#prefix'      => '<div id="org_boxes">',
00114         '#suffix'      => '</div>',
00115         '#options'     => $proj_boxes,
00116       );
00117       $form['sync']['button'] = array(
00118         '#type' => 'submit',
00119         '#value' => t('Submit Sync Job')
00120       );
00121     }
00122     // we don't have any projects to select from
00123     else {
00124     $form['sync']['value'] = array(
00125         '#value' => t('All projects in Chado are currently synced with Drupal.')
00126     );
00127     }
00128   }
00129   // we don't want to present a form since we have an active job running
00130   else {
00131     $form['sync']['value'] = array(
00132         '#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.')
00133     );
00134   }
00135 }
00140 function tripal_project_admin_validate($form, &$form_state) {
00141   global $user;  // we need access to the user info
00142   $job_args = array();
00143 
00144   if ($form_state['values']['op'] == t('Submit Sync Job')) {
00145 
00146     // check to see if the user wants to sync chado and drupal.  If
00147     // so then we need to register a job to do so with tripal
00148     $projects = $form_state['values']['projects'];
00149     $do_all = FALSE;
00150     $to_sync = array();
00151 
00152     foreach ($projects as $project_id) {
00153       if (preg_match("/^all$/i" , $project_id)) {
00154         $do_all = TRUE;
00155       }
00156       if ($project_id and preg_match("/^\d+$/i" , $project_id)) {
00157         // get the list of projects
00158         $sql = "SELECT * FROM {Project} WHERE project_id = %d";
00159         $project = db_fetch_object(chado_query($sql, $project_id));
00160         $to_sync[$project_id] = "$project->genus $project->species";
00161       }
00162     }
00163 
00164     // submit the job the tripal job manager
00165     if ($do_all) {
00166       tripal_add_job('Sync all projects' , 'tripal_project',
00167       'tripal_project_sync_projects' , $job_args , $user->uid);
00168     }
00169     else{
00170       foreach ($to_sync as $project_id => $name) {
00171         $job_args[0] = $project_id;
00172         tripal_add_job("Sync project: $name" , 'tripal_project',
00173           'tripal_project_sync_projects' , $job_args , $user->uid);
00174       }
00175     }
00176   }
00177 
00178   // -------------------------------------
00179   // Submit the Reindex Job if selected
00180   if ($form_state['values']['op'] == t('Reindex Features')) {
00181     $projects = $form_state['values']['re-projects'];
00182     foreach ($projects as $project_id) {
00183       if ($project_id and preg_match("/^\d+$/i" , $project_id)) {
00184         // get the project info
00185         $sql = "SELECT * FROM {project} WHERE project_id = %d";
00186         $project = db_fetch_object(chado_query($sql , $project_id));
00187         $job_args[0] = $project_id;
00188         tripal_add_job("Reindex features for project: $project->genus ".
00189          "$project->species", 'tripal_project' ,
00190          'tripal_project_reindex_features', $job_args, $user->uid);
00191       }
00192     }
00193   }
00194 
00195   // -------------------------------------
00196   // Submit the taxonomy Job if selected
00197   if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
00198     $projects = $form_state['values']['tx-projects'];
00199     foreach ($projects as $project_id) {
00200       if ($project_id and preg_match("/^\d+$/i", $project_id)) {
00201         // get the project info
00202         $sql = "SELECT * FROM {project} WHERE project_id = %d";
00203         $project = db_fetch_object(chado_query($sql , $project_id));
00204         $job_args[0] = $project_id;
00205         tripal_add_job("Set taxonomy for features in project: ".
00206           "$project->genus $project->species" , 'tripal_project',
00207           'tripal_project_taxonify_features', $job_args, $user->uid);
00208       }
00209     }
00210   }
00211 
00212   // -------------------------------------
00213   // Submit the Cleanup Job if selected
00214   if ($form_state['values']['op'] == t('Clean up orphaned projects')) {
00215     tripal_add_job('Cleanup orphaned projects', 'tripal_project',
00216       'tripal_project_cleanup', $job_args, $user->uid);
00217   }
00218 }
00224 function tripal_project_sync_projects($project_id = NULL, $job_id = NULL) {
00225   global $user;
00226   $page_content = '';
00227 
00228   if (!$project_id) {
00229     $sql = "SELECT * FROM {project} P";
00230     $results = chado_query($sql);
00231   }
00232   else {
00233     $sql = "SELECT * FROM {project} P WHERE project_id = %d";
00234     $results = chado_query($sql, $project_id);
00235   }
00236 
00237   // We'll use the following SQL statement for checking if the project
00238   // already exists as a drupal node.
00239   $sql = "SELECT * FROM {chado_project} ".
00240          "WHERE project_id = %d";
00241 
00242   while ($project = db_fetch_object($results)) {
00243 
00244     // check if this project already exists in the drupal database. if it
00245     // does then skip this project and go to the next one.
00246     if (!db_fetch_object(db_query($sql, $project->project_id))) {
00247 
00248       $new_node = new stdClass();
00249       $new_node->type = 'chado_project';
00250       $new_node->uid = $user->uid;
00251       $new_node->title = "$project->name";
00252       $new_node->project_id = $project->project_id;
00253       $new_node->name = $project->name;
00254       $new_node->description = $project->description;
00255       node_validate($new_node);
00256       if (!form_get_errors()) {
00257         $node = node_submit($new_node);
00258         node_save($node);
00259         if ($node->nid) {
00260           print "Added $project->name\n";
00261         }
00262       }
00263       else {
00264         print "Failed to insert project $project->name\n";
00265       }
00266     }
00267     else {
00268       print "Skipped $project->name\n";
00269     }
00270   }
00271   return $page_content;
00272 }
00273 /*
00274  * 
00275  */
00276 function tripal_project_sync_projects_form_submit($form, &$form_state) {
00277   global $user;
00278 
00279   //sync'ing is done by a tripal_job that is added here
00280   $job_id = tripal_add_job('Sync Projects', 'tripal_project',
00281   'tripal_project_sync_all_projects', array(), $user->uid);
00282 
00283 }
00284 
00285 /*
00286  * 
00287  */
00288 function tripal_project_sync_all_projects() {
00289 
00290   //retrieve all projects in drupal
00291   $resource = db_query('SELECT project_id FROM {chado_project}');
00292   $drupal_projects = array();
00293   while ($r = db_fetch_object($resource)) {
00294     $drupal_projects[$r->project_id] = $r->project_id;
00295   }
00296 
00297   // retrieve all projects in chado
00298   $chado_projects = array();
00299   $resource = chado_query('SELECT project_id FROM {project}');
00300   while ($r = db_fetch_object($resource)) {
00301     // if not already in drupal add to list to be sync'd
00302     if (!isset($drupal_projects[$r->project_id])) {
00303       $chado_projects[$r->project_id] = $r->project_id;
00304     }
00305   }
00306 
00307   print 'Number of Projects to Sync: ' . sizeof($chado_projects) . "\n";
00308 
00309   foreach ($chado_projects as $project_id) {
00310     $project = tripal_core_chado_select('project', array('name', 'description'), array('project_id' => $project_id));
00311 
00312     // create node
00313     $new_node = new stdClass();
00314     $new_node->type = 'chado_project';
00315     $new_node->uid = $user->uid;
00316     $new_node->title = $project[0]->name;
00317     $new_node->project_id = $project_id;
00318     $new_node->description = $project[0]->description;
00319     node_validate($new_node);
00320     $errors = form_get_errors();
00321     if (!$errors) {
00322       $node = node_submit($new_node);
00323       node_save($node);
00324       if ($node->nid) {
00325         print "Added " . $project[0]->name . " (Node ID:" . $node->nid . ")\n";
00326       }
00327     }
00328     else {
00329       print "Failed to insert project: " . $project[0]->name . "\n";
00330       print "Errors: " . print_r($errors, TRUE) . "\n";
00331     }
00332 
00333   }
00334 
00335 
00336 }
00337 
00348 function tripal_project_cleanup($dummy = NULL, $job_id = NULL) {
00349 
00350   return tripal_core_clean_orphaned_nodes('project', $job_id);
00351 }
 All Classes Files Functions Variables