You are here

function notifications_subscription_base_form in Notifications 7

Same name and namespace in other branches
  1. 6.4 notifications.pages.inc \notifications_subscription_base_form()

Base subscription form. Present the main fields and check all values

1 call to notifications_subscription_base_form()
notifications_subscription_user_form in notifications_account/notifications_account.module
Main Subscription form builder

File

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

Code

function notifications_subscription_base_form($form_state, $subscription, $destination_path = NULL) {

  // We may have a destination query string instead of a destination path
  if (!empty($_REQUEST['destination'])) {
    $destination_path = $_REQUEST['destination'];
  }
  if (isset($destination_path)) {
    $form['#redirect'] = $destination_path;
  }

  // Check the subscription object is built properly
  $subscription = notifications_subscription_build($subscription);

  // Pass on whole subscription and then the fields to be saved along
  $form['subscription'] = array(
    '#type' => 'value',
    '#value' => $subscription,
  );
  $account = $subscription
    ->get_user();
  $save_fields = array();

  // The subscription description will be added here, fields may be editable or not
  if (!$subscription
    ->is_editable()) {

    // Just display general information, no editable fields
    $form['info'] = $subscription
      ->form_info();
  }
  else {
    $form['fields'] = $subscription
      ->fields_subform();
    $save_fields[] = 'fields';
  }
  if ($subscription
    ->is_instance()) {
    $form['info']['#title'] = t('Edit subscription');
  }
  else {
    $form['info']['#title'] = t('Create @type subscription', array(
      '@type' => $subscription
        ->get_title(),
    ));
  }

  // Send interval, visible only if more than one
  if ($send_intervals = notifications_send_intervals($account)) {
    $save_fields[] = 'send_interval';
    if (count($send_intervals) == 1) {
      $form['send_interval'] = array(
        '#type' => 'value',
        '#value' => key($send_intervals),
      );
    }
    else {
      $form['notifications']['send_interval'] = array(
        '#type' => 'select',
        '#title' => t('Send interval'),
        '#options' => $send_intervals,
        '#default_value' => $subscription->send_interval,
      );
    }
  }
  else {
    drupal_set_message(t('No sending intervals available.'), 'error');
    $form['#error']['send_interval'] = TRUE;
  }

  // Send method, visible only if more than one
  if ($send_methods = notifications_send_methods($account)) {
    $save_fields[] = 'send_method';
    if (count($send_methods) == 1) {
      $form['send_method'] = array(
        '#type' => 'value',
        '#value' => key($send_methods),
      );
    }
    else {
      $form['notifications']['send_method'] = array(
        '#type' => 'select',
        '#title' => t('Send method'),
        '#options' => $send_methods,
        '#default_value' => $subscription->send_method,
        '#disabled' => count($send_methods) < 2,
      );
    }
  }
  else {
    drupal_set_message(t('No sending methods available.'), 'error');
    $form['#error']['send_method'] = TRUE;
  }

  // Add notifications fieldset if send method or send_interval
  if (!empty($form['notifications'])) {
    $form['notifications']['#title'] = t('Notifications');
    $form['notifications']['#type'] = 'fieldset';
    $form['notifications']['#description'] = t('How do you want to get notifications for this subscription.');
  }

  // Status field, show blocked only to administrators
  if ($subscription
    ->is_instance()) {
    $save_fields[] = 'status';
    $status = Notifications_Subscription::status_list();
    if (!user_access('administer notifications') && !user_access('manage all subscriptions')) {
      unset($status[Notifications_Subscription::STATUS_BLOCKED]);
    }
    $form['status'] = array(
      '#type' => 'radios',
      '#title' => t('Status'),
      '#options' => $status,
      '#default_value' => $subscription->status,
      '#description' => t('You can temporarily disable this subscription for not getting notifications.'),
    );
  }

  // Fields to be saved on submit. If error we just show the 'Cancel' button.
  $form['subscription_fields'] = array(
    '#type' => 'value',
    '#value' => $save_fields,
  );

  // Add form buttons depending on what we are doing
  if ($subscription
    ->is_instance()) {
    $form['actions']['save'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
  }
  elseif ($subscription
    ->is_editable()) {
    $form['actions']['subscribe'] = array(
      '#type' => 'submit',
      '#value' => t('Create subscription'),
    );
  }
  else {

    // Subscription parameters are fixed so this is just a confirmation form
    $form['actions']['subscribe'] = array(
      '#type' => 'submit',
      '#value' => t('Subscribe'),
    );
  }
  if (isset($destination_path)) {
    $form['actions']['cancel'] = array(
      '#value' => l(t('Cancel'), $destination_path),
    );
  }

  // Add the right callbacks
  $form['#submit'][] = 'notifications_subscription_form_submit';
  $form['#validate'][] = 'notifications_subscription_form_validate';
  return $form;
}