notifications_anonymous.module in Notifications 6.4
Notifications for anonymous users
File
notifications_anonymous/notifications_anonymous.moduleView source
<?php
/**
* @file
* Notifications for anonymous users
*/
/**
* Implementation of hook_help()
*/
function notifications_anonymous_help($path, $arg) {
switch ($path) {
case 'admin/messaging/notifications/subscriptions/anonymous':
$output = '<p>' . t('These are specific settings for anonymous users. Not all send methods or send intervals available for registered users may be suitable for anonymous users.') . '</p>';
return $output;
}
}
/**
* Implementation of hook_menu()
*/
function notifications_anonymous_menu() {
// Subscribe links. For this items access will be checked later in the page
$items['notifications/anonymous/subscribe'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'notifications_anonymous_page_subscribe',
'access callback' => 'notifications_anonymous_subscription_access',
'access arguments' => array(
'create',
),
'file' => 'notifications_anonymous.pages.inc',
);
// Present form to request subscriptions
$items['notifications/anonymous/request'] = array(
'title' => 'Manage subscriptions',
'page callback' => 'notifications_anonymous_request_page',
'access callback' => TRUE,
'file' => 'notifications_anonymous.pages.inc',
'type' => MENU_CALLBACK,
);
// Destination operations
foreach (array(
'edit',
'delete',
'manage',
) as $op) {
$items['notifications/anonymous/destination/%messaging_destination/' . $op] = array(
'title' => 'Edit destination',
'page callback' => 'notifications_anonymous_destination_page',
'page arguments' => array(
3,
4,
),
'access callback' => 'notifications_anonymous_destination_access',
'access arguments' => array(
4,
3,
),
'file' => 'notifications_anonymous.pages.inc',
'type' => MENU_CALLBACK,
);
}
// Subscription operations
foreach (array(
'edit',
'unsubscribe',
) as $op) {
$items['notifications/anonymous/subscription/%notifications_subscription/' . $op] = array(
'title' => 'Edit subscription',
'page callback' => 'notifications_anonymous_subscription_page',
'page arguments' => array(
3,
4,
),
'access callback' => 'notifications_anonymous_subscription_access',
'access arguments' => array(
4,
3,
),
'file' => 'notifications_anonymous.pages.inc',
'type' => MENU_CALLBACK,
);
}
// Admin settings page
$items['admin/messaging/notifications/subscriptions/anonymous'] = array(
'title' => 'Anonymous',
'description' => 'Settings for anonymous subscriptions',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'notifications_anonymous_admin_settings_form',
),
'access arguments' => array(
'administer notifications',
),
'type' => MENU_LOCAL_TASK,
'file' => 'notifications_anonymous.pages.inc',
);
return $items;
}
/**
* Menu access callback for subscriptions
*/
function notifications_anonymous_subscription_access($op = 'create', $subscription = NULL, $account = NULL) {
global $user;
if ((user_access('addminister notifications') || !$user->uid) && (!$account || !$account->uid)) {
switch ($op) {
case 'create':
return (bool) notifications_anonymous_send_methods();
case 'unsubscribe':
return $subscription && !$subscription->uid;
}
}
}
/**
* Get subscribe / unsubscribe link for subscription
*/
function notifications_anonymous_subscription_get_link($type, $subscription, $options = array()) {
return notifications_build_link($type, $options, 'subscription', $subscription);
}
/**
* Menu access callback for destinations
*/
function notifications_anonymous_destination_access($op, $destination = NULL, $account = NULL) {
global $user;
$account = $account ? $account : $user;
// Access will be granted if anonymous destination or administrator
if (user_access('administer notifications')) {
return TRUE;
}
elseif (!$destination->uid) {
switch ($op) {
case 'manage':
return user_access('manage own subscriptions', $account);
case 'edit':
return user_access('maintain own subscriptions', $account);
case 'confirm':
case 'delete':
return TRUE;
}
}
else {
return FALSE;
}
}
/**
* Get sending method for destination
*
* This is possible because for anonymous users we have just one send method for address type
*/
function notifications_anonymous_destination_method($destination) {
$address_type = is_object($destination) ? $destination->type : $destination;
$allowed_methods = notifications_anonymous_send_methods();
foreach (messaging_send_methods() as $method => $info) {
if (isset($allowed_methods[$method]) && isset($info['address_type']) && $address_type == $info['address_type']) {
return $method;
}
}
}
/**
* Get available sending methods for anonymous users
*/
function notifications_anonymous_send_methods() {
$list =& messaging_static(__FUNCTION__);
if (!isset($list)) {
$list = array();
$allowed = variable_get('notifications_anonymous_send_methods', array(
'mail',
));
foreach (messaging_method_list() as $method => $name) {
if (in_array($method, $allowed)) {
$list[$method] = $name;
}
}
}
return $list;
}
/**
* Get available send intervals for anonymous users
*/
function notifications_anonymous_send_intervals() {
$list =& messaging_static(__FUNCTION__);
if (!isset($list)) {
$list = array();
$allowed = variable_get('notifications_anonymous_send_intervals', array(
0,
));
foreach (notifications_send_intervals() as $interval => $name) {
if (in_array($interval, $allowed)) {
$list[$interval] = $name;
}
}
}
return $list;
}
/**
* Build subscription options as an array of links
*
* These links can be added as node link elements or rendered some other way
*
* @param $subscription_types
* Array of subscription objects, either actual subscriptions or subscription templates
* @param $prefix
* Prefix to use for the link indexes
*/
function notifications_anonymous_subscribe_links($type, $object) {
global $user;
notifications_include('object.inc');
// For subscribe, we need confirmation form always, as we need destination address
$subscribe = array();
$unsubscribe = array(
'type' => 'subscription',
'signed' => TRUE,
);
return notifications_object_subscribe_links($type, $object, $user, $subscribe, $unsubscribe);
}
/**
* Implementation of hook_forms()
*/
function notifications_anonymous_forms() {
$forms['notifications_anonymous_subscription_add_form']['callback'] = 'notifications_anonymous_subscription_form';
$forms['notifications_anonymous_subscription_edit_form']['callback'] = 'notifications_anonymous_subscription_form';
$forms['notifications_anonymous_subscription_confirm_form']['callback'] = 'notifications_anonymous_subscription_form';
return $forms;
}
/**
* Anonymous Subscription form builder. Adds destination subform.
*/
function notifications_anonymous_subscription_form(&$form_state, $subscription, $destination_path = '') {
$account = drupal_anonymous_user();
$form = notifications_subscription_form($form_state, $subscription, $destination_path);
$form = notifications_subscription_destination_subform($form, $account, NULL, NULL, 20);
return $form;
}
/**
* Get raw /formatted links to manage subscription / destination
*
* @param $format
* Format to return the links
* - 'link', Full built link
* - 'url', Plain url
*/
function notifications_anonymous_manage_links($type, $object, $format = 'array') {
$links = array();
$params = array(
'absolute' => TRUE,
'signed' => TRUE,
);
if ($type == 'destination') {
$params['oid'] = $params['mdid'] = $object->mdid;
$link_types = array(
'manage' => t('Manage'),
'edit' => t('Edit'),
'delete' => t('Delete'),
);
}
elseif ($type == 'subscription') {
$params['oid'] = $params['sid'] = $object->sid;
$link_types = array(
'unsubscribe' => t('Unsubscribe'),
'edit' => t('Edit'),
);
}
foreach ($link_types as $op => $name) {
if (_notifications_anonymous_access($type, $object, $op)) {
$params['title'] = $name;
$links[$op] = notifications_build_link($op, $params, $type, $object, $format);
}
}
return $links;
}
/**
* Check for object access
*/
function _notifications_anonymous_access($type, $object, $op) {
if (function_exists($function = 'notifications_anonymous_' . $type . '_access')) {
return $function($op, $object);
}
}
/**
* Create signed URLs for all pages
*/
function notifications_anonymous_url($url, $options = array()) {
$elements = explode('/', $url);
$options += array(
'absolute' => TRUE,
);
$options['query']['signature'] = _notifications_signature($elements);
return url($url, $options);
}
/**
* Implementation of hook_notifications_subscription()
*/
function notifications_anonymous_notifications_subscription($op, $subscription = NULL, $account = NULL) {
global $user;
switch ($op) {
case 'access':
if (!$user->uid) {
return notifications_anonymous_flood_control();
}
break;
}
}
/**
* Check flood control, optionally printing an error message
*/
function notifications_anonymous_flood_control($warning = FALSE) {
// Check flood control
$threshold = variable_get('notifications_anonymous_hourly_threshold', 3);
if (flood_is_allowed('notifications_anonymous', $threshold)) {
return TRUE;
}
elseif ($warning) {
drupal_set_message(t("You cannot create more than %number subscriptions per hour. Please try again later.", array(
'%number' => $threshold,
)), 'warning');
}
return FALSE;
}
/**
* Implementation of hook_messaging()
*
* This hook provides information about the mensaje templates this module uses and related tokens.
*
* Depending on $op, this hook takes different parameters and returns different pieces of information:
*
* - 'message groups'
* Get array of message groups, each of which will have one or more keys for different templates
* Each group should have a unique key, so it should start with the module name
* - 'message keys'
* Get message template parts for a given group ($arg1)
* Return array of key => name for each part
* - 'messages'
* Get default message templates for a given group ($arg1).
* It should return default texts, indexed by message key that will be the default templates
* These templates may be edited on the 'Messaging templates' page
* - 'tokens'
* Get available tokens for a given message group and key ($arg1).
* Return array of token keys that will be available for this message templates
* The tokens themselves may be default tokens (provided by token module) or we can add new
* tokens implementing hook_token_list() and hook_token_value()
*
* @param $op
* Operation, type of information to retrieve
* @param $arg1, $arg2...
* Different parameters depending on $op
*/
function notifications_anonymous_messaging($op, $type = 'all', $language = NULL) {
switch ($op) {
case 'message types':
$info['notifications-anonymous'] = array(
'name' => t('Anonymous Notifications'),
'description' => t('Confirmation messages for anonymous users'),
);
return $info;
case 'message groups':
// Generic notifications event
$info['notifications-anonymous-subscribe'] = array(
'module' => 'notifications_anonymous',
'name' => t('Confirm subscription'),
'description' => t('Common parts for all Notifications messages for a single event. This is useful for defining a common header and/or footer for all these messages.'),
);
$info['notifications-anonymous-unsubscribe'] = array(
'module' => 'notifications_anonymous',
'name' => t('Cancel subscription'),
'description' => t('Depending on your settings for each Send interval, Notifications may be digested, this is grouped and summarized in a single message. These are the common parts for Notifications digests.'),
);
return $info;
case 'message keys':
switch ($type) {
case 'notifications-anonymous':
return array(
'subject' => t('Subject'),
'footer' => t('Footer'),
);
case 'notifications-anonymous-subscribe':
case 'notifications-anonymous-unsubscribe':
case 'notifications-anonymous-manage':
// Anonymous confirmation mails
return array(
'subject' => t('Subject'),
'header' => t('Header'),
'main' => t('Content'),
'footer' => t('Footer'),
);
}
break;
case 'messages':
switch ($type) {
case 'notifications-anonymous':
return array(
'subject' => t('Message from [site-name]', array(), $language->language),
'footer' => array(
t('This is an automatic message from [site-name]', array(), $language->language),
t('To manage your subscriptions, browse to [destination-manage-url]', array(), $language->language),
t('You can unsubscribe at [destination-unsubscribe-url]', array(), $language->language),
),
);
// Subscribe confirmation
case 'notifications-anonymous-subscribe':
return array(
'subject' => t('Confirm your subscription to [site-name]', array(), $language->language),
'header' => t("Someone has subscribed to [site-name] using [destination-address],", array(), $language->language),
'main' => t("To confirm your subscription, browse to [subscription-confirm]", array(), $language->language),
);
// Unsubscribe all message for a destination
case 'notifications-anonymous-unsubscribe':
return array(
'subject' => t('Cancel subscription to [site-name]', array(), $language->language),
'header' => t('Someone has requested to cancel all subscriptions for [destination-address]', array(), $language->language),
'main' => array(
t("To cancel your subscription, browse to [subscription-unsubscribe-url]", array(), $language->language),
t('To edit your subscription, browse to [subscription-edit-url]', array(), $language->language),
),
);
case 'notifications-anonymous-manage':
return array(
'subject' => t('Edit your subscriptions to [site-name]', array(), $language->language),
'header' => t('Someone has requested to change the subscriptions for [destination-address]', array(), $language->language),
'main' => array(
t('To edit your subscriptions, browse to [destination-manage-url]', array(), $language->language),
t("To cancel all your subscriptions, browse to [destination-unsubscribe-url]", array(), $language->language),
),
);
}
break;
case 'tokens':
$tokens = array();
// These are the token groups that will be used for this module's messages
if (strpos($type, 'notifications-anonymous') === 0) {
$tokens = array(
'subscription',
'destination',
);
}
return $tokens;
}
}
/**
* Implementation of hook_token_values()
*
* Provide user-like tokens for anonymous users
*/
function notifications_anonymous_token_values($type, $object = NULL, $options = array()) {
switch ($type) {
case 'destination':
$values = array();
// Just for anonymous destinations, for the others the notifications module will do the job
if (($destination = messaging_check_object($object, 'Messaging_Destination')) && empty($destination->uid)) {
$missing = '';
$links = notifications_anonymous_manage_links('destination', $destination, 'url');
$values['destination-unsubscribe-url'] = isset($links['delete']) ? $links['delete'] : $missing;
$values['destination-manage-url'] = isset($links['manage']) ? $links['manage'] : $missing;
$values['destination-edit-url'] = isset($links['edit']) ? $links['edit'] : $missing;
// This user token goes with destination for anonymous
$values['unsubscribe-url-global'] = $values['destination-unsubscribe-url'];
$values['subscriptions-manage'] = $values['destination-manage-url'];
}
return $values;
}
}
Functions
Name![]() |
Description |
---|---|
notifications_anonymous_destination_access | Menu access callback for destinations |
notifications_anonymous_destination_method | Get sending method for destination |
notifications_anonymous_flood_control | Check flood control, optionally printing an error message |
notifications_anonymous_forms | Implementation of hook_forms() |
notifications_anonymous_help | Implementation of hook_help() |
notifications_anonymous_manage_links | Get raw /formatted links to manage subscription / destination |
notifications_anonymous_menu | Implementation of hook_menu() |
notifications_anonymous_messaging | Implementation of hook_messaging() |
notifications_anonymous_notifications_subscription | Implementation of hook_notifications_subscription() |
notifications_anonymous_send_intervals | Get available send intervals for anonymous users |
notifications_anonymous_send_methods | Get available sending methods for anonymous users |
notifications_anonymous_subscribe_links | Build subscription options as an array of links |
notifications_anonymous_subscription_access | Menu access callback for subscriptions |
notifications_anonymous_subscription_form | Anonymous Subscription form builder. Adds destination subform. |
notifications_anonymous_subscription_get_link | Get subscribe / unsubscribe link for subscription |
notifications_anonymous_token_values | Implementation of hook_token_values() |
notifications_anonymous_url | Create signed URLs for all pages |
_notifications_anonymous_access | Check for object access |