You are here

function notifications_send_intervals_form in Notifications 6.3

Same name and namespace in other branches
  1. 5 notifications.admin.inc \notifications_send_intervals_form()
  2. 6.4 notifications.admin.inc \notifications_send_intervals_form()
  3. 6 notifications.admin.inc \notifications_send_intervals_form()
  4. 6.2 notifications.admin.inc \notifications_send_intervals_form()

Send intervals administration

1 string reference to 'notifications_send_intervals_form'
notifications_menu in ./notifications.module
Implementation of hook_menu().

File

./notifications.admin.inc, line 120

Code

function notifications_send_intervals_form() {

  // Collect information about building methods and print out some help
  $form['build'] = array(
    '#title' => t('Build formats'),
    '#type' => 'fieldset',
    '#description' => t('These are the building formats available for each interval.'),
  );
  foreach (notifications_build_method() as $type => $method) {
    $build_methods[$type] = $method['name'];
    $rows[] = array(
      $method['name'],
      $method['description'],
    );
  }
  $form['build']['info'] = array(
    '#value' => theme('table', array(), $rows),
  );

  // Build options. Need some complex formatting.
  $units = array(
    60 => t('Minutes'),
    60 * 60 => t('Hours'),
    24 * 60 * 60 => t('Days'),
  );
  $form['intervals'] = array(
    '#type' => 'fieldset',
    '#title' => t('Send intervals'),
    '#tree' => TRUE,
    //'#theme' => 'notifications_send_intervals',
    '#description' => t('To delete an interval, set the time unit and the name empty. If the time is negative, notifications will never be sent.'),
  );
  $intervals = _notifications_send_intervals() + array(
    'new1' => '',
    'new2' => '',
  );

  // We need to use different indexes (not time value) as negative ones won't work
  $index = 0;
  $index_name = $time_index = array();
  foreach ($intervals as $time => $name) {

    // Store index => name mapping
    $index_options[$index] = '';
    $time_index[$time] = $index;

    // Calculate value and unit
    $current = $number = 0;
    if (!is_numeric($time)) {
      $number = $unit = '';
    }
    elseif ($time >= 0) {
      foreach (array_reverse(array_keys($units)) as $unit) {
        if ($time % $unit == 0) {
          $current = $unit;
          $number = $time / $unit;
          break;
        }
      }
    }
    else {

      // May be -1 for 'Never'
      $number = $time;
      $unit = '';
    }
    $form['intervals'][$index]['time'] = array(
      '#default_value' => $number,
      '#type' => 'textfield',
      '#size' => 2,
    );
    $form['intervals'][$index]['unit'] = array(
      '#type' => 'select',
      '#options' => array(
        '' => '',
      ) + $units,
      '#default_value' => $unit,
    );
    $form['intervals'][$index]['name'] = array(
      '#type' => 'textfield',
      '#default_value' => $name,
      '#size' => 40,
    );
    $build = notifications_build_method($time);
    $form['intervals'][$index]['build'] = array(
      '#type' => 'select',
      '#disabled' => $number < 0,
      '#default_value' => $build ? $build['type'] : 'simple',
      '#options' => $build_methods,
    );

    // Store also index -> time mapping
    $form['intervals'][$index]['key'] = array(
      '#type' => 'value',
      '#value' => $time,
    );
    $index++;
  }
  $form['default'] = array(
    '#type' => 'radios',
    '#options' => $index_options,
    '#default_value' => $time_index[variable_get('notifications_default_send_interval', 0)],
  );

  // New row
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );
  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset to defaults'),
  );
  return $form;
}