? subscriptions_anonymous_by_making_user_202982.patch
Index: subscriptions_ui.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/subscriptions/Attic/subscriptions_ui.module,v
retrieving revision 1.1.2.3
diff -u -p -r1.1.2.3 subscriptions_ui.module
--- subscriptions_ui.module	21 Dec 2007 00:42:51 -0000	1.1.2.3
+++ subscriptions_ui.module	22 Dec 2007 18:20:12 -0000
@@ -146,3 +146,88 @@ function subscriptions_ui_node_form_subm
     }
   }
 }
+
+
+
+function subscriptions_comment($a1, $op) {
+  switch($op) {
+    case 'validate':
+      if ($a1['subscriptions_followup'] == TRUE) {
+        if (!strlen($a1['mail'])) {
+          form_set_error('mail', t('You must enter an email address if you wish to receive followups'));
+        }
+        $comment_uid = db_result(db_query("SELECT uid FROM {users} WHERE mail = '%s'", $a1['mail'])); // MAYBEDO use a LOWER() so this works properly on case sensitve databases
+        if ($comment_uid) {
+          form_set_error('mail', t('This email is already associated with an account on the site.  Please <a href="!login">login</a> before commenting</a>', array('!login' => url('user/login'))));
+        }
+      }
+      break;
+    case 'insert':
+      if ($a1['subscriptions_followup'] == TRUE) {
+        $values = array(
+          'name' => $a1['name'] ? $a1['name'] : $a1['mail'], // No need to validate the name for uniqueness, comment module already does.  If name is not present, just use mail
+          'init' => $a1['mail'],
+          'mail' => $a1['mail'],
+          'pass' => user_password(),
+        );
+
+        $account = user_save('', $values);
+        // This is copied from user.module because it cannot simply be re-used from there - ideally user.module would be refactored so we could re-use this
+        // TODO we probably shouldn't do any of these drupal_set_messages for new users or at least make it optional
+        // TODO decide whether or not we want to send these emails either
+        if (!variable_get('user_email_verification', TRUE) && $account->status && !$admin) {
+          // No e-mail verification is required, create new user account, and login user immediately.
+          $subject = _user_mail_text('welcome_subject', $variables);
+          $body = _user_mail_text('welcome_body', $variables);
+          drupal_mail('user-register-welcome', $mail, $subject, $body, $from);
+          user_authenticate($account->name, trim($pass));
+          $edit = array();
+          user_module_invoke('login', $edit, $account);
+        }
+        else if ($account->status) {
+          // Create new user account, no administrator approval required.
+          $subject = _mail_text('welcome_subject', $variables);
+          $body = _mail_text('welcome_body', $variables);
+
+          drupal_mail('user-register-welcome', $mail, $subject, $body, $from);
+        
+          drupal_set_message(t('Your password and further instructions have been sent to your e-mail address.'));
+        }
+        else {
+          // Create new user account, administrator approval required.
+          $subject = _user_mail_text('approval_subject', $variables);
+          $body = _user_mail_text('approval_body', $variables);
+          
+          drupal_mail('user-register-approval-user', $mail, $subject, $body, $from);
+          drupal_mail('user-register-approval-admin', $from, $subject, t("!username has applied for an account.\n\n!edit_uri", $variables), $from);
+          drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, your password and further instructions have been sent to your e-mail address.'));
+        }
+        // Subscribe the new user to comments on this content type
+        $recipient_uid = $account->uid;
+        subscriptions_write_subscription('node', 'nid', $a1['nid'], -1, $account->uid, $account->uid, 1, 0, 1);
+        // TODO provide a way to unsubscribe
+        
+        // TODO associate the comment to the user (it takes more than what I've done here)
+        $a1['uid'] = $account->uid;
+        
+        break;
+      }
+    default:
+      break;
+  }
+}
+
+function subscriptions_form_alter($form_id, &$form) {
+  global $user;
+  // Only do this simple system for anonymous if anonymous is allowed to subscribe to content. Registered users get the fancy form.
+  if ($form_id == 'comment_form' && user_access('subscribe to content') && !$user->uid) {
+    // Only do this if there is a box for them to enter their email
+    if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAY_CONTACT || variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
+      $form['subscriptions_followup'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Notify me of follow-up comments posted here.'),
+        '#default_value' => FALSE,
+      );
+    }
+  }
+}
\ No newline at end of file
