diff --git simplenews.module simplenews.module old mode 100644 new mode 100755 diff --git simplenews_rules/simplenews_rules.info simplenews_rules/simplenews_rules.info new file mode 100755 index 0000000..4be5d61 --- /dev/null +++ simplenews_rules/simplenews_rules.info @@ -0,0 +1,7 @@ +; $Id$ +name = Simplenews rules +description = Provides integration with Rules module for Simplenews. +dependencies[] = simplenews +dependencies[] = rules +package = Mail +core = 6.x diff --git simplenews_rules/simplenews_rules.module simplenews_rules/simplenews_rules.module new file mode 100755 index 0000000..963ac82 --- /dev/null +++ simplenews_rules/simplenews_rules.module @@ -0,0 +1,37 @@ + 'noreply@example.org', 'formatted' => 'site name ') + * [newsletter] : newsletter object (tid, name) + * [tokens] : tokens for variable replacement. Defaults to: user_mail_tokens() + */ +function simplenews_rules_mail($key, &$message, $params) { + module_load_include('inc', 'simplenews', 'includes/simplenews.mail'); + $context = $params['context']; + + switch ($key) { + case 'mail_to_subscribed_users': + $subject = token_replace($params['subject'], 'all', $context); + $subject = str_replace(array("\r", "\n"), '', $subject); + $body = token_replace($params['message'], 'all', $context); + $message['subject'] .= $subject; + $message['body'][] = $body; + break; + } +} \ No newline at end of file diff --git simplenews_rules/simplenews_rules.rules.inc simplenews_rules/simplenews_rules.rules.inc new file mode 100755 index 0000000..28b41fc --- /dev/null +++ simplenews_rules/simplenews_rules.rules.inc @@ -0,0 +1,228 @@ + array( + 'label' => t('Subscribe an e-mail adress to a newsletter'), + 'module' => 'Simplenews', + 'eval input' => array('email', 'newsletter', 'advanced_options|tids', 'confirmation'), + ), + 'simplenews_action_subscribe_user' => array( + 'label' => t('Subscribe an User to a newsletter'), + 'arguments' => array( + 'user' => array('type' => 'user', 'label' => t('User')), + ), + 'module' => 'Simplenews', + 'eval input' => array('newsletter', 'advanced_options|tids', 'confirmation'), + ), + 'simplenews_action_unsubscribe_email' => array( + 'label' => t('Unsubscribe an e-mail adress'), + 'module' => 'Simplenews', + 'eval input' => array('email', 'newsletter', 'advanced_options|tids', 'confirmation'), + ), + 'simplenews_action_unsubscribe_user' => array( + 'label' => t('Unsubscribe an User'), + 'arguments' => array( + 'user' => array('type' => 'user', 'label' => t('User')), + ), + 'module' => 'Simplenews', + 'eval input' => array('newsletter', 'advanced_options|tids', 'confirmation'), + ), + 'simplenews_action_mail_to_subscribed_users' => array( + 'label' => t('Send a mail to subscribed users'), + 'module' => 'Simplenews', + 'eval input' => array('subject', 'message', 'from') + ) + ); +} + +/******************************************************************************* + * Callback Functions, Forms, and Tables + ******************************************************************************/ + +/** + * Action Implementation: Subscribe an e-mail adress to a Simplenews newsletter + */ +function simplenews_action_subscribe_email($settings) { + //validate settings + $settings = simplenews_rules_action_validate($settings); + //set newsletters + $tids = $settings['newsletter']; + if (!empty($settings['email']) && !empty($tids)) { + foreach ($settings['email'] as $email) { + foreach ($tids as $tid) { + $status = simplenews_subscribe_user($email, $tid, $settings['confirmation']); + } + } + } + if (!empty($status)) { + $newsletter_all = simplenews_rules_action_get_newsletters(); + foreach ($tids as $tid) { + $newsletters[] = $newsletter_all[$tid]; + } + watchdog('rules', 'E-mail %email subscribed to newsletter %newsletter.', array('%email' => implode(', ', $settings['email']), '%newsletter' => implode(', ', $newsletters))); + } +} + +/** + * Action Implementation: Subscribe an User to a Simplenews newsletter + */ +function simplenews_action_subscribe_user($user, $settings) { + //validate settings + $settings = simplenews_rules_action_validate($settings); + //set newsletters + $tids = $settings['newsletter']; + if ($user->mail && !empty($tids)) { + foreach ($tids as $tid) { + $status = simplenews_subscribe_user($user->mail, $tid, $settings['confirmation']); + } + } + if (!empty($status)) { + $newsletter_all = simplenews_rules_action_get_newsletters(); + foreach ($tids as $tid) { + $newsletters[] = $newsletter_all[$tid]; + } + watchdog('rules', 'E-mail %email subscribed to newsletter %newsletter.', array('%email' => $user->mail, '%newsletter' => implode(', ', $newsletters))); + } +} + +/** + * Action Implementation: Subscribe an e-mail adress from a Simplenews newsletter + */ +function simplenews_action_unsubscribe_email($settings) { + //validate settings + $settings = simplenews_rules_action_validate($settings); + //set newsletters + $tids = $settings['newsletter']; + if (!empty($settings['email']) && !empty($tids)) { + foreach ($settings['email'] as $email) { + foreach ($tids as $tid) { + $status = simplenews_unsubscribe_user($email, $tid, $settings['confirmation']); + } + } + } + if (!empty($status)) { + $newsletter_all = simplenews_rules_action_get_newsletters(); + foreach ($tids as $tid) { + $newsletters[] = $newsletter_all[$tid]; + } + watchdog('rules', 'E-mail %email unsubscribed from newsletter %newsletter.', array('%email' => implode(', ', $settings['email']), '%newsletter' => implode(', ', $newsletters))); + } +} + +/** + * Action Implementation: Unsubscribe an User from a Simplenews newsletter + */ +function simplenews_action_unsubscribe_user($user, $settings) { + //validate settings + $settings = simplenews_rules_action_validate($settings); + //set newsletters + $tids = $settings['newsletter']; + if ($user->mail && !empty($tids)) { + foreach ($tids as $tid) { + $status = simplenews_unsubscribe_user($user->mail, $tid, $settings['confirmation']); + } + } + if (!empty($status)) { + $newsletter_all = simplenews_rules_action_get_newsletters(); + foreach ($tids as $tid) { + $newsletters[] = $newsletter_all[$tid]; + } + watchdog('rules', 'E-mail %email unsubscribed from newsletter %newsletter.', array('%email' => $user->mail, '%newsletter' => implode(', ', $newsletters))); + } +} + +/** + * Action: Send mail to all users of a specific role group(s). + */ +function simplenews_action_mail_to_subscribed_users($settings) { + $recipients = $settings['recipients']; + $from = ($settings['from']) ? str_replace(array("\r", "\n"), '', $settings['from']) : NULL; + $settings['context'] = array(); + $message = array('result' => TRUE); + foreach ($recipients as $tid) { + // Get email address of all accounts subscribed to this newsletter. + $result = db_query('SELECT s.mail, s.snid, s.uid FROM {simplenews_subscriptions} s INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid WHERE s.activated = %d AND t.tid = %d', 1, $tid); + while (($account = db_fetch_object($result)) && $message['result']) { + $newsletter_object = (object) array('tid' => $tid); + $settings['context']['newsletter'] = $newsletter_object; + + $settings['context']['snid'] = $account->snid; + if ($account->uid) { + $context_account = user_load($account->uid); + } + else { + // in case the user is anonymous set the mail manually + $context_account = drupal_anonymous_user(); + $context_account->mail = $account->mail; + } + // set the subscription id + $context_account->snid = $account->snid; + $settings['context']['account'] = $context_account; + $message = drupal_mail('simplenews', 'mail_to_subscribed_users', $account->mail, language_default(), $settings, $from); + } + } + if ($message['result']) { + $tree = taxonomy_get_tree(variable_get('simplenews_vid', '')); + $newsletters = array(); + foreach ($tree as $newsletter) { + $newsletters[$newsletter->tid] = $newsletter->name; + } + $newsletters = array_intersect_key($newsletters, drupal_map_assoc($recipients)); + watchdog('rules', 'Successfully sent email to the users subscribed to newsletter(s) %newsletters.', array('%newsletters' => implode(', ', $newsletters))); + } +} + +/******************************************************************************* + * Module and Helper Functions + ******************************************************************************/ + +/** + * Helper function: Load all available newsletters. + */ +function simplenews_rules_action_get_newsletters() { + $tree = taxonomy_get_tree(variable_get('simplenews_vid', '')); + $terms = array(); + foreach ($tree as $newsletter) { + $terms[$newsletter->tid] = $newsletter->name; + } + return $terms; +} + +/* + * Helper function for validating form settings on execution time, so that tokens/input evaluators apply + */ +function simplenews_rules_action_validate($settings) { + $settings['email'] = empty($settings['email']) ? array() : explode(',', $settings['email']); + foreach ($settings['email'] as $key => $address) { + if (valid_email_address($address)) { + $settings['email'][$key] = str_replace(array("\r", "\n"), '', trim($address)); + } + } + if ($settings['advanced_options']['tids']) { + $tids = explode(',', $settings['advanced_options']['tids']); + foreach ($tids as $tid) { + $newsletter_tids[] = str_replace(array("\r", "\n"), '', trim($tid)); + } + } + $settings['newsletter'] = array_filter($settings['newsletter']); + //overwrite with tid values if given + $settings['newsletter'] = $newsletter_tids ? $newsletter_tids : $settings['newsletter']; + return $settings; +} + +/** + * @} + */ diff --git simplenews_rules/simplenews_rules.rules_forms.inc simplenews_rules/simplenews_rules.rules_forms.inc new file mode 100755 index 0000000..515ec8c --- /dev/null +++ simplenews_rules/simplenews_rules.rules_forms.inc @@ -0,0 +1,114 @@ + array(), 'advanced_options' => array('tids' => ''), 'confirmation' => ''); + $form['settings']['newsletter'] = array( + '#type' => 'checkboxes', + '#title' => t('Simplenews newsletter'), + '#options' => simplenews_rules_action_get_newsletters(), + '#default_value' => $settings['newsletter'], + '#description' => t("The Simplenews newsletter series the e-mail will be subscribed to or unsubscribed from."), + '#required' => TRUE, + '#weight' => -2, + ); + // Advanced: PHP code. + $form['settings']['advanced_options'] = array( + '#type' => 'fieldset', + '#title' => t('Advanced: Specify the newsletter IDs manually'), + '#collapsible' => TRUE, + '#collapsed' => empty($settings['advanced_options']['tids']), + '#weight' => -1, + ); + $form['settings']['advanced_options']['tids'] = array( + '#type' => 'textarea', + '#title' => t('Advanced: Newsletter Term IDs'), + '#default_value' => $settings['advanced_options']['tids'], + '#description' => t("Here you can specify the Newsletters as Term IDs. You may separate multiple entries with ','. Above checkbox entries will be overwritten!"), + '#weight' => -1, + //'#tree' => FALSE, + ); + $form['settings']['confirmation'] = array( + '#type' => 'checkbox', + '#title' => t('Send Simplenews confirmation mail'), + '#default_value' => $settings['confirmation'], + '#description' => t("Check this box to send the Simplenews confirmation e-mail. The User receives an email to verify the address and complete the subscription/unsubscription."), + '#required' => FALSE, + '#weight' => 0, + ); +} + +/** + * Action "Subscribe an e-mail adress to a Simplenews newsletter" configuration form + */ +function simplenews_action_subscribe_email_form($settings = array(), &$form) { + $settings += array('email' => ''); + $form['settings']['email'] = array( + '#type' => 'textfield', + '#title' => t('E-mail address'), + '#default_value' => $settings['email'], + '#description' => t("The e-mail addresses you want to subscribe or unsubscribe. You may separate multiple addresses with ','."), + '#required' => TRUE, + '#weight' => -5, + ); + //add rest of the form + simplenews_action_subscribe_user_form($settings, $form); +} + +/** + * Action "Subscribe an User to a Simplenews newsletter" configuration form + */ +function simplenews_action_unsubscribe_user_form($settings = array(), &$form) { + //we have the same forms for the unsubscribe actions + simplenews_action_subscribe_user_form($settings, $form); +} +/** + * Action "Subscribe an User to a Simplenews newsletter" configuration form + */ +function simplenews_action_unsubscribe_email_form($settings = array(), &$form) { + //we have the same forms for the unsubscribe actions + simplenews_action_subscribe_email_form($settings, $form); +} + +/** + * Action "Send a mail to users of a role" configuration form + */ +function simplenews_action_mail_to_subscribed_users_form($settings = array(), &$form) { + $options = array(); + $tree = taxonomy_get_tree(variable_get('simplenews_vid', '')); + + foreach ($tree as $newsletter) { + $options[$newsletter->tid] = $newsletter->name; + } + + $form['settings']['recipients'] = array( + '#type' => 'checkboxes', + '#title' => t('Recipient newsletters'), + '#prefix' => t('WARNING: This may cause problems if there are too many users of these roles on your site, as your server may not be able to handle all the mail requests all at once.'), + '#description' => t('Select the newsletters whose users should receive this email.'), + '#default_value' => (isset($settings['recipients'])) ? $settings['recipients'] : array(), + '#options' => $options, + '#required' => TRUE + ); + + rules_action_mail_to_user_form($settings, $form); +} + +function simplenews_action_mail_to_subscribed_users_submit(&$settings, $form, $form_state) { + $settings['recipients'] = array_filter(array_keys(array_filter($settings['recipients']))); +} + +/** + * @} + */