You are here

function simplenews_roles_newsletter_submit in Simplenews Roles 7

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

Forms API callback; additional submit handler for newsletter form.

Every submit leads to a resync.

1 string reference to 'simplenews_roles_newsletter_submit'
simplenews_roles_form_simplenews_admin_category_form_alter in ./simplenews_roles.module
Implements hook_form_FORM_ID_alter().

File

./simplenews_roles.module, line 55
Synchronize user subscriptions based on user roles membership.

Code

function simplenews_roles_newsletter_submit($form, &$form_state) {
  $tid = $form_state['values']['tid'];
  $roleids = array_values(array_filter($form_state['values']['roles']));

  // Save auto remove settings
  $auto_remove = variable_get('simplenews_roles_auto_remove', array());
  $auto_remove[$tid] = $form_state['values']['auto_remove'];
  variable_set('simplenews_roles_auto_remove', $auto_remove);

  // Update subscriptions
  $role_newsletters = variable_get('simplenews_roles_tids_rids', array());
  $resync = variable_get('simpelnews_roles_resync', array());
  if (count($roleids) > 0) {
    if (!isset($role_newsletters[$tid]) || $role_newsletters[$tid] != $roleids) {

      // Sync selection changed. Trigger resync once.
      $role_newsletters[$tid] = $roleids;
      $sync_done = simplenews_roles_update_subscriptions($tid, $roleids);
      if (!$sync_done) {

        // Incomplete, continue resync via cron.
        $resync[$tid] = $roleids;
      }
    }
  }
  else {
    if (isset($role_newsletters[$tid])) {

      // Previously synced. Unsubscribe only if needed
      if (!isset($auto_remove[$tid]) || $auto_remove[$tid]) {

        // @todo: This can be heavy operation to a large numbers of rows. Try to optimize.
        // Unsubscribe all users from $tid category
        // @todo Possibly only unsubscribe auto subscribed users?
        db_update('simplenews_subscription')
          ->fields(array(
          'status' => 0,
        ))
          ->condition('tid', $tid)
          ->execute();
      }
    }

    // Remove orphans
    unset($role_newsletters[$tid]);
    unset($resync[$tid]);
  }
  variable_set('simplenews_roles_tids_rids', $role_newsletters);
  variable_set('simpelnews_roles_resync', $resync);
}