You are here

function mailgun_admin_settings in Mailgun 7

Menu callback: displays the Mailgun module settings page.

Parameters

array $form: Form render array.

array $form_state: Array containing form state values.

Return value

array An array containing form items to place on the module settings page.

1 string reference to 'mailgun_admin_settings'
mailgun_menu in ./mailgun.module
Implements hook_menu().

File

./mailgun.admin.inc, line 19
Administration page callbacks for Mailgun.

Code

function mailgun_admin_settings(array $form, array &$form_state) {

  // Check if the Mailgun PHP library is installed.
  if (!mailgun_check_library()) {
    drupal_set_message(t('The Mailgun PHP library is not installed. Please see Installation section in the !link.', array(
      '!link' => l(t('documentation'), MAILGUN_DOCUMENTATION_LINK),
    )), 'error');
  }
  global $language;

  // Get title and description from mailgun_variable_info().
  module_load_include('inc', 'mailgun', 'mailgun.variable');
  $variables = module_invoke('mailgun', 'variable_info', array(
    'language' => $language,
  ));
  $key = variable_get(MAILGUN_API_KEY, '');
  $form[MAILGUN_API_KEY] = array(
    '#title' => $variables[MAILGUN_API_KEY]['title'],
    '#type' => 'textfield',
    '#description' => $variables[MAILGUN_API_KEY]['description'],
    '#default_value' => $key,
    '#required' => TRUE,
  );
  $client = FALSE;
  if (!empty($key)) {
    try {
      $endpoint = !empty($form_state['values'][MAILGUN_API_ENDPOINT]) ? $form_state['values'][MAILGUN_API_ENDPOINT] : '';
      $client = mailgun_get_client($key, $endpoint);
    } catch (Exception $e) {
      watchdog('mailgun', 'An exception occurred. @code: @message', array(
        '@code' => $e
          ->getCode(),
        '@message' => $e
          ->getMessage(),
      ), WATCHDOG_WARNING, MAILGUN_ADMIN_PAGE);
      drupal_set_message(t('Mailgun: %message', array(
        '%message' => $e
          ->getMessage(),
      )), 'error');
    }
  }

  // Display settings only when a valid API key is present and client is active.
  if ($client) {
    $domain_options = array(
      '_sender' => t('Get domain from sender address'),
    );
    $result = $client
      ->domains()
      ->index();
    if (!empty($result)) {
      if ($result
        ->getTotalCount() > 100) {
        $result = $client
          ->domains()
          ->index($result
          ->getTotalCount());
      }
      $options = [];
      foreach ($result
        ->getDomains() as $domain) {
        $options[$domain
          ->getName()] = $domain
          ->getName();
      }
      ksort($options);
      $domain_options = array_merge($domain_options, $options);
    }
    $form[MAILGUN_DOMAIN] = array(
      '#title' => $variables[MAILGUN_DOMAIN]['title'],
      '#type' => 'select',
      '#options' => $domain_options,
      '#description' => $variables[MAILGUN_DOMAIN]['description'],
      '#default_value' => variable_get(MAILGUN_DOMAIN, '_sender'),
      '#prefix' => '<div id="mailgun-domains-wrapper">',
      '#suffix' => '</div>',
    );
    $form[MAILGUN_API_ENDPOINT] = array(
      '#title' => $variables[MAILGUN_API_ENDPOINT]['title'],
      '#type' => 'select',
      '#options' => [
        'https://api.mailgun.net' => t('Default (US)'),
        'https://api.eu.mailgun.net' => t('Europe'),
      ],
      '#description' => $variables[MAILGUN_API_ENDPOINT]['description'],
      '#default_value' => variable_get(MAILGUN_API_ENDPOINT, $variables[MAILGUN_API_ENDPOINT]['default']),
      '#ajax' => [
        'wrapper' => 'mailgun-domains-wrapper',
        'callback' => 'mailgun_admin_domains_callback',
      ],
    );
    $form[MAILGUN_TEST_MODE] = array(
      '#title' => $variables[MAILGUN_TEST_MODE]['title'],
      '#type' => 'checkbox',
      '#default_value' => variable_get(MAILGUN_TEST_MODE, FALSE),
      '#description' => $variables[MAILGUN_TEST_MODE]['description'],
    );
    $form[MAILGUN_LOG_EMAILS] = array(
      '#title' => $variables[MAILGUN_LOG_EMAILS]['title'],
      '#type' => 'checkbox',
      '#description' => $variables[MAILGUN_LOG_EMAILS]['description'],
      '#default_value' => variable_get(MAILGUN_LOG_EMAILS, FALSE),
    );
    $form['extra'] = array(
      '#type' => 'fieldset',
      '#title' => t('Additional settings'),
      '#description' => t('These default settings apply to messages sent using Mailgun and may be overriden on a per-message basis.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );

    // We have the same options for all settings.
    $options = array(
      'default' => t('Use default setting'),
      'enabled' => t('Enabled'),
      'disabled' => t('Disabled'),
    );
    $form['extra']['tracking'] = array(
      '#type' => 'fieldset',
      '#title' => t('Tracking'),
    );
    $form['extra']['tracking'][MAILGUN_TRACKING] = array(
      '#title' => $variables[MAILGUN_TRACKING]['title'],
      '#type' => 'select',
      '#options' => $options,
      '#description' => $variables[MAILGUN_TRACKING]['description'],
      '#default_value' => variable_get(MAILGUN_TRACKING, 'default'),
    );
    $form['extra']['tracking'][MAILGUN_TRACKING_CLICKS] = array(
      '#title' => $variables[MAILGUN_TRACKING_CLICKS]['title'],
      '#type' => 'select',
      '#options' => $options,
      '#description' => $variables[MAILGUN_TRACKING_CLICKS]['description'],
      '#default_value' => variable_get(MAILGUN_TRACKING_CLICKS, 'default'),
    );
    $form['extra']['tracking'][MAILGUN_TRACKING_OPENS] = array(
      '#title' => $variables[MAILGUN_TRACKING_OPENS]['title'],
      '#type' => 'select',
      '#options' => $options,
      '#description' => $variables[MAILGUN_TRACKING_OPENS]['description'],
      '#default_value' => variable_get(MAILGUN_TRACKING_OPENS, 'default'),
    );
    $formats = array(
      '_none' => t('- None -'),
    );
    foreach (filter_formats() as $format) {
      if ($format->format === 'php_code') {
        continue;
      }
      $formats[$format->format] = $format->name;
    }
    $form['extra'][MAILGUN_FORMAT] = array(
      '#title' => $variables[MAILGUN_FORMAT]['title'],
      '#type' => 'select',
      '#description' => $variables[MAILGUN_FORMAT]['description'],
      '#options' => $formats,
      '#default_value' => variable_get(MAILGUN_FORMAT, '_none'),
    );
    $form['extra'][MAILGUN_QUEUE] = array(
      '#title' => $variables[MAILGUN_QUEUE]['title'],
      '#type' => 'checkbox',
      '#description' => $variables[MAILGUN_QUEUE]['description'],
      '#default_value' => variable_get(MAILGUN_QUEUE, FALSE),
    );
    $form['extra'][MAILGUN_TAGGING_MAILKEY] = array(
      '#type' => 'checkbox',
      '#title' => $variables[MAILGUN_TAGGING_MAILKEY]['title'],
      '#description' => $variables[MAILGUN_TAGGING_MAILKEY]['description'],
      '#default_value' => variable_get(MAILGUN_TAGGING_MAILKEY, TRUE),
    );
  }
  $form = system_settings_form($form);
  $form['#validate'][] = 'mailgun_admin_settings_validate';
  return $form;
}