Index: modules/invite_lists/rsvp_invite_lists.info
===================================================================
--- modules/invite_lists/rsvp_invite_lists.info	(revision 0)
+++ modules/invite_lists/rsvp_invite_lists.info	(revision 0)
@@ -0,0 +1,6 @@
+name = RSVP invite lists
+description = Optionally load invite lists from past events.
+package = RSVP
+dependencies[] = rsvp
+core = 6.x
+
Index: modules/invite_lists/rsvp_invite_lists.module
===================================================================
--- modules/invite_lists/rsvp_invite_lists.module	(revision 0)
+++ modules/invite_lists/rsvp_invite_lists.module	(revision 0)
@@ -0,0 +1,163 @@
+<?php
+
+/**
+ * @module rsvp_invite_lists
+ * 
+ * @package rsvp - A drupal module developed for civicspace - a distribution of drupal.
+ * @description Load invitation lists from prior events while creating an event
+ * @author mikestefff
+ */
+
+/**
+ * Implementation of hook_form_FORM_ID_alter().
+ */
+function rsvp_invite_lists_form_rsvp_add_guests_form_alter(&$form, &$form_state, $form_id) {
+  // Generate a list of past invitations
+  $invitations = _rsvp_invite_lists_get_allowed_invitations();
+  $options = array();
+  foreach ($invitations as $invitation) {
+    $options[$invitation->rid] = $invitation->title . " ({$invitation->name})";
+  }
+  
+  // See if an invitation list was chosen to load
+  if (isset($_GET['list'])) {
+    // Make sure it was an allowed value
+    $invite_list_rid = $_GET['list'];
+    if (isset($options[$invite_list_rid])) {
+      // Fetch the invitees for the selected invitation list
+      $invitees = _rsvp_invite_lists_get_invitees($invite_list_rid);
+      // Add the invitees to the guest form
+      if (!empty($invitees)) {
+        foreach ($invitees as $invitee) {
+          $form['recipients']['#default_value'] .= $invitee . "\n";
+        }
+      }
+    }
+    else {
+      $invite_list_rid = NULL;
+    }
+  }
+  
+  $form['rsvp']['rsvp_invite_lists'] = array(
+    '#weight' => 41,
+    '#value' => '<div class="rsvp_form rsvp_add_guests_form rsvp_color_border rsvp_color_inner">',
+    '#suffix' => '</div>',
+  );
+
+  $form['rsvp']['rsvp_invite_lists']['header'] = array(
+    '#value' => t('Load the invitation list of another event'),
+    '#prefix' => '<div class="rsvp_form_header rsvp_add_guests_form_header rsvp_color_outer">',
+    '#suffix' => '</div>',
+  );
+  
+  // Only show the invitation selection if there are any availables
+  if (!empty($options)) {
+    // Add a message if the selected list was empty
+    if (isset($invitees) && empty($invitees)) {
+      $form['rsvp']['rsvp_invite_lists']['empty_invites'] = array(
+        '#type' => 'item',
+        '#prefix' => '<div class="rsvp-invite-lists-empty-invites">',
+        '#suffix' => '</div>',
+        '#value' => t('The selected invitation list is currently empty. Please choose another to load.'),
+      );
+    }
+    
+    // Add the invitation lists
+    $form['rsvp']['rsvp_invite_lists']['invitation_list_rid'] = array(
+      '#type' => 'select',
+      '#options' => $options,
+      '#default_value' => isset($invite_list_rid) ? $invite_list_rid : NULL,
+    );
+  
+    // Add a button to load the invitation list
+    $form['rsvp']['rsvp_invite_lists']['load'] = array(
+      '#type' => 'button',
+      '#value' => t('Load'),
+      // Add this so the button can be submitted without
+      // invoking the entire forms validation
+      '#validate' => array('rsvp_invite_lists_test'),
+    );
+  }
+  else {
+    $form['rsvp']['rsvp_invite_lists']['empty_invitations'] = array(
+      '#type' => 'item',
+      '#value' => t('There are no past invitations available'),
+      '#prefix' => '<div class="rsvp-invite-lists-empty-invitations">',
+      '#suffix' => '</div>',
+    );
+  }
+}
+
+function rsvp_invite_lists_test(&$form, &$form_state) {
+  $invite_list_rid = $form_state['values']['invitation_list_rid'];
+  $rsvp_rid = $form_state['values']['rsvp']->rid;
+  drupal_goto("rsvp/{$rsvp_rid}/attendees", array('list' => $invite_list_rid));
+}
+
+/**
+ * Retrieve all invitations accessible by the current user
+ * 
+ * @param $user;
+ *   Optionally supply the user object, otherwise use the current user
+ * @return
+ *   An associative array of invitations, keyed by the RSVP id
+ */
+function _rsvp_invite_lists_get_allowed_invitations($user = NULL) {
+  // Load the current user, if needed
+  if (!$user) {
+    global $user;
+  }
+  
+  // Determine if the user has admin priviledges for RSVP
+  $is_admin = user_access(RSVP_PERM_ADMIN, $user);
+  
+  // Fetch all accessible invitations
+  $invitations = array();
+  $sql = "SELECT r.*, n.title FROM {rsvp} r";
+  $sql .= " LEFT JOIN {node} n ON n.nid = r.nid";
+  $sql .= " WHERE n.status = 1";
+  if (!$is_admin) {
+    // Unless use can admin RSVP, only show invites he/she created
+    // or is a moderator of
+    $sql .= " AND %d IN (r.uid, r.uid_moderator)";
+  }
+  $sql .= " ORDER BY n.title ASC";
+  $results = db_query($sql, !$is_admin ? $user->uid : NULL);
+  while ($invitation = db_fetch_object($results)) {
+    $invitations[$invitation->rid] = $invitation;
+  }
+  
+  return $invitations;
+}
+
+/**
+ * Get a list of invitees for a given invitation
+ *
+ * @param $rid
+ *   The RSVP invitation id
+ * @return
+ *   An associative array of invitees, keyed by the user id, with a 
+ *   value of the username or email, if uid is 0. If no user
+ *   id is available, a key of 0:{email} will be used.
+ */
+function _rsvp_invite_lists_get_invitees($rid) {
+  $invitees = array();
+  
+  // Fetch the invitees, making sure that users are still active
+  $sql = "SELECT DISTINCT ri.uid, ri.email, u.name FROM {rsvp_invite} ri";
+  $sql .= " INNER JOIN {users} u ON u.uid = ri.uid";
+  $sql .= " WHERE ri.rid = %d";
+  $sql .= " AND (u.status = 1 OR u.uid = 0)";
+  $sql .= " ORDER BY u.name ASC";
+  $results = db_query($sql, $rid);
+  while ($invitee = db_fetch_object($results)) {
+    if ($invitee->uid == 0) {
+      $invitees["0:{$invitee->email}"] = $invitee->email;
+    }
+    else {
+      $invitees[$invitee->uid] = $invitee->name;
+    }
+  }
+  
+  return $invitees;
+}
