You are here

function notifications_custom_account_update in Notifications 6.4

Same name and namespace in other branches
  1. 6 notifications_custom/notifications_custom.module \notifications_custom_account_update()

Update custom subscriptions for user account

Parameters

$account: User account

$update: Array of type => status

Return value

int The number of updated subscriptions

2 calls to notifications_custom_account_update()
notifications_custom_user in notifications_custom/notifications_custom.module
Implementation of hook_user().
notifications_custom_user_overview_submit in notifications_custom/notifications_custom.module
Form submission with custom notifications

File

notifications_custom/notifications_custom.module, line 228
Custom notifications module

Code

function notifications_custom_account_update($account, $update) {
  $changed = 0;
  foreach ($update as $type => $value) {
    if ($subscription = notifications_custom_get_subscription($type, $account, TRUE)) {
      if ($value && !$subscription
        ->is_instance()) {

        // Add new subscription
        $subscription
          ->set_account($account);
        if ($result = notifications_save_subscription($subscription)) {
          $changed++;
        }
        else {
          $message = !empty($subscription->error_message) ? $subscription->error_message : t('The subscription cannot be saved.');
          drupal_set_message(t('Cannot create subscription !name. !message', array(
            '!name' => $subscription
              ->get_name(),
            '!message' => $message,
          )), 'error');
        }
      }
      elseif (!$value && $subscription
        ->is_instance()) {

        // Disable existing subscription
        notifications_subscription_delete($subscription->sid);
        $changed++;
      }
    }
  }
  return $changed;
}