Hello all,

Is there a simple/clean way of including the form for SimpleNews subscriptions on the user registration page? Though users can add that information after registering, it would be useful to allow them to elect newsletter upon registration.

I'm comfortable hacking up the code if necessary, but I've generally found there's usually an elegant way to do it instead.

Thanks

Comments

venkat-rk’s picture

I think you can use the info on this page to display the subscription form on the registration page:
http://drupal.org/node/38630

ideaoforder’s picture

Thanks...but that's the first place I looked. That's for embedding the script on a separate page. I have a client who wants this as part of the user registration--so it's something a user has to fill out, not something optional that they do later.

I guess I could make it on a separate page and force users onto that page from the registration page, but I was hoping for something a little more elegant.

Any other takers?

ideaoforder’s picture

For what it's worth...I ended up doing a redirect from the user_register function using drupal_goto and created a few new functions in the newletter module, which in turn bring the user to a welcom page. So the registration becomes a two step process.

If anyone would like the gory details, I'd be happy to supply them.

Rock.

mikegb’s picture

Is there any non-coding way of doing this?

Yours, a very non-techy Drupal user
Mike

ideaoforder’s picture

Hey Mike,

That's what I was hoping for too, but never came up with anything. I'd be more than happy to send you my hacked versions of user.module and simplenews.module. You should be able to just paste them into the "modules" directory over the old modules and go from there...actually, you also have to make a static page or two--I can give you the names and the code that should get pasted.

Though it sounds a little sloppy, the execution is pretty elegant. The org I did it for was perfectly happy.

Cheers,
Mark

mikegb’s picture

Thanks!

Mike

mbites.com

mariagwyn’s picture

May I have a copy of these as well?

thanks,
Maria

ngstigator’s picture

The following code goes into simplenews_user(). The "register" case is new, and the "insert" case replaces the existing one.

<?php
case 'register':
  $tree = taxonomy_get_tree(simplenews_get_vid());
  if ($tree) {

    $checkboxes = array();
    foreach( $tree as $newsletter ) {
      $checkboxes[$newsletter->tid]=$newsletter->name;
    }
    $default = variable_get('simplenews_default_subscription',array());

    $form['simplenews'] = array('#type' => 'fieldset', '#title' => 'Subscribe to Newsletters');
    $form['simplenews']['#weight'] = 1;
    $form['simplenews']['newsletters'] = array(
        '#type' => 'checkboxes',
        '#title' => $newsletter->name,
        '#options' => $checkboxes,
        '#default_value' => $default,
        '#attributes' => $checked ? array('checked' => 'checked') : NULL,
    );
  }
  return $form;
break;
case 'insert':
  if ($edit['mail']) {
    $mail = $edit['mail'];
    if (is_array($edit['newsletters'])) {
      foreach ($edit['newsletters'] as $tid) {
        if ($tid > 0) {
          db_query("INSERT INTO {sn_subscriptions} (mail, uid, a_status) VALUES ('%s', %d, %d)", $mail, $account->uid, 1);
          $snid = db_result(db_query("SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'", $mail));
          db_query("INSERT INTO {sn_snid_tid} (snid, tid) VALUES (%d, %d)", $snid, $tid);
        }
      }
    }
    $query = "SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'";
    if ($result = db_fetch_object(db_query($query, $mail))) {
      db_query("UPDATE {sn_subscriptions} SET uid = %d WHERE snid = %d", $edit['uid'], $result->snid);
    }
  }
break;
?>

There is provision for a default newsletter. To use this feature set a value named "simplenews_default_subscription" into the variables table, or run the following code:

variable_set('simplenews_default_subscription', array(123));

...where 123 is the tid (refer to sn_snid_tid db table) of the newsletter you wish to set as default.

Cheers.

desm0n’s picture

Little confused on this. Where about does this actually go and what do i replace ?
--
http://www.porttalbotchat.co.uk
-------------------------------------------------
Our mission is to discuss issues and topics of residents of Port Talbot. We provide info on events, issues, concerns and discussions of our local area.

ngstigator’s picture

It goes in simplenews.module, in the function simplenews_user (~line 410). The cases belong in the switch ($op) conditional. Currently there is no case for "register" so just paste it in, but the case for "insert" provided in the code above replaces the existing case.

Oh heck here is the entire function...

function simplenews_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
		case 'register':
			$tree = taxonomy_get_tree(simplenews_get_vid());
			if ($tree) {

				$checkboxes = array();
				foreach( $tree as $newsletter ) {
					$checkboxes[$newsletter->tid]=$newsletter->name;
				}
  			$default = variable_get('simplenews_default_subscription',array());

				$form['simplenews'] = array('#type' => 'fieldset', '#title' => 'Subscribe to Newsletters');
				$form['simplenews']['#weight'] = 1;
				$form['simplenews']['newsletters'] = array(
						'#type' => 'checkboxes',
						'#title' => $newsletter->name,
						'#options' => $checkboxes,
						'#default_value' => $default,
						'#attributes' => $checked ? array('checked' => 'checked') : NULL,
				);
			}
			return $form;
		break;
		case 'insert':
      if ($edit['mail']) {
				$mail = $edit['mail'];
				if (is_array($edit['newsletters'])) {
					foreach ($edit['newsletters'] as $tid) {
						if ($tid > 0) {
							db_query("INSERT INTO {sn_subscriptions} (mail, uid, a_status) VALUES ('%s', %d, %d)", $mail, $account->uid, 1);
							$snid = db_result(db_query("SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'", $mail));
							db_query("INSERT INTO {sn_snid_tid} (snid, tid) VALUES (%d, %d)", $snid, $tid);
						}
					}
				}
        $query = "SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'";
				if ($result = db_fetch_object(db_query($query, $mail))) {
					db_query("UPDATE {sn_subscriptions} SET uid = %d WHERE snid = %d", $edit['uid'], $result->snid);
				}
      }
    break;
    case 'update':
      if ($category == 'account' && $edit['mail']) {
        $query = "SELECT snid FROM {sn_subscriptions} WHERE uid = %d";
        if ($result = db_fetch_object(db_query($query, $account->uid))) {
          db_query("DELETE FROM {sn_subscriptions} WHERE mail = '%s' AND uid = %d", $edit['mail'], 0);
          db_query("UPDATE {sn_subscriptions} SET mail = '%s' WHERE snid = %d", $edit['mail'], $result->snid);
        }
        else {
          $query = "SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'";
          if ($result = db_fetch_object(db_query($query, $edit['mail']))) {
            db_query("UPDATE {sn_subscriptions} SET uid = %d WHERE snid = %d", $account->uid, $result->snid);
          }
        }
      }
      elseif ($category == 'newsletter' && user_access('subscribe to newsletters')) {
        _simplenews_subscription_manager(FALSE, FALSE, TRUE);
      }
    break;
    case 'delete':
      $query = "SELECT snid FROM {sn_subscriptions} WHERE uid = %d";
      if ($result = db_fetch_object(db_query($query, $account->uid))) {
        db_query("UPDATE {sn_subscriptions} SET uid = %d WHERE snid = %d", 0, $result->snid);
      }
    break;
    case 'form':
      if ($category == 'newsletter' && user_access('subscribe to newsletters')) {
        $form['newsletters'] = _simplenews_subscription_manager($account);
        $form['newsletters']['#title'] =  t('Current newsletter subscriptions');
        $form['newsletters']['sn_email'] = array(
          '#type' => 'hidden',
          '#value' => $account->mail,
        );
        $form['newsletters']['sn_update'] = array(
          '#type' => 'hidden',
          '#value' => 'Update',
        );
        return $form;
      }
    break;
    case 'categories':
      if (user_access('subscribe to newsletters')) {
        $output[] = array('name' => 'newsletter', 'title' => t('my newsletters'), 'weight' => 10);
      }
      return $output;
    break;
    case 'view':
      global $user;
      if ($user->uid == $account->uid || user_access('administer users')) {
        $tree = taxonomy_get_tree(simplenews_get_vid());
        foreach ($tree as $newsletter) {
          if (db_num_rows(db_query('SELECT s.uid FROM {sn_subscriptions} s INNER JOIN {sn_snid_tid} t ON s.snid = t.snid WHERE s.uid = %d AND t.tid = %d', $account->uid, $newsletter->tid))) {
            $subscriptions[] = l($newsletter->name, 'taxonomy/term/'. $newsletter->tid);
          }
        }
        if ($subscriptions) {
          $subscriptions = implode(', ', $subscriptions);
        }
        else {
          $subscriptions = t('Currently no subscriptions');
        }
        $items[] = array(
          'class' => 'item',
          'title' => t('Current subcriptions'),
          'value' => $subscriptions,
        );
        $items[] = array(
          'class' => 'item',
          'title' => t('Manage subscriptions'),
          'value' => l(t('my newsletters'), 'user/'. $account->uid .'/edit/newsletter'),
        );
        return array(t('Newsletters') => $items);
      }
    break;
  }
}
IGadmin’s picture

When I use the patch, above, I get the Simplenews subscription form all right at registration time, but an extraneous line of text is added just below the title of the box and just before the list of newsletter checkboxes. That text is, in fact, one of the newsletter's names, in bold, with a colon at the end of it. I'd add a screenshot, but I don't appear to have privs. Any idea why this is? And/or a fix?

Also... how would one enhance this newsletter form in order to display the newsletters' description fields below each name? Simplenews offers a description field in addition to the name, but doesn't appear to render this description text anywhere. I'd REALLY like to be able to show description text for my newsletters.

Am yet another non-PHP weenie that would really benefit from others' assistance(!) Thanks.

ngstigator’s picture

You'll need to replace the "register" and "insert" cases in the simplenews_user function (line 410).

		case 'register':
			$tree = taxonomy_get_tree(simplenews_get_vid());
			if ($tree) {

				$default = variable_get('simplenews_default_subscription',array());

				$form['simplenews'] = array('#type' => 'fieldset', '#title' => 'Subscribe to Newsletters');
				$form['simplenews']['#weight'] = 1;

				$form['simplenews']['newsletters'] = array('#tree' => true);
				foreach( $tree as $newsletter ) {
					$checkboxes[$newsletter->tid]=$newsletter->name;
					$form['simplenews']['newsletters'][$newsletter->tid] = array(
						'#type' => 'checkbox',
						'#return_value' => $newsletter->tid,
						'#default_value' => in_array($newsletter->tid, $default),
						'#title' => $newsletter->name,
						'#description' => $newsletter->description,
						);
				}
			}
			return $form;
		break;
		case 'insert':
      if ($edit['mail']) {
				$mail = $edit['mail'];
				if (is_array($edit['newsletters'])) {
					foreach ($edit['newsletters'] as $tid) {
						if ($tid > 0) {
							db_query("INSERT INTO {sn_subscriptions} (mail, uid, a_status) VALUES ('%s', %d, %d)", $mail, $account->uid, 1);
							$snid = db_result(db_query("SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'", $mail));
							db_query("INSERT INTO {sn_snid_tid} (snid, tid) VALUES (%d, %d)", $snid, $tid);
						}
					}
				}
        $query = "SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'";
				if ($result = db_fetch_object(db_query($query, $mail))) {
					db_query("UPDATE {sn_subscriptions} SET uid = %d WHERE snid = %d", $edit['uid'], $result->snid);
				}
      }
			unset($edit['newsletters']);
    break;

IGadmin’s picture

Implemented the above and it works as advertised. Thanks, chris_five.

IGadmin’s picture

Not sure if this is related to the code above, but I just installed the LoginToboggan module, and am now getting duplicate subscribers in simplenews.

Previously, simplenews did a good job of checking email addresses at registration time to ensure duplicates weren't made in its subscription database. If an email address of an unregistered user had previously been imported, and then that user with that email address subsequently registered themselves an account in Drupal, only a single email address would still exist in the subscription database -- the username shown in the subscriber list would previously have been "unregistered user", but would become the actual username upon registration.

Now that logintoboggan is installed, I'm getting a separate subscription created for EVERY newsletter the user subscribes to. So if they subscribe to two newsletters at registration time, I actually end up with THREE entries in the simplenews subscribers database: a separate entry for each newsletter requested, plus the original unregistered user previously imported. To badly paraphrase Highlander, "there should be only one!" :)

Help!

ngstigator’s picture

...except that I'm not getting an error. Likely just some bad checking.

I'm looking into it.

IGadmin’s picture

So, in my efforts to get simplenews working, I've temporarily disabled LoginToboggan.

And lo and behold, simplenews subscriptions now work properly.

But ... I also now get an error reported after clicking the register button if the registrant's email already exists in the simplenews database because it had previously been imported.

Duplicate entry '35-27' for key 1 query: INSERT INTO sn_snid_tid (snid, tid)
VALUES (35, 27) in
/home/content/c/t/c/ctchronicle/html/includes/database.mysql.inc on line
120.

Ran across a thread (here http://drupal.org/node/65665) on Drupal.org that
suggests I should delete the sn_snid_tid table and recreate snid and tid as
auto-incrementing. Advisable?

Let me repeat that things DO indeed now technically work as they should. It's just the reporting of this error that's unfortunate.

FYI, in trying to get to the bottom of this I've recently exchanged email with Dries Knapen, the developer of simplenews, and he believes my issues may be related to the "case insert" code in chris_five's patch, which he believes is wrong for several unspecified reasons.

ngstigator’s picture

...is that it does not use the "simplenews_process_subscription" function to insert. Using it fixes the duplicate entry into the "sn_subscriptions" table, but inserts a tid of "0" in table "sn_snid_tid" no matter how many newsletters are checked. I should be able to figure it out soon.

BTW, I do NOT recommend auto-incrementing snid and tid in the "sn_snid_tid" table. That would make absolutely no sense.

Too bad the developer didn't suggest a solution.

ngstigator’s picture

... b/c it checks for $user->uid instead of $uid. It looks as if "we" have found a solution ;-)

2 functions enclosed:

function simplenews_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
		case 'register':
			$tree = taxonomy_get_tree(simplenews_get_vid());
			if ($tree) {

				$checkboxes = array();
				foreach( $tree as $newsletter ) {
					$checkboxes[$newsletter->tid]=$newsletter->name;
				}
  			$default = variable_get('simplenews_default_subscription',array());

				$form['simplenews'] = array('#type' => 'fieldset', '#title' => 'Subscribe to Newsletters');
				$form['simplenews']['#weight'] = 1;
				$form['simplenews']['newsletters'] = array(
						'#type' => 'checkboxes',
						'#title' => $newsletter->name,
						'#options' => $checkboxes,
						'#default_value' => $default,
						'#attributes' => $checked ? array('checked' => 'checked') : NULL,
				);
			}
			return $form;
		break;
		case 'insert':
      if ($edit['mail']) {
				$mail = $edit['mail'];
				if (is_array($edit['newsletters']) && array_values($edit['newsletters'])) {
					foreach ($edit['newsletters'] as $tid) {
						if ($tid > 0) { 
              simplenews_process_subscription($tid, $mail, 'Subscribe');
						}
					}
				}
        $query = "SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'";
				if ($result = db_fetch_object(db_query($query, $mail))) {
					db_query("UPDATE {sn_subscriptions} SET uid = %d WHERE snid = %d", $edit['uid'], $result->snid);
				}
      }
    break;
    case 'update':
      if ($category == 'account' && $edit['mail']) {
        $query = "SELECT snid FROM {sn_subscriptions} WHERE uid = %d";
        if ($result = db_fetch_object(db_query($query, $account->uid))) {
          db_query("DELETE FROM {sn_subscriptions} WHERE mail = '%s' AND uid = %d", $edit['mail'], 0);
          db_query("UPDATE {sn_subscriptions} SET mail = '%s' WHERE snid = %d", $edit['mail'], $result->snid);
        }
        else {
          $query = "SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'";
          if ($result = db_fetch_object(db_query($query, $edit['mail']))) {
            db_query("UPDATE {sn_subscriptions} SET uid = %d WHERE snid = %d", $account->uid, $result->snid);
          }
        }
      }
      elseif ($category == 'newsletter' && user_access('subscribe to newsletters')) {
        _simplenews_subscription_manager(FALSE, FALSE, TRUE);
      }
    break;
    case 'delete':
      $query = "SELECT snid FROM {sn_subscriptions} WHERE uid = %d";
      if ($result = db_fetch_object(db_query($query, $account->uid))) {
        db_query("UPDATE {sn_subscriptions} SET uid = %d WHERE snid = %d", 0, $result->snid);
      }
    break;
    case 'form':
      if ($category == 'newsletter' && user_access('subscribe to newsletters')) {
        $form['newsletters'] = _simplenews_subscription_manager($account);
        $form['newsletters']['#title'] =  t('Current newsletter subscriptions');
        $form['newsletters']['sn_email'] = array(
          '#type' => 'hidden',
          '#value' => $account->mail,
        );
        $form['newsletters']['sn_update'] = array(
          '#type' => 'hidden',
          '#value' => 'Update',
        );
        return $form;
      }
    break;
    case 'categories':
      if (user_access('subscribe to newsletters')) {
        $output[] = array('name' => 'newsletter', 'title' => t('my newsletters'), 'weight' => 10);
      }
      return $output;
    break;
    case 'view':
      global $user;
      if ($user->uid == $account->uid || user_access('administer users')) {
        $tree = taxonomy_get_tree(simplenews_get_vid());
        foreach ($tree as $newsletter) {
          if (db_num_rows(db_query('SELECT s.uid FROM {sn_subscriptions} s INNER JOIN {sn_snid_tid} t ON s.snid = t.snid WHERE s.uid = %d AND t.tid = %d', $account->uid, $newsletter->tid))) {
            $subscriptions[] = l($newsletter->name, 'taxonomy/term/'. $newsletter->tid);
          }
        }
        if ($subscriptions) {
          $subscriptions = implode(', ', $subscriptions);
        }
        else {
          $subscriptions = t('Currently no subscriptions');
        }
        $items[] = array(
          'class' => 'item',
          'title' => t('Current subcriptions'),
          'value' => $subscriptions,
        );
        $items[] = array(
          'class' => 'item',
          'title' => t('Manage subscriptions'),
          'value' => l(t('my newsletters'), 'user/'. $account->uid .'/edit/newsletter'),
        );
        return array(t('Newsletters') => $items);
      }
    break;
  }
}
function simplenews_process_subscription($tid, $mail, $sub) {
  global $user;
  //valid_email_address() allows empty address, so check this first
  if ($mail == '') {
    simplenews_handle_messages(t('You have to supply an e-mail address'), t('You have to supply an e-mail address'), 'error');
  }
  elseif (!valid_email_address($mail)) {
    simplenews_handle_messages(t('The e-mail address you supplied is not valid'), t('The e-mail address you supplied is not valid'), 'error');
  }
  else {
    global $base_url;
    $snid_result = db_query("SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'", $mail); 
    $snid_obj = db_fetch_object($snid_result); 
    $name_default = variable_get('site_name', 'drupal');
    $newsletter = taxonomy_get_term($tid);
    if ($sub == 'Subscribe') {
      if (!db_num_rows($snid_result)) { 
        $query = "SELECT uid FROM {users} WHERE mail = '%s'";
        if ($result = db_fetch_object(db_query($query, $mail))) {
          $uid = $result->uid;
        }
        else {
          $uid = 0;
        }
        db_query("INSERT INTO {sn_subscriptions} (mail, uid, a_status) VALUES ('%s', %d, %d)", $mail, $uid, 1);
        $snid_result = db_query("SELECT snid FROM {sn_subscriptions} WHERE mail = '%s'", $mail);
        $snid_obj = db_fetch_object($snid_result);
        $watchdog = t('User %email added to the database.', array('%email'=>theme('placeholder',$mail)));
        watchdog('newsletter', $watchdog);
      }
      else {
        $query = "SELECT uid FROM {users} WHERE mail = '%s'";
        if ($result = db_fetch_object(db_query($query, $mail))) {
          $uid = $result->uid;
        }
        else {
          $uid = 0;
        }
      }
      if ($uid == 0) { // changed to $uid from $user->uid
        //send confirmation email: "Confirm subscription"
        $body = t('We have received a request for subscription of your e-mail address, %mail, to the "%newsletter" newsletter from %site (%uri). To confirm that you want to be added to this mailing list, simply visit the confirmation link at the bottom of this e-mail.', array('%mail'=>$mail, '%newsletter'=>$newsletter->name, '%site'=>variable_get('simplenews_from_name', $name_default), '%uri'=>$base_url));
        $body .= "\n\n".t('If you do not wish to be subscribed to this list, please disregard this message.');
        sn_mail_confirm($mail, $body, $newsletter->name, $snid_obj->snid, $tid, 'subscribe');
        simplenews_handle_messages(t('You will receive a confirmation e-mail shortly.'), t('You will receive confirmation e-mails shortly.'));
      }
      else {   
        $snid_tid = db_query("SELECT snid FROM {sn_snid_tid} WHERE snid = %d AND tid = %d", $snid_obj->snid, $tid);
        if (!db_num_rows($snid_tid)) {
          db_query("INSERT INTO {sn_snid_tid} (snid, tid) VALUES (%d, %d)", $snid_obj->snid, $tid);
        }
        simplenews_handle_messages(t('The newsletter subscriptions have been updated.'), t('The newsletter subscriptions have been updated.'));
      }
    }
    elseif ($sub == 'Unsubscribe') {
      if ($user->uid == 0) {
        $snid_tid = db_query("SELECT snid FROM {sn_snid_tid} WHERE snid = %d AND tid = %d", $snid_obj->snid, $tid);
        if (db_num_rows($snid_result) && db_num_rows($snid_tid)) {
          //send confirmation email: "Confirm deletion"
          $body = t('We have received a request for the removal of your e-mail address, %mail, from the "%newsletter" newsletter from %site (%uri). If you want to unsubscribe, simply visit the confirmation link at the bottom of this e-mail.', array('%mail'=>$mail, '%newsletter'=>$newsletter->name, '%site'=>variable_get('simplenews_from_name', $name_default), '%uri'=>$base_url));
          $body .= "\n\n".t('If you do not wish to be removed from this list, please disregard this message.');
          sn_mail_confirm($mail, $body, $newsletter->name, $snid_obj->snid, $tid, 'unsubscribe');
        }
        else {
          //send confirmation email: "You were not subscribed. Visit site to subscribe."
          $body = t('We have received a request for the removal of your e-mail address, %mail, from the "%newsletter" newsletter from %site (%uri). However, you were not subscribed to this newsletter. If you want to subscribe, you can visit our website by using the link at the bottom of this e-mail.', array('%mail'=>$mail, '%newsletter'=>$newsletter->name, '%site'=>variable_get('simplenews_from_name', $name_default), '%uri'=>$base_url));
          $body .= "\n\n".t('If you do not wish to be subscribed to this list, please disregard this message.');
          sn_mail_confirm($mail, $body, $newsletter->name);
        }
        simplenews_handle_messages(t('You will receive a confirmation e-mail shortly.'), t('You will receive confirmation e-mails shortly.'));
      }
      else {
        if (db_num_rows($snid_result)) {
          $query = "DELETE FROM {sn_snid_tid} WHERE snid = %d AND tid = %d";
          if (db_affected_rows(db_query($query, $snid_obj->snid, $tid))) {
            simplenews_handle_messages(t('Your newsletter subscriptions have been updated.'), t('Your newsletter subscriptions have been updated.'));
          }
          //Perform db cleanup tasks
          if (!db_num_rows(db_query('SELECT tid FROM {sn_snid_tid} WHERE snid = %d', $snid_obj->snid))) {
            db_query('DELETE FROM {sn_subscriptions} WHERE snid = %d', $snid_obj->snid);
            $watchdog = t('User %email deleted from the database.', array('%email'=>theme('placeholder', $mail)));
            watchdog('newsletter', $watchdog);
          }
        }
      }
    }
  }
}
ShErbO’s picture

I did exactly what you said. Works flawlessly. It might do with the newsletter description, but it's not really important. Might think of a fix for it.

Thank you chris_five for your help :)

http://www.2knowmyself.com - The Ultimate Source for Self-Understanding and Personal Development.

IGadmin’s picture

...that we're on the right track. I'll stay watching this thread eagerly for a suggested fix.

IGadmin’s picture

Thanks for this. I was able to test your work only tonight.

The patch above fixed my bugs (hooray!), but unfortunately also TOOK AWAY the feature you'd incorporated earlier that showed the newsletter descriptions under the newsletter names at registration time. As patched now, only the newsletter names show beside the checkboxes.

Possible to resurrect the newsletter descriptions?

Thanks a million. This works great now.

IGadmin’s picture

In addition to my comment above, as previously reported October 3rd, above, an extraneous line of text is being added just below the title of the box and just before the list of newsletter checkboxes. That text is, in fact, one of the newsletter's names, in bold, with a colon at the end of it. Am anxious to get rid of it, in addition to getting the descriptions of the newsletters back.

ngstigator’s picture

In function simplenews_user, replace the "register" case with the one below:

		case 'register':
			$tree = taxonomy_get_tree(simplenews_get_vid());
			if ($tree) {
  			$default = variable_get('simplenews_default_subscription',array());
				$form['simplenews'] = array('#type' => 'fieldset', 
          '#title' => 'Subscribe to Newsletters', 
          '#weight' => 1);
        $form['simplenews']['newsletters'] = array('#tree' => true,);

        foreach( $tree as $newsletter ) { 
          $checked = in_array($newsletter->tid, $default);
					$form['simplenews']['newsletters'][$newsletter->tid] = array('#type' => 'checkbox',
            '#return_value' => $newsletter->tid,
            '#title' => $newsletter->name,
            '#description' => $newsletter->description,
            '#attributes' => $checked ? array('checked' => 'checked') : NULL,
            );

				}
      }
			return $form;
		break;

Thanks for testing!

IGadmin’s picture

So, your change above takes care of the optics (the registration form now LOOKs great) but after registering a new user, has created a bunch of errors as follows:

After registration of a new account, but before clicking on the confirmation link (I'm using LoginToboggan), trying one of the pages of my site where access is restricted to registered users gives the appropriate access denied message, but ALSO generates the following user warning:

user warning: Unknown column 'users.uid' in 'field list' query: SELECT node.nid, users.uid AS users_uid, node.title AS node_title FROM node node ORDER BY created DESC LIMIT 0, 1 in /home/.justy/gtink/insidegreentech.com/includes/database.mysql.inc on line 121.

Then, after the email registration link is clicked, the user is sent to the home page as he should be, but the following two errors are reported, not just in watchdog but to the user as well:

user warning: Unknown column 'users.uid' in 'field list' query: SELECT node.nid, users.uid AS users_uid, node.title AS node_title FROM node node ORDER BY created DESC LIMIT 0, 1 in /home/.justy/gtink/insidegreentech.com/includes/database.mysql.inc on line 121.

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN ('content_weekly_quote')) ORDER BY created DESC LIMIT 0, 1' at line 1 query: SELECT node.nid, node.title AS node_title, node_data_field_newsmaker_name.field_newsmaker_name_value AS node_data_field_newsmaker_name_field_newsmaker_name_value FROM node node LEFT JOIN node_content_weekly_quote node_data_field_newsmaker_name ON node.vid = node_data_field_newsmaker_name.vid WHERE (.type IN ('content_weekly_quote')) ORDER BY created DESC LIMIT 0, 1 in /home/.justy/gtink/insidegreentech.com/includes/database.mysql.inc on line 121.

Clicking on other pages of my site, in particular node lists, results in:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= term_node. WHERE (.status = '1') AND ((term_node.tid = '6'))' at line 1 query: SELECT count(node.nid) FROM node node LEFT JOIN term_node term_node ON . = term_node. WHERE (.status = '1') AND ((term_node.tid = '6')) in /home/.justy/gtink/insidegreentech.com/includes/database.mysql.inc on line 121.

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= term_node. WHERE (.status = '1') AND ((term_node.tid = '6')) ORDER BY sticky ' at line 1 query: SELECT node.nid FROM node node LEFT JOIN term_node term_node ON . = term_node. WHERE (.status = '1') AND ((term_node.tid = '6')) ORDER BY sticky DESC, created DESC LIMIT 0, 10 in /home/.justy/gtink/insidegreentech.com/includes/database.mysql.inc on line 121.

user warning: Unknown column 'users.uid' in 'field list' query: SELECT node.nid, users.uid AS users_uid, node.title AS node_title FROM node node ORDER BY created DESC LIMIT 0, 1 in /home/.justy/gtink/insidegreentech.com/includes/database.mysql.inc on line 121.

I'm not getting these errors on another server that has your code on it, with LoginToboggan, but DOESN'T have this latest patch above applied.

Unfortunately, the server that's generating the errors is my production one - I installed your patch on the wrong server. Rolling back before today's patch has not fixed the issue. I'd appreciate your guidance in helping restore the integrity of my production site's user db. Thx.

ngstigator’s picture

I'm also using logintoboggan (v1.7.2.43), but I have not set up a Non-authenticated role.

Looking at your SQL, it looks as though something is generating wonky queries. For example, the following is clearly missing a reference to the "users" table, but I have no idea where this is being generated.

SELECT node.nid, users.uid AS users_uid, node.title AS node_title 
FROM node node 
ORDER BY created DESC LIMIT 0, 1

My patch no longer includes any SQL, but only uses built-in simplenews functions to do all the processing.

If you are on Dreamhost, you can roll back your DB. Otherwise, you might have to go into the DB itself and surgically remove the offending records. Have you have a look at the term_node, node, and user tables?

I am using the patch without any errors. It may have to do with other modules you're using.

Lemme know how it goes.

IGadmin’s picture

I've restored my db and after hammering with new user accounts am having a hard time reproducing the problem, myself. Will post more if/when it returns. Thanks.

ngstigator’s picture

Great to hear that it's working for you. It'd be nice to know how it's doing with anyone else who is using this patch.

milksamsa’s picture

It works fine for me.... just a little fix is needed... I get a blank page after the submission of registration form. The user gets registered anyway, but I would really love to fix this annoying thing. Any suggestion?

ngstigator’s picture

I haven't run into this problem, but I can't recall whether it's been tested without the logintoboggan module installed.

Can you tell me what version of Drupal and simplenews you're using? I'll have a look when I get some time.

brakai295’s picture

Sorry, I'm trying to understand the posts above, but I'm a bit lost.

What I'm trying to achieve: Every user that signs up is automatically subscribed to a certain newsletter.

How can I do this? It's ok if I just hard-code it into the module. Don't know that much about Drupal dev, so could anyone help?

Cheers,
Kai

Webdesign Melbourne Australia
www.brizk.com

ngstigator’s picture

You have to modify your simplenews.module file, in the sections outlined above.

To automatically subscribe all new users to a newsletter, you have to first create the newsletter. Then you have to run the code provided in http://drupal.org/node/41745#comment-156216 ONCE, where "123" is the term id of the newsletter that you want new users to be automatically subscribed to. Alternatively, you can use a tool like PHPMyAdmin to insert it into the "variables" database table.

You can find out the id of newsletters by looking at the database table "sn_snid_tid".

brakai295’s picture

I was actually after a technique that automatically subscribes all new users to the default newsletter - so they DON'T get to see this checkbox but are automatically added to the subscriber list (via a hidden text field)

Does anyone know how to change the code above to use a hidden text field to add the email address to the list?

They can unsubscribe anytime in their user menu.

Kai

Webdesign Melbourne Australia - www.brizk.com
Work Travel Australien - www.ausmag.de

desm0n’s picture

Thats more what im after too to be honest. Any help appreciated.

--
http://www.porttalbotchat.co.uk
-------------------------------------------------
Our mission is to discuss issues and topics of residents of Port Talbot. We provide info on events, issues, concerns and discussions of our local area.

ngstigator’s picture

You should be able to change the form to accommodate $form['simplenews']['newsletters'] '#type' => 'hidden'.

Forms API here: http://api.drupal.org/api/4.7/file/developer/topics/forms_api.html

Lemme know how it goes.

Chris

brakai295’s picture

thanks!

could anyone try it and post the final code here, so that we all know how to do it? I'm currently not at home and can't implement it.
help is much appreciated! :-)

cheers,

kai

----

Webdesign Melbourne Australia - www.brizk.com
Work Travel Australien - www.ausmag.de

patchak’s picture

easier method to subscribe everyone to a newsletter is to use the simplenews_role.module that allows to auto subscribe all users from a certian role to a newsletter. So all autheticated users can be subscribed automatically and unsubscribe if they want from their account.

The fix for the first issue (presenting the newsletter checkbox at subscription) seems good I'll try to try it soon.

milksamsa’s picture

Where can I download it?

wayfarer_boy’s picture

Go to this page:

http://drupal.org/node/49980

At the time of posting, the module is submitted at update #25 and a patch to apply to the module is at #37. Phew! Took some searching!

leoklein’s picture

It'd be nice to do this in 5.x as well.

albertc’s picture

I would also like to have this feature for Drupal 5.x. Thanks in advance.

sutharsan’s picture

The feature to subscribe at user registration has made it into simplenews. You find it in both 5.x-1.x-dev development release and HEAD.

-- Sutha

-- Erik

Christefano-oldaccount’s picture

After being missing in action, this feature has reappeared at http://drupal.org/node/178485. This will hopefully be the official and supported way to enable subscription at registration time and I encourage everyone to test the patch (and please provide feedback).

gomezbl’s picture

I was developing a module like this and I've found the module you commented. Thanks bud!