--- fieldactions.module.original 2007-03-16 20:13:45.000000000 -0400 +++ fieldactions.module 2007-10-17 14:59:51.000000000 -0400 @@ -160,6 +160,210 @@ } /** + * Implementation of a Drupal action. + * This action sends email to each user in a user reference field. + * If the field is empty, no email will be sent. + * Multiple fields send multiple emails. + * + */ +function action_send_email_to_nodereference($op, $edit = array(), $node) { + switch($op) { + case 'metadata': + return array( + 'description' => t('Send email to the author of the node selected in the node reference field'), + 'type' => t('Email'), + 'batchable' => false, + 'configurable' => true, + ); + + case 'do': + // does this node have the node reference field? + if (array_key_exists($edit['node'], $node)) { + // yup, make sure it is an array so we can foreach it + if (is_array($node->{$edit['node']})) { + // pull out each represenation of the field (an array with nid) in it + foreach ($node->{$edit['node']} as $field) { + //a nonexistant nid field means that the nodereference box was not used + if (!isset($field['nid'])) continue; + + // load the node referenced in the field + $referred_node = node_load($field['nid']); + + // create the variables array from this node and the referred node + $vars = _fieldactions_nodereference_token_replacements($referred_node, $node); + + // construct the actual message + $from = $vars['%site_name'] . '<' . $vars['%site_mail'] . '>'; + $subject = $edit['subject']; + $message = $edit['message']; + + // replace the subjects and messages with their token replaced equivalents + $subject = strtr($subject, $vars); + $subject = str_replace(array("\r", "\n"), '', $subject); + $message = strtr($message, $vars); + + // do it and log it + if (drupal_mail('fieldactions_userreference_mail', $vars['%referred_node_owner_mail'], $subject, $message, $from)) { + watchdog('action', t('Sent email to %recipient', array('%recipient' => $vars['%referred_node_owner_mail']))); + } + else { + watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $vars['%referred_node_owner_mail']))); + } + } + } + } + break; + + // return an HTML config form for the action + case 'form': + // default values for form + if (!isset($edit['subject'])) $edit['subject'] = ''; + if (!isset($edit['message'])) $edit['message'] = ''; + $form = array(); + + // add form components + $form['node'] = array ( + '#type' => 'select', + '#title' => t('Recipient Node Reference Field'), + '#default_value' => $edit['node'], + '#options' => _fieldactions_nodereference_fields(), + '#description' => t("Select the node reference field that will reference the node(s) who's author(s) will recieve this email. If this field does not exist in the node when the action runs, email will not be sent."), + ); + $form['subject'] = array( + '#type' => 'textfield', + '#title' => t('Subject'), + '#default_value' => $edit['subject'], + '#size' => '20', + '#maxlength' => '254', + '#description' => t('The subject of the message.'), + ); + $form['message'] = array( + '#type' => 'textarea', + '#title' => t('Message'), + '#default_value' => $edit['message'], + '#cols' => '80', + '#rows' => '20', + '#description' => t('The message that should be sent. You may include the following variables:') . ' ' . implode(', ', array_keys(_fieldactions_nodereference_token_replacements())), + ); + return $form; + + // validate the HTML form + case 'validate': + // no errors possible now + return true; + + // process the HTML form to store configuration + case 'submit': + $params = array( + 'node' => $edit['node'], + 'subject' => $edit['subject'], + 'message' => $edit['message']); + return $params; + } +} + + +/** + * Implementation of a Drupal action. + * This action sends email to the creator of a node. + * + */ +function action_send_email_to_owner($op, $edit = array(), $node) { + switch($op) { + case 'metadata': + return array( + 'description' => t('Send email to the creator of the node'), + 'type' => t('Email'), + 'batchable' => false, + 'configurable' => true, + ); + + case 'do': + + // load the user we want + $user = user_load(array('uid' => $node->uid)); + + // create the variables array from this user and the node + $vars = _fieldactions_userreference_token_replacements($user, $node); + + // construct the actual message + $from = $vars['%site_name'] . '<' . $vars['%site_mail'] . '>'; + $subject = $edit['subject']; + $message = $edit['message']; + + // replace the subjects and messages with their token replaced equivalents + $subject = strtr($subject, $vars); + $subject = str_replace(array("\r", "\n"), '', $subject); + $message = strtr($message, $vars); + + // do it and log it + if (drupal_mail('fieldactions_userreference_mail', $vars['%node_owner_mail'], $subject, $message, $from)) { + watchdog('action', t('Sent email to %recipient', array('%recipient' => $vars['%node_owner_mail']))); + } + else { + watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $vars['%node_owner_mail']))); + } + + + // return an HTML config form for the action + case 'form': + // default values for form + if (!isset($edit['subject'])) $edit['subject'] = ''; + if (!isset($edit['message'])) $edit['message'] = ''; + $form = array(); + + $form['subject'] = array( + '#type' => 'textfield', + '#title' => t('Subject'), + '#default_value' => $edit['subject'], + '#size' => '20', + '#maxlength' => '254', + '#description' => t('The subject of the message.'), + ); + $form['message'] = array( + '#type' => 'textarea', + '#title' => t('Message'), + '#default_value' => $edit['message'], + '#cols' => '80', + '#rows' => '20', + '#description' => t('The message that should be sent. You may include the following variables:') . ' ' . implode(', ', array_keys(_fieldactions_userreference_token_replacements())), + ); + return $form; + + // validate the HTML form + case 'validate': + // no errors possible now + return true; + + // process the HTML form to store configuration + case 'submit': + $params = array( + 'subject' => $edit['subject'], + 'message' => $edit['message']); + return $params; + } +} + +/** + * Get a list of all the user reference fields out there + */ + +function _fieldactions_nodereference_fields() { + $allfields = content_fields(); + $fields = array(); + foreach($allfields as $name => $field) { + if ($field['type'] == 'nodereference') { + // if a field is used twice, we only need one label, but we append + // the field name to remove ambiguity about where this will apply + $fields[$name] = $field['widget']['label'] . ' (' . $name . ')'; + } + } + + return $fields; +} + + +/** * Get a list of all the user reference fields out there */ @@ -203,3 +407,32 @@ return $variables; } + +function _fieldactions_nodereference_token_replacements($referred_node = NULL, $referring_node = NULL) { + global $user;//creator of referring node + $referred_node_owner = user_load(array('uid' => $referred_node->uid));//creator of referred node + + $variables = array( + '%site_name' => variable_get('site_name', 'Drupal'), + '%site_mail' => variable_get('site_mail', ini_get('sendmail_from')), + '%referred_node_owner_name' => $referred_node_owner->name, + '%referred_node_owner_mail' => $referred_node_owner->mail, + '%referred_node_owner_uid' => $referred_node_owner->uid, + '%referring_node_owner_name' => $user->name, + '%referring_node_owner_mail' => $user->mail, + '%referring_node_owner_uid' => $user->uid, + '%referred_node_url' => url('node/' . $referred_node->nid, NULL, NULL, TRUE), + '%referring_node_url' => url('node/' . $referring_node->nid, NULL, NULL, TRUE), + '%site_url' => url('', NULL, NULL, TRUE), + '%referred_node_type' => $referred_node->type, + '%referring_node_type' => $referring_node->type, + '%referred_node_title' => $referred_node->title, + '%referring_node_title' => $referring_node->title, + '%referred_node_teaser' => strip_tags($referred_node->teaser), + '%referring_node_teaser' => strip_tags($referring_node->teaser), + '%referred_node_body' => strip_tags($referred_node->body), + '%referring_node_body' => strip_tags($referring_node->body), + ); + + return $variables; +}