simplenews_action.module in Simplenews 6.2
Same filename and directory in other branches
Provide actions for simplenews.
File
simplenews_action/simplenews_action.moduleView source
<?php
/**
* @file
* Provide actions for simplenews.
*
* @ingroup simplenews
*/
/*
* Implementation of hook_action_info().
*/
function simplenews_action_info() {
return array(
'simplenews_send_newsletter_action' => array(
'description' => t('Send single simplenews newsletter'),
'type' => 'simplenews',
'configurable' => TRUE,
'hooks' => array(
'cron' => array(
'run',
),
'user' => array(
'insert',
),
'simplenews' => array(
'subscribe',
),
),
),
'simplenews_cron_action' => array(
'description' => t('Send pending simplenews newsletters'),
'type' => 'simplenews',
'configurable' => FALSE,
'hooks' => array(
'cron' => array(
'run',
),
),
),
'simplenews_subscribe_user_action' => array(
'type' => 'simplenews',
'description' => t('Subscribe the user to a newsletter'),
'configurable' => TRUE,
'hooks' => array(
'user' => array(
'insert',
'update',
),
),
),
'simplenews_unsubscribe_user_action' => array(
'type' => 'simplenews',
'description' => t('Unsubscribe the user from a newsletter'),
'configurable' => TRUE,
'hooks' => array(
'user' => array(
'update',
'delete',
),
),
),
);
}
/**
* Implementation of hook_action_info_alter().
*
* Makes user and system actions available to the Simplenews 'subscribe' and 'unsubscribe' triggers.
*/
function simplenews_action_info_alter(&$actions) {
foreach ($actions as $id => $action) {
if ($action['type'] == 'user' || $action['type'] == 'system') {
$actions[$id]['hooks']['simplenews'] = array(
'subscribe',
'unsubscribe',
);
}
}
}
/**
* A configurable Drupal action. Send a simplenews newsletter.
* hook = cron: Send a newsletter to all subscribers.
* hook = user: Send a newsletter to the user who triggered the action.
*
* Available context:
* $context['nid'] newsletter id
* $context['title'] newsletter title
* $context['resend'] allow resending of a previously send newsletter
*
* @see simplenews_send_newsletter_action_form()
* @see simplenews_send_newsletter_action_submit()
*/
function simplenews_send_newsletter_action(&$object, $context = array()) {
if ($context['hook'] == 'cron' || $context['hook'] == 'user' || $context['hook'] == 'simplenews') {
module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
// Determine newsletter recipients
$accounts = array();
if ($context['hook'] == 'user' || $context['hook'] == 'simplenews') {
$accounts[] = $context['account'];
}
// Get sent status of this newsletter. The global newsletter sent status is used not the sent status of individual emails.
$s_status = db_result(db_query("\n SELECT s_status\n FROM {simplenews_newsletters}\n WHERE nid = %d", $context['nid']));
// Send newsletter if resend is allowed OR newsletter is not yet send.
if ($context['resend'] or $s_status == SIMPLENEWS_STATUS_SEND_NOT) {
if ($context['hook'] == 'cron') {
// Set newsletter sent status to pending if send by cron.
db_query("\n UPDATE {simplenews_newsletters}\n SET s_status = %d\n WHERE nid = %d", SIMPLENEWS_STATUS_SEND_PENDING, $context['nid']);
}
simplenews_send_node($context['nid'], $accounts);
}
watchdog('action', 'Simplenews newsletter %title send.', array(
'%title' => $context['title'],
));
}
}
/**
* Implementation of a configurable Drupal action. Send newsletter
*/
function simplenews_send_newsletter_action_form($context) {
//TODO improve usability by adding a pre-selection of newsletters before the newsletter issue selection
// Requires AHAH function to select newsletters issues based on selected newsletter
//
// if (!isset($context['newsletter'])) {
// $context['newsletter'] = array();
// }
if (!isset($context['newsletter_issue'])) {
$context['newsletter_issue'] = array();
}
if (!isset($context['resend'])) {
$context['resend'] = FALSE;
}
$tree = taxonomy_get_tree(variable_get('simplenews_vid', ''));
$terms = array();
foreach ($tree as $newsletter) {
$terms[$newsletter->tid] = $newsletter->name;
}
// $form['newsletter'] = array(
// '#title' => t('Newsletter'),
// '#type' => 'select',
// '#options' => $terms,
// '#default_value' => $context['newsletter'],
// '#description' => t('The newsletter series'),
// );
$result = taxonomy_select_nodes(array_keys($terms));
$nids = array();
while ($node = db_fetch_object($result)) {
$nids[$node->nid] = $node->title;
}
$form['newsletter_issue'] = array(
'#title' => t('Newsletter issue'),
'#type' => 'select',
'#options' => $nids,
'#default_value' => $context['newsletter_issue'],
'#description' => t('The newsletter issue to send'),
);
$form['resend'] = array(
'#title' => t('Allow to resend newsletter'),
'#type' => 'checkbox',
'#default_value' => $context['resend'],
'#description' => t('When checked a newsletter will be send even when already sent before. The newsletter sent status is checked, not the status of individual email addresses. Use this for repeated (e.g. monthly) newsletters.'),
);
return $form;
}
/**
* Validate simplenews_send_newsletter_action form submissions.
*/
function simplenews_send_newsletter_action_validate($form, $form_state) {
$form_values = $form_state['values'];
// Validate the send newsletter form.
if (empty($form_values['newsletter_issue'])) {
form_set_error('newsletter_issue', t('Please select a newsletter issue.'));
}
}
/**
* Process simplenews_send_newsletter_action form submissions.
*/
function simplenews_send_newsletter_action_submit($form, $form_state) {
$form_values = $form_state['values'];
$params = array(
'nid' => $form_values['newsletter_issue'],
'title' => $form['newsletter_issue']['#options'][$form_values['newsletter_issue']],
'resend' => $form_values['resend'],
);
return $params;
}
/**
* Implementation of a Drupal action. Send pending simplenews newsletters.
*/
function simplenews_cron_action(&$object, $context = array()) {
simplenews_cron();
watchdog('action', 'Simplenews cron executed.');
}
/**
* A configurable Drupal action. Subscribe the user to a newsletter
* hook = user: Subscribe this user to selected newsletter
*
* Available context:
* $context['tid'] newsletter tid
* $context['name'] newsletter name
*
* @see simplenews_subscribe_user_action_form()
* @see simplenews_subscribe_user_action_submit()
*/
function simplenews_subscribe_user_action(&$object, $context = array()) {
if ($context['hook'] == 'user') {
if (isset($context['tid'])) {
// This action is only called in the context of user. User data is in $context.
$account = $context['account'];
simplenews_subscribe_user($account->mail, $context['tid'], FALSE, 'action');
drupal_set_message(t('You have been subscribed to newsletter %newsletter.', array(
'%newsletter' => $context['name'],
)));
watchdog('action', 'User %name subscribed to newsletter %newsletter.', array(
'%name' => $account->name,
'%newsletter' => $context['name'],
));
}
}
}
/**
* Implementation of a configurable Drupal action.
*/
function simplenews_subscribe_user_action_form($context) {
$tree = taxonomy_get_tree(variable_get('simplenews_vid', ''));
$terms = array();
foreach ($tree as $newsletter) {
$terms[$newsletter->tid] = $newsletter->name;
}
$form['tid'] = array(
'#title' => t('Newsletter'),
'#type' => 'select',
'#options' => $terms,
'#default_value' => isset($context['tid']) ? $context['tid'] : key($terms),
'#description' => t('The newsletter series the user will be subscribed to.'),
);
return $form;
}
/**
* Process simplenews_subscribe_user_action form submissions.
*/
function simplenews_subscribe_user_action_submit($form, $form_state) {
$form_values = $form_state['values'];
$params = array(
'tid' => $form_values['tid'],
'name' => $form['tid']['#options'][$form_values['tid']],
);
return $params;
}
/**
* A configurable Drupal action. Unsubscribe the user from a newsletter
* hook = user: Unsubscribe this user from selected newsletter
*
* Available context:
* $context['tid'] newsletter tid
* $context['name'] newsletter name
*
* @see simplenews_unsubscribe_user_action_form()
* @see simplenews_unsubscribe_user_action_submit()
*/
function simplenews_unsubscribe_user_action(&$object, $context = array()) {
if ($context['hook'] == 'user') {
if (isset($context['tid'])) {
// This action is only called in the context of user. User data is in $context.
$account = $context['account'];
//TODO: Unsubscribing should be done by simplenews_unsubscribe_user but simplenews_get_user_subscription fails because the user is already removed
if ($result = db_fetch_object(db_query("\n SELECT snid\n FROM {simplenews_subscriptions}\n WHERE mail = '%s'", $account->mail))) {
db_query("\n UPDATE {simplenews_snid_tid}\n SET status = %d,\n timestamp = %d,\n source = '%s'\n WHERE snid = %d\n AND tid = %d", SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, time(), t('Action'), $result->snid, $context['tid']);
}
drupal_set_message(t('You have been removed from the %newsletter subscription list.', array(
'%newsletter' => $context['name'],
)));
watchdog('action', 'User %name unsubscribed from newsletter %newsletter.', array(
'%name' => $account->name,
'%newsletter' => $context['name'],
));
}
}
}
/**
* Implementation of a configurable Drupal action.
*/
function simplenews_unsubscribe_user_action_form($context) {
$tree = taxonomy_get_tree(variable_get('simplenews_vid', ''));
$terms = array();
foreach ($tree as $newsletter) {
$terms[$newsletter->tid] = $newsletter->name;
}
$form['tid'] = array(
'#title' => t('Newsletter'),
'#type' => 'select',
'#options' => $terms,
'#default_value' => isset($context['tid']) ? $context['tid'] : key($terms),
'#description' => t('The newsletter series the user will be unsubscribed from.'),
);
return $form;
}
/**
* Process simplenews_unsubscribe_user_action form submissions.
*/
function simplenews_unsubscribe_user_action_submit($form, $form_state) {
$form_values = $form_state['values'];
$params = array(
'tid' => $form_values['tid'],
'name' => $form['tid']['#options'][$form_values['tid']],
);
return $params;
}
/**
* Implementation of hook_hook_info().
*/
function simplenews_hook_info() {
return array(
'simplenews' => array(
'simplenews' => array(
'subscribe' => array(
'runs when' => t('After a user has been subscribed'),
),
'unsubscribe' => array(
'runs when' => t('After a user has been unsubscribed'),
),
),
),
);
}
/**
* Implementation of hook_taxonomy().
*
* Deletes actions related to newsletter when it is deleted (term deleted).
*/
function simplenews_action_taxonomy($op, $type, $term = NULL) {
if ($op == 'delete' && $term['vid'] == variable_get('simplenews_vid', '')) {
switch ($type) {
case 'term':
$result = db_query("\n SELECT aid, parameters FROM {actions}\n WHERE callback IN ('simplenews_subscribe_user_action', 'simplenews_unsubscribe_user_action')\n ");
while ($action = db_fetch_object($result)) {
$parameters = unserialize($action->parameters);
if ($parameters['tid'] == $term['tid']) {
actions_delete($action->aid);
}
}
break;
}
}
}
/**
* Implementation of hook_nodeapi().
*
* Deletes actions related to newsletter issue when it is deleted (node deleted).
*/
function simplenews_action_nodeapi(&$node, $op, $teaser, $page) {
// Operate only on node types set in 'simplenews_content_types' variable.
if (!in_array($node->type, variable_get('simplenews_content_types', array(
'simplenews',
)))) {
return;
}
switch ($op) {
case 'delete':
$result = db_query("SELECT aid, parameters FROM {actions} WHERE callback='simplenews_send_newsletter_action'");
while ($action = db_fetch_object($result)) {
$parameters = unserialize($action->parameters);
if ($parameters['nid'] == $node->nid) {
actions_delete($action->aid);
}
}
break;
}
}
Functions
Name | Description |
---|---|
simplenews_action_info | |
simplenews_action_info_alter | Implementation of hook_action_info_alter(). |
simplenews_action_nodeapi | Implementation of hook_nodeapi(). |
simplenews_action_taxonomy | Implementation of hook_taxonomy(). |
simplenews_cron_action | Implementation of a Drupal action. Send pending simplenews newsletters. |
simplenews_hook_info | Implementation of hook_hook_info(). |
simplenews_send_newsletter_action | A configurable Drupal action. Send a simplenews newsletter. hook = cron: Send a newsletter to all subscribers. hook = user: Send a newsletter to the user who triggered the action. |
simplenews_send_newsletter_action_form | Implementation of a configurable Drupal action. Send newsletter |
simplenews_send_newsletter_action_submit | Process simplenews_send_newsletter_action form submissions. |
simplenews_send_newsletter_action_validate | Validate simplenews_send_newsletter_action form submissions. |
simplenews_subscribe_user_action | A configurable Drupal action. Subscribe the user to a newsletter hook = user: Subscribe this user to selected newsletter |
simplenews_subscribe_user_action_form | Implementation of a configurable Drupal action. |
simplenews_subscribe_user_action_submit | Process simplenews_subscribe_user_action form submissions. |
simplenews_unsubscribe_user_action | A configurable Drupal action. Unsubscribe the user from a newsletter hook = user: Unsubscribe this user from selected newsletter |
simplenews_unsubscribe_user_action_form | Implementation of a configurable Drupal action. |
simplenews_unsubscribe_user_action_submit | Process simplenews_unsubscribe_user_action form submissions. |