You are here

function subscriptions_ui_node_form in Subscriptions 5.2

Same name and namespace in other branches
  1. 6 subscriptions_ui.module \subscriptions_ui_node_form()
  2. 7 subscriptions_ui.module \subscriptions_ui_node_form()
  3. 2.0.x subscriptions_ui/subscriptions_ui.module \subscriptions_ui_node_form()

Returns the form definition for the node subform.

Parameters

$node: Must be a valid node object.

$account: If not given, will be populated with current user (either anonymous).

2 string references to 'subscriptions_ui_node_form'
subscriptions_ui_block in ./subscriptions_ui.module
Implementation of hook_block().
subscriptions_ui_nodeapi in ./subscriptions_ui.module
Implementation of hook_nodeapi().

File

./subscriptions_ui.module, line 121
Provides a user interface for Subscriptions.

Code

function subscriptions_ui_node_form($node, $account = NULL) {
  global $user;
  if (!isset($account)) {
    $account = $user;
  }
  if (subscriptions_node_is_blocked($node->nid)) {
    return NULL;
  }
  $blocked_types = variable_get('subscriptions_blocked_content_types', array());
  if (in_array($node->type, $blocked_types)) {
    if (!user_access('subscribe to all content types', $account)) {
      return NULL;
    }
    $is_blocked = TRUE;
  }
  init_theme();

  // just in case, before calling theme_get_setting()
  $show_node_info = theme_get_setting('toggle_node_info_' . $node->type);
  $node_options = module_invoke_all('subscriptions', 'node_options', $account, $node);
  if (!$node_options || !user_access('subscribe to content', $account)) {
    return array();
  }
  uasort($node_options, '_subscriptions_cmp_by_weight');
  $result = db_query("SELECT sid, module, field, value, author_uid, send_interval, send_updates, send_comments FROM {subscriptions} WHERE module = 'node' AND recipient_uid = %d", $account->uid);
  while ($s = db_fetch_array($result)) {
    $subscriptions[$s['field']][$s['value']][$s['author_uid']] = $s;
  }

  // Process all options building the array of indexed params for each
  $nonlabeled_options = $options = $params = $default_comments = $default_updates = $default_subscriptions = array();
  $index = 1;

  // If we start with zero, get some value sent as 0 => 0
  foreach ($node_options as $field => $field_options) {
    foreach ($field_options as $option) {
      if (!is_array($option)) {
        continue;
      }
      if ((!$show_node_info || !variable_get('subscriptions_show_by_author_options', 1)) && isset($option['params']['author_uid']) && $option['params']['author_uid'] >= 0) {
        continue;
      }
      if ($option['params']['module'] == 'node' && $option['params']['field'] == 'type' && !empty($is_blocked)) {
        $option['name'] .= ' ' . SUBSCRIPTIONS_UNAVAILABLE;
      }

      //$options[$index] = (isset($option['link']) ? l($option['name'], 'subscriptions/add/'. $option['link'], array(), drupal_get_destination(), NULL, FALSE, TRUE) : $option['name']);
      $options[$index] = $option['name'];
      $nonlabeled_options[$index] = '';
      $params[$index] = $param = $option['params'] + array(
        'author_uid' => -1,
      );
      if (isset($subscriptions[$param['field']][$param['value']][$param['author_uid']])) {
        $default_subscriptions[] = $index;
        $default_send_intervals[$index] = $subscriptions[$param['field']][$param['value']][$param['author_uid']]['send_interval'];
        if ($subscriptions[$param['field']][$param['value']][$param['author_uid']]['send_comments']) {
          $default_comments[] = $index;
        }
        if ($subscriptions[$param['field']][$param['value']][$param['author_uid']]['send_updates']) {
          $default_updates[] = $index;
        }
      }
      else {
        $default_send_intervals[$index] = _subscriptions_get_setting('send_interval', $account);
        if (_subscriptions_get_setting('send_comments', $account)) {
          $default_comments[] = $index;
        }
        if (_subscriptions_get_setting('send_updates', $account)) {
          $default_updates[] = $index;
        }
      }
      $index++;
    }
  }
  $form['params'] = array(
    '#type' => 'value',
    '#value' => $params,
  );
  $form['wrapper'] = array(
    '#type' => 'fieldset',
    '#title' => t('Subscribe') . (!empty($is_blocked) ? ' ' . SUBSCRIPTIONS_UNAVAILABLE : ''),
    '#collapsible' => TRUE,
    '#collapsed' => !variable_get('subscriptions_form_expanded', 0),
    '#theme' => 'subscriptions_ui_table',
    '#attributes' => array(
      'id' => 'subscribe',
    ),
  );
  $form['wrapper']['subscriptions'] = array(
    '#type' => 'checkboxes',
    '#default_value' => $default_subscriptions,
    '#options' => $options,
    '#access' => TRUE,
  );
  $form['wrapper']['updates'] = array(
    '#type' => 'checkboxes',
    '#default_value' => $default_updates,
    '#options' => $nonlabeled_options,
    '#access' => _subscriptions_get_setting('send_updates_visible', $user) > 0,
  );
  if (module_exists('comment') && $node->comment == COMMENT_NODE_READ_WRITE && user_access('access comments')) {
    $form['wrapper']['comments'] = array(
      '#type' => 'checkboxes',
      '#default_value' => $default_comments,
      '#options' => $nonlabeled_options,
      '#access' => _subscriptions_get_setting('send_comments_visible', $user) > 0,
    );
  }
  $form['wrapper']['footer'] = array(
    '#type' => 'item',
    '#description' => t('The master checkboxes on the left turn the given subscription on or off. Depending on the setup of the site, you may have additional options for active subscriptions.'),
    '#weight' => 9,
  );
  $form['wrapper']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#id' => 'subscriptions-submit',
    '#weight' => 10,
  );
  $form['account'] = array(
    '#type' => 'value',
    '#value' => $account,
  );
  $form['send_intervals'] = array(
    '#type' => 'value',
    '#value' => $default_send_intervals,
  );
  return $form;
}