tripal_stock-administration.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. // $Id$
  3. /*************************************************************************
  4. * Purpose: Provide administration options for chado_stocks
  5. *
  6. * @return form array (as described by the drupal form api)
  7. */
  8. function tripal_stock_admin() {
  9. $form = array();
  10. // before proceeding check to see if we have any
  11. // currently processing jobs. If so, we don't want
  12. // to give the opportunity to sync Stocks
  13. $active_jobs = FALSE;
  14. if(tripal_get_module_active_jobs('tripal_stock')){
  15. $active_jobs = TRUE;
  16. }
  17. if($active_jobs){
  18. $form['notice'] = array(
  19. '#type' => 'fieldset',
  20. '#title' => t('Stock Management Temporarily Unavailable')
  21. );
  22. $form['notice']['message'] = array(
  23. '#value' => t("Currently, stock management jobs are waiting or ".
  24. "are running. Managemment features have been hidden until these ".
  25. "jobs complete. Please check back later once these jobs have ".
  26. "finished. You can view the status of pending jobs in the Tripal ".
  27. "jobs page."),
  28. );
  29. } else {
  30. // SET Vocabularies -----------------------------------------------------------------------------------------
  31. $form['set_cv'] = array(
  32. '#type' => 'fieldset',
  33. '#title' => t('Set Stock Controlled Vocabularies'),
  34. '#weight' => -10
  35. );
  36. $form['set_cv']['message'] = array(
  37. '#value' => t("This setting allows you to set which chado controlled vocabularies (cv)"
  38. ." are used. Cvs are used to control user input for the type of stock,"
  39. ." any properties they enter for a stock & the types of relationships"
  40. ." between stocks. Only cvs already loaded into chado can be selected here.")
  41. );
  42. $cv_options = tripal_cv_get_cv_options();
  43. $form['set_cv']['stock_types_cv'] = array(
  44. '#type' => 'select',
  45. '#title' => t('Controlled Vocabulary governing Stock Types'),
  46. '#options' => $cv_options,
  47. '#default_value' => variable_get('chado_stock_types_cv', 0)
  48. );
  49. $form['set_cv']['stock_prop_types_cv'] = array(
  50. '#type' => 'select',
  51. '#title' => t('Controlled Vocabulary governing Types of Stock Properties'),
  52. '#description' => t("This cv must contain a cvterm entry where name='synonym'."),
  53. '#options' => $cv_options,
  54. '#default_value' => variable_get('chado_stock_prop_types_cv', 0)
  55. );
  56. $form['set_cv']['stock_relationship_cv'] = array(
  57. '#type' => 'select',
  58. '#title' => t('Controlled Vocabulary governing Types of Relationsips between Stocks'),
  59. '#options' => $cv_options,
  60. '#default_value' => variable_get('chado_stock_relationship_cv', 0)
  61. );
  62. $form['set_cv']['button'] = array(
  63. '#type' => 'submit',
  64. '#value' => t('Set Controlled Vacabularies')
  65. );
  66. // SYNC STOCKS-----------------------------------------------------------------------------------------------
  67. $form['sync'] = array(
  68. '#type' => 'fieldset',
  69. '#title' => t('Sync Stocks'),
  70. '#weight' => -10
  71. );
  72. $form['sync']['description'] = array(
  73. '#type' => 'item',
  74. '#value' => t("Click the 'Sync all Germplasm' button to create Drupal ".
  75. "content for stocks in chado. Depending on the ".
  76. "number of stocks in the chado database this may take a long ".
  77. "time to complete. ")
  78. );
  79. $form['sync']['organisms'] = array(
  80. '#type' => 'checkboxes',
  81. '#title' => t('Organisms for which Stocks should be sync\'d'),
  82. '#description' => t('Only sync\'d Organisms are listed. Leaving an organism unchecked does not delete already sync\'d Stocks.'),
  83. '#options' => tripal_organism_get_organism_options(),
  84. '#required' => FALSE,
  85. '#prefix' => '<div id="lib_boxes">',
  86. '#suffix' => '</div>'
  87. );
  88. $form['sync']['button'] = array(
  89. '#type' => 'submit',
  90. '#value' => t('Sync Stocks')
  91. );
  92. }
  93. return system_settings_form($form);
  94. }
  95. /**
  96. * Implements hook_form_submit()
  97. *
  98. * Handles Submit actions of all subforms
  99. */
  100. function tripal_stock_admin_validate($form, &$form_state) {
  101. global $user; // we need access to the user info
  102. $job_args = array();
  103. // Sync Stocks
  104. if ($form_state['values']['op'] == t('Sync Stocks')) {
  105. // Array organism_id => organims common_name
  106. // which only includes those organisms which the user wants to select stocks for
  107. $organisms_2b_syncd = $form_state['values']['organisms'];
  108. //for each organism selected submit job (handled by tripal_stock_sync_stock_set)
  109. // which syncs all stocks with an organism_id equal to the selelcted organism
  110. foreach ( $organisms_2b_syncd as $organism_id ) {
  111. if($organism_id != 0) {
  112. $job_args[0] = $organism_id;
  113. tripal_add_job("Sync Stocks from Organism $organism_id",'tripal_stock',
  114. 'tripal_stock_sync_stock_set',$job_args,$user->uid);
  115. }
  116. }
  117. }
  118. if ($form_state['values']['op'] == t('Set Controlled Vacabularies')) {
  119. variable_set('chado_stock_types_cv', $form_state['values']['stock_types_cv']);
  120. variable_set('chado_stock_prop_types_cv', $form_state['values']['stock_prop_types_cv']);
  121. variable_set('chado_stock_relationship_cv', $form_state['values']['stock_relationship_cv']);
  122. }
  123. }
  124. /**************************************************************************************************************/
  125. /**
  126. * Syncs all Stocks associated with an organism
  127. * Note: Handling of multiple organisms is done in tripal_stock_admin_validate()
  128. *
  129. * @params $organisms
  130. */
  131. function tripal_stock_sync_stock_set($organism_id, $job_id) {
  132. global $user;
  133. if(!$organism_id) {
  134. print '0 Stocks to Sync -No Organisms Selected.\n';
  135. } else {
  136. // Get list of stocks to sync
  137. $previous_db = tripal_db_set_active('chado');
  138. $result = db_query(
  139. "SELECT stock_id, uniquename, type_id, organism_id FROM stock WHERE organism_id=%d",
  140. $organism_id
  141. );
  142. tripal_db_set_active($previous_db);
  143. $stocks_created_count = 0; //keeps track of total number of stocks successfully created
  144. $stocks_attempted = 0;
  145. // foreach stock to be sync'd -> create node & add stock_id
  146. while ( $r = db_fetch_object($result) ) {
  147. // $r is the current stock to be sync'd
  148. $stocks_attempted++;
  149. print 'Processing '.$r->uniquename."\n";
  150. // check not already in drupal
  151. $in_drupal_query = db_query(
  152. "SELECT * FROM {chado_stock} WHERE stock_id=%d",
  153. $r->stock_id
  154. );
  155. if ( !db_fetch_object($in_drupal_query) ) {
  156. //create new chado_stock node
  157. $new_node = new stdClass();
  158. $new_node->type = 'chado_stock';
  159. $new_node->uid = $user->uid;
  160. $new_node->title = $r->uniquename;
  161. $new_node->type_id = $r->type_id;
  162. $new_node->organism_id = $r->organism_id;
  163. $new_node->stock_id = $r->stock_id;
  164. //print 'New Node:';
  165. //print_r($new_node);
  166. node_validate($new_node);
  167. if(!form_get_errors()){
  168. //print 'Try to Create Node ';
  169. $node = node_submit($new_node);
  170. node_save($node);
  171. if($node->nid){
  172. $stocks_created_count++;
  173. //Add stock id to chado_stock table
  174. db_query(
  175. "UPDATE {chado_stock} SET stock_id=%d WHERE nid=%d AND vid=%d",
  176. $r->stock_id,
  177. $node->nid,
  178. $node->vid
  179. );
  180. }
  181. } else {
  182. print "\tCreate Stock Form Errors: ";
  183. print_r(form_get_errors());
  184. }
  185. print "\n\tNid=".$node->nid."\n";
  186. } else {
  187. print "\tSkipped $r->uniquename because it's already in drupal.\n";
  188. } //end of if not already in drupal
  189. } //end of while still stocks to be sync'd
  190. } //end of if organism_id not supplied
  191. if ($stocks_attempted == 0) {
  192. print "No stocks retrieved for organism (".$organism_id.")\n";
  193. return 1;
  194. } else {
  195. if ($stocks_created_count > 0) {
  196. print "$stocks_created_count Stocks Successfully Created\n";
  197. return 1;
  198. } else {
  199. return 0;
  200. }
  201. }
  202. }