Tripal v1.0 (6.x-1.0)
|
00001 <?php 00015 function tripal_stock_admin() { 00016 $form = array(); 00017 00018 // before proceeding check to see if we have any 00019 // currently processing jobs. If so, we don't want 00020 // to give the opportunity to sync Stocks 00021 $active_jobs = FALSE; 00022 if (tripal_get_module_active_jobs('tripal_stock')) { 00023 $active_jobs = TRUE; 00024 } 00025 if ($active_jobs) { 00026 $form['notice'] = array( 00027 '#type' => 'fieldset', 00028 '#title' => t('Stock Management Temporarily Unavailable') 00029 ); 00030 $form['notice']['message'] = array( 00031 '#value' => t("Currently, stock management jobs are waiting or ". 00032 "are running. Managemment features have been hidden until these ". 00033 "jobs complete. Please check back later once these jobs have ". 00034 "finished. You can view the status of pending jobs in the Tripal ". 00035 "jobs page."), 00036 ); 00037 } 00038 else { 00039 00040 // SET Vocabularies ----------------------------------------------------------------------------------------- 00041 $form['set_cv'] = array( 00042 '#type' => 'fieldset', 00043 '#title' => t('Set Stock Controlled Vocabularies'), 00044 '#weight' => -10 00045 ); 00046 00047 $form['set_cv']['message'] = array( 00048 '#value' => t("This setting allows you to set which chado controlled vocabularies (cv)" 00049 ." are used. Cvs are used to control user input for the type of stock," 00050 ." any properties they enter for a stock & the types of relationships" 00051 ." between stocks. Only cvs already loaded into chado can be selected here.") 00052 ); 00053 00054 // get the list of CVs for the next form element 00055 $sql = "SELECT * FROM {cv} ORDER BY name"; 00056 $results = chado_query($sql); 00057 $cv_options = array(); 00058 while ($r = db_fetch_object($results)) { 00059 $cv_options[$r->cv_id] = $r->name; 00060 } 00061 00062 $form['set_cv']['stock_types_cv'] = array( 00063 '#type' => 'select', 00064 '#title' => t('Controlled Vocabulary governing Stock Types'), 00065 '#options' => $cv_options, 00066 '#default_value' => variable_get('chado_stock_types_cv', 0) 00067 ); 00068 00069 $form['set_cv']['stock_prop_types_cv'] = array( 00070 '#type' => 'select', 00071 '#title' => t('Controlled Vocabulary governing Types of Stock Properties'), 00072 '#description' => t("This cv must contain a cvterm entry where name='synonym'."), 00073 '#options' => $cv_options, 00074 '#default_value' => variable_get('chado_stock_prop_types_cv', 0) 00075 ); 00076 00077 $form['set_cv']['stock_relationship_cv'] = array( 00078 '#type' => 'select', 00079 '#title' => t('Controlled Vocabulary governing Types of Relationsips between Stocks'), 00080 '#options' => $cv_options, 00081 '#default_value' => variable_get('chado_stock_relationship_cv', 0) 00082 ); 00083 00084 $form['set_cv']['button'] = array( 00085 '#type' => 'submit', 00086 '#value' => t('Set Controlled Vacabularies') 00087 ); 00088 00089 // SYNC STOCKS----------------------------------------------------------------------------------------------- 00090 $form['sync'] = array( 00091 '#type' => 'fieldset', 00092 '#title' => t('Sync Stocks'), 00093 '#weight' => -10 00094 ); 00095 00096 $form['sync']['description'] = array( 00097 '#type' => 'item', 00098 '#value' => t("Click the 'Sync all Germplasm' button to create Drupal ". 00099 "content for stocks in chado. Depending on the ". 00100 "number of stocks in the chado database this may take a long ". 00101 "time to complete. ") 00102 ); 00103 00104 // get the list of organisms 00105 $sql = "SELECT * FROM {Organism} ORDER BY genus, species"; 00106 $org_rset = chado_query($sql); 00107 $organisms = array(); 00108 $organisms[''] = ''; 00109 while ($organism = db_fetch_object($org_rset)) { 00110 $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)"; 00111 } 00112 $form['sync']['organisms'] = array( 00113 '#type' => 'checkboxes', 00114 '#title' => t('Organisms for which Stocks should be sync\'d'), 00115 '#description' => t('Only sync\'d Organisms are listed. Leaving an organism unchecked does not delete already sync\'d Stocks.'), 00116 '#options' => $organisms, 00117 '#required' => FALSE, 00118 '#prefix' => '<div id="lib_boxes">', 00119 '#suffix' => '</div>' 00120 ); 00121 00122 $form['sync']['button'] = array( 00123 '#type' => 'submit', 00124 '#value' => t('Sync Stocks') 00125 ); 00126 get_tripal_stock_admin_form_cleanup_set($form); 00127 } 00128 00129 return system_settings_form($form); 00130 00131 } 00132 00143 function tripal_stock_admin_validate($form, &$form_state) { 00144 global $user; // we need access to the user info 00145 $job_args = array(); 00146 00147 // Sync Stocks 00148 if ($form_state['values']['op'] == t('Sync Stocks')) { 00149 // Array organism_id => organims common_name 00150 // which only includes those organisms which the user wants to select stocks for 00151 $organisms_2b_syncd = $form_state['values']['organisms']; 00152 00153 //for each organism selected submit job (handled by tripal_stock_sync_stock_set) 00154 // which syncs all stocks with an organism_id equal to the selelcted organism 00155 foreach ( $organisms_2b_syncd as $organism_id ) { 00156 if ($organism_id != 0) { 00157 $job_args[0] = $organism_id; 00158 tripal_add_job("Sync Stocks from Organism $organism_id", 'tripal_stock', 00159 'tripal_stock_sync_stock_set', $job_args, $user->uid); 00160 } 00161 } 00162 } 00163 00164 if ($form_state['values']['op'] == t('Set Controlled Vacabularies')) { 00165 variable_set('chado_stock_types_cv', $form_state['values']['stock_types_cv']); 00166 variable_set('chado_stock_prop_types_cv', $form_state['values']['stock_prop_types_cv']); 00167 variable_set('chado_stock_relationship_cv', $form_state['values']['stock_relationship_cv']); 00168 } 00169 00170 // Submit the Cleanup Job if selected 00171 if ($form_state['values']['op'] == t('Clean up orphaned stocks')) { 00172 tripal_add_job('Cleanup orphaned stocks', 'tripal_stock', 00173 'tripal_stock_cleanup', $job_args, $user->uid); 00174 } 00175 } 00176 00187 function tripal_stock_sync_stocks($organism_id, $job_id) { 00188 00189 if ($organism_id) { 00190 return tripal_stock_sync_stock_set($organism_id, $job_id); 00191 } 00192 else { 00193 //get a list of all organisms and sync all stocks for all organisms 00194 $organisms = tripal_core_chado_select('organism', array('organism_id','genus','species','common_name'), array()); 00195 foreach ($organisms as $o) { 00196 print "Syncing stocks associated with $o->genus $o->species ($o->common_name)\n"; 00197 tripal_stock_sync_stock_set($o->organism_id, $job_id); 00198 } 00199 } 00200 00201 } 00202 00218 function tripal_stock_sync_stock_set($organism_id, $job_id) { 00219 global $user; 00220 00221 if (!$organism_id) { 00222 print '0 Stocks to Sync -No Organisms Selected.\n'; 00223 } 00224 else { 00225 00226 // Get list of stocks to sync 00227 $result = chado_query( 00228 "SELECT stock_id, uniquename, type_id, organism_id FROM {stock} WHERE organism_id=%d", 00229 $organism_id 00230 ); 00231 00232 $stocks_created_count = 0; //keeps track of total number of stocks successfully created 00233 $stocks_attempted = 0; 00234 // foreach stock to be sync'd -> create node & add stock_id 00235 while ( $r = db_fetch_object($result) ) { 00236 // $r is the current stock to be sync'd 00237 $stocks_attempted++; 00238 00239 print 'Processing ' . $r->uniquename . "... "; 00240 00241 // check not already in drupal 00242 $in_drupal_query = db_query( 00243 "SELECT * FROM {chado_stock} WHERE stock_id=%d", 00244 $r->stock_id 00245 ); 00246 if ( !db_fetch_object($in_drupal_query) ) { 00247 00248 //create new chado_stock node 00249 $new_node = new stdClass(); 00250 $new_node->type = 'chado_stock'; 00251 $new_node->uid = $user->uid; 00252 $new_node->title = $r->uniquename; 00253 $new_node->type_id = $r->type_id; 00254 $new_node->organism_id = $r->organism_id; 00255 $new_node->stock_id = $r->stock_id; 00256 $new_node->chado_stock_exists = TRUE; 00257 00258 //print 'New Node:'; 00259 //print_r($new_node); 00260 00261 node_validate($new_node); 00262 00263 if (!form_get_errors()) { 00264 //print 'Try to Create Node '; 00265 $node = node_submit($new_node); 00266 node_save($node); 00267 if ($node->nid) { 00268 $stocks_created_count++; 00269 00270 //Add stock id to chado_stock table 00279 } 00280 } 00281 else { 00282 print "Not completed due to errors:\nCreate Stock Form Errors: "; 00283 print_r(form_get_errors()); 00284 } 00285 print "Nid=" . $node->nid . "\n"; 00286 } 00287 else { 00288 print "Skipped $r->uniquename because it's already in drupal.\n"; 00289 } //end of if not already in drupal 00290 } //end of while still stocks to be sync'd 00291 } //end of if organism_id not supplied 00292 00293 if ($stocks_attempted == 0) { 00294 print "No stocks retrieved for organism (" . $organism_id . ")\n"; 00295 return 1; 00296 } 00297 else { 00298 if ($stocks_created_count > 0) { 00299 print "$stocks_created_count Stocks Successfully Created\n"; 00300 return 1; 00301 } 00302 else { 00303 return 0; 00304 } 00305 } 00306 } 00307 00313 function get_tripal_stock_admin_form_cleanup_set(&$form) { 00314 $form['cleanup'] = array( 00315 '#type' => 'fieldset', 00316 '#title' => t('Clean Up') 00317 ); 00318 $form['cleanup']['description'] = array( 00319 '#type' => 'item', 00320 '#value' => t("With Drupal and Chado residing in different databases ". 00321 "it is possible that nodes in Drupal and stocks in Chado become ". 00322 "\"orphaned\". This can occur if an stock node in Drupal is ". 00323 "deleted but the corresponding chado stock is not and/or vice ". 00324 "versa. Click the button below to resolve these discrepancies."), 00325 '#weight' => 1, 00326 ); 00327 $form['cleanup']['button'] = array( 00328 '#type' => 'submit', 00329 '#value' => t('Clean up orphaned stocks'), 00330 '#weight' => 2, 00331 ); 00332 } 00343 function tripal_stock_cleanup($dummy = NULL, $job_id = NULL) { 00344 00345 return tripal_core_clean_orphaned_nodes('stock', $job_id); 00346 00347 }