Index: invite.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/invite/Attic/invite.module,v
retrieving revision 1.10.2.31
diff -u -r1.10.2.31 invite.module
--- invite.module	10 Mar 2007 20:32:55 -0000	1.10.2.31
+++ invite.module	13 Mar 2007 00:07:33 -0000
@@ -202,8 +202,7 @@
     $items[] = array(
       'path' => 'invite',
       'title' => variable_get('invite_page_title', t('Invite your friends and colleagues')),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => '_invite_form',
+      'callback' => 'invite_overview',
       'access' => user_access('send invitations'),
       'type' => MENU_NORMAL_ITEM);
     $items[] = array(
@@ -336,6 +335,29 @@
 }
 
 /**
+ * Implementation of hook_block().
+ */
+function invite_block($op = 'list', $delta = 0, $edit = array()) {
+  if (user_access('send invitations')) {
+    if ($op == 'list') {
+      $blocks[0] = array('info' => t('Invite a friend'));
+      return $blocks;
+    }
+    else if ($op == 'view') {
+      switch($delta) {
+        case 0:
+          $block = array(
+            'subject' => t('Invite a friend'),
+            'content' => drupal_get_form('invite_form', 'block'),
+          );
+          break;
+      }
+      return $block;
+    }
+  }
+}
+
+/**
  * Implementation of hook_cron().
  */
 function invite_cron() {
@@ -345,19 +367,12 @@
 }
 
 /**
- * Displays the Invite form for users with appropriate permissions and
- * processes and validates the form, sending an email to the invitee
+ * Display an overview of sent invitations and present the invite form.
  */
-function _invite_form() {
+function invite_overview() {
   global $user;
-  
-  //this displays all invites for a user, and counts how many invites they have left to give
-  $form['invitations'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Your invitations'),
-  );
 
-  $inc = 0;
+  $items = array();
   $result = db_query("SELECT i.*, (u.uid IS NOT NULL) AS account_exists FROM {invite} i LEFT JOIN {users} u ON u.uid = i.mid WHERE i.uid = %d ORDER BY i.expiry DESC", $user->uid);
   while ($invite = db_fetch_object($result)){
     $delete_link = l(t('Delete invitation'), 'invite/delete/'. urlencode($invite->email));
@@ -372,67 +387,134 @@
       $action_cell = variable_get('invite_allow_join_delete', 0) ? $delete_link : '&nbsp;';
     }
     $items[] = array($email_cell, $status_cell, $action_cell);
-    $inc++;
   }
-  
+
+  return theme('invite_overview', $items);
+}
+
+/**
+ * Theme function for the invites page.
+ */
+function theme_invite_overview($items) {
+  // display table of invites
+  $output = '<div><fieldset><legend>'. t('Your invitations') .'</legend>';
   if (count($items) > 0) {
     $headers = array(t('Email'), t('Status'), '&nbsp;');
-    $table = theme('table', $headers, $items, array('id' => 'invite_table'));
+    $output .= theme('table', $headers, $items, array('id' => 'invite_table'));
   }
   else {
-    $table = t('You have not sent any invitations yet.');
+    $output .= t('You have not sent any invitations yet.');
   }
-  $form['invitations']['table'] = array(
-    '#type' => 'markup',
-    '#value' => $table,
-  );
-  
-  $form['invite_form'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Invite a friend'),
-  );      
+  $output .= '</fieldset></div>';
   
-  $maximum_invites = _invite_max_invites_by_role();
-  if ($maximum_invites > 0) {
-    $invites_left = $maximum_invites - $inc;
-    $form['invite_form']['remaining_invites'] = array(
-      '#type' => 'markup',
-      '#value' => t('You have @invites invites left.', array('@invites' => $invites_left)),
-    );
+  // add the invitation form
+  $output .= drupal_get_form('invite_form', 'page');
+
+  return $output;
+}
+
+/**
+ * Form generation.
+ */
+function invite_form($op) {
+  global $user;
+
+  $max_invites = _invite_max_invites_by_role();
+
+  if ($max_invites > 0) {
+    $invites_sent = db_result(db_query("SELECT COUNT(*) FROM {invite} WHERE uid = %d", $user->uid));
   }
-  
-  //the invitation form
-  if ($inc < $maximum_invites  || $maximum_invites == 0){
-    $form['invite_form']['email'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Email'),
-      '#default_value' => '',
-      '#size' => 20,
-      '#maxlength' => 64,
-      '#description' => t('Type the email of the person you would like to invite'),
-      '#required' => TRUE,
-    );
-    $form['invite_form']['message'] = array(
-      '#type' => 'textarea',
-      '#title' => t('Your message'),
-      '#default_value' => '',
-      '#required' => FALSE,
-      '#description' => t('This message will be added to the mail sent to the person you are inviting.'),
-    );        
-    $form['invite_form']['submit'] = array(
-      '#type' => 'submit',
-      '#value' => t('Submit'),
-    );
-  } else {
-    drupal_set_message(t('Maximum number (@max) of invitations reached.', array('@max' => $maximum_invites)));
+
+  switch ($op) {
+    case 'block':
+      if ($max_invites > 0 && $invites_sent >= $max_invites) {
+        // hide the block
+        $form['#access'] = FALSE;
+        break;
+      }
+
+      $form['#action'] = url('invite');
+
+      $form['invite'] = array(
+        '#type' => 'markup',
+        '#value' => t('Give @site-name to:', array('@site-name' => variable_get('site_name', t('Drupal')))),
+      );
+      if ($max_invites > 0) {
+        $invites_left = $max_invites - $invites_sent;
+        $description = format_plural($invites_left, '@count invite left', '@count invites left');
+      }
+      $form['email'] = array(
+        '#type' => 'textfield',
+        '#size' => 20,
+        '#maxlength' => 64,
+        '#description' => $description,
+        '#required' => TRUE,
+      );
+      $form['submit'] = array(
+        '#type' => 'submit',
+        '#value' => t('Send invite'),
+      );
+      $form['link'] = array(
+        '#type' => 'markup',
+        '#prefix' => '<div><small>',
+        '#value' => l('Manage invites', 'invite'),
+        '#suffix' => '</small></div>',
+      );
+      break;
+
+    case 'page':
+      $form['invite_form'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Invite a friend'),
+      );
+      $disabled = FALSE;
+      if ($max_invites > 0) {
+        if ($invites_sent >= $max_invites) {
+          $disabled = TRUE;
+          $markup = t('Maximum number (@max) of invitations reached.', array('@max' => $max_invites));
+        }
+        else {
+          $invites_left = $max_invites - $invites_sent;
+          $markup = format_plural($invites_left, 'You have one invite left.', 'You have @count invites left.');
+        }
+        $form['invite_form']['remaining_invites'] = array(
+          '#type' => 'markup',
+          '#value' => $markup,
+        );
+      }
+      $form['invite_form']['email'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Email'),
+        '#size' => 20,
+        '#maxlength' => 64,
+        '#description' => t('Type the email of the person you would like to invite.'),
+        '#required' => TRUE,
+        '#disabled' => $disabled,
+      );
+      $form['invite_form']['message'] = array(
+        '#type' => 'textarea',
+        '#title' => t('Your message'),
+        '#required' => FALSE,
+        '#description' => t('This message will be added to the mail sent to the person you are inviting.'),
+        '#disabled' => $disabled,
+      );        
+      $form['invite_form']['submit'] = array(
+        '#type' => 'submit',
+        '#value' => t('Submit'),
+        '#disabled' => $disabled,
+      );
+      break;
   }
 
   return $form;
 }
 
-function _invite_form_validate($form_id, &$edit) {
+/**
+ * Form validation.
+ */
+function invite_form_validate($form_id, &$edit, &$form) {
   global $user;
-  
+
   if (!valid_email_address(trim($edit['email']))) {
     form_set_error('email', t('The email address does not appear to be valid syntax.'));
   }
@@ -443,9 +525,12 @@
   }
 }
 
-function _invite_form_submit($form_id, $edit) {
+/**
+ * Form submission.
+ */
+function invite_form_submit($form_id, $edit) {
   global $user;
-  
+
   // generate code
   $code = _invite_create_regcode();
 
@@ -459,6 +544,9 @@
   }
 }
 
+/**
+ * Delete an invitation.
+ */
 function invite_delete($email) {
   global $user;
 
