tripal_project.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. *
  8. */
  9. function tripal_project_admin_project_view() {
  10. $output = '';
  11. // set the breadcrumb
  12. $breadcrumb = array();
  13. $breadcrumb[] = l('Home', '<front>');
  14. $breadcrumb[] = l('Administration', 'admin');
  15. $breadcrumb[] = l('Tripal', 'admin/tripal');
  16. $breadcrumb[] = l('Chado', 'admin/tripal/chado');
  17. $breadcrumb[] = l('Projects', 'admin/tripal/chado/tripal_project');
  18. drupal_set_breadcrumb($breadcrumb);
  19. // Add the view
  20. $view = views_embed_view('tripal_project_admin_projects','default');
  21. if (isset($view)) {
  22. $output .= $view;
  23. }
  24. else {
  25. $output .= '<p>The Project module uses primarily views to provide an '
  26. . 'administrative interface. Currently one or more views needed for this '
  27. . 'administrative interface are disabled. <strong>Click each of the following links to '
  28. . 'enable the pertinent views</strong>:</p>';
  29. $output .= '<ul>';
  30. $output .= '<li>'.l('Projects View', 'admin/tripal/chado/tripal_project/views/projects/enable').'</li>';
  31. $output .= '</ul>';
  32. }
  33. return $output;
  34. }
  35. function tripal_project_admin($form_state = NULL) {
  36. $form = array();
  37. // before proceeding check to see if we have any
  38. // currently processing jobs. If so, we don't want
  39. // to give the opportunity to sync libraries
  40. $active_jobs = FALSE;
  41. if (tripal_get_module_active_jobs('tripal_project')) {
  42. $active_jobs = TRUE;
  43. }
  44. // add the field set for syncing libraries
  45. if (!$active_jobs) {
  46. get_tripal_project_admin_form_sync_set($form);
  47. get_tripal_project_admin_form_cleanup_set($form);
  48. // get_tripal_project_admin_form_reindex_set($form);
  49. }
  50. else {
  51. $form['notice'] = array(
  52. '#type' => 'fieldset',
  53. '#title' => t('Project Management Temporarily Unavailable')
  54. );
  55. $form['notice']['message'] = array(
  56. '#value' => t('Currently, project management jobs are waiting or are running. . Managemment features have been hidden until these jobs complete. Please check back later once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.'),
  57. );
  58. }
  59. return system_settings_form($form);
  60. }
  61. /**
  62. *
  63. *
  64. * @ingroup tripal_project
  65. */
  66. function get_tripal_project_admin_form_cleanup_set(&$form) {
  67. $form['cleanup'] = array(
  68. '#type' => 'fieldset',
  69. '#title' => t('Clean Up')
  70. );
  71. $form['cleanup']['description'] = array(
  72. '#type' => 'item',
  73. '#value' => t("With Drupal and chado residing in different databases ".
  74. "it is possible that nodes in Drupal and projects in Chado become ".
  75. "\"orphaned\". This can occur if an project node in Drupal is ".
  76. "deleted but the corresponding chado project is not and/or vice ".
  77. "versa. Click the button below to resolve these discrepancies."),
  78. '#weight' => 1,
  79. );
  80. $form['cleanup']['button'] = array(
  81. '#type' => 'submit',
  82. '#value' => t('Clean up orphaned projects'),
  83. '#weight' => 2,
  84. );
  85. }
  86. /**
  87. *
  88. * @ingroup tripal_project
  89. */
  90. function get_tripal_project_admin_form_sync_set(&$form) {
  91. // define the fieldsets
  92. $form['sync'] = array(
  93. '#type' => 'fieldset',
  94. '#title' => t('Sync Projects')
  95. );
  96. // before proceeding check to see if we have any
  97. // currently processing jobs. If so, we don't want
  98. // to give the opportunity to sync libraries
  99. $active_jobs = FALSE;
  100. if (tripal_get_module_active_jobs('tripal_project')) {
  101. $active_jobs = TRUE;
  102. }
  103. if (!$active_jobs) {
  104. // get the list of projects
  105. $sql = "SELECT * FROM {project} ORDER BY name";
  106. $org_rset = chado_query($sql);
  107. // if we've added any projects to the list that can be synced
  108. // then we want to build the form components to allow the user
  109. // to select one or all of them. Otherwise, just present
  110. // a message stating that all projects are currently synced.
  111. $proj_boxes = array();
  112. $added = 0;
  113. while ($project = $org_rset->fetchObject()) {
  114. // check to see if the project is already present as a node in drupal.
  115. // if so, then skip it.
  116. $sql = "SELECT * FROM {chado_project} WHERE project_id = :project_id";
  117. if (!db_query($sql, array(':project_id' => $project->project_id))->fetchObject()) {
  118. $proj_boxes[$project->project_id] = $project->name;
  119. $added++;
  120. }
  121. }
  122. // if we have projects we need to add to the checkbox then
  123. // build that form element
  124. if ($added > 0) {
  125. $proj_boxes['all'] = "All Projects";
  126. $form['sync']['projects'] = array(
  127. '#title' => t('Available Projects'),
  128. '#type' => t('checkboxes'),
  129. '#description' => t("Check the projects you want to sync. Drupal content will be created for each of the projects listed above. Select 'All Projects' to sync all of them."),
  130. '#required' => FALSE,
  131. '#prefix' => '<div id="org_boxes">',
  132. '#suffix' => '</div>',
  133. '#options' => $proj_boxes,
  134. );
  135. $form['sync']['button'] = array(
  136. '#type' => 'submit',
  137. '#value' => t('Submit Sync Job')
  138. );
  139. }
  140. // we don't have any projects to select from
  141. else {
  142. $form['sync']['value'] = array(
  143. '#value' => t('All projects in Chado are currently synced with Drupal.')
  144. );
  145. }
  146. }
  147. // we don't want to present a form since we have an active job running
  148. else {
  149. $form['sync']['value'] = array(
  150. '#value' => t('Currently, jobs exist related to chado projects. Please check back later for projects that can by synced once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.')
  151. );
  152. }
  153. }
  154. /**
  155. *
  156. * @ingroup tripal_project
  157. */
  158. function tripal_project_admin_validate($form, &$form_state) {
  159. global $user; // we need access to the user info
  160. $job_args = array();
  161. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  162. // check to see if the user wants to sync chado and drupal. If
  163. // so then we need to register a job to do so with tripal
  164. $projects = $form_state['values']['projects'];
  165. $do_all = FALSE;
  166. $to_sync = array();
  167. foreach ($projects as $project_id) {
  168. if (preg_match("/^all$/i" , $project_id)) {
  169. $do_all = TRUE;
  170. }
  171. if ($project_id and preg_match("/^\d+$/i" , $project_id)) {
  172. // get the list of projects
  173. $sql = "SELECT * FROM {project} WHERE project_id = :project_id";
  174. $project = chado_query($sql, array(':project_id' => $project_id))->fetchObject();
  175. $to_sync[$project_id] = "$project->genus $project->species";
  176. }
  177. }
  178. // submit the job the tripal job manager
  179. if ($do_all) {
  180. tripal_add_job('Sync all projects' , 'tripal_project',
  181. 'tripal_project_sync_projects' , $job_args , $user->uid);
  182. }
  183. else{
  184. foreach ($to_sync as $project_id => $name) {
  185. $job_args[0] = $project_id;
  186. tripal_add_job("Sync project: $name" , 'tripal_project',
  187. 'tripal_project_sync_projects' , $job_args , $user->uid);
  188. }
  189. }
  190. }
  191. // -------------------------------------
  192. // Submit the Reindex Job if selected
  193. if ($form_state['values']['op'] == t('Reindex Features')) {
  194. $projects = $form_state['values']['re-projects'];
  195. foreach ($projects as $project_id) {
  196. if ($project_id and preg_match("/^\d+$/i" , $project_id)) {
  197. // get the project info
  198. $sql = "SELECT * FROM {project} WHERE project_id = :project_id";
  199. $project = chado_query($sql , array(':project_id' => $project_id))->fetchObject();
  200. $job_args[0] = $project_id;
  201. tripal_add_job("Reindex features for project: $project->genus ".
  202. "$project->species", 'tripal_project' ,
  203. 'tripal_project_reindex_features', $job_args, $user->uid);
  204. }
  205. }
  206. }
  207. // -------------------------------------
  208. // Submit the taxonomy Job if selected
  209. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  210. $projects = $form_state['values']['tx-projects'];
  211. foreach ($projects as $project_id) {
  212. if ($project_id and preg_match("/^\d+$/i", $project_id)) {
  213. // get the project info
  214. $sql = "SELECT * FROM {project} WHERE project_id = :project_id";
  215. $project = chado_query($sql , array(':project_id' => $project_id))->fetchObject();
  216. $job_args[0] = $project_id;
  217. tripal_add_job("Set taxonomy for features in project: ".
  218. "$project->genus $project->species" , 'tripal_project',
  219. 'tripal_project_taxonify_features', $job_args, $user->uid);
  220. }
  221. }
  222. }
  223. // -------------------------------------
  224. // Submit the Cleanup Job if selected
  225. if ($form_state['values']['op'] == t('Clean up orphaned projects')) {
  226. tripal_add_job('Cleanup orphaned projects', 'tripal_project',
  227. 'tripal_project_cleanup', $job_args, $user->uid);
  228. }
  229. }
  230. /**
  231. * Synchronize projects from chado to drupal
  232. *
  233. * @ingroup tripal_project
  234. */
  235. function tripal_project_sync_projects($project_id = NULL, $job_id = NULL) {
  236. global $user;
  237. $page_content = '';
  238. if (!$project_id) {
  239. $sql = "SELECT * FROM {project} P";
  240. $results = chado_query($sql);
  241. }
  242. else {
  243. $sql = "SELECT * FROM {project} P WHERE project_id = :project_id";
  244. $results = chado_query($sql, array(':project_id' => $project_id));
  245. }
  246. // We'll use the following SQL statement for checking if the project
  247. // already exists as a drupal node.
  248. $sql = "SELECT * FROM {chado_project} WHERE project_id = :project_id";
  249. while ($project = $results->fetchObject()) {
  250. // check if this project already exists in the drupal database. if it
  251. // does then skip this project and go to the next one.
  252. if (!db_query($sql, array(':project_id' => $project->project_id))->fetchObject()) {
  253. $new_node = new stdClass();
  254. $new_node->type = 'chado_project';
  255. $new_node->uid = $user->uid;
  256. $new_node->title = "$project->name";
  257. $new_node->project_id = $project->project_id;
  258. $new_node->name = $project->name;
  259. $new_node->description = $project->description;
  260. node_validate($new_node);
  261. if (!form_get_errors()) {
  262. $node = node_submit($new_node);
  263. node_save($node);
  264. if ($node->nid) {
  265. print "Added $project->name\n";
  266. }
  267. }
  268. else {
  269. print "Failed to insert project $project->name\n";
  270. }
  271. }
  272. else {
  273. print "Skipped $project->name\n";
  274. }
  275. }
  276. return $page_content;
  277. }
  278. /*
  279. *
  280. */
  281. function tripal_project_sync_projects_form_submit($form, &$form_state) {
  282. global $user;
  283. //sync'ing is done by a tripal_job that is added here
  284. $job_id = tripal_add_job('Sync Projects', 'tripal_project',
  285. 'tripal_project_sync_all_projects', array(), $user->uid);
  286. }
  287. /*
  288. *
  289. */
  290. function tripal_project_sync_all_projects() {
  291. //retrieve all projects in drupal
  292. $resource = db_query('SELECT project_id FROM {chado_project}');
  293. $drupal_projects = array();
  294. while ($r = $resource->fetchObject()) {
  295. $drupal_projects[$r->project_id] = $r->project_id;
  296. }
  297. // retrieve all projects in chado
  298. $chado_projects = array();
  299. $resource = chado_query('SELECT project_id FROM {project}');
  300. while ($r = $resource->fetchObject()) {
  301. // if not already in drupal add to list to be sync'd
  302. if (!isset($drupal_projects[$r->project_id])) {
  303. $chado_projects[$r->project_id] = $r->project_id;
  304. }
  305. }
  306. print 'Number of Projects to Sync: ' . sizeof($chado_projects) . "\n";
  307. foreach ($chado_projects as $project_id) {
  308. $project = tripal_core_chado_select('project', array('name', 'description'), array('project_id' => $project_id));
  309. // create node
  310. $new_node = new stdClass();
  311. $new_node->type = 'chado_project';
  312. $new_node->uid = $user->uid;
  313. $new_node->title = $project[0]->name;
  314. $new_node->project_id = $project_id;
  315. $new_node->description = $project[0]->description;
  316. node_validate($new_node);
  317. $errors = form_get_errors();
  318. if (!$errors) {
  319. $node = node_submit($new_node);
  320. node_save($node);
  321. if ($node->nid) {
  322. print "Added " . $project[0]->name . " (Node ID:" . $node->nid . ")\n";
  323. }
  324. }
  325. else {
  326. print "Failed to insert project: " . $project[0]->name . "\n";
  327. print "Errors: " . print_r($errors, TRUE) . "\n";
  328. }
  329. }
  330. }
  331. /**
  332. * Remove orphaned drupal nodes
  333. *
  334. * @param $dummy
  335. * Not Used -kept for backwards compatibility
  336. * @param $job_id
  337. * The id of the tripal job executing this function
  338. *
  339. * @ingroup tripal_project
  340. */
  341. function tripal_project_cleanup($dummy = NULL, $job_id = NULL) {
  342. return tripal_core_clean_orphaned_nodes('project', $job_id);
  343. }