<?php
/**
 * @file
 * The backoffice of module.
 */

/**
 * Implements hook_settings().
 */
function mailjet_admin_settings() {
  $form['onoff'] = array(
    '#type'  => 'fieldset',
    '#title' => t('General settings'),
  );
  $form['onoff']['mailjet_on'] = array(
    '#type'          => 'radios',
    '#title'         => t('Send emails through Mailjet'),
    '#default_value' => variable_get('mailjet_on', FALSE),
    '#options'       => array(1 => t('Yes'), 0 => t('No')),
  );

  $form['onoff']['mailjet_from'] = array(
    '#type'          => 'textfield',
    '#title'         => t('E-mail from address'),
    '#default_value' => variable_get('mailjet_from', variable_get('site_mail', '')),
    '#description'   => t('The e-mail address that all e-mails will be from.'),
    '#required'      => TRUE,
  );

  $form['onoff']['mailjet_test'] = array(
    '#type'          => 'radios',
    '#title'         => t('Send test mail now'),
    '#default_value' => variable_get('mailjet_test', FALSE),
    '#options'       => array(1 => t('Yes'), 0 => t('No')),
  );

  $form['onoff']['mailjet_test_address'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Recipient of test mail'),
    '#default_value' => variable_get('mailjet_test_address', ''),
  );

  $form['auth'] = array(
    '#type'        => 'fieldset',
    '#title'       => t('Mailjet settings'),
  );
  $form['auth']['mailjet_username'] = array(
    '#type'          => 'textfield',
    '#title'         => t('API Key'),
    '#default_value' => variable_get('mailjet_username', ''),
    '#required'      => TRUE,
  );
  $form['auth']['mailjet_password'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Secret Key'),
    '#default_value' => variable_get('mailjet_password', ''),
    '#required'      => TRUE,
  );

  $test_address = variable_get('mailjet_test_address', '');

  if (variable_get('mailjet_test', FALSE) && valid_email_address($test_address)) {
    $mailjet_on = variable_get('mailjet_on', FALSE);

    variable_set('mail_system', array('default-system' => 'MailjetSmtpMailSystem'));
    variable_set('mailjet_test', FALSE);

    global $language;

    $params['subject'] = t('Your test mail from Mailjet');
    $params['body']    = array(t('Your Mailjet configuration is ok!'));
    $mail = drupal_mail('mailjet', 'mailjet-test', $test_address, $language, $params);

    variable_set('mailjet_on', $mailjet_on);

    if (isset($mail['result']) && $mail['result']) {
      drupal_set_message(t('A test e-mail has been sent to @email.', array('@email' => check_plain($test_address))));
    }
  }

  if (variable_get('mailjet_on', FALSE)) {
    variable_set('mail_system', array('default-system' => 'MailjetSmtpMailSystem'));
  }
  else {
    variable_set('mail_system', array('default-system' => 'DefaultMailSystem'));
  }

  return system_settings_form($form);
}

/**
 * Implements hook_admin_settings_validat().
 */
function mailjet_admin_settings_validate($form, &$form_state) {

  if (!valid_email_address($form_state['values']['mailjet_from'])) {
    form_set_error('mailjet_from', t('The provided from e-mail address is not valid.'));
  }

  if ($form_state['values']['mailjet_test'] && !valid_email_address($form_state['values']['mailjet_test_address'])) {
    form_set_error('mailjet_test_address', t('The provided test e-mail address is not valid.'));
  }

  $configs = array(array('ssl://', 465),
    array('tls://', 587),
    array('', 587),
    array('', 588),
    array('tls://', 25),
    array('', 25));

  $host = variable_get('mailjet_host', 'in.mailjet.com');
  $connected = FALSE;

  for ($i = 0; $i < count($configs); ++$i) {
    $soc = @ fsockopen($configs[$i][0] . $host, $configs[$i][1], $errno, $errstr, 5);

    if ($soc) {
      fClose($soc);

      $connected = TRUE;

      break;
    }
  }

  if ($connected) {
    if ('ssl://' == $configs[$i][0]) {
      variable_set('mailjet_protocol', 'ssl');
    }
    elseif ('tls://' == $configs[$i][0]) {
      variable_set('mailjet_protocol', 'tls');
    }
    else {
      variable_set('mailjet_protocol', 'standard');
    }

    variable_set('mailjet_port', $configs[$i][1]);
  }
  else {
    form_set_error('mailjet_on', t('Please contact Mailjet support to sort this out.<br /><br />Error @errno - @errstr', array ('@errno' => $errno, '@errstr' => $errstr)));
  }
}
