tripal_organism.admin.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @file
  4. * Administration of organisms
  5. */
  6. /**
  7. * Admin launchpad
  8. *
  9. * @ingroup tripal_organism
  10. */
  11. function tripal_organism_admin_organism_view() {
  12. $output = '';
  13. // set the breadcrumb
  14. $breadcrumb = array();
  15. $breadcrumb[] = l('Home', '<front>');
  16. $breadcrumb[] = l('Administration', 'admin');
  17. $breadcrumb[] = l('Tripal', 'admin/tripal');
  18. $breadcrumb[] = l('Chado', 'admin/tripal/chado');
  19. $breadcrumb[] = l('Organisms', 'admin/tripal/chado/tripal_organism');
  20. drupal_set_breadcrumb($breadcrumb);
  21. // Add the view
  22. $view = views_embed_view('tripal_organism_admin_organisms','default');
  23. if (isset($view)) {
  24. $output .= $view;
  25. }
  26. else {
  27. $output .= '<p>The Organism module uses primarily views to provide an '
  28. . 'administrative interface. Currently one or more views needed for this '
  29. . 'administrative interface are disabled. <strong>Click each of the following links to '
  30. . 'enable the pertinent views</strong>:</p>';
  31. $output .= '<ul>';
  32. $output .= '<li>'.l('Organisms View', 'admin/tripal/chado/tripal_organism/views/organisms/enable').'</li>';
  33. $output .= '</ul>';
  34. }
  35. return $output;
  36. }
  37. /**
  38. * Administrative settings for chado_orgnism
  39. *
  40. * @ingroup tripal_organism
  41. */
  42. function tripal_organism_admin() {
  43. $form = array();
  44. $form['nothing'] = array(
  45. '#markup' => t('There are currently no settings to configure.')
  46. );
  47. return system_settings_form($form);
  48. }
  49. /**
  50. * Validate the organism settings form
  51. *
  52. * @ingroup tripal_organism
  53. */
  54. function tripal_organism_admin_validate($form, &$form_state) {
  55. global $user; // we need access to the user info
  56. $job_args = array();
  57. // -------------------------------------
  58. // Submit the Reindex Job if selected
  59. if ($form_state['values']['op'] == t('Reindex Features')) {
  60. $organisms = $form_state['values']['re-organisms'];
  61. foreach ($organisms as $organism_id) {
  62. if ($organism_id and preg_match("/^\d+$/i" , $organism_id)) {
  63. // get the organism info
  64. $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
  65. $organism = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  66. $job_args[0] = $organism_id;
  67. tripal_add_job("Reindex features for organism: $organism->genus " .
  68. "$organism->species", 'tripal_organism' ,
  69. 'tripal_organism_reindex_features', $job_args, $user->uid);
  70. }
  71. }
  72. }
  73. // -------------------------------------
  74. // Submit the taxonomy Job if selected
  75. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  76. $organisms = $form_state['values']['tx-organisms'];
  77. foreach ($organisms as $organism_id) {
  78. if ($organism_id and preg_match("/^\d+$/i", $organism_id)) {
  79. // get the organism info
  80. $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
  81. $organism = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  82. $job_args[0] = $organism_id;
  83. tripal_add_job("Set taxonomy for features in organism: " .
  84. "$organism->genus $organism->species" , 'tripal_organism',
  85. 'tripal_organism_taxonify_features', $job_args, $user->uid);
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * Add a job to reindex the features associated with an organism for drupal search
  92. *
  93. * @param $organism_id
  94. * The organism_id of the organism to re-index features of
  95. * @param $job_id
  96. * (optional) specify a tripal job_id to create
  97. *
  98. * @ingroup tripal_organism
  99. */
  100. function tripal_organism_reindex_features($organism_id = NULL, $job_id = NULL) {
  101. $i = 0;
  102. if (!$organism_id) {
  103. return;
  104. }
  105. $sql = "
  106. SELECT *
  107. FROM {feature}
  108. WHERE organism_id = :organism_id
  109. ORDER BY feature_id";
  110. $results = chado_query($sql, array(':organism_id' => $organism_id));
  111. // load into ids array
  112. $count = 0;
  113. $ids = array();
  114. foreach ($results as $id) {
  115. $ids[$count] = $id->feature_id;
  116. $count++;
  117. }
  118. $interval = intval($count * 0.01);
  119. foreach ($ids as $feature_id) {
  120. // update the job status every 1% features
  121. if ($job_id and $i % $interval == 0) {
  122. tripal_set_job_progress($job_id , intval(($i/$count)*100));
  123. }
  124. $i++;
  125. }
  126. }
  127. /**
  128. * Add a tripal job to add drupal taxonomy to organisms
  129. *
  130. * @param $organism_id
  131. * The organism_id of the organism to add taxonomy to
  132. * @param $job_id
  133. * (optional) specify a tripal job_id to create
  134. *
  135. * @ingroup tripal_organism
  136. */
  137. function tripal_organism_taxonify_features($organism_id = NULL, $job_id = NULL) {
  138. $i = 0;
  139. if (!$organism_id) {
  140. return;
  141. }
  142. $sql = "
  143. SELECT *
  144. FROM {feature}
  145. WHERE organism_id = :organism_id
  146. ORDER BY feature_id
  147. ";
  148. $results = chado_query($sql, array(':organism_id' => $organism_id));
  149. // load into ids array
  150. $count = 0;
  151. $ids = array();
  152. foreach ($results as $id) {
  153. $ids[$count] = $id->feature_id;
  154. $count++;
  155. }
  156. // make sure our vocabularies are set before proceeding
  157. tripal_feature_set_vocabulary();
  158. // use this SQL for getting the nodes
  159. $nsql = "SELECT * FROM {chado_feature} CF " .
  160. " INNER JOIN {node} N ON N.nid = CF.nid " .
  161. "WHERE feature_id = :feature_id";
  162. // iterate through the features and set the taxonomy
  163. $interval = intval($count * 0.01);
  164. foreach ($ids as $feature_id) {
  165. // update the job status every 1% features
  166. if ($job_id and $i % $interval == 0) {
  167. tripal_set_job_progress($job_id, intval(($i/$count)*100));
  168. }
  169. $node = db_query($nsql, array(':feature_id' => $feature_id))->fetchObject();
  170. tripal_feature_set_taxonomy($node, $feature_id);
  171. $i++;
  172. }
  173. }