You are here

notifications_ui.module in Notifications 6.4

User Interface for subscriptions modules

File

notifications_ui/notifications_ui.module
View source
<?php

/**
 * @file
 * User Interface for subscriptions modules
 */

/**
 * Implementation of hook_help()
 */
function notifications_ui_help($path, $arg) {
  switch ($path) {
    case 'admin/messaging/notifications/ui':
      $output = '<p>' . t('These are UI settings only and will define which options will be visible for end users and how they\'ll be displayed. Which options will be finally available will be determined by:');
      $output .= '<ol>';
      $output .= '<li>' . t('Enabled subscription types on the other Notifications settings pages') . '</li>';
      $output .= '<li>' . t('Permissions that you can configure on <a href="@administer-permissions">Administer permissions</a>.', array(
        '@administer-permissions' => url('admin/user/permissions'),
      )) . '</li>';
      $output .= '<li>' . t('The enabled options on this page.') . '</li>';
      $output .= '</ol></p>';
      return $output;
  }
}

/**
 * Implementation of hook_menu()
 */
function notifications_ui_menu() {
  $items['admin/messaging/notifications/subscriptions/ui'] = array(
    'title' => 'User interface',
    'description' => 'Enables site settings for user subscriptions.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'notifications_ui_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'notifications_ui.admin.inc',
  );

  // Add single subscription page
  $items['user/%user/notifications/add'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'Add subscription',
    'page callback' => 'notifications_ui_page_user_add',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'notifications_ui_access_user_add',
    'access arguments' => array(
      1,
      4,
    ),
    'file' => 'notifications_ui.pages.inc',
    'weight' => 100,
  );
  return $items;
}

/**
 * Implementation of hook_menu_alter()
 */
function notifications_ui_menu_alter(&$items) {
  foreach (notifications_subscription_types() as $type => $info) {
    if (!empty($info['user page'])) {
      $items[$info['user page']]['access callback'] = 'notifications_ui_access_page';
      $items[$info['user page']]['access arguments'] = array(
        $type,
        1,
      );
    }
  }
}

/**
 * Menu access callback: add subscription
 */
function notifications_ui_access_user_add($account, $type = NULL) {

  // Main tab, visible only if any subscription type enabled
  $check_types = $type ? array(
    $type,
  ) : array_keys(notifications_ui_subscription_types());
  foreach ($check_types as $type) {
    if (notifications_ui_type_enabled($type) && notifications_ui_user_options('create') && notifications_access_user_add($account, $type)) {
      return TRUE;
    }
  }
}

/**
 * Menu access callback: account pages
 */
function notifications_ui_access_page($type, $account) {

  // Global user permissions
  if (notifications_access_user($account) && notifications_ui_type_enabled($type) && notifications_ui_user_options('page')) {

    // Check specifics for this subscription type
    $access = notifications_subscription_types($type, 'access');
    return $access ? user_access($access, $account) : TRUE;
  }
}

/**
 * Implementation of hook_notifications_event().
 * 
 * We need to handle some things when an event is triggered on form subsmission
 */
function notifications_ui_notifications_event($op, $event = NULL, $account = NULL) {
  if ($op == 'trigger' && $event->type == 'node') {
    if (($node = $event
      ->get_object('node')) && isset($node->subscriptions)) {
      if ($event->action == 'insert') {

        // On insert some field information will be missing, we need to recreate it.
        // @todo Check this one, we may not need it anymore
        foreach ($node->subscriptions['params'] as $i => $subscriptions) {
          foreach ($subscriptions->fields as $key => $value) {
            if (!$value && isset($event->params[$key])) {
              $event->node->subscriptions['params'][$i]->fields[$key] = $event->params[$key];
            }
          }
        }
      }
      $form_state['values']['subscriptions'] = $node->subscriptions;
      notifications_subscriptions_options_form_submit('', $form_state);
    }
    elseif (($comment = $event
      ->get_object('comment')) && isset($comment->subscriptions)) {
      $form_state['values']['subscriptions'] = $comment->subscriptions;
      notifications_subscriptions_options_form_submit('', $form_state);
    }
  }
}

/**
 * Check whether this subscription type is enabled / disabled for the UI
 * 
 * @param $type
 *   Subscription type
 */
function notifications_ui_type_enabled($type) {
  $settings = variable_get('notifications_ui_types', array(
    'thread',
    'nodetype',
    'author',
  ));
  return notifications_subscription_type_enabled($type) && in_array($type, $settings, TRUE);
}

/**
 * Get info about subscription types, exclude custom types
 *
 * @see notifications_subscription_types()
 */
function notifications_ui_subscription_types($type = NULL, $field = NULL) {
  static $types;
  if (!isset($types)) {
    $types = notifications_subscription_types();

    // Filter out disabled types
    $types = array_intersect_key($types, notifications_subscription_type_enabled());

    // Filter out custom subscriptions
    $types = notifications_array_filter($types, array(
      'custom' => TRUE,
    ), TRUE);
  }
  return notifications_array_info($types, $type, $field);
}

/**
 * Implementation of hook_forms()
 */
function notifications_ui_forms($form_id) {
  $forms = array();
  if (strpos($form_id, 'notifications_ui_options_form_') === 0) {
    $forms[$form_id] = array(
      'callback' => 'notifications_subscriptions_options_form',
    );
  }
  return $forms;
}

/**
 * Implementation of hook_form_alter()
 * 
 */
function notifications_ui_form_alter(&$form, $form_state, $form_id) {
  global $user;

  // Content type settings
  switch ($form_id) {
    case 'node_type_form':
      if (isset($form['identity']['type'])) {
        module_load_include('admin.inc', 'notifications_ui');

        // Just in case we want to add more settings here
        $form['notifications']['notifications_node_ui'] = array(
          '#type' => 'checkboxes',
          '#title' => t('Subscriptions UI'),
          '#default_value' => notifications_ui_node_options($form['#node_type']->type),
          '#options' => _notifications_ui_node_options(),
          '#description' => t('Enable different display options for subscriptions to this content type.'),
        );
        if (!variable_get('notifications_ui_per_type', 0)) {
          $form['notifications']['notifications_node_ui']['#disabled'] = TRUE;
          $form['notifications']['notifications_node_ui']['#description'] .= ' <strong>' . t('To enable these options check the <a href="@notifications-ui-settings">Notifications UI settings</a>', array(
            '@notifications-ui-settings' => url('admin/messaging/notifications/ui'),
          )) . '</strong>';
        }
      }
      break;
    case 'comment_form':

      // Add to comment forms.
      $node = node_load($form['nid']['#value']);
      if ($subscriptions = notifications_ui_node_subscriptions($node, 'comment')) {
        $form[] = notifications_subscriptions_options_subform($subscriptions, FALSE);
      }
      break;
    case 'notifications_user_overview':

      // Create new subscription
      $account = $form['account']['#value'];
      foreach (notifications_ui_subscription_types() as $key => $type) {
        if (notifications_ui_type_enabled($key) && notifications_ui_user_options('create') && notifications_access_user_add($account, $key)) {
          $create[] = l($type['title'], "user/{$account->uid}/notifications/add/{$key}");
        }
      }
      if (!empty($create)) {

        // $output .= theme('item_list', $create, t('or create a new subscription'));
        $form['create'] = array(
          '#type' => 'item',
          '#weight' => 30,
          '#title' => t('or create a new subscription'),
          '#value' => theme('item_list', $create),
        );
      }
      break;
    default:
      if (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id && ($subscriptions = notifications_ui_node_subscriptions($form['#node'], 'form'))) {
        $form = array_merge_recursive($form, notifications_subscriptions_options_subform($subscriptions, FALSE));
      }
  }
}

/**
 * Implementation of hook hook_content_extra_fields().
 *
 * Enables CCK (admin/content/types/CONTENT_TYPE/fields) to configure the
 * position of the subscriptions fieldset within the node.
 *
 * @ingroup hooks
 */
function notifications_ui_content_extra_fields($type_name) {
  $extra = array();
  if (notifications_ui_node_options($type_name, 'form')) {
    $extra['subscriptions'] = array(
      'label' => t('Subscriptions'),
      'description' => t('Notifications UI subscriptions form.'),
      'weight' => 100,
    );
  }
  return $extra;
}

/**
 * Implementation of hook_link()
 * 
 * Add subscriptions links to nodes
 */
function notifications_ui_link($type, $node = NULL, $teaser = FALSE) {
  if ($type == 'node' && notifications_ui_user_access() && ($teaser && notifications_ui_node_options($node->type, 'teaserlinks') || !$teaser && notifications_ui_node_options($node->type, 'links'))) {

    // Now we have the array of allowed options ready, build single links
    return notifications_ui_subscribe_links('node', $node);
  }
}

/**
 * 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_ui_subscribe_links($type, $object) {
  if (notifications_ui_user_access()) {
    notifications_include('object.inc');
    return notifications_object_subscribe_links($type, $object);
  }
}

/**
 * Display a button + js overlay
 * 
 * From http://groups.drupal.org/node/17779
 */
function notifications_ui_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global $user;
  static $form_instance_id = 0;
  if ($op == 'alter' && ($subscriptions = notifications_ui_node_subscriptions($node, 'subform'))) {
    $node->body .= drupal_get_form('notifications_ui_options_form_' . $form_instance_id, $subscriptions, TRUE, TRUE);
    $form_instance_id++;
  }
}

/**
 * Get list of allowed subscriptions types
 * 
 * Checks permissions and settings
 * 
 * @return
 *   Subscription types allowed for this user
 */
function notifications_ui_allowed_types() {
  $allowed = array();
  foreach (notifications_subscription_types() as $type => $info) {
    if (notifications_ui_type_enabled($type) && !empty($info['access']) && user_access($info['access'])) {
      $allowed[$type] = $info;
    }
  }
  return $allowed;
}

/**
 * Get list of possible and existing subscriptions for user/object
 * 
 * @param $account
 *   User account to get options/subscriptions for
 * @param $type
 *   Subscription type to get options: 'user', 'node'
 * @param $object
 *   The object to subscribe. It may be $node or $user
 * 
 * @return
 *   Array of subscription objects, that will be mixed subscription templates and actual subscriptions
 */
function notifications_ui_object_subscriptions($type, $object) {
  notifications_include('object.inc');
  return notifications_object_user_subscriptions($type, $object, $account);
}

/**
 * Implementation of hook_block()
 * 
 * This block is just for registered users. For anonymous see notifications_anonymous module.
 */
function notifications_ui_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Subscriptions');
    $blocks[0]['cache'] = BLOCK_NO_CACHE;
    return $blocks;
  }
  else {
    if ($op == 'view' && notifications_ui_user_access()) {

      // Only return block if we have something for it
      if ($options = notifications_ui_page_options('block')) {
        $block['subject'] = t('Subscriptions');
        $block['content'] = drupal_get_form('notifications_subscriptions_options_form', $options);
        return $block;
      }
    }
  }
}

/**
 * Get subscription options for current page/user and a display type
 * 
 * @param $display
 *   Display type: 'links', 'form', 'block', etc...
 */
function notifications_ui_page_options($display) {
  notifications_include('object.inc');
  $objects = array();
  foreach (notifications_object_page_objects() as $type => $object) {
    if (notifications_ui_display_options($type, $display, $object)) {
      $objects[$type] = $object;
    }
  }
  if ($objects && ($subscriptions = notifications_object_page_subscriptions($objects))) {

    // Filter out not displayable subscription types
    foreach ($subscriptions as $index => $subscription) {
      if (!notifications_ui_type_enabled($subscription->type)) {
        unset($subscriptions[$index]);
      }
    }
    return $subscriptions;
  }
}

/**
 * Implementation of hook node_type
 */
function notifications_ui_node_type($op, $info) {
  if ($op == 'delete') {

    // Remove settings for this node type
    variable_del('notifications_node_ui_' . $info->type);
  }
}

/**
 * Find subscriptions options for this node
 */
function notifications_ui_node_subscriptions($node, $display) {
  if (notifications_ui_user_access() && notifications_ui_node_options($node->type, $display)) {
    return notifications_ui_object_subscriptions('node', $node);
  }
}

/**
 * Get settings value for content types
 * 
 * @param $type
 *   Content type to get settings for
 * @param $option
 *   Optional option to check (each option can be enabled or disabled)
 */
function notifications_ui_node_options($type = NULL, $option = NULL) {

  // We can use global options or per content type options. The default setting will be 'links' = 1
  $options = variable_get('notifications_ui_node_options', array(
    'links',
    'block',
  ));
  if ($type && variable_get('notifications_ui_per_type', 0)) {
    $options = variable_get('notifications_node_ui_' . $type, $options);
  }
  return $option ? in_array($option, $options, TRUE) : $options;
}

/**
 * Check user access to notifications_ui
 */
function notifications_ui_user_access($account = NULL) {
  $account = $account ? $account : $GLOBALS['user'];
  return notifications_access_subscribe($account);
}

/**
 * Check enabled option / Get options for user account pages
 * 
 * @param $type
 *   Option type = 'page', 'create'
 *   Null to get all of them
 */
function notifications_ui_user_options($display = NULL) {
  return notifications_ui_display_options('user', $display, NULL, array(
    'page',
    'create',
  ));
}

/**
 * Check enabled option for object type / display type
 * 
 * @param $type
 *   Object type: node, user (subscribe to another user), account (own user account tab)
 * @pram $check_option
 *   Option to check for object type: 'links', 'form', 'page', 'create'
 */
function notifications_ui_display_options($type, $check_option = NULL, $object = NULL, $defaults = array(
  'links',
  'block',
)) {
  if ($type == 'node' && $object) {
    $options = notifications_ui_node_options($object->type);
  }
  else {
    $options = variable_get('notifications_ui_' . $type . '_options', $defaults);
  }
  if ($check_option) {
    return in_array($check_option, $options, TRUE);
  }
  else {
    return $options;
  }
}

/**
 * Implementation of hook_user().
 */
function notifications_ui_user($op, &$edit, &$account, $category = NULL) {
  if ($op == 'view') {

    // Add plain links if enabled
    if (notifications_ui_display_options('account', 'links') && ($links = notifications_ui_subscribe_links('user', $account))) {
      $account->content['summary']['notifications'] = array(
        '#type' => 'user_profile_item',
        '#title' => t('Subscriptions'),
        '#value' => theme('links', $links, array(
          'class' => 'item-list',
        )),
      );
    }
  }
}

/**
 * Implementation of hook_theme()
 */
function notifications_ui_theme() {
  return array(
    'notifications_ui_subscription_types' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'notifications_ui.admin.inc',
    ),
    'notifications_ui_content_types' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'notifications_ui.admin.inc',
    ),
    'notifications_ui_add_list' => array(
      'arguments' => array(
        'content' => NULL,
      ),
      'file' => 'notifications_ui.admin.inc',
    ),
  );
}

Functions

Namesort descending Description
notifications_ui_access_page Menu access callback: account pages
notifications_ui_access_user_add Menu access callback: add subscription
notifications_ui_allowed_types Get list of allowed subscriptions types
notifications_ui_block Implementation of hook_block()
notifications_ui_content_extra_fields Implementation of hook hook_content_extra_fields().
notifications_ui_display_options Check enabled option for object type / display type
notifications_ui_forms Implementation of hook_forms()
notifications_ui_form_alter Implementation of hook_form_alter()
notifications_ui_help Implementation of hook_help()
notifications_ui_link Implementation of hook_link()
notifications_ui_menu Implementation of hook_menu()
notifications_ui_menu_alter Implementation of hook_menu_alter()
notifications_ui_nodeapi Display a button + js overlay
notifications_ui_node_options Get settings value for content types
notifications_ui_node_subscriptions Find subscriptions options for this node
notifications_ui_node_type Implementation of hook node_type
notifications_ui_notifications_event Implementation of hook_notifications_event().
notifications_ui_object_subscriptions Get list of possible and existing subscriptions for user/object
notifications_ui_page_options Get subscription options for current page/user and a display type
notifications_ui_subscribe_links Build subscription options as an array of links
notifications_ui_subscription_types Get info about subscription types, exclude custom types
notifications_ui_theme Implementation of hook_theme()
notifications_ui_type_enabled Check whether this subscription type is enabled / disabled for the UI
notifications_ui_user Implementation of hook_user().
notifications_ui_user_access Check user access to notifications_ui
notifications_ui_user_options Check enabled option / Get options for user account pages