View source
<?php
function notifications_autosubscribe($type, $event_type, $field, $value) {
global $user;
if (notifications_user_setting('auto', $user) && !notifications_user_get_subscriptions($user->uid, $event_type, $field, $value)) {
$subscription = array(
'uid' => $user->uid,
'type' => $type,
'event_type' => $event_type,
'fields' => array(
$field => $value,
),
);
notifications_save_subscription($subscription);
}
}
function notifications_autosubscribe_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'user_edit':
case 'user_profile_form':
if (isset($form['messaging'])) {
$form['messaging']['notifications_auto'] = array(
'#type' => 'checkbox',
'#title' => t('Autosubscribe'),
'#default_value' => notifications_user_setting('auto', $form['_account']['#value']),
'#description' => t('Checking this box allows you to automatically subscribe to any thread you create or post a comment to.'),
);
}
break;
case 'notifications_content_settings_form':
$form['autosubscribe'] = array(
'#type' => 'fieldset',
'#title' => t('Autosubscribe'),
'#weight' => -10,
);
$form['autosubscribe']['notifications_default_auto'] = array(
'#type' => 'checkbox',
'#title' => t('Set all users to "autosubscribe" by default'),
'#default_value' => variable_get('notifications_default_auto', 0),
'#description' => t("If checked the option will be 'enabled' by default for user account settings. This won't change existing settings for users who have already defined it."),
);
break;
}
}
function notifications_autosubscribe_notifications($op, $arg0, $arg1 = NULL, $arg2 = NULL) {
if ($op == 'event trigger') {
$event = $arg0;
if ($event->type == 'node' && isset($event->node->nid) && (!isset($event->node->autosubscribe) || $event->node->autosubscribe) && $event->action !== 'update') {
notifications_autosubscribe('thread', 'node', 'nid', $event->node->nid);
}
}
}
function notifications_autosubscribe_notifications_node_form_alter(&$form) {
global $user;
if (!empty($form['subscriptions']['params']) && notifications_user_setting('auto', $form['subscriptions']['account']['#value'])) {
foreach ($form['subscriptions']['params']['#value'] as $index => $current) {
if ($current['type'] == 'thread' && empty($current->sid)) {
$form['subscriptions']['autosubscribe'] = array(
'#type' => 'checkbox',
'#default_value' => 1,
'#disabled' => TRUE,
'#title' => $form['subscriptions']['options']['#options'][$index],
'#description' => t('You are currently set to receive notifications for replies to content which you create. To change this default, uncheck the autosubscribe option in your user account settings.'),
);
unset($form['subscriptions']['options']['#options'][$index]);
}
}
}
}