Index: pifr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project_issue_file_review/pifr.module,v
retrieving revision 1.34
diff -u -r1.34 pifr.module
--- pifr.module	31 Oct 2008 07:20:09 -0000	1.34
+++ pifr.module	10 Nov 2008 01:43:41 -0000
@@ -43,6 +43,7 @@
 
 require_once drupal_get_path('module', 'pifr') . '/pifr.file.inc';
 require_once drupal_get_path('module', 'pifr') . '/pifr.server.inc';
+require_once drupal_get_path('module', 'pifr') . '/pifr.environment.inc';
 
 /**
  * Implementation of hook_menu().
@@ -133,6 +134,15 @@
       'type' => MENU_CALLBACK,
       'file' => 'pifr.pages.inc'
     );
+    
+    $items['pifr/environment/%/%'] = array(
+      'title' => 'Environment',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('pifr_environment_confirm_form', 2, 3),
+      'access arguments' => array('access content'),
+      'type' => MENU_CALLBACK,
+      'file' => 'pifr.environment.inc'
+    );
   }
 
   if ($role[PIFR_SERVER_SLAVE]) {
Index: pifr.environment.inc
===================================================================
RCS file: pifr.environment.inc
diff -N pifr.environment.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ pifr.environment.inc	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,115 @@
+<?php
+// $Id$
+/**
+ * @file
+ * Automatically review patches attached to issues.
+ *
+ * Copyright 2008 by Jimmy Berry ("boombatower", http://drupal.org/user/214218)
+ */
+
+/**
+ * Confirm that an environment should be created to review the patch.
+ *
+ * @param mixed $server_id Server ID or URL.
+ * @param mixed $file_id File ID or name.
+ * @return array Confirmation form.
+ */
+function pifr_environment_confirm_form(&$form_state, $server, $file) {
+  $server = pifr_server_check($server, PIFR_SERVER_PROJECT);
+  if (!$server) {
+    drupal_set_message(t('Invalid server'), 'error');
+    return '';
+  }
+  
+  $file = pifr_file_check($file, $server->server_id);
+  if ($file) {
+    $form = array();
+    $form['server_id'] = array(
+      '#type' => 'value',
+      '#value' => $server->server_id
+    );
+    $form['file_id'] = array(
+      '#type' => 'value',
+      '#value' => $file->file_id
+    );
+    
+    return confirm_form($form, t('Do you want to create an environment to review patch %name?', array('%name' => $file->filename)),
+                        'pifr/file/' . $server->server_id . '/' . $file->file_id, t('This may take a few seconds, please be patient.'));
+  }
+  else {
+    drupal_set_message(t('Invalid file.'), 'error');
+    return array();
+  }
+}
+
+/**
+ * Start batch process to create review environment.
+ */
+function pifr_environment_confirm_form_submit($form, &$form_state) {
+  $server = pifr_server_check($form_state['values']['server_id'], PIFR_SERVER_PROJECT);
+  if ($server) {
+    $file = pifr_file_check($form_state['values']['file_id'], $server->server_id);
+    if ($file) {
+      pifr_environment_batch($server, $file);
+    }
+  }
+  
+  if (!$server || !$file) {
+    drupal_set_message(t('Invalid parameters'), 'error');
+  }
+}
+
+/**
+ * Setup and start batch process.
+ *
+ * @param object $server Server object.
+ * @param object $file File object.
+ */
+function pifr_environment_batch($server, $file) {
+  $batch = array(
+    'operations' => array(
+      array('pifr_environment_batch_process', array($file, $server)),
+      array('pifr_review_fetch', array(pifr_file_get_url($file))),
+//      array('pifr_environment_batch_process', array($file, $server)),
+//      array('pifr_environment_batch_process', array($file, $server)),
+//      array('pifr_environment_batch_process', array($file, $server)),
+//      array('pifr_environment_batch_process', array($file, $server)),
+//      array('pifr_environment_batch_process', array($file, $server)),
+    ),
+    'finished' => 'pifr_environment_batch_finished',
+    'title' => t('Creating review environment'),
+    'init_message' => t('Fetching patch.'),
+//    'progress_message' => t('Processed @current out of @total.'),
+    'error_message' => t('Failed to create review environment.'),
+  );
+  batch_set($batch);
+}
+
+function pifr_environment_batch_process($server, $file, &$context) {
+//  if (!isset($context['sandbox']['progress'])) {
+//    $context['sandbox']['progress'] = 0;
+//  }
+  
+//  $context['sandbox']['progress']++;
+//  $context['results'][] = 'hax' . $context['sandbox']['progress'];
+//  $context['message'] = t('Now processing %step', array('%step' => $context['sandbox']['progress']));
+  sleep(2);
+  
+//  $context['finished'] = $context['sandbox']['progress'] / 2;
+}
+
+
+/**
+ * Batch finished, environment created.
+ */
+function pifr_environment_batch_finished($success, $results, $operations) {
+  if ($success) {
+    drupal_set_message('login');
+    drupal_set_message(theme('item_list', $results));
+  }
+  else {
+    drupal_set_message('fail');
+    $error_operation = reset($operations);
+    drupal_set_message('An error occurred while processing '. $error_operation[0] .' with arguments :'. print_r($error_operation[0], TRUE));
+  }
+}
