You are here

notifications_forum.module in Forum notifications 7

Same filename and directory in other branches
  1. 6 notifications_forum.module

File

notifications_forum.module
View source
<?php

/**
 * Implements hook_theme().
 */
function notifications_forum_theme() {
  return array(
    'notifications_forum_user_subscriptions_form' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_node_view().
 */

// TODO: since 'notifications_tags' no longer puts anything in the node links, I don't know the
// correct place to enable disable this...
function notifications_forum_node_view($node, $view_mode, $lang_code) {
  global $user;

  // user has no permission to create subscriptions, exit early.
  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;
    }
  }
}

/**
 * Implements hook_node_view_alter().
 */
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'),
        ));
      }
    }
  }
}

/**
 * Implements hook_menu().
 */
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;
}

// Helper function to subscribe a user to a forum
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);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
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) {

      // TODO: do something useful with containers
      if (empty($forum->container)) {
        $forums[$forum->tid] = $forum->name;
      }
    }

    // TODO: this should probably use the same form as the one we put on the user page
    $form['notifications_forum']['forums'] = array(
      '#type' => 'checkboxes',
      //'#title' => t('Forums'),
      '#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;
}

/**
 * Implements hook_user_insert().
 */
function notifications_forum_user_insert(&$edit, $account, $category) {
  if (isset($edit['notifications_forum']) && !empty($edit['notifications_forum']['forums'])) {
    $values = $edit['notifications_forum'];

    // subscribe to the forums
    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'],
        ));
      }
    }
  }
}

/**
 * Access check for determining if this user can add a taxonomy subscription.
 */
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);
}

/**
 * Access callback for the user forum notifications page.
 */
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);

  // if we have no default from the subscription or the settings, just take the
  // first option (the first array value is returned by reset()).
  $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;
}

/**
 * User forum notifications form.
 */

// TODO: could we use drupal_get_form('notifications_subscription_list_form', 'checkboxes')?
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'),
  );

  // lookup existing subscriptions and index by 'tid'
  $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)) {

        // TODO: handle containers better!
        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;
}

/**
 * Submit function for the user forum notifactions 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.'));
  }
}

/**
 * Theme the user forum notifications form.
 */
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,
  );
}

/**
 * Implements hook_requirements().
 */
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;
}

/**
 * Checks if the forum vocabulary is enabled for subscriptions with
 * notifications_tag.
 */
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;
}

/**
 * Preprocess function for theme_forum_list().
 *
 * Adds a subscribe/unsubscribe link under each forum on the forum list.
 */
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>";
        }
      }
    }
  }
}

/**
 * Implements function for hook_menu_local_tasks_alter().
 *
 * Adds a subscribe/unsubscribe link to each forum topic listing.
 */
function notifications_forum_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  if (_notifications_forum_access()) {
    if ($root_path == "forum" || $root_path == "forum/%") {

      // TODO: add subscription links here, when on forum topic pages?
      // Get forum item
      $tid = isset($router_item['page_arguments'][0]) ? $router_item['page_arguments'][0]->tid : 0;
      $forum = forum_forum_load($tid);

      // TODO: if top level forum, add link to subscribe to all forum content
      //       (by content type, not taxonomy)
      if ($forum) {

        // Ensure current forum isn't a container (thus, can have subscriptions)
        // TODO: double check forum api / logic. Why no $forum->is_container?
        $is_container = isset($forum->container) && (bool) $forum->container;
        if (!$is_container) {

          // Get subscription data
          list($type, $href, $options) = _notifications_forum_toggle_subscription_url($tid);
          if (!is_null($type)) {

            // Create subscription link local task item
            $item = array();
            $item['title'] = t(($type == 'subscribe' ? 'Subscribe to' : 'Unsubscribe from') . ' this forum');
            $item['href'] = $href;
            $item['localized_options'] = $options;

            // Add item to data output
            $data['actions']['output'][] = array(
              '#theme' => 'menu_local_action',
              '#link' => $item,
            );
          }
        }
      }
    }
  }
}

/**
 * Preprocess function for themek_preprocess_views_view__advanced_forum_topic_list().
 *
 * Adds subscription link to Advanced Forums link toolbar.
 */
function notifications_forum_preprocess_views_view__advanced_forum_topic_list(&$variables) {
  if (_notifications_forum_access()) {

    // Get forum item
    $tid = $variables['view']->args[0];
    $forum = forum_forum_load($tid);

    // Create subscription link
    $subscription = '';
    if ($forum) {

      // Get subscription data
      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>';
      }
    }

    // Add subscription link button to other buttons
    $variables['node_create_list'] = $subscription . $variables['node_create_list'];
  }
}

/**
 * Implements hook_theme_registry_alter()
 *
 * Ensures certain hooks from this modules are run after hooks from the Advanced
 * Forums module.
 */
function notifications_forum_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['views_view__advanced_forum_topic_list']['preprocess functions'])) {

    // get preprocess function indexes
    $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']);

    // remove old preprocess function entry, insert into new position
    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");
    }
  }
}

Functions

Namesort descending Description
notifications_forum_form_user_register_form_alter Implements hook_form_FORM_ID_alter().
notifications_forum_menu Implements hook_menu().
notifications_forum_menu_local_tasks_alter Implements function for hook_menu_local_tasks_alter().
notifications_forum_node_view
notifications_forum_node_view_alter Implements hook_node_view_alter().
notifications_forum_preprocess_forum_list Preprocess function for theme_forum_list().
notifications_forum_preprocess_views_view__advanced_forum_topic_list Preprocess function for themek_preprocess_views_view__advanced_forum_topic_list().
notifications_forum_requirements Implements hook_requirements().
notifications_forum_theme Implements hook_theme().
notifications_forum_theme_registry_alter Implements hook_theme_registry_alter()
notifications_forum_user_forum_form
notifications_forum_user_forum_form_submit Submit function for the user forum notifactions form.
notifications_forum_user_insert Implements hook_user_insert().
theme_notifications_forum_user_subscriptions_form Theme the user forum notifications form.
_notifications_forum_access Access check for determining if this user can add a taxonomy subscription.
_notifications_forum_enabled Checks if the forum vocabulary is enabled for subscriptions with notifications_tag.
_notifications_forum_send_element
_notifications_forum_subscribe_to_forum
_notifications_forum_toggle_subscription_url
_notifications_forum_user_forum_access Access callback for the user forum notifications page.