You are here

function notifications_admin_subscriptions_settings_submit in Notifications 6.4

Same name and namespace in other branches
  1. 7 notifications.admin.inc \notifications_admin_subscriptions_settings_submit()

Subscription settings submit, disable all subscriptions not allowed

1 string reference to 'notifications_admin_subscriptions_settings_submit'
notifications_admin_subscriptions_settings in ./notifications.admin.inc
Subscription settings

File

./notifications.admin.inc, line 333

Code

function notifications_admin_subscriptions_settings_submit($form, &$form_state) {

  // Submission may have been Save or Reset to defaults, so we cannot really trust submitted values
  // Instead we do a cache refresh and re-check enabled / disabled types
  messaging_static_reset('notifications_subscription_type_enabled');
  $types = notifications_subscription_type_enabled();
  $sql_update = 'UPDATE {notifications} SET status = %d WHERE status = %d';
  $enabled = $disabled = 0;
  if ($types) {

    // Enable subscriptions of these types that were disabled before
    $placeholders = db_placeholders($types, 'varchar');
    $sql_where = ' AND type IN (' . $placeholders . ')';
    $params = array_merge(array(
      Notifications_Subscription::STATUS_ACTIVE,
      Notifications_Subscription::STATUS_DISABLED,
    ), $types);
    db_query($sql_update . $sql_where, $params);
    $enabled = db_affected_rows();
    $params = array_merge(array(
      Notifications_Subscription::STATUS_DISABLED,
      Notifications_Subscription::STATUS_ACTIVE,
    ), $types);
    $sql_where = ' AND type NOT IN (' . $placeholders . ')';
  }
  else {
    $params = array(
      Notifications_Subscription::STATUS_DISABLED,
      Notifications_Subscription::STATUS_ACTIVE,
    );
    $sql_where = '';
  }
  db_query($sql_update . $sql_where, $params);
  $disabled = db_affected_rows();
  if ($enabled) {
    drupal_set_message(t('@count existing subscriptions have been enabled.', array(
      '@count' => $enabled,
    )), 'warning');
  }
  if ($disabled) {
    drupal_set_message(t('@count existing subscriptions have been disabled.', array(
      '@count' => $disabled,
    )), 'warning');

    // Delete rows from queue and do some clean up.
    notifications_queue()
      ->queue_clean(FALSE);
  }
}