Index: simplenews_action.module =================================================================== RCS file: /cvs/drupal/contributions/modules/simplenews/simplenews_action/simplenews_action.module,v retrieving revision 1.4 diff -u -r1.4 simplenews_action.module --- simplenews_action.module 4 Aug 2008 06:10:21 -0000 1.4 +++ simplenews_action.module 15 Dec 2008 15:21:53 -0000 @@ -47,6 +47,14 @@ 'user' => array('update', 'delete'), ), ), + 'simplenews_send_email_action' => array( + 'description' => t('Send email upon subscription'), + 'type' => 'simplenews', + 'configurable' => TRUE, + 'hooks' => array( + 'simplenews' => array('subscribe', 'unsubscribe'), + ), + ), ); } @@ -296,6 +304,87 @@ } /** + * A configurable Drupal action. Send an email when someone subscribes or unsubscribes from a newsletter. + * + * Available context: + * $context['recipient'] email recipient + * + * @see simplenews_send_email_action_form() + * @see simplenews_send_email_action_submit() + */ +function simplenews_send_email_action($object, $context) { + if (in_array($context['tid'], $context['newsletter'])) { + $params['from'] = _simplenews_set_from(); + $params['newsletter'] = taxonomy_get_term( $context['tid'] ); + $params['context']['account'] = $object; + + drupal_mail('simplenews', 'action_send_email', $context['recipient'], $object->language, $params, $params['from']['address']); + } +} + +/** + * Implementation of a configurable Drupal action. + */ +function simplenews_send_email_action_form($context) { + // Set default values for form. + if (!isset($context['recipient'])) { + $context['recipient'] = ''; + } + + $form['recipient'] = array( + '#type' => 'textfield', + '#title' => t('Recipient'), + '#default_value' => $context['recipient'], + '#maxlength' => '254', + '#description' => t('The email address to which the message should be sent.'), + ); + + if (!isset($context['newsletter'])) { + $context['newsletter'] = array(); + } + + $tree = taxonomy_get_tree(variable_get('simplenews_vid', '')); + $terms = array(); + foreach ($tree as $newsletter) { + $terms[$newsletter->tid] = check_plain($newsletter->name); + } + $form['newsletter'] = array( + '#title' => t('Newsletter'), + '#type' => 'select', + '#multiple' => 'true', + '#options' => $terms, + '#description' => t('The newsletter series (select as many as you like) of which the recipient will be alerted of subscriptions or unsubscriptions.'), + ); + + return $form; +} + +/** + * Process simplenews_send_email_action form submissions. + */ +function simplenews_send_email_action_submit($form, $form_state) { + $form_values = $form_state['values']; + + $params = array( + 'recipient' => $form_values['recipient'], + 'newsletter' => $form_values['newsletter'], + ); + return $params; +} + +/** + * Validate simplenews_send_email_action form submissions. + */ +function simplenews_send_email_action_validate($form, $form_state) { + $form_values = $form_state['values']; + // Validate the configuration form. + if (!valid_email_address($form_values['recipient'])) { + form_set_error('recipient', t('Please enter a valid email address.')); + } +} + + +/** * Implementation of hook_hook_info(). */ function simplenews_hook_info() {