Index: project_issue.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project_issue/project_issue.module,v
retrieving revision 1.78
diff -u -F^f -u -F^f -r1.78 project_issue.module
--- project_issue.module	14 Jan 2008 20:01:25 -0000	1.78
+++ project_issue.module	15 Jan 2008 15:29:02 -0000
@@ -182,6 +182,17 @@ function project_issue_settings_form() {
     '#default_value' => variable_get('project_issue_reply_to', variable_get('site_mail', ini_get('sendmail_from'))),
     '#description' => t('All issue e-mails sent via subscriptions will appear from this e-mail address. You can use %project as a placeholder which will be replaced with the %short_project_name setting for the issue\'s current project.', array('%project' => '%project', '%short_project_name' => t('Short project name'))),
   );
+
+  $form['project_issue_auto_close_user'] = array(
+    '#title' => t('Auto-close user'),
+    '#type' => 'textfield',
+    '#default_value' => variable_get('project_issue_auto_close_user', ''),
+    '#maxlength' => 60,
+    '#description' => t('Enter the user which will auto-close fixed issues -- leave empty to use the anonymous user or set to <em>&lt;none&gt;</em> to disable auto-closing.'),
+    '#validate' => array('project_issue_validate_auto_close_user' => array()),
+    '#autocomplete_path' => 'user/autocomplete',
+  );
+
   if (module_exists('mailhandler')) {
     // TODO: move this stuff to mailhandler.module ?
     $items = array(t('<none>'));
@@ -207,9 +218,28 @@ function project_issue_validate_issues_p
   }
 }
 
-function project_issue_cron() {
-  global $user;
+/**
+ * Validates that the auto-close user exists, and has sufficient permissions
+ * to auto-close issues.
+ */
+function project_issue_validate_auto_close_user($form) {
+  $name = $form['#value'];
+  if ($name != '<none>') {
+    // Load the user, or the anonymous user if none is provided.
+    // Don't see a constant for uid 0...  :(
+    $account = $name ? user_load(array('name' => $name)) : user_load(array('uid' => 0));
+    if ($account) {
+      if (!user_access('access project issues', $account)) {
+        form_error($form, t('%name does not have sufficient permissions to auto-close issues.', array('%name' => $name ? $name : t('The anonymous user'))));
+      }
+    }
+    else {
+      form_error($form, t('%name is not a valid user.', array('%name' => $name)));
+    }
+  }
+}
 
+function project_issue_cron() {
   if (time() - variable_get('project_issue_digest_last', 0) > variable_get('project_issue_digest_interval', 7 * 24 * 60 * 60)) {
     variable_set('project_issue_digest_last', time());
     project_mail_digest();
@@ -220,7 +250,34 @@ function project_issue_cron() {
     project_mail_reminder();
   }
 
-  $result = db_query('SELECT p.nid, p.pid, p.category, p.component, p.priority, p.rid, p.assigned, p.sid, n.title FROM {project_issues} p INNER JOIN {node} n ON n.nid = p.nid WHERE n.status = 1 AND p.sid = 2 AND n.changed < %d', time() - 14 * 24 * 60 * 60);
+  // Auto-close fixed issues;
+  project_issue_auto_close();
+}
+
+/**
+ * Automatically closes issues marked as fixed after two weeks.
+ */
+function project_issue_auto_close() {
+  global $user;
+
+  // If a custom user exists for auto-closing comments, load them into
+  // the global user object temporarily. We use session_save_session()
+  // to provide safe user impersonation.
+  if ($name = variable_get('project_issue_auto_close_user', '')) {
+    // Skip auto-closing.
+    if ($name == '<none>') {
+      return;
+    }
+    $original_user = $user;
+    session_save_session(FALSE);
+    // Safety check -- we have to have a valid user here.
+    if(!($user = user_load(array('name' => $name)))) {
+      watchdog('project_issue', t('Auto-close user failed to load.'), WATCHDOG_ERROR);
+      return;
+    }
+  }
+
+    $result = db_query('SELECT p.nid, p.pid, p.category, p.component, p.priority, p.rid, p.assigned, p.sid, n.title FROM {project_issues} p INNER JOIN {node} n ON n.nid = p.nid WHERE n.status = 1 AND p.sid = 2 AND n.changed < %d', time() - 14 * 24 * 60 * 60);
 
   // Set up the persistent {comments} data.
   $comment['pid'] = 0;
@@ -276,6 +333,12 @@ function project_issue_cron() {
     comment_invoke_comment($comment, 'insert');
   }
 
+  // Load the original user back in.
+  if ($name) {
+    $user = $original_user;
+    session_save_session(TRUE);
+  }
+
   if ($clear_cache) {
     // Clear cache so anonymous users can see the new post.
     cache_clear_all();
