You are here

function og_save_subscription in Organic groups 5.2

Same name and namespace in other branches
  1. 5.8 og.module \og_save_subscription()
  2. 5 og.module \og_save_subscription()
  3. 5.3 og.module \og_save_subscription()
  4. 5.7 og.module \og_save_subscription()
  5. 6.2 og.module \og_save_subscription()
  6. 6 og.module \og_save_subscription()

Low level function for managing subscriptions

Parameters

$gid node ID of a group:

$uid user ID of user:

$args an array with details of this subscription. Possible array keys are:: is_active, is_admin, mail_type, created

7 calls to og_save_subscription()
og_add_users_submit in ./og.module
og_approve in ./og.module
og_create_admin_confirm_submit in ./og.module
Confirm og create admin form
og_manage_form_submit in ./og.module
og_nodeapi in ./og.module
Implementation of hook_nodeapi().

... See full list

File

./og.module, line 410

Code

function og_save_subscription($gid, $uid, $args = array()) {
  $sql = "SELECT COUNT(*) FROM {og_uid} WHERE nid = %d AND uid = %d";
  $cnt = db_result(db_query($sql, $gid, $uid));
  $time = time();
  if ($cnt == 0) {

    // this pattern borrowed from user_save()
    $fields = array(
      'nid',
      'uid',
      'created',
      'changed',
    );
    $group = node_load($gid);
    $values = array(
      $gid,
      $uid,
      $args['created'] ? $args['created'] : $time,
      $time,
    );
    unset($args['created']);
    foreach ($args as $key => $value) {
      $fields[] = db_escape_string($key);
      $values[] = $value;
      $s[] = "'%s'";
    }
    db_query('INSERT INTO {og_uid} (' . implode(', ', $fields) . ') VALUES (%d, %d, %d, %d, ' . implode(', ', $s) . ')', $values);
    module_invoke_all('og', 'user insert', $gid, $uid, $args);
  }
  else {
    $cond[] = 'changed = ' . $time;
    foreach ($args as $key => $value) {
      $cond[] = db_escape_string($key) . " = '" . db_escape_string($value) . "'";
    }
    $cond = implode(', ', $cond);
    db_query("UPDATE {og_uid} SET {$cond} WHERE nid = %d AND uid = %d", $gid, $uid);
    module_invoke_all('og', 'user update', $gid, $uid, $args);
  }
}