You are here

function notifications_save_subscription in Notifications 7

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.2 notifications.module \notifications_save_subscription()
  5. 6.3 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

$check: Whether to check parameters, can be skipped if they've been previously checked

Return value

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

4 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_submit_subscription in ./notifications.pages.inc
Submit subscription printing out the results
_notifications_manage_subscriptions_mass_update_helper in ./notifications.manage.inc

File

./notifications.module, line 777
Notifications module

Code

function notifications_save_subscription(&$subscription, $check = TRUE) {

  // Build object if not built previously
  $subscription = notifications_subscription_build($subscription);

  // Check all the parameters are ok, add error message and return if not
  if ($check && !$subscription
    ->check_all()) {
    return FALSE;
  }

  // Parameters are checked, now proceed
  if (!empty($subscription->sid)) {
    $op = 'update';
    $result = $subscription
      ->save();
  }
  else {
    if ($duplicate = notifications_get_subscriptions(array(
      'uid' => $subscription->uid,
      'mdid' => $subscription->mdid,
      'type' => $subscription->type,
      'event_type' => $subscription->event_type,
      'send_interval' => $subscription->send_interval,
      'module' => $subscription->module,
    ), $subscription
      ->get_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->sid = $update->sid;

      // If there are more, delete, keep the table clean
      while ($dupe = array_shift($duplicate)) {
        Notifications_Subscription::delete_subscription($dupe->sid);
      }
      return notifications_save_subscription($subscription, $check);
    }
    else {
      $op = 'insert';
      $result = $subscription
        ->save();
    }
  }

  // If the operation has worked so far, update fields and inform other modules
  if ($result !== FALSE) {
    $subscription
      ->invoke_all($op);
  }
  return $result;
}