You are here

function notifications_get_link in Notifications 6.2

Same name and namespace in other branches
  1. 5 notifications.module \notifications_get_link()
  2. 6.4 notifications.module \notifications_get_link()
  3. 6 notifications.module \notifications_get_link()
  4. 6.3 notifications.module \notifications_get_link()

Return link array for subscriptions

Parameters

$type: Link type: 'subscribe' | 'unsubscribe'

$params: Aditional parameters for the subscription, may be

  • uid, the user for which the link is generated
  • confirm, whether to show confirmation page or not
  • destination, form destination or TRUE to use current path
  • signed, to produce a signed link that can be used by anonymous users (Example: unsubscribe link in emails)
  • Other subscription parameters: type, fields...
4 calls to notifications_get_link()
NotificationsContentTests::testNotificationsContent in tests/notifications_content.test
Play with creating, retrieving, deleting a pair subscriptions
notifications_token_values in ./notifications.module
Implementation of hook_token_values()
notifications_ui_build_links in notifications_ui/notifications_ui.module
Build subscription options as an array of links
notifications_user_overview in ./notifications.pages.inc
Menu callback. Overview page for user subscriptions.

File

./notifications.module, line 1307
Notifications module

Code

function notifications_get_link($type, $params) {

  // Add some default values
  $params += array(
    'uid' => 0,
    'confirm' => TRUE,
    'signed' => FALSE,
    'destination' => FALSE,
    'query' => array(),
  );
  if ($params['destination'] === TRUE) {
    $params['destination'] = $_GET['q'];
  }
  $elements = array();
  switch ($type) {
    case 'subscribe':
      $elements = array(
        'subscribe',
        $params['uid'],
        $params['type'],
        implode(',', array_keys($params['fields'])),
        implode(',', $params['fields']),
      );
      break;
    case 'unsubscribe':
      $elements[] = 'unsubscribe';

      // The unsubscribe link can be for a single subscription or all subscriptions for a user
      if (!empty($params['sid'])) {
        $elements[] = 'sid';
        $elements[] = $params['sid'];
      }
      elseif (!empty($params['uid'])) {
        $elements[] = 'uid';
        $elements[] = $params['uid'];
      }
      break;
  }

  // Build query string using named parameters
  $query = $params['query'];
  if ($params['destination']) {
    $query['destination'] = $params['destination'];
  }

  // To skip the confirmation form, the link must be signed
  // Note tat the query string will be 'confirm=1' to skip confirmation form
  if (!$params['confirm']) {
    $query['confirm'] = 1;
    $params['signed'] = 1;
  }
  if ($params['signed']) {
    $query['signature'] = _notifications_signature($elements, !$params['confirm']);
  }

  // Build final link parameters
  $options['query'] = $query;
  foreach (array(
    'absolute',
    'html',
  ) as $name) {
    if (isset($params[$name])) {
      $options[$name] = $params[$name];
    }
  }
  return array(
    'href' => 'notifications/' . implode('/', $elements),
    'options' => $options,
  );
}