You are here

function og_notifications_update_6001 in Organic groups 6.2

Remove direct group subscriptions in favour of grouptype subscriptions. The upgrade is being performed directly at the DB-level instead of using the notifications APIs. It is assumed that D5 users will always upgrade to D6 first rather than skip major versions.

File

modules/og_notifications/og_notifications.install, line 114

Code

function og_notifications_update_6001() {
  $ret = array();

  // Only subscribe the user to enabled types.
  $content_types = array_filter(variable_get('og_notifications_content_types', array()));
  $result = db_query("SELECT n.*, nof.intval as gid FROM {notifications} n INNER JOIN {notifications_fields} nof USING (sid) WHERE n.type = 'group'");
  while ($subscription = db_fetch_array($result)) {

    // Subscription status does not really matter until notifications-6--2.
    // The current group subscription settings are directly transferred to the
    // grouptype subscription.
    foreach ($content_types as $type) {
      $content_subscription = $subscription;
      unset($content_subscription['sid'], $content_subscription['gid']);
      $content_subscription['type'] = 'grouptype';
      $content_subscription['conditions'] = 2;

      // Use drupal_write_record as a fail-safe. Notifications would have been
      // upgraded prior to OGN.
      $save = drupal_write_record('notifications', $content_subscription);
      if ($save !== FALSE) {
        $fields = array(
          'sid' => $content_subscription['sid'],
          'field' => 'group',
          'value' => $subscription['gid'],
          'intval' => $subscription['gid'],
        );
        drupal_write_record('notifications_fields', $fields);
        $fields['field'] = 'type';
        $fields['value'] = $type;
        unset($fields['intval']);
        drupal_write_record('notifications_fields', $fields);
      }
    }

    // Avoid using update_sql for efficiency and to minimise screen spam.
    db_query("DELETE FROM {notifications} WHERE sid = %d", $subscription['sid']);
    db_query("DELETE FROM {notifications_fields} WHERE sid = %d", $subscription['sid']);

    // There's a possibility that we might be losing notifications here. But, in
    // the interests of keeping things efficient, we're avoiding extended loads
    // where possible.
    db_query("DELETE FROM {notifications_queue} WHERE sid = %d", $subscription['sid']);
  }
  return $ret;
}