Tripal v1.0 (6.x-1.0)
|
00001 <?php 00002 00008 function tripal_organism_admin() { 00009 00010 $form = array(); 00011 00012 // before proceeding check to see if we have any 00013 // currently processing jobs. If so, we don't want 00014 // to give the opportunity to sync libraries 00015 $active_jobs = FALSE; 00016 if (tripal_get_module_active_jobs('tripal_organism')) { 00017 $active_jobs = TRUE; 00018 } 00019 00020 // add the field set for syncing libraries 00021 if (!$active_jobs) { 00022 get_tripal_organism_admin_form_sync_set($form); 00023 get_tripal_organism_admin_form_reindex_set($form); 00024 get_tripal_organism_admin_form_taxonomy_set($form); 00025 get_tripal_organism_admin_form_cleanup_set($form); 00026 } 00027 else { 00028 $form['notice'] = array( 00029 '#type' => 'fieldset', 00030 '#title' => t('Organism Management Temporarily Unavailable') 00031 ); 00032 $form['notice']['message'] = array( 00033 '#value' => t('Currently, organism 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.'), 00034 ); 00035 } 00036 00037 return system_settings_form($form); 00038 } 00044 function get_tripal_organism_admin_form_cleanup_set(&$form) { 00045 $form['cleanup'] = array( 00046 '#type' => 'fieldset', 00047 '#title' => t('Clean Up') 00048 ); 00049 $form['cleanup']['description'] = array( 00050 '#type' => 'item', 00051 '#value' => t("With Drupal and chado residing in different databases ". 00052 "it is possible that nodes in Drupal and organisms in Chado become ". 00053 "\"orphaned\". This can occur if an organism node in Drupal is ". 00054 "deleted but the corresponding chado organism is not and/or vice ". 00055 "versa. Click the button below to resolve these discrepancies."), 00056 '#weight' => 1, 00057 ); 00058 $form['cleanup']['button'] = array( 00059 '#type' => 'submit', 00060 '#value' => t('Clean up orphaned organisms'), 00061 '#weight' => 2, 00062 ); 00063 } 00069 function get_tripal_organism_admin_form_taxonomy_set(&$form) { 00070 $form['taxonify'] = array( 00071 '#type' => 'fieldset', 00072 '#title' => t('Assign Drupal Taxonomy to Organism Features') 00073 ); 00074 00075 // get the list of libraries 00076 $sql = "SELECT * FROM {Organism} ORDER BY genus,species"; 00077 $org_rset = chado_query($sql); 00078 00079 // iterate through all of the libraries 00080 $org_boxes = array(); 00081 while ($organism = db_fetch_object($org_rset)) { 00082 $org_boxes[$organism->organism_id] = "$organism->genus $organism->species"; 00083 } 00084 00085 $form['taxonify']['description'] = array( 00086 '#type' => 'item', 00087 '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " . 00088 "nodes. These terms allow for advanced filtering during searching. This option allows ". 00089 "for setting taxonomy only for features that belong to the selected organisms below. All other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."), 00090 '#weight' => 1, 00091 ); 00092 00093 $form['taxonify']['tx-organisms'] = array( 00094 '#title' => t('Organisms'), 00095 '#type' => t('checkboxes'), 00096 '#description' => t("Check the organisms whose features you want to reset taxonomy. Note: this list contains all organisms, even those that may not be synced."), 00097 '#required' => FALSE, 00098 '#prefix' => '<div id="lib_boxes">', 00099 '#suffix' => '</div>', 00100 '#options' => $org_boxes, 00101 '#weight' => 2 00102 ); 00103 $form['taxonify']['tx-button'] = array( 00104 '#type' => 'submit', 00105 '#value' => t('Set Feature Taxonomy'), 00106 '#weight' => 3 00107 ); 00108 } 00113 function get_tripal_organism_admin_form_reindex_set(&$form) { 00114 // define the fieldsets 00115 $form['reindex'] = array( 00116 '#type' => 'fieldset', 00117 '#title' => t('Reindex Organism Features') 00118 ); 00119 00120 // get the list of libraries 00121 $sql = "SELECT * FROM {Organism} ORDER BY genus,species"; 00122 $org_rset = chado_query($sql); 00123 00124 // iterate through all of the libraries 00125 $org_boxes = array(); 00126 while ($organism = db_fetch_object($org_rset)) { 00127 $org_boxes[$organism->organism_id] = "$organism->genus $organism->species"; 00128 } 00129 $form['reindex']['description'] = array( 00130 '#type' => 'item', 00131 '#value' => t("This option allows for reindexing of only those features that belong to the selected organisms below. All other features will be unaffected. To reindex all features in the site see the Feature Administration page."), 00132 '#weight' => 1, 00133 ); 00134 00135 $form['reindex']['re-organisms'] = array( 00136 '#title' => t('Organisms'), 00137 '#type' => t('checkboxes'), 00138 '#description' => t("Check the organisms whoee features you want to reindex. Note: this list contains all organisms, even those that may not be synced."), 00139 '#required' => FALSE, 00140 '#prefix' => '<div id="lib_boxes">', 00141 '#suffix' => '</div>', 00142 '#options' => $org_boxes, 00143 '#weight' => 2, 00144 ); 00145 $form['reindex']['re-button'] = array( 00146 '#type' => 'submit', 00147 '#value' => t('Reindex Features'), 00148 '#weight' => 3, 00149 ); 00150 } 00155 function get_tripal_organism_admin_form_sync_set(&$form) { 00156 // define the fieldsets 00157 $form['sync'] = array( 00158 '#type' => 'fieldset', 00159 '#title' => t('Sync Organisms') 00160 ); 00161 00162 // before proceeding check to see if we have any 00163 // currently processing jobs. If so, we don't want 00164 // to give the opportunity to sync libraries 00165 $active_jobs = FALSE; 00166 if (tripal_get_module_active_jobs('tripal_organism')) { 00167 $active_jobs = TRUE; 00168 } 00169 00170 if (!$active_jobs) { 00171 00172 // get the list of organisms 00173 $sql = "SELECT * FROM {Organism} ORDER BY genus,species"; 00174 $org_rset = chado_query($sql); 00175 00176 // if we've added any organisms to the list that can be synced 00177 // then we want to build the form components to allow the user 00178 // to select one or all of them. Otherwise, just present 00179 // a message stating that all organisms are currently synced. 00180 $org_boxes = array(); 00181 $added = 0; 00182 while ($organism = db_fetch_object($org_rset)) { 00183 // check to see if the organism is already present as a node in drupal. 00184 // if so, then skip it. 00185 $sql = "SELECT * FROM {chado_organism} WHERE organism_id = %d"; 00186 if (!db_fetch_object(db_query($sql, $organism->organism_id))) { 00187 $org_boxes[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)"; 00188 $added++; 00189 } 00190 } 00191 00192 // if we have organisms we need to add to the checkbox then 00193 // build that form element 00194 if ($added > 0) { 00195 $org_boxes['all'] = "All Organisms"; 00196 00197 $form['sync']['organisms'] = array( 00198 '#title' => t('Available Organisms'), 00199 '#type' => t('checkboxes'), 00200 '#description' => t("Check the organisms you want to sync. Drupal content will be created for each of the organisms listed above. Select 'All Organisms' to sync all of them."), 00201 '#required' => FALSE, 00202 '#prefix' => '<div id="org_boxes">', 00203 '#suffix' => '</div>', 00204 '#options' => $org_boxes, 00205 ); 00206 $form['sync']['button'] = array( 00207 '#type' => 'submit', 00208 '#value' => t('Submit Sync Job') 00209 ); 00210 } 00211 // we don't have any organisms to select from 00212 else { 00213 $form['sync']['value'] = array( 00214 '#value' => t('All organisms in Chado are currently synced with Drupal.') 00215 ); 00216 } 00217 } 00218 // we don't want to present a form since we have an active job running 00219 else { 00220 $form['sync']['value'] = array( 00221 '#value' => t('Currently, jobs exist related to chado organisms. Please check back later for organisms that can by synced once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.') 00222 ); 00223 } 00224 } 00229 function tripal_organism_admin_validate($form, &$form_state) { 00230 global $user; // we need access to the user info 00231 $job_args = array(); 00232 00233 if ($form_state['values']['op'] == t('Submit Sync Job')) { 00234 00235 // check to see if the user wants to sync chado and drupal. If 00236 // so then we need to register a job to do so with tripal 00237 $organisms = $form_state['values']['organisms']; 00238 $do_all = FALSE; 00239 $to_sync = array(); 00240 00241 foreach ($organisms as $organism_id) { 00242 if (preg_match("/^all$/i" , $organism_id)) { 00243 $do_all = TRUE; 00244 } 00245 if ($organism_id and preg_match("/^\d+$/i" , $organism_id)) { 00246 // get the list of organisms 00247 $sql = "SELECT * FROM {Organism} WHERE organism_id = %d"; 00248 $organism = db_fetch_object(chado_query($sql, $organism_id)); 00249 $to_sync[$organism_id] = "$organism->genus $organism->species"; 00250 } 00251 } 00252 00253 // submit the job the tripal job manager 00254 if ($do_all) { 00255 tripal_add_job('Sync all organisms' , 'tripal_organism', 00256 'tripal_organism_sync_organisms' , $job_args , $user->uid); 00257 } 00258 else{ 00259 foreach ($to_sync as $organism_id => $name) { 00260 $job_args[0] = $organism_id; 00261 tripal_add_job("Sync organism: $name" , 'tripal_organism', 00262 'tripal_organism_sync_organisms' , $job_args , $user->uid); 00263 } 00264 } 00265 } 00266 00267 // ------------------------------------- 00268 // Submit the Reindex Job if selected 00269 if ($form_state['values']['op'] == t('Reindex Features')) { 00270 $organisms = $form_state['values']['re-organisms']; 00271 foreach ($organisms as $organism_id) { 00272 if ($organism_id and preg_match("/^\d+$/i" , $organism_id)) { 00273 // get the organism info 00274 $sql = "SELECT * FROM {organism} WHERE organism_id = %d"; 00275 $organism = db_fetch_object(chado_query($sql , $organism_id)); 00276 $job_args[0] = $organism_id; 00277 tripal_add_job("Reindex features for organism: $organism->genus ". 00278 "$organism->species", 'tripal_organism' , 00279 'tripal_organism_reindex_features', $job_args, $user->uid); 00280 } 00281 } 00282 } 00283 00284 // ------------------------------------- 00285 // Submit the taxonomy Job if selected 00286 if ($form_state['values']['op'] == t('Set Feature Taxonomy')) { 00287 $organisms = $form_state['values']['tx-organisms']; 00288 foreach ($organisms as $organism_id) { 00289 if ($organism_id and preg_match("/^\d+$/i", $organism_id)) { 00290 // get the organism info 00291 $sql = "SELECT * FROM {organism} WHERE organism_id = %d"; 00292 $organism = db_fetch_object(chado_query($sql , $organism_id)); 00293 $job_args[0] = $organism_id; 00294 tripal_add_job("Set taxonomy for features in organism: ". 00295 "$organism->genus $organism->species" , 'tripal_organism', 00296 'tripal_organism_taxonify_features', $job_args, $user->uid); 00297 } 00298 } 00299 } 00300 00301 // ------------------------------------- 00302 // Submit the Cleanup Job if selected 00303 if ($form_state['values']['op'] == t('Clean up orphaned organisms')) { 00304 tripal_add_job('Cleanup orphaned organisms', 'tripal_organism', 00305 'tripal_organism_cleanup', $job_args, $user->uid); 00306 } 00307 } 00308 00314 function tripal_organism_sync_organisms($organism_id = NULL, $job_id = NULL) { 00315 global $user; 00316 $page_content = ''; 00317 00318 if (!$organism_id) { 00319 $sql = "SELECT * FROM {Organism} O"; 00320 $results = chado_query($sql); 00321 } 00322 else { 00323 $sql = "SELECT * FROM {Organism} L WHERE organism_id = %d"; 00324 $results = chado_query($sql, $organism_id); 00325 } 00326 00327 // We'll use the following SQL statement for checking if the organism 00328 // already exists as a drupal node. 00329 $sql = "SELECT * FROM {chado_organism} ". 00330 "WHERE organism_id = %d"; 00331 00332 while ($organism = db_fetch_object($results)) { 00333 00334 // check if this organism already exists in the drupal database. if it 00335 // does then skip this organism and go to the next one. 00336 if (!db_fetch_object(db_query($sql, $organism->organism_id))) { 00337 00338 $new_node = new stdClass(); 00339 $new_node->type = 'chado_organism'; 00340 $new_node->uid = $user->uid; 00341 $new_node->title = "$organism->genus $organism->species"; 00342 $new_node->organism_id = $organism->organism_id; 00343 $new_node->genus = $organism->genus; 00344 $new_node->species = $organism->species; 00345 $new_node->description = ''; 00346 node_validate($new_node); 00347 if (!form_get_errors()) { 00348 $node = node_submit($new_node); 00349 node_save($node); 00350 if ($node->nid) { 00351 print "Added $organism->common_name\n"; 00352 } 00353 } 00354 else { 00355 print "Failed to insert organism $organism->common_name\n"; 00356 } 00357 } 00358 else { 00359 print "Skipped $organism->common_name\n"; 00360 } 00361 } 00362 return $page_content; 00363 } 00368 function tripal_organism_reindex_features($organism_id = NULL, $job_id = NULL) { 00369 $i = 0; 00370 00371 if (!$organism_id) { 00372 return; 00373 } 00374 00375 $sql = "SELECT * ". 00376 "FROM {feature} ". 00377 "WHERE organism_id = $organism_id ". 00378 "ORDER BY feature_id"; 00379 $results = chado_query($sql); 00380 00381 // load into ids array 00382 $count = 0; 00383 $ids = array(); 00384 while ($id = db_fetch_object($results)) { 00385 $ids[$count] = $id->feature_id; 00386 $count++; 00387 } 00388 00389 $interval = intval($count * 0.01); 00390 foreach ($ids as $feature_id) { 00391 // update the job status every 1% features 00392 if ($job_id and $i % $interval == 0) { 00393 tripal_job_set_progress($job_id , intval(($i/$count)*100)); 00394 } 00395 tripal_feature_sync_feature($feature_id); 00396 $i++; 00397 } 00398 } 00403 function tripal_organism_taxonify_features($organism_id = NULL, $job_id = NULL) { 00404 $i = 0; 00405 00406 if (!$organism_id) { 00407 return; 00408 } 00409 00410 $sql = "SELECT * ". 00411 "FROM {feature} ". 00412 "WHERE organism_id = $organism_id ". 00413 "ORDER BY feature_id"; 00414 $results = chado_query($sql); 00415 00416 // load into ids array 00417 $count = 0; 00418 $ids = array(); 00419 while ($id = db_fetch_object($results)) { 00420 $ids[$count] = $id->feature_id; 00421 $count++; 00422 } 00423 00424 // make sure our vocabularies are set before proceeding 00425 tripal_feature_set_vocabulary(); 00426 00427 // use this SQL for getting the nodes 00428 $nsql = "SELECT * FROM {chado_feature} CF ". 00429 " INNER JOIN {node} N ON N.nid = CF.nid ". 00430 "WHERE feature_id = %d"; 00431 00432 // iterate through the features and set the taxonomy 00433 $interval = intval($count * 0.01); 00434 foreach ($ids as $feature_id) { 00435 // update the job status every 1% features 00436 if ($job_id and $i % $interval == 0) { 00437 tripal_job_set_progress($job_id, intval(($i/$count)*100)); 00438 } 00439 $node = db_fetch_object(db_query($nsql, $feature_id)); 00440 tripal_feature_set_taxonomy($node, $feature_id); 00441 $i++; 00442 } 00443 } 00454 function tripal_organism_cleanup($dummy = NULL, $job_id = NULL) { 00455 00456 return tripal_core_clean_orphaned_nodes('organism', $job_id); 00457 }