tripal_stock-administration.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Purpose: Provide administration options for chado_stocks
  8. *
  9. * @return
  10. * Form array (as described by the drupal form api)
  11. *
  12. * @ingroup tripal_stock
  13. */
  14. function tripal_stock_admin() {
  15. $form = array();
  16. // before proceeding check to see if we have any
  17. // currently processing jobs. If so, we don't want
  18. // to give the opportunity to sync Stocks
  19. $active_jobs = FALSE;
  20. if (tripal_get_module_active_jobs('tripal_stock')) {
  21. $active_jobs = TRUE;
  22. }
  23. if ($active_jobs) {
  24. $form['notice'] = array(
  25. '#type' => 'fieldset',
  26. '#title' => t('Stock Management Temporarily Unavailable')
  27. );
  28. $form['notice']['message'] = array(
  29. '#value' => t("Currently, stock management jobs are waiting or ".
  30. "are running. Managemment features have been hidden until these ".
  31. "jobs complete. Please check back later once these jobs have ".
  32. "finished. You can view the status of pending jobs in the Tripal ".
  33. "jobs page."),
  34. );
  35. }
  36. else {
  37. // SET Vocabularies -----------------------------------------------------------------------------------------
  38. $form['set_cv'] = array(
  39. '#type' => 'fieldset',
  40. '#title' => t('Set Stock Controlled Vocabularies'),
  41. '#weight' => -10
  42. );
  43. $form['set_cv']['message'] = array(
  44. '#value' => t("This setting allows you to set which chado controlled vocabularies (cv)"
  45. ." are used. Cvs are used to control user input for the type of stock,"
  46. ." any properties they enter for a stock & the types of relationships"
  47. ." between stocks. Only cvs already loaded into chado can be selected here.")
  48. );
  49. // get the list of CVs for the next form element
  50. $sql = "SELECT * FROM {cv} ORDER BY name";
  51. $results = chado_query($sql);
  52. $cv_options = array();
  53. while ($r = db_fetch_object($results)) {
  54. $cv_options[$r->cv_id] = $r->name;
  55. }
  56. $form['set_cv']['stock_types_cv'] = array(
  57. '#type' => 'select',
  58. '#title' => t('Controlled Vocabulary governing Stock Types'),
  59. '#options' => $cv_options,
  60. '#default_value' => variable_get('chado_stock_types_cv', 0)
  61. );
  62. $form['set_cv']['stock_prop_types_cv'] = array(
  63. '#type' => 'select',
  64. '#title' => t('Controlled Vocabulary governing Types of Stock Properties'),
  65. '#description' => t("This cv must contain a cvterm entry where name='synonym'."),
  66. '#options' => $cv_options,
  67. '#default_value' => variable_get('chado_stock_prop_types_cv', 0)
  68. );
  69. $form['set_cv']['stock_relationship_cv'] = array(
  70. '#type' => 'select',
  71. '#title' => t('Controlled Vocabulary governing Types of Relationsips between Stocks'),
  72. '#options' => $cv_options,
  73. '#default_value' => variable_get('chado_stock_relationship_cv', 0)
  74. );
  75. $form['set_cv']['button'] = array(
  76. '#type' => 'submit',
  77. '#value' => t('Set Controlled Vacabularies')
  78. );
  79. // SYNC STOCKS-----------------------------------------------------------------------------------------------
  80. $form['sync'] = array(
  81. '#type' => 'fieldset',
  82. '#title' => t('Sync Stocks'),
  83. '#weight' => -10
  84. );
  85. $form['sync']['description'] = array(
  86. '#type' => 'item',
  87. '#value' => t("Click the 'Sync all Germplasm' button to create Drupal ".
  88. "content for stocks in chado. Depending on the ".
  89. "number of stocks in the chado database this may take a long ".
  90. "time to complete. ")
  91. );
  92. // get the list of organisms
  93. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  94. $org_rset = chado_query($sql);
  95. $organisms = array();
  96. $organisms[''] = '';
  97. while ($organism = db_fetch_object($org_rset)) {
  98. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  99. }
  100. $form['sync']['organisms'] = array(
  101. '#type' => 'checkboxes',
  102. '#title' => t('Organisms for which Stocks should be sync\'d'),
  103. '#description' => t('Only sync\'d Organisms are listed. Leaving an organism unchecked does not delete already sync\'d Stocks.'),
  104. '#options' => $organisms,
  105. '#required' => FALSE,
  106. '#prefix' => '<div id="lib_boxes">',
  107. '#suffix' => '</div>'
  108. );
  109. $form['sync']['button'] = array(
  110. '#type' => 'submit',
  111. '#value' => t('Sync Stocks')
  112. );
  113. get_tripal_stock_admin_form_cleanup_set($form);
  114. }
  115. return system_settings_form($form);
  116. }
  117. /**
  118. * Implements hook_form_validate(): Validates user input
  119. *
  120. * @param $form
  121. * An array describing the form that was rendered
  122. * @param $form_state
  123. * An array describing the current state of the form including user input
  124. *
  125. * @ingroup tripal_stock
  126. */
  127. function tripal_stock_admin_validate($form, &$form_state) {
  128. global $user; // we need access to the user info
  129. $job_args = array();
  130. // Sync Stocks
  131. if ($form_state['values']['op'] == t('Sync Stocks')) {
  132. // Array organism_id => organims common_name
  133. // which only includes those organisms which the user wants to select stocks for
  134. $organisms_2b_syncd = $form_state['values']['organisms'];
  135. //for each organism selected submit job (handled by tripal_stock_sync_stock_set)
  136. // which syncs all stocks with an organism_id equal to the selelcted organism
  137. foreach ( $organisms_2b_syncd as $organism_id ) {
  138. if ($organism_id != 0) {
  139. $job_args[0] = $organism_id;
  140. tripal_add_job("Sync Stocks from Organism $organism_id", 'tripal_stock',
  141. 'tripal_stock_sync_stock_set', $job_args, $user->uid);
  142. }
  143. }
  144. }
  145. if ($form_state['values']['op'] == t('Set Controlled Vacabularies')) {
  146. variable_set('chado_stock_types_cv', $form_state['values']['stock_types_cv']);
  147. variable_set('chado_stock_prop_types_cv', $form_state['values']['stock_prop_types_cv']);
  148. variable_set('chado_stock_relationship_cv', $form_state['values']['stock_relationship_cv']);
  149. }
  150. // Submit the Cleanup Job if selected
  151. if ($form_state['values']['op'] == t('Clean up orphaned stocks')) {
  152. tripal_add_job('Cleanup orphaned stocks', 'tripal_stock',
  153. 'tripal_stock_cleanup', $job_args, $user->uid);
  154. }
  155. }
  156. /**
  157. * Sync stocks associated with a given organism or sync all stocks
  158. *
  159. * Note: This is essentially an API function to make tripal stock sync act similar to other tripal modules
  160. *
  161. * @param $organism_id
  162. * The ID of the organism to sync all stocks for
  163. * @param $job_id
  164. * The ID of the tripal job
  165. */
  166. function tripal_stock_sync_stocks($organism_id, $job_id) {
  167. if ($organism_id) {
  168. return tripal_stock_sync_stock_set($organism_id, $job_id);
  169. }
  170. else {
  171. //get a list of all organisms and sync all stocks for all organisms
  172. $organisms = tripal_core_chado_select('organism', array('organism_id','genus','species','common_name'), array());
  173. foreach ($organisms as $o) {
  174. print "Syncing stocks associated with $o->genus $o->species ($o->common_name)\n";
  175. tripal_stock_sync_stock_set($o->organism_id, $job_id);
  176. }
  177. }
  178. }
  179. /**
  180. * Syncs all Stocks associated with an organism
  181. *
  182. * Note: Handling of multiple organisms is done in tripal_stock_admin_validate()
  183. *
  184. * @param $organism_id
  185. * The chado primary key of the organism for which stocks should be sync'd
  186. * @param $job_id
  187. * The tripal job ID
  188. *
  189. * @return
  190. * TRUE if successful; FALSE otherwise
  191. *
  192. * @ingroup tripal_stock
  193. */
  194. function tripal_stock_sync_stock_set($organism_id, $job_id) {
  195. global $user;
  196. if (!$organism_id) {
  197. print '0 Stocks to Sync -No Organisms Selected.\n';
  198. }
  199. else {
  200. // Get list of stocks to sync
  201. $result = chado_query(
  202. "SELECT stock_id, uniquename, type_id, organism_id FROM {stock} WHERE organism_id=%d",
  203. $organism_id
  204. );
  205. $stocks_created_count = 0; //keeps track of total number of stocks successfully created
  206. $stocks_attempted = 0;
  207. // foreach stock to be sync'd -> create node & add stock_id
  208. while ( $r = db_fetch_object($result) ) {
  209. // $r is the current stock to be sync'd
  210. $stocks_attempted++;
  211. print 'Processing ' . $r->uniquename . "... ";
  212. // check not already in drupal
  213. $in_drupal_query = db_query(
  214. "SELECT * FROM {chado_stock} WHERE stock_id=%d",
  215. $r->stock_id
  216. );
  217. if ( !db_fetch_object($in_drupal_query) ) {
  218. //create new chado_stock node
  219. $new_node = new stdClass();
  220. $new_node->type = 'chado_stock';
  221. $new_node->uid = $user->uid;
  222. $new_node->title = $r->uniquename;
  223. $new_node->type_id = $r->type_id;
  224. $new_node->organism_id = $r->organism_id;
  225. $new_node->stock_id = $r->stock_id;
  226. $new_node->chado_stock_exists = TRUE;
  227. //print 'New Node:';
  228. //print_r($new_node);
  229. node_validate($new_node);
  230. if (!form_get_errors()) {
  231. //print 'Try to Create Node ';
  232. $node = node_submit($new_node);
  233. node_save($node);
  234. if ($node->nid) {
  235. $stocks_created_count++;
  236. //Add stock id to chado_stock table
  237. /**
  238. db_query(
  239. "INSERT INTO {chado_stock} (stock_id, nid, vid) VALUES (%d, %d, %d)",
  240. $r->stock_id,
  241. $node->nid,
  242. $node->vid
  243. );
  244. */
  245. }
  246. }
  247. else {
  248. print "Not completed due to errors:\nCreate Stock Form Errors: ";
  249. print_r(form_get_errors());
  250. }
  251. print "Nid=" . $node->nid . "\n";
  252. }
  253. else {
  254. print "Skipped $r->uniquename because it's already in drupal.\n";
  255. } //end of if not already in drupal
  256. } //end of while still stocks to be sync'd
  257. } //end of if organism_id not supplied
  258. if ($stocks_attempted == 0) {
  259. print "No stocks retrieved for organism (" . $organism_id . ")\n";
  260. return 1;
  261. }
  262. else {
  263. if ($stocks_created_count > 0) {
  264. print "$stocks_created_count Stocks Successfully Created\n";
  265. return 1;
  266. }
  267. else {
  268. return 0;
  269. }
  270. }
  271. }
  272. /**
  273. *
  274. *
  275. * @ingroup tripal_stock
  276. */
  277. function get_tripal_stock_admin_form_cleanup_set(&$form) {
  278. $form['cleanup'] = array(
  279. '#type' => 'fieldset',
  280. '#title' => t('Clean Up')
  281. );
  282. $form['cleanup']['description'] = array(
  283. '#type' => 'item',
  284. '#value' => t("With Drupal and Chado residing in different databases ".
  285. "it is possible that nodes in Drupal and stocks in Chado become ".
  286. "\"orphaned\". This can occur if an stock node in Drupal is ".
  287. "deleted but the corresponding chado stock is not and/or vice ".
  288. "versa. Click the button below to resolve these discrepancies."),
  289. '#weight' => 1,
  290. );
  291. $form['cleanup']['button'] = array(
  292. '#type' => 'submit',
  293. '#value' => t('Clean up orphaned stocks'),
  294. '#weight' => 2,
  295. );
  296. }
  297. /**
  298. * Remove orphaned drupal nodes
  299. *
  300. * @param $dummy
  301. * Not Used -kept for backwards compatibility
  302. * @param $job_id
  303. * The id of the tripal job executing this function
  304. *
  305. * @ingroup tripal_stock
  306. */
  307. function tripal_stock_cleanup($dummy = NULL, $job_id = NULL) {
  308. return tripal_core_clean_orphaned_nodes('stock', $job_id);
  309. }