You are here

function notifications_save_subscription in Notifications 6.2

Same name and namespace in other branches
  1. 5 notifications.module \notifications_save_subscription()
  2. 6.4 notifications.module \notifications_save_subscription()
  3. 6 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

Return value

integer Failure to write a record will return FALSE. Otherwise SAVED_NEW or SAVED_UPDATED is returned depending on the operation performed.

11 calls to notifications_save_subscription()
NotificationsBasicTests::testNotificationsBasicAPI in tests/notifications_api.test
Play with creating, retrieving, deleting a pair subscriptions
NotificationsContentTests::testNotificationsContent in tests/notifications_content.test
Play with creating, retrieving, deleting a pair subscriptions
notifications_add_subscription_form_submit in ./notifications.pages.inc
Submit new subscription
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.pages.inc
Process generic form submission

... See full list

File

./notifications.module, line 697
Notifications module

Code

function notifications_save_subscription(&$subscription) {
  global $user;
  $result = FALSE;
  $subscription = (object) $subscription;
  $subscription->conditions = count($subscription->fields);
  $account = $subscription->uid ? messaging_load_user($subscription->uid) : $user;

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

  // Fill in event type if not set
  if (empty($subscription->event_type)) {
    $subscription->event_type = notifications_subscription_types($subscription->type, 'event_type');
  }
  if (!empty($subscription->sid)) {
    $op = 'update';
    $result = drupal_write_record('notifications', $subscription, 'sid');
  }
  elseif ($duplicate = notifications_get_subscriptions(array(
    'uid' => $subscription->uid,
    'type' => $subscription->type,
    'event_type' => $subscription->event_type,
    'module' => $subscription->module,
    'send_method' => $subscription->send_method,
    'send_interval' => $subscription->send_interval,
  ), $subscription->fields, TRUE)) {

    // We've found duplicates, resolve conflict updating first, deleting the rest
    // It is possible that we had a disabled one, this updating will fix it
    $update = array_shift($duplicate);
    unset($subscription->sid);

    // It may be 0
    foreach ($subscription as $key => $value) {
      if (isset($value)) {
        $update->{$key} = $value;
      }
    }
    $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';
    $result = drupal_write_record('notifications', $subscription);
  }

  // If the operation has worked so far, update fields and inform other modules
  if ($result !== FALSE) {
    if ($op == 'update') {
      db_query("DELETE FROM {notifications_fields} WHERE sid = %d", $subscription->sid);
    }

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