You are here

function og_notifications_user_page in Organic groups 6

Same name and namespace in other branches
  1. 5.8 og_notifications/og_notifications.module \og_notifications_user_page()
  2. 5 og_notifications/og_notifications.module \og_notifications_user_page()
  3. 5.3 og_notifications/og_notifications.module \og_notifications_user_page()
  4. 5.7 og_notifications/og_notifications.module \og_notifications_user_page()
  5. 6.2 modules/og_notifications/og_notifications.pages.inc \og_notifications_user_page()

Menu callback: Group and grouptype subscription management form. This should ideally play nicer with notifications_user_form. However, due to issues with support for multi-field subscriptions, it is currently going about things in a roundabout manner.

Parameters

Object $account: User object of the user whose page is to be displayed.

Return value

Array $form Form array.

3 string references to 'og_notifications_user_page'
og_notifications_menu in modules/og_notifications/og_notifications.module
Implementation of hook_menu().
og_notifications_notifications in modules/og_notifications/og_notifications.module
Implementation of hook_notifications().
og_notifications_user_page_submit in modules/og_notifications/og_notifications.module
Process og_notifications_user_page form submission.

File

modules/og_notifications/og_notifications.module, line 401
Provide notifications and messaging support for organic groups.

Code

function og_notifications_user_page($form_state, $account = NULL) {
  global $user;
  $account = $account ? $account : $user;
  if (!empty($account->og_groups)) {
    $ngroups = _og_notifications_user_groups($account->uid);
    $ngrouptypes = _og_notifications_user_grouptypes($account->uid);

    // Tap into notifications_user_form to retrieve variables and defaults.
    $group_form = notifications_user_form(NULL, $account, 'group', $ngroups, $account->og_groups, array(
      'type' => 'group',
      'event_type' => 'node',
    ), array(
      'title' => t('Group'),
    ));

    // Grouptype uses multiple fields which are not addressed adequately by
    // notifications_user_form.
    $grouptype_form = notifications_user_form(NULL, $account, 'grouptype', $ngrouptypes, variable_get('og_notifications_content_types', array()), array(
      'type' => 'grouptype',
      'event_type' => 'node',
    ), array(
      'title' => t('Group Type'),
    ));

    // TODO: Trim unnecessary items.
    $form = array(
      'account' => $group_form['account'],
      'group_defaults' => $group_form['defaults'],
      'group_current' => $group_form['current'],
      'group_subscription_fields' => $group_form['subscription_fields'],
      'grouptype_defaults' => $grouptype_form['defaults'],
      'grouptype_current' => $grouptype_form['current'],
      'grouptype_subscription_fields' => $grouptype_form['subscription_fields'],
    );
    $content_types = array_filter(variable_get('og_notifications_content_types', array()));
    $content_names = node_get_types('names');
    foreach ($account->og_groups as $gid => $group) {
      $group_index = 'groups-' . $gid;
      $types = array();
      foreach ($content_types as $type => $title) {

        // Check if value exists; Only enable node type subscriptions if the
        // group is not already subscribed to (for all posts).
        $bool = isset($ngrouptypes[$gid]) && isset($ngrouptypes[$gid][$type]);
        $types[$type] = array(
          'title' => check_plain($content_names[$type]),
          'checkbox' => $bool,
          'send_interval' => $bool ? $ngrouptypes[$gid][$type]->send_interval : $grouptype_form['defaults']['#value']['send_interval'],
          'send_method' => $bool ? $ngrouptypes[$gid][$type]->send_method : $grouptype_form['defaults']['#value']['send_method'],
        );
      }
      $bool = isset($ngroups[$gid]);
      $all = array(
        'all' => array(
          'title' => t('All'),
          'checkbox' => $bool,
          'send_interval' => $bool ? $ngroups[$gid]->send_interval : $group_form['defaults']['#value']['send_interval'],
          'send_method' => $bool ? $ngroups[$gid]->send_method : $group_form['defaults']['#value']['send_method'],
        ),
      );
      $types = $all + $types;
      $form[$group_index] = array(
        '#type' => 'fieldset',
        '#title' => check_plain($group['title']),
        '#collapsible' => TRUE,
        '#collapsed' => !isset($ngroups[$gid]) && empty($ngrouptypes[$gid]),
      );

      // Reuse theme function.
      $form[$group_index]['subscriptions'] = array(
        '#tree' => TRUE,
        '#parents' => array(
          'groups',
          $gid,
        ),
        '#theme' => 'notifications_form_table',
        '#header' => array(
          theme('table_select_header_cell'),
          t('Type'),
          t('Send interval'),
          t('Send method'),
        ),
      );
      foreach ($types as $key => $value) {
        $form[$group_index]['subscriptions']['checkbox'][$key] = array(
          '#type' => 'checkbox',
          '#default_value' => $value['checkbox'],
        );
        $form[$group_index]['subscriptions']['title'][$key] = array(
          '#value' => t('%type posts in this group', array(
            '%type' => $value['title'],
          )),
        );
        $form[$group_index]['subscriptions']['send_interval'][$key] = array(
          '#type' => 'select',
          '#options' => _notifications_send_intervals(),
          '#default_value' => $value['send_interval'],
        );
        $form[$group_index]['subscriptions']['send_method'][$key] = array(
          '#type' => 'select',
          '#options' => _notifications_send_methods(),
          '#default_value' => $value['send_method'],
        );
      }
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
  }
  else {
    $form = array();
    drupal_set_message(t('There are no active group subscriptions available.'));
  }
  return $form;
}