]+href=")([^"]*)/emi', '"\\1"._send_qs("\2","'.$qs.'")', $body);
}
// call the delivery callback
$ret = (($count = call_user_func($deliver, $sender, $r, $values)) !== false )
&& $ret;
// log the recipient if there are no nodes in the message
if (!count($values['nids'])) {
db_query("INSERT INTO {send_recipient}
( sid ,nid ,rid ,rtype ,mail, name, rcount )
VALUES ( %d ,%d ,%d ,'%s', '%s', '%s', %d )",
$values['sid'], 0, $rid, $rtype, $r_mail, $r_name, $count );
}
// log each node/user combination
foreach ($values['nids'] as $nid) {
db_query("INSERT INTO {send_recipient}
( sid ,nid ,rid ,rtype ,mail, name, rcount )
VALUES ( %d ,%d ,%d ,'%s', '%s', '%s', %d )",
$values['sid'], $nid, $rid, $rtype, $r_mail, $r_name, $count );
}
}
if ($ret) {
flood_register_event("send $module");
if ($values['sender_cc']) {
$self = $user->uid ? $user : $sender; //if it's a registered user, use their "real" address
mimemail($sender, $self, $subject, $values['body']);
drupal_set_message(t('A copy of this message has also been sent to %mail',array('%mail'=>$values['sender_mail'])));
}
}
// get confirmation callback
if($callback = module_invoke($module, 'send', 'confirm', $module)) {
return call_user_func($callback, $values);
}
return _send_confirm($values);
}
function _send_deliver($sender, $recipient, $values) {
if (mimemail($sender, $recipient, $values['subject'], $values['body'])) {
drupal_set_message(t('Message sent to %mail',array('%mail'=>$recipient['mail'])));
}
// return the count of recipients
if (is_object($recipient) || isset($recipient['mail'])) return 1;
return count($recipient);
}
function _send_confirm($values) {
if (count($nids = $values['nids']) == 1) {
drupal_goto('node/'.$nids[0]);
}
$out = ''.$values['subject'].'
';
$out .= $body;
echo theme('page',$out);
exit;
}
/**
* build the body text
*/
function _send_body($message, $nids, $module, $mode) {
$modules = send_modules();
$body = '';
foreach($nids as $nid) {
$node = node_load($nid);
switch ($mode) {
case 'title' :
$body .= $node->title.'
';
break;
case 'teaser' :
$text = node_view($node, true, false, false);
$body .= _send_hook_modify($text, 'node', $module, $node->type, $node);
break;
case 'body' :
$text = node_view($node, false, false, false);
$body .= _send_hook_modify($text, 'node', $module, $node->type, $node);
break;
}
}
// let modules prepend / append stuff to the body, or otherwise modify it
$body = _send_hook_modify($body, 'node', $module, $node->type, $node);
return $body;
}
// callback for querystring preg
function _send_qs($url, $qs) {
return $url . (strpos($url, '?') !== false ? '&' : '?') . $qs;
}
function _send_hook_modify($text, $op, $module, $nodetype='', $node=null) {
$modules = send_modules();
foreach ($modules as $m => $name) {
$callback = module_invoke($m, 'send', $op, $nodetype, $node);
if ($callback && !is_null($altered = call_user_func($callback, $text))) {
$text = $altered;
}
}
return $text;
}
/**
* Set configuration variables for send
*/
function _send_value($name, $module='', $nodetype = '', $node = null, $value = null) {
$nid = is_object($node) ? $node->nid : 0;
$pernode = variable_get("{$module}_{$nodetype}_pernode", 0);
// set a new value
if (!is_null($value)) {
if ($nid && !$pernode) {
return; // trying to set a per-node value but can't
}
db_query("DELETE FROM {send_setting}
WHERE name='%s' AND module='%s' AND nodetype='%s' AND nid=%d"
, $name, $module, $nodetype, $nid);
/* We override in this order: module, nodetype, node
don't set this value if it's the same as the one above it in the stack
*/
if ($nid) {
// this node has the same value as its nodetype
if ($value == _send_value($name, $module, $nodetype)) return;
}
elseif ($nodetype) {
// this nodetype has the same value as its module
if ($value == _send_value($name, $module)) return;
// for future consistency, delete children that would inherit this value
db_query("DELETE FROM {send_setting}
WHERE name='%s' AND module='%s' AND nodetype='%s' AND value='%s'"
, $name, $module, $nodetype, $value);
}
elseif ($module) {
// this module has the same value as the default
if ($value == _send_value($name)) return;
// for future consistency, delete children that would inherit this value
db_query("DELETE FROM {send_setting}
WHERE name='%s' AND module='%s' AND value='%s'"
, $name, $module, $value);
}
// save the value
db_query("INSERT INTO {send_setting} ( name, module, nodetype, nid, value )
VALUES ('%s', '%s', '%s', %d, '%s')", $name, $module, $nodetype, $nid, $value);
return;
}
// try getting value from variables to save resources
if (!$pernode && $val = variable_get("{$module}_{$nodetype}_{$name}", FALSE)) {
return $val;
}
// get value from database
if ($val = db_result(db_query("SELECT value FROM {send_setting}
WHERE name='%s' AND
nid IN (0, %d) AND nodetype IN ('', '%s') AND module IN ('', '%s')
ORDER BY (nid = 0), (nodetype = ''), (module = '')
LIMIT 1", $name, $nid, $nodetype, $module))) {
return $val;
}
// return module's default value
if ($val = module_invoke($module, 'send', $name, $module, $nodetype, $node)) {
return $val;
}
// return send's default value
if ($val = module_invoke('send', 'send', $name, $module, $nodetype, $node)) {
return $val;
}
return;
}
/**
* load and/or update contact info in CiviCRM
*
* $params can contain name/value pairs for civicrm's data fields, optionally
* prepended by "{$scope}_". The contact record will be created and/or filled
* with the data from $params, and $params will similarly be filled with CRM data
* for use by the rest of this module.
*/
function _send_crm_contact($mail, &$params, $user=null, $scope='') {
if (!module_exists('civicrm')) return false;
if ($scope) $scope .= '_';
civicrm_initialize(true);
// fields allowed in the contact record
$contact_fields = array('first_name', 'last_name', 'email', 'source', 'prefix', 'suffix',
'display_name', 'legal_identifier', 'external_identifier', 'job_title', 'gender',
'birth_date', 'is_deceased', 'preferred_communication_method', 'do_not_phone',
'do_not_email', 'do_not_mail', 'home_URL', 'image_URL', 'street_address' );
// create an array of CRM data from $params. Strip out the part of
// the variable that conveys scope (sender_ , recipient_) if you have to
$record = array();
foreach ($params as $key => $value) {
if (!in_array($name = str_replace($scope, '', $key), $contact_fields)) continue;
$record[$name] = $params[$key];
}
// find contact
$search = array();
if ($user && $id = crm_uf_get_match_id($user->uid)) {
$search['id'] = $id;
}
elseif ($mail) {
$search['email'] = $mail;
}
else {
return; // we don't have enough to work with here
}
if ($res = crm_contact_search($search)) $contact = current($res[0]);
if($contact['contact_id']) {
$updates = array();
foreach ($record as $key => $value) {
// populate contact info if it's not there already
if (!isset($contact[$key]) && $value) {
$updates[$key] = $value;
}
// update our referenced array with CRM info
else {
$record[$key] = $contact[$key];
}
}
$contact = crm_get_contact($contact);
if (count($updates)) {
$contact = crm_update_contact($contact, $updates);
}
}
else {
// no contact. let's create one
$contact = array_merge(array('email' => $mail), $record);
$contact = crm_create_contact($contact);
}
// save data to the referenced $params array if it's not already set
foreach ($record as $key => $value) {
if (!$params[$scope.$key])$params[$scope.$key] = $value;
}
return $contact;
}
function _send_previous_recipients($user) {
$recipients = array();
$res = db_query('SELECT r.mail, r.name, MAX(s.timestamp)
FROM {send} s RIGHT JOIN {send_recipient} r ON r.sid = s.sid
WHERE s.uid = %d GROUP BY r.mail, r.name, s.timestamp
ORDER BY r.mail, s.timestamp ASC',$user->uid);
while ($row = db_fetch_object($res)) {
$recipients[] = Array('mail' => $row->mail,
'name' => $row->name,
);
}
return $recipients;
}
function theme_send_form(&$form) {
$from = drupal_render($form['from']);
$to = drupal_render($form['to']);
$buttons = drupal_render($form['buttons']);
$subject = drupal_render($form['subject']);
$message = drupal_render($form['message']);
$template= drupal_render($form['template']);
$body = drupal_render($form['body']);
$extra = drupal_render($form);
$replace = array('%message', '%body');
$values = array($message, $body);
$content = str_replace($replace, $values, $template);
$headers = theme('table', array(t('From'),t('To')),array(array('data' => array($from, $to), 'valign' => 'top')),array('width'=>'100%'));
return $headers.'
'.$extra.$buttons.'
'.$subject.'
'.$content;
}
function theme_send_template() {
$template = "%message\n%body\n";
return $template;
}
function theme_send_body($values) {
$body = '';
$template .= $values['template'];
if ($values['message']) {
$message = ''.check_markup($values['message']).'
';
}
$body = $values['node_body'];
$replace = array('%message', '%body');
$values = array($message, $body);
return str_replace($replace, $values, $template);
}