You are here

function notifications_manage_subscriptions_form in Notifications 7

Same name and namespace in other branches
  1. 6.4 notifications.manage.inc \notifications_manage_subscriptions_form()
  2. 6 notifications.manage.inc \notifications_manage_subscriptions_form()
  3. 6.2 notifications.manage.inc \notifications_manage_subscriptions_form()
  4. 6.3 notifications.manage.inc \notifications_manage_subscriptions_form()

Administer subscriptions. For user tabs and admin tabs

Parameters

$conditions: Filter conditions. I.e. for user administering its own subscriptions, we'll have an 'uid' condition

2 calls to notifications_manage_subscriptions_form()
notifications_account_manage_subscriptions_form in notifications_account/notifications_account.pages.inc
Menu callback: user subscriptions management
notifications_admin_manage_subscriptions in ./notifications.admin.inc
Menu callback: subscriptions administration.

File

./notifications.manage.inc, line 33
Common functions for bulk management of subscriptions

Code

function notifications_manage_subscriptions_form($conditions = array()) {
  $admin_access = user_access('administer notifications') || user_access('manage all subscriptions') || user_access('maintain own subscriptions');

  // It is a user list if we have an uid parameter
  $account = !empty($conditions['uid']) ? user_load($conditions['uid']) : NULL;
  $status_list = Notifications_Subscription::status_list();
  $type_list = notifications_subscription_type(NULL, 'title');
  $send_methods = messaging_method_info(NULL, 'name');
  $send_intervals = notifications_send_intervals();
  $send_intervals[-1] = t('Scheduled');
  $drupal_destination = drupal_get_destination();

  // This is the full options subsform
  $form = notifications_manage_subscriptions_form_options($admin_access);

  // Depending on the context (user, admin) we'll have a different base path for the operation links
  $base_path = $account ? 'user/' . $account->uid . '/notifications/subscription' : 'notifications/subscription';

  // Build the sortable table header.
  $header = array(
    'sid' => array(
      'data' => t('Id'),
      'field' => 's.sid',
    ),
    'type' => array(
      'data' => t('Type'),
      'field' => 's.type',
    ),
    'status' => array(
      'data' => t('Status'),
      'field' => 's.status',
    ),
    'name' => t('Description'),
    'created' => array(
      'data' => t('Created'),
      'field' => 's.created',
      'sort' => 'desc',
    ),
  );
  if (!$account) {
    $header['uid'] = array(
      'data' => t('User'),
      'field' => 's.uid',
    );
  }

  // If we have more than one send method or interval, add the columns
  if (count($send_methods) > 1) {
    $header['send_method'] = array(
      'data' => t('Send method'),
      'field' => 's.send_method',
    );
  }
  if (count($send_intervals) > 1) {
    $header['send_interval'] = array(
      'data' => t('Send interval'),
      'field' => 's.send_interval',
    );
  }

  // Add operations at the end
  $header['operations'] = array(
    'data' => t('Operations'),
  );

  // Query data with $conditions and filters
  $query = db_select('notifications_subscription', 's')
    ->extend('PagerDefault')
    ->extend('TableSort');
  foreach ($conditions as $field => $value) {
    $query
      ->condition('s.' . $field, $value);
  }
  notifications_manage_subscriptions_build_filter_query($query, $conditions);
  $sids = $query
    ->fields('s', array(
    'sid',
  ))
    ->limit(50)
    ->orderByHeader($header)
    ->execute()
    ->fetchCol();
  $subscriptions = Notifications_Subscription_Table::build_sids($sids)
    ->set_header($header);

  // Only use a tableselect when the current user is able to perform any operations.
  if ($admin_access) {
    $form['subscriptions'] = $subscriptions
      ->table_select();
  }
  else {
    $form['subscriptions'] = $subscriptions
      ->table_list();
  }
  $form['pager'] = array(
    '#markup' => theme('pager', array(
      'tags' => NULL,
    )),
  );
  return $form;
}