You are here

function og_subscribe_user_action in Organic groups 6.2

Same name and namespace in other branches
  1. 6 modules/og_actions/og_actions.module \og_subscribe_user_action()

A configurable action to subscribe a user to specific groups.

File

modules/og_actions/og_actions.module, line 229

Code

function og_subscribe_user_action($account, $context) {
  if (isset($context['groups'])) {
    $new_groups = $existing_groups = array();
    foreach ($context['groups'] as $gid) {

      // Add user if they are not already an active member.
      if (empty($account->og_groups[$gid])) {
        og_save_subscription($gid, $account->uid, array(
          'is_active' => 1,
        ));
        $new_groups[] = $gid;
      }
      else {
        $existing_groups[] = $gid;
      }
    }
    $text = '';
    if ($new_groups) {
      $text .= 'Subscribed user %name to groups %groups. ';
      watchdog('action', 'Subscribed user %name to groups %newgroups.', array(
        '%name' => $account->name,
        '%groups' => implode(',', $new_groups),
      ));
    }
    if ($existing_groups) {
      $text .= 'User %name is already a member of groups %oldgroups. ';
    }
    if (!empty($text)) {
      watchdog('action', $text, array(
        '%name' => $account->name,
        '%newgroups' => implode(',', $new_groups),
        '%oldgroups' => implode(',', $existing_groups),
      ));
    }
  }
}