tripal_stock-administration.inc 11 KB

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