You are here

function notifications_save_subscription in Notifications 5

Same name and namespace in other branches
  1. 6.4 notifications.module \notifications_save_subscription()
  2. 6 notifications.module \notifications_save_subscription()
  3. 6.2 notifications.module \notifications_save_subscription()
  4. 6.3 notifications.module \notifications_save_subscription()
  5. 7 notifications.module \notifications_save_subscription()

Update or create subscription

This function checks for duplicated subscriptions before saving. If a similar subscription is found it will be updated. If no subscription is found and it is new, the sid will be added into the object.

Parameters

$subscription: Subscription object or array

6 calls to notifications_save_subscription()
Notifications_API_Tests::testNotificationsBasicAPI in tests/notifications_api.test
Play with creating, retrieving, deleting a pair subscriptions
notifications_autosubscribe in notifications_autosubscribe/notifications_autosubscribe.module
Subscribes users to content they post, if not already subscribed
notifications_content_form_submit in notifications_content/notifications_content.module
Process generic form submission
notifications_form_confirm_submit in ./notifications.admin.inc
Process form submission
notifications_ui_node_form_submit in notifications_ui/notifications_ui.module
Form submission, node subscriptions form

... See full list

File

./notifications.module, line 448
Notifications module

Code

function notifications_save_subscription(&$subscription) {
  global $user;
  $subscription = (object) $subscription;
  $subscription->conditions = count($subscription->fields);
  $account = $subscription->uid ? user_load(array(
    'uid' => $subscription->uid,
  )) : $user;

  // Default values for fields: send_interval, send_method, etc...
  foreach (_notifications_subscription_defaults($account) as $field => $value) {
    if (!isset($subscription->{$field})) {
      $subscription->{$field} = $value;
    }
  }

  // The cron parameter will be always enabled because we are not managing push/pull methods anymore at this level.
  $subscription->cron = 1;
  if ($subscription->sid) {
    $op = 'update';
    db_query("UPDATE {notifications} SET uid = %d, type = '%s', event_type = '%s', conditions = %d, send_interval = '%d', send_method = '%s', cron = %d, module = '%s', status = %d WHERE sid = %d", $subscription->uid, $subscription->type, $subscription->event_type, $subscription->conditions, $subscription->send_interval, $subscription->send_method, $subscription->cron, $subscription->module, $subscription->status, $subscription->sid);
    db_query("DELETE FROM {notifications_fields} WHERE sid = %d", $subscription->sid);
  }
  elseif ($duplicate = notifications_get_subscriptions(array(
    'uid' => $subscription->uid,
    'type' => $subscription->type,
    'event_type' => $subscription->event_type,
    'module' => $subscription->module,
  ), $subscription->fields)) {

    // We've found duplicates, resolve conflict updating first, deleting the rest
    $update = array_shift($duplicate);
    $update->send_interval = $subscription->send_interval;
    $uddate->send_method = $subscription->send_method;
    $subscription = $update;

    // If there are more, delete, keep the table clean
    while (array_shift($duplicate)) {
      notifications_delete_subscription($duplicate->sid);
    }
    return notifications_save_subscription($subscription);
  }
  else {
    $op = 'insert';
    $subscription->sid = db_next_id('{notifications}_sid');
    db_query("INSERT INTO {notifications} (sid, uid, type, event_type, conditions, send_interval, send_method, cron, module, status) VALUES(%d, %d, '%s', '%s', %d, %d, '%s', %d, '%s', %d)", $subscription->sid, $subscription->uid, $subscription->type, $subscription->event_type, $subscription->conditions, $subscription->send_interval, $subscription->send_method, $subscription->cron, $subscription->module, $subscription->status);
  }

  // There may be subscriptions with no fields, some people are coding such plug-ins.
  if ($subscription->fields) {
    foreach ($subscription->fields as $name => $value) {
      db_query("INSERT INTO {notifications_fields} (sid, field, value) VALUES(%d, '%s', '%s')", $subscription->sid, $name, $value);
    }
  }
  notifications_module_invoke($op, $subscription);
}