View source
<?php
function notifications_forum_theme() {
return array(
'notifications_forum_user_subscriptions_form' => array(
'render element' => 'form',
),
);
}
function notifications_forum_node_view($node, $view_mode, $lang_code) {
global $user;
if (!_notifications_forum_access($user)) {
return;
}
if ($node->type == 'forum') {
$taxonomy_term = array(
notifications_object('taxonomy_term', $node->taxonomy_forums['und'][0]['taxonomy_term']),
);
$subscription_list = Notifications_Subscription::object_subscriptions($taxonomy_term, $user)
->set_user($user)
->get_instances();
foreach ($subscription_list as $key => $subscription) {
$forum_name = $subscription
->get_field('term:tid')
->drupal_object()->name;
$type = $subscription
->is_stored() ? 'Unsubscribe from' : 'Subscribe to';
$link = $subscription
->element_link('subscription');
$item = array(
'title' => t($type . ': @name', array(
'@name' => t('@forum forum', array(
'@forum' => $forum_name,
)),
)),
'href' => $link['#href'],
) + $link['#options'];
$node->content['links']['notifications_content']['#links']['notifications-forum-' . $key] = $item;
}
}
}
function notifications_forum_node_view_alter(&$build) {
if ($build['#bundle'] == 'forum' && isset($build['links']['notifications_content'])) {
foreach ($build['links']['notifications_content']['#links'] as $key => &$item) {
if ($item['title'] == t('Subscribe to: @name', array(
'@name' => t('This post'),
))) {
$item['title'] = t('Subscribe to: @name', array(
'@name' => t('This thread'),
));
}
elseif ($item['title'] == t('Unsubscribe from: @name', array(
'@name' => t('This post'),
))) {
$item['title'] = t('Unsubscribe from: @name', array(
'@name' => t('This thread'),
));
}
}
}
}
function notifications_forum_menu() {
$items = array();
$items['user/%user/notifications/forum'] = array(
'type' => MENU_LOCAL_TASK,
'access callback' => '_notifications_forum_user_forum_access',
'access arguments' => array(
1,
),
'title' => t('Forums'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'notifications_forum_user_forum_form',
1,
),
'weight' => 10,
);
return $items;
}
function _notifications_forum_subscribe_to_forum($account, $tid, $opts) {
$subscription = array(
'type' => 'taxonomy_term',
'uid' => $account->uid,
'send_method' => $opts['send_method'],
'send_interval' => $opts['send_interval'],
'event_type' => 'node',
);
if (!empty($opts['sid'])) {
$subscription['sid'] = $opts['sid'];
}
$subscription = notifications_subscription_build($subscription);
$subscription
->add_field('term:tid', $tid);
return notifications_save_subscription($subscription);
}
function notifications_forum_form_user_register_form_alter(&$form, &$form_state) {
$send_methods = messaging_method_list();
$send_intervals = notifications_send_intervals();
if (count($send_methods) > 0 && count($send_intervals) > 0) {
$form['notifications_forum'] = array(
'#type' => 'fieldset',
'#title' => t('Forum subscriptions'),
'#tree' => TRUE,
);
$forums = array();
foreach (forum_forum_load()->forums as $forum) {
if (empty($forum->container)) {
$forums[$forum->tid] = $forum->name;
}
}
$form['notifications_forum']['forums'] = array(
'#type' => 'checkboxes',
'#options' => $forums,
'#description' => t('The forums you would like to subscribe to.'),
);
$form['notifications_forum']['send_method'] = array(
'#type' => count($send_methods) > 1 ? 'select' : 'hidden',
'#title' => t('Send method'),
'#options' => $send_methods,
'#default_value' => messaging_method_default(),
);
$form['notifications_forum']['send_interval'] = array(
'#type' => count($send_intervals) > 1 ? 'select' : 'hidden',
'#title' => t('Send interval'),
'#options' => $send_intervals,
'#default_value' => variable_get('notifications_default_send_interval', 0),
);
}
return $form;
}
function notifications_forum_user_insert(&$edit, $account, $category) {
if (isset($edit['notifications_forum']) && !empty($edit['notifications_forum']['forums'])) {
$values = $edit['notifications_forum'];
foreach ($values['forums'] as $tid) {
if ($tid > 0) {
_notifications_forum_subscribe_to_forum($account, $tid, array(
'send_method' => $values['send_method'],
'send_interval' => $values['send_interval'],
));
}
}
}
}
function _notifications_forum_access($account = NULL) {
global $user;
if (is_null($account)) {
$account = $user;
}
return _notifications_forum_enabled() && notifications_subscription_type_enabled('taxonomy_term') && user_access('create subscriptions', $account) && user_access('subscribe to taxonomy terms', $account);
}
function _notifications_forum_user_forum_access($account) {
global $user;
return ($user->uid == $account->uid || $user->uid == 1) && module_exists('notifications_account') && user_access('subscribe to taxonomy terms') && notifications_account_tab_access($user, 'taxonomy');
}
function _notifications_forum_send_element(&$wrapper, $type, $title, $sub, $account) {
$options = $type === 'send_method' ? notifications_send_methods($account) : notifications_send_intervals($account);
$default = !is_null($sub) ? $sub->{$type} : notifications_user_setting($type, $account);
$default = !is_null($default) ? $default : reset($options);
$element = array(
'#title' => $title,
);
if (count($options) > 1) {
$element = array_merge($element, array(
'#type' => 'select',
'#options' => $options,
'#default_value' => $default,
));
}
else {
$element = array_merge($element, array(
'#type' => 'hidden',
'#value' => $default,
));
}
$wrapper[$type] = $element;
}
function notifications_forum_user_forum_form($form, &$form_state, $account) {
$form = array();
$account = messaging_user_object($account);
$send_elements = array(
'send_method' => t('Send method'),
'send_interval' => t('Send interval'),
);
$subs = array();
foreach (notifications_get_subscriptions(array(
'type' => 'taxonomy_term',
'uid' => $account->uid,
)) as $sub) {
$tid = $sub
->get_field('term:tid')->value;
$subs[$tid] = $sub;
}
$form['account'] = array(
'#type' => 'value',
'#value' => $account,
);
$form['info'] = array(
'#type' => 'item',
'#title' => t('@type subscriptions', array(
'@type' => 'Forum',
)),
'#description' => t('Subscribe to all threads posted on a forum.'),
);
$form['subscriptions'] = array(
'#theme' => 'notifications_forum_user_subscriptions_form',
'#tree' => TRUE,
);
$forums = forum_forum_load();
$send_methods = messaging_method_list();
$send_intervals = notifications_send_intervals();
if (count($forums->forums) > 0 && count($send_methods) > 0 && count($send_intervals) > 0) {
foreach ($forums->forums as $forum) {
if (!empty($forum->container)) {
continue;
}
$sub = !empty($subs[$forum->tid]) ? $subs[$forum->tid] : NULL;
$wrapper = array();
$wrapper['subscribe'] = array(
'#type' => 'checkbox',
'#title' => l($forum->name, "forum/{$forum->tid}"),
'#default_value' => !is_null($sub),
);
$wrapper['tid'] = array(
'#type' => 'value',
'#value' => $forum->tid,
);
if (!is_null($sub)) {
$wrapper['sid'] = array(
'#type' => 'value',
'#value' => $sub->sid,
);
}
$wrapper['description'] = array(
'#type' => 'item',
'#title' => t('Description'),
'#markup' => $forum->description,
);
foreach ($send_elements as $send_element_type => $send_element_title) {
_notifications_forum_send_element($wrapper, $send_element_type, $send_element_title, $sub, $account);
}
$form['subscriptions'][] = $wrapper;
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
}
return $form;
}
function notifications_forum_user_forum_form_submit($form, &$form_state) {
if (!empty($form_state['values']['subscriptions'])) {
$account = (object) $form_state['values']['account'];
foreach ($form_state['values']['subscriptions'] as $value) {
if ($value['subscribe']) {
_notifications_forum_subscribe_to_forum($account, $value['tid'], $value);
}
elseif (!empty($value['sid'])) {
Notifications_Subscription::delete_subscription($value['sid']);
}
}
drupal_set_message(t('Your forum subscriptions have been updated.'));
}
}
function theme_notifications_forum_user_subscriptions_form($variables) {
$form = $variables['form'];
$header = array(
t('Forum'),
t('Description'),
);
if (isset($form[0]['send_method']['#type']) && $form[0]['send_method']['#type'] == 'select') {
$header[] = t('Method');
}
if (isset($form[0]['send_interval']['#type']) && $form[0]['send_interval']['#type'] == 'select') {
$header[] = t('Interval');
}
$children = element_children($form);
if (!empty($children)) {
$rows = array();
foreach (element_children($form) as $key) {
$element = $form[$key];
$row = array();
$row[] = drupal_render($element['subscribe']);
foreach (array(
'description',
'send_method',
'send_interval',
) as $name) {
if ($element[$name]['#type'] == 'hidden') {
$row[0] .= drupal_render($element[$name]);
}
else {
unset($element[$name]['#title']);
$row[] = drupal_render($element[$name]);
}
}
$rows[] = $row;
}
}
else {
$rows[][] = array(
'data' => t('No subscriptions available.'),
'colspan' => count($header),
);
}
return theme('table', array(
'header' => $header,
'rows' => $rows,
), array(), t('Forum subscriptions'));
}
function _notifications_forum_toggle_subscription_url($tid) {
global $user;
$taxonomy_term = notifications_object('taxonomy_term', $tid);
if ($subscriptions = $taxonomy_term
->user_subscriptions($user)
->get_instances()) {
$subscription = array_shift($subscriptions);
$type = $subscription
->is_stored() ? 'unsubscribe' : 'subscribe';
$link = $subscription
->element_link();
return array(
$type,
$link['#href'],
$link['#options'],
);
}
return array(
NULL,
NULL,
NULL,
);
}
function notifications_forum_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
$enabled = _notifications_forum_enabled();
$requirements['notifications_forum'] = array(
'title' => t('Forum notifications'),
'severity' => $enabled ? REQUIREMENT_OK : REQUIREMENT_ERROR,
'value' => $enabled ? t('Enabled') : t('Disabled'),
);
if (!$enabled) {
$requirements['notifications_forum']['description'] = t('The forum notifications module won\'t work if you don\'t <a href="!url">enable the "Forum" vocabulary</a> for tag subscriptions.', array(
'!url' => url('admin/config/messaging/subscriptions/tags'),
));
}
}
return $requirements;
}
function _notifications_forum_enabled() {
if ($vid = variable_get('forum_nav_vocabulary', 0)) {
$allowed_vocabularies = variable_get('notifications_tags_vocabularies', array());
return (bool) $allowed_vocabularies[$vid];
}
return FALSE;
}
function notifications_forum_preprocess_forum_list(&$vars) {
if (_notifications_forum_access()) {
foreach ($vars['forums'] as $forum_id => &$forum) {
if (module_exists('advanced_forum')) {
$is_container = isset($forum->container) && $forum->container;
}
else {
$is_container = (bool) $forum->is_container;
}
if (!$is_container) {
list($type, $href, $options) = _notifications_forum_toggle_subscription_url($forum_id);
if (!is_null($type)) {
$url = url($href, $options);
$link = t($type == 'subscribe' ? '<a href="!url">Subscribe</a> to this forum' : '<a href="!url">Unsubscribe</a> from this forum', array(
'!url' => $url,
));
$forum->description .= "<div class='forum-subscribe'>{$link}</div>";
}
}
}
}
}
function notifications_forum_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if (_notifications_forum_access()) {
if ($root_path == "forum" || $root_path == "forum/%") {
$tid = isset($router_item['page_arguments'][0]) ? $router_item['page_arguments'][0]->tid : 0;
$forum = forum_forum_load($tid);
if ($forum) {
$is_container = isset($forum->container) && (bool) $forum->container;
if (!$is_container) {
list($type, $href, $options) = _notifications_forum_toggle_subscription_url($tid);
if (!is_null($type)) {
$item = array();
$item['title'] = t(($type == 'subscribe' ? 'Subscribe to' : 'Unsubscribe from') . ' this forum');
$item['href'] = $href;
$item['localized_options'] = $options;
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
}
}
}
function notifications_forum_preprocess_views_view__advanced_forum_topic_list(&$variables) {
if (_notifications_forum_access()) {
$tid = $variables['view']->args[0];
$forum = forum_forum_load($tid);
$subscription = '';
if ($forum) {
list($type, $href, $options) = _notifications_forum_toggle_subscription_url($tid);
if (!is_null($type)) {
$subscription = '<div class="forum-add-node forum-' . $type . '">';
$subscription .= theme('advanced_forum_l', array(
'text' => t($type == 'subscribe' ? 'Subscribe' : 'Unsubscribe'),
'path' => $href,
'options' => $options,
'button_class' => 'large',
));
$subscription .= '</div>';
}
}
$variables['node_create_list'] = $subscription . $variables['node_create_list'];
}
}
function notifications_forum_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['views_view__advanced_forum_topic_list']['preprocess functions'])) {
$nf_index = array_search("notifications_forum_preprocess_views_view__advanced_forum_topic_list", $theme_registry['views_view__advanced_forum_topic_list']['preprocess functions']);
$af_index = array_search("advanced_forum_preprocess_views_view__advanced_forum_topic_list", $theme_registry['views_view__advanced_forum_topic_list']['preprocess functions']);
if ($nf_index && $af_index && $af_index > $nf_index) {
array_splice($theme_registry['views_view__advanced_forum_topic_list']['preprocess functions'], $nf_index, 1);
array_splice($theme_registry['views_view__advanced_forum_topic_list']['preprocess functions'], $af_index, 0, "notifications_forum_preprocess_views_view__advanced_forum_topic_list");
}
}
}