You are here

function notifications_forum_user_forum_form in Forum notifications 7

Same name and namespace in other branches
  1. 6 notifications_forum.module \notifications_forum_user_forum_form()
1 string reference to 'notifications_forum_user_forum_form'
notifications_forum_menu in ./notifications_forum.module
Implements hook_menu().

File

./notifications_forum.module, line 216

Code

function notifications_forum_user_forum_form($form, &$form_state, $account) {
  $form = array();
  $account = messaging_user_object($account);
  $send_elements = array(
    'send_method' => t('Send method'),
    'send_interval' => t('Send interval'),
  );

  // lookup existing subscriptions and index by 'tid'
  $subs = array();
  foreach (notifications_get_subscriptions(array(
    'type' => 'taxonomy_term',
    'uid' => $account->uid,
  )) as $sub) {
    $tid = $sub
      ->get_field('term:tid')->value;
    $subs[$tid] = $sub;
  }
  $form['account'] = array(
    '#type' => 'value',
    '#value' => $account,
  );
  $form['info'] = array(
    '#type' => 'item',
    '#title' => t('@type subscriptions', array(
      '@type' => 'Forum',
    )),
    '#description' => t('Subscribe to all threads posted on a forum.'),
  );
  $form['subscriptions'] = array(
    '#theme' => 'notifications_forum_user_subscriptions_form',
    '#tree' => TRUE,
  );
  $forums = forum_forum_load();
  $send_methods = messaging_method_list();
  $send_intervals = notifications_send_intervals();
  if (count($forums->forums) > 0 && count($send_methods) > 0 && count($send_intervals) > 0) {
    foreach ($forums->forums as $forum) {
      if (!empty($forum->container)) {

        // TODO: handle containers better!
        continue;
      }
      $sub = !empty($subs[$forum->tid]) ? $subs[$forum->tid] : NULL;
      $wrapper = array();
      $wrapper['subscribe'] = array(
        '#type' => 'checkbox',
        '#title' => l($forum->name, "forum/{$forum->tid}"),
        '#default_value' => !is_null($sub),
      );
      $wrapper['tid'] = array(
        '#type' => 'value',
        '#value' => $forum->tid,
      );
      if (!is_null($sub)) {
        $wrapper['sid'] = array(
          '#type' => 'value',
          '#value' => $sub->sid,
        );
      }
      $wrapper['description'] = array(
        '#type' => 'item',
        '#title' => t('Description'),
        '#markup' => $forum->description,
      );
      foreach ($send_elements as $send_element_type => $send_element_title) {
        _notifications_forum_send_element($wrapper, $send_element_type, $send_element_title, $sub, $account);
      }
      $form['subscriptions'][] = $wrapper;
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
  }
  return $form;
}