You are here

function notifications_subscription_list_form in Notifications 6.4

Same name and namespace in other branches
  1. 7 notifications.module \notifications_subscription_list_form()

List form for subscriptions of the same type for a user

@todo Add enable /disable operations @todo Add pager

Parameters

$type: Subscription type

$account: User account, owner of these subscriptions

$subscriptions: Predefined array of subscriptions to display, so we don't search more nor allow new ones.

$fields: Predefined field values for new subscriptions, as type => value array

1 call to notifications_subscription_list_form()
notifications_user_form in ./notifications.module
Generic subscriptions content form
1 string reference to 'notifications_subscription_list_form'
notifications_user_subscription_list_page in ./notifications.module
Generic user page for a subscription type

File

./notifications.pages.inc, line 597
User pages for Notifications

Code

function notifications_subscription_list_form($form_state, $subscription_type, $account, $subscriptions = NULL, $fields = array()) {
  if (isset($form_state['values']['op']) && $form_state['values']['op'] == t('Delete')) {
    if ($selected = array_filter($form_state['values']['subscriptions']['select'])) {
      $form = notifications_multiple_delete_confirm($form_state, $selected);
      return $form;
    }
  }
  notifications_include('object.inc');
  $account = messaging_user_object($account);
  $form['account'] = array(
    '#type' => 'value',
    '#value' => $account,
  );

  // Build a template subscription. These subscriptions should have a unique subscription variable field
  $template = notifications_build_subscription($subscription_type);
  $template
    ->set_account($account);
  $template
    ->set_fields($fields);
  $form['template'] = array(
    '#type' => 'value',
    '#value' => $template,
  );

  // Print template information: subscription type, description, fixed fields

  //$form['info'] = $template->form_info();
  $form['info'] = array(
    '#type' => 'item',
    '#title' => t('@type subscriptions', array(
      '@type' => $template
        ->get_title(),
    )),
    '#description' => $template
      ->get_description(),
  );

  // Just allow one field of each type to be editable
  $editable_fields = array();
  foreach ($template
    ->get_editable_fields() as $field) {
    $editable_fields[$field->field] = $field;
  }
  $form['editable_fields'] = array(
    '#type' => 'value',
    '#value' => $editable_fields,
  );
  if (!isset($subscriptions)) {

    // We get all subscriptions that at least have the given fields, but may have more
    $subscriptions = notifications_get_subscriptions(array(
      'type' => $subscription_type,
      'uid' => $account->uid,
    ), $fields, FALSE);
  }

  // If we don't have subscriptions and we cannot create new ones, just display a message
  if (!$subscriptions) {
    $form['warning']['#value'] = t("There are no subscriptions of this type.");
  }
  $form['current_subscriptions'] = array(
    '#type' => 'value',
    '#value' => $subscriptions,
  );
  $subscription_list = $subscriptions;

  // Add our template to the list, but not to the values in form
  if ($editable_fields && notifications_access_user_add($account, $subscription_type)) {
    $subscription_list['new'] = $template;
  }

  // Prepare other parameters to build the list
  $send_methods = notifications_send_methods($account);
  $send_intervals = notifications_send_intervals($account);

  // Build header with selector, fields, send interval, send method if apply
  $header = array(
    theme('table_select_header_cell'),
  );
  $table_fields = array(
    'select',
  );
  foreach ($editable_fields as $type => $field) {
    $header[] = notifications_field_format_name($field->field);
    $table_fields[] = 'field_' . $type;
  }

  // Hide Send method, send interval columns if only one
  if (count($send_intervals)) {
    $header[] = t('Send interval');
    $table_fields[] = 'send_interval';
  }
  else {
    $send_intervals = NULL;
  }
  if (count($send_methods) > 1) {
    $header[] = t('Send method');
    $table_fields[] = 'send_method';
  }
  else {
    $send_methods = NULL;
  }
  $form['subscriptions'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#theme' => 'notifications_table_form',
    '#table_header' => $header,
    '#table_fields' => $table_fields,
  );

  // Add our template to the subscription list and
  $elements = array();
  foreach ($subscription_list as $key => $subscription) {
    if ($subscription
      ->is_instance()) {
      $form['subscriptions']['select'][$key] = array(
        '#type' => 'checkbox',
        '#default_value' => FALSE,
      );
    }
    else {
      $form['subscriptions']['select']['new'] = array(
        '#type' => 'submit',
        '#value' => t('Add'),
      );
    }
    foreach ($editable_fields as $type => $field) {
      if ($subscription
        ->is_instance()) {
        $conditions = $subscription
          ->get_conditions();
        $value = $conditions[$type];
        $form['subscriptions']['field_' . $type][$key] = array(
          '#value' => notifications_field_format_value($type, $value),
        );
      }
      else {

        // Add new subscription, editable fields
        $form['subscriptions']['field_' . $type][$key] = notifications_field_form_element($type, NULL, $subscription, FALSE, FALSE, 30);
      }
    }
    if ($send_intervals) {
      $form['subscriptions']['send_interval'][$key] = array(
        '#type' => 'select',
        '#options' => $send_intervals,
        '#default_value' => $subscription->send_interval,
      );
    }
    if ($send_methods) {
      $form['subscriptions']['send_method'][$key] = array(
        '#type' => 'select',
        '#options' => $send_methods,
        '#default_value' => $subscription->send_method,
      );
    }
  }
  if ($subscriptions) {
    $form['operations'] = array(
      '#type' => 'fieldset',
    );
    $form['operations']['update'] = array(
      '#type' => 'submit',
      '#value' => t('Update'),
    );
    $form['operations']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );

    //$form['operations']['enable'] = array('#type' => 'submit', '#value' => t('Enable'));

    //$form['operations']['disable'] = array('#type' => 'submit', '#value' => t('Disable'));
  }
  return $form;
}