You are here

function og_mail in Organic groups 5

Same name and namespace in other branches
  1. 5.2 og.module \og_mail()
  2. 5.7 og.module \og_mail()
  3. 6.2 og.module \og_mail()
  4. 6 og.module \og_mail()

Send this node/comment via email to all email members. Called from og_nodeapi() and og_comment(). Sometimes called from during cron if job_queue.module is enabled (recommended). TODO: this function is a bit messy. rethink.

Parameters

$type: the object type: node or comment

$id: a node or comment object. if a non object is supplied, a load() operation is performed.

Return value

none

2 calls to og_mail()
og_comment in ./og.module
og_nodeapi in ./og.module
Implementation of hook_nodeapi().
3 string references to 'og_mail'
og_comment in ./og.module
og_email_form_submit in ./og.module
og_nodeapi in ./og.module
Implementation of hook_nodeapi().

File

./og.module, line 1879

Code

function og_mail($type, $obj) {
  if ($type == 'comment') {
    if (!is_object($obj)) {
      $obj = _comment_load($obj);
    }

    // registered user
    if ($obj->uid) {
      $account = user_load(array(
        'uid' => $obj->uid,
      ));
      $obj->name = $account->name;
    }
    else {
      $obj->name = variable_get('anonymous', 'Anonymous');
    }
    $rendered = check_markup($obj->comment, $obj->format, FALSE);
    $obj->body = $rendered;
    $obj->teaser = $rendered;
    $originalurl = url("node/{$obj->nid}", NULL, "comment-{$obj->cid}", TRUE);
    $replyurl = url("comment/reply/{$obj->nid}/{$obj->cid}", NULL, NULL, TRUE);
    $node = node_load($obj->nid);
    $obj->og_groups = $node->og_groups;
    $obj->title = $node->title;
    $obj->msgid = $obj->nid . '-' . $obj->cid . og_msgid_server();
    $reply = $obj->nid . '-';
    if ($obj->pid) {
      $reply .= $obj->pid;
    }
    else {
      $reply .= '0';
    }
    $obj->in_reply_to .= $reply . og_msgid_server();
    $type_friendly = 'comment';
  }
  else {
    if (!is_object($obj)) {
      $obj = node_load($obj);
    }
    $account = user_load(array(
      'uid' => $obj->uid,
    ));
    $obj->name = $account->name;
    $obj->subject = $obj->title;

    // These calls to og_node_view() setup the node body and teaser, apply the
    // appropriate input filters, and handle CCK fields, event fields, etc.
    $obj->body = og_node_view($obj, FALSE);
    $obj->teaser = og_node_view($obj, TRUE);
    $obj->msgid = $obj->nid . '-0' . og_msgid_server();
    $originalurl = url("node/{$obj->nid}", NULL, NULL, TRUE);
    $replyurl = url("comment/reply/{$obj->nid}", NULL, 'comment-form', TRUE);
    $type_friendly = node_get_types('name', $obj);
  }

  // set email from variables
  $variables = array(
    '@user_mail' => $account->mail ? $account->mail : variable_get("site_mail", ini_get("sendmail_from")),
    '@user_name' => mime_header_encode($obj->name),
    '@site_mail' => variable_get("site_mail", ini_get("sendmail_from")),
    '@site_name' => mime_header_encode(variable_get("site_name", 'Drupal')),
  );
  $from_mail = strtr(variable_get("og_email_notification_pattern", '@user_name  <@site_mail>'), $variables);
  $headers = array(
    'X-Mailer' => 'Drupal - og_mail',
    'Precedence' => 'list',
    'Message-Id' => "<{$obj->msgid}>",
  );
  if ($obj->in_reply_to) {
    $headers['In-Reply-To'] = "<{$obj->in_reply_to}>";
  }

  // set email body variables
  $variables = array(
    '@site' => variable_get('site_name', 'drupal'),
    '!read_more' => $obj->readmore ? t('Read more') : t('View original'),
    '!content_url' => $originalurl,
    '!reply_url' => $replyurl,
    '@title' => trim($obj->title),
    '@subject' => trim($obj->subject),
    '@node_full' => trim(og_mail_output($obj->body)),
    '@node_teaser' => trim(og_mail_output($obj->teaser)),
    '@username' => $obj->name,
  );

  // Send email to selective subscribers and global subscribers.
  // We use if() here in case node/comment no longer has any groups (i.e. this function can be called from cron).
  if (is_array($obj->og_groups) && !empty($obj->og_groups)) {
    $groups = implode(', ', $obj->og_groups);

    // tricky query here. mysql returns NULL in the case of NULL != 0 so i rework this for 2 positive statements about og_email field
    // ORDER BY favors newer groups to help them get ramped up. Thus, they are perceived as active.
    $sql = "SELECT u.mail, ou.nid AS gid, n.title AS group_name, n.uid AS group_uid, u.name AS group_owner, oug.og_email \n            FROM {og_uid} ou INNER JOIN {users} u ON ou.uid=u.uid \n            INNER JOIN {og_uid_global} oug ON ou.uid=oug.uid \n            INNER JOIN {node} n ON ou.nid=n.nid \n            WHERE ou.nid IN ({$groups}) AND (\n              (oug.og_email = %d AND ou.mail_type=1) OR\n              (oug.og_email = %d)\n              ) AND u.status = 1 AND u.mail != '' AND ou.is_active = 1 \n              ORDER by u.mail DESC, n.created DESC\n              ";
    $result = db_query($sql, OG_NOTIFICATION_SELECTIVE, OG_NOTIFICATION_ALWAYS);
    $last_mail = '';
    while ($row = db_fetch_object($result)) {

      // only notify each user once. we used to do this with GROUP BY but got very hard to assure that all the selected fields came from same record.
      if ($row->mail == $last_mail) {
        continue;
      }
      $last_mail = $row->mail;

      // TODO: add node_access('view') call here in case node is moderated or other.
      // Hopefully D6 will avert need to impersonate.
      // Append these group specific variables.
      $variables['@group'] = $row->group_name;
      $variables['!group_url'] = url("og/manage/{$row->gid}", NULL, NULL, TRUE);
      $variables['@type'] = $type_friendly;
      $unsubscribe = url("og/manage/{$row->gid}", NULL, NULL, TRUE);
      $ownerurl = url("user/{$row->group_uid}", NULL, NULL, TRUE);
      $group_home = url("node/{$row->gid}", NULL, NULL, TRUE);
      $groupheaders = $headers + array(
        'List-Id' => mime_header_encode($row->group_name) . " <{$group_home}>",
        'List-Unsubscribe' => "<{$unsubscribe}>",
        'List-Owner' => mime_header_encode($row->group_owner) . " <{$ownerurl}>",
        "List-Archive" => "<{$group_home}>",
      );
      drupal_mail('og_mail', $row->mail, _og_user_mail_text('og_new_node_subject', $variables), _og_user_mail_text('og_new_node_body', $variables), $from_mail, $groupheaders);
    }
  }
}