You are here

function simplenews_roles_update_subscriptions in Simplenews Roles 6.2

Same name and namespace in other branches
  1. 5 simplenews_roles.module \simplenews_roles_update_subscriptions()
  2. 6 simplenews_roles.module \simplenews_roles_update_subscriptions()
  3. 7 simplenews_roles.module \simplenews_roles_update_subscriptions()

API function; clears subscription list for specified newsletter and replaces it with users from the specified roles.

This function should probably be only run on cron. It may well need better optimising.

Parameters

$tid: The newsletter id (ie term id) to update.

$rids: An array of role ids.

2 calls to simplenews_roles_update_subscriptions()
SimplenewsRolesTestCase::testRoleSync in tests/simplenews_roles.test
Test role synchronization.
simplenews_roles_cron in ./simplenews_roles.module
Implementation of hook_cron().

File

./simplenews_roles.module, line 290
Simplenews Roles module

Code

function simplenews_roles_update_subscriptions($tid, $rids) {
  $rids_string = implode(', ', $rids);

  /*
  the rules are:
    - a: we can change status from 1 to 0 in all cases
      (we may unsub anybody)
    - b: we can only change status from 0 to 1 if we are the source of the 0
      (we can't resub users who unsubbed manually.)
      (this accounts for role changes)
    - c: we can add subs if status doesn't exist.

   the steps:
    - 0
    delete the snids for anon users.

    - 1
    unsubscribe if user doesn't have role

    - 2
    subscribe all from role who are either unknown to SN, or who were
    previously unsubscribed by ourselves (eg due to a user losing then
    regaining a role).
  */

  /**
   * step 0:
   * remove anon users
   *
   * skip this step: it breaks simpletest.
   * (cookie if you can tell me why!)
   * and if you intend to have a synced newsletter you really should keep
   * anon users out of it.
   */
  $query_0 = "\n    DELETE {simplenews_snid_tid}\n    FROM \n      {simplenews_snid_tid} snst\n    JOIN \n      {simplenews_subscriptions} sns\n      ON snst.snid = sns.snid\n    WHERE\n      snst.tid = %d /* {$tid} */\n    AND\n      sns.uid = 0";

  // Just delete these. They're not interesting.
  // Why does this break simpletest?

  //db_query($query_0, $tid);

  /**
   * step 1:
   * remove users who are not in the role(s)
   */
  $query_1 = "\n    SELECT sns.mail \n    FROM \n      {simplenews_snid_tid} snst \n    JOIN \n      {simplenews_subscriptions} sns\n      ON snst.snid = sns.snid\n    JOIN \n      {users} u\n      ON sns.uid = u.uid \n    LEFT JOIN /* LEFT JOIN because we want the NULLs */\n      {users_roles} ur \n      ON \n        u.uid = ur.uid \n      AND \n        ur.rid IN (%s) /* {$rids_string} */\n    WHERE \n      snst.tid = %d   /* {$tid} */\n    AND    \n      ur.uid IS NULL\n    ";
  $mails_1 = db_query($query_1, $rids_string, $tid);
  while ($mail = db_result($mails_1)) {

    // Call SN API to unsub users.
    simplenews_unsubscribe_user($mail, $tid, FALSE, 'simplenews_roles');
  }

  /**
   * step 2:
   * add users who are in the role(s), and not subscribed
   * but only if the prior source of unsubscription is ourselves
   * In other words, if the source of their unsub is 'website', say
   * we don't touch it.
   */
  $query_2 = "\n    SELECT u.mail\n    FROM\n      {users} u\n    JOIN\n      {users_roles} ur \n      ON \n        u.uid = ur.uid \n      AND \n        ur.rid IN (%s) /* {$rids_string} */\n    LEFT JOIN /* LEFT JOIN because we want the NULLs */\n    (\n      {simplenews_snid_tid} snst \n    JOIN \n      {simplenews_subscriptions} sns\n      ON \n        snst.snid = sns.snid\n      AND\n        snst.tid = %d   /* {$tid} */\n    )\n    ON\n      u.uid = sns.uid\n    WHERE\n      sns.snid IS NULL /* those that don't exist at all for this tid or for SN as a whole */\n      OR\n      snst.source = 'simplenews_roles' /* those we unsubbed ourselves previously */\n  ";
  $mails_2 = db_query($query_2, $rids_string, $tid);
  while ($mail = db_result($mails_2)) {

    // Call SN API to sub users.
    simplenews_subscribe_user($mail, $tid, FALSE, 'simplenews_roles');
  }
  watchdog('newsletter', t('Newsletter subscription list was synchronized to roles.'));
}