You are here

function mailhandler_add_edit_mailbox in Mailhandler 6

Same name and namespace in other branches
  1. 7 mailhandler.admin.inc \mailhandler_add_edit_mailbox()

Return a form for editing or creating an individual mailbox.

1 string reference to 'mailhandler_add_edit_mailbox'
mailhandler_menu in ./mailhandler.module
Implementation of hook_menu().

File

./mailhandler.admin.inc, line 13
Administrator pages for the Mailhandler module.

Code

function mailhandler_add_edit_mailbox($form_state, $mailbox = FALSE, $clone = FALSE) {

  // @todo: remove this line when decoupling retrieving. used to get information
  // about authentication methods (where authentication has to be decoupled).
  module_load_include('inc', 'mailhandler', 'mailhandler.retrieve');

  // $mid parameter is expected to be a valid mailbox or NULL. If mid is defined
  // but no mailbox is found for this id, then return a 404, as the only way to
  // reach this situation is by a wrong link.
  if ($mailbox && !isset($mailbox['mid'])) {
    drupal_not_found();
    return;
  }

  // If there is a not finished form submission, get the current form values and
  // populate the form fields.
  if (isset($form_state['mailbox'])) {
    $mailbox = $form_state['mailbox'] + (array) $mailbox;
  }

  // Attach default fields for new created mailboxes.
  $mailbox = $mailbox ? $mailbox : mailhandler_mailbox_defaults();
  $form['mail'] = array(
    '#type' => 'textfield',
    '#title' => t('E-mail address'),
    '#default_value' => $mailbox['mail'],
    '#description' => t('The e-mail address to which users should send their submissions.'),
    '#required' => TRUE,
  );

  // Currently has no known usage in mailhandler module, but other modules (e.g.
  // listhandler) may be making use of this.
  // @todo: verify usage and decide removal or not.
  $form['mailto'] = array(
    '#type' => 'textfield',
    '#title' => t('Second E-mail address'),
    '#default_value' => $mailbox['mailto'],
    '#description' => t('Optional. The e-mail address to which modules should send generated content.'),
  );
  $form['folder'] = array(
    '#type' => 'textfield',
    '#title' => t('Folder'),
    '#default_value' => $mailbox['folder'],
    '#description' => t('Optional. The folder where the mail is stored. If you want this mailbox to read from a local folder, give the full path. Leave domain, port, name, and pass empty below. Remember to set the folder to readable and writable by the webserver.'),
  );
  $form['imap'] = array(
    '#type' => 'select',
    '#title' => t('POP3 or IMAP Mailbox'),
    '#options' => array(
      'POP3',
      'IMAP',
    ),
    '#default_value' => $mailbox['imap'],
    '#description' => t('If you wish to retrieve mail from a POP3 or IMAP mailbox instead of a Folder, select POP3 or IMAP. Also, complete the Mailbox items below.'),
  );
  $form['domain'] = array(
    '#type' => 'textfield',
    '#title' => t('Mailbox domain'),
    '#default_value' => $mailbox['domain'],
    '#description' => t('The domain of the server used to collect mail.'),
  );
  $form['port'] = array(
    '#type' => 'textfield',
    '#title' => t('Mailbox port'),
    '#size' => 5,
    '#maxlength' => 5,
    '#default_value' => $mailbox['port'],
    '#description' => t('The port of the mailbox used to collect mail (usually 110 for POP3, 143 for IMAP).'),
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Mailbox username'),
    '#default_value' => $mailbox['name'],
    '#description' => t('This username is used while logging into this mailbox during mail retrieval.'),
  );
  $form['pass'] = array(
    '#type' => 'textfield',
    '#title' => t('Mailbox password'),
    '#default_value' => $mailbox['pass'],
    '#description' => t('The password corresponding to the username above. Consider using a non-vital password, since this field is stored without encryption in the database.'),
  );

  // Allow administrators to configure the mailbox with extra IMAP commands (notls, novalidate-cert etc.)
  $form['extraimap'] = array(
    '#type' => 'textfield',
    '#title' => t('Extra commands'),
    '#default_value' => $mailbox['extraimap'],
    '#description' => t('Optional. In some circumstances you need to issue extra commands to connect to your mail server (e.g. "/notls", "/novalidate-cert" etc.). See documentation for <a href="http://php.net/imap_open">imap_open</a>. Begin the string with a "/", separating each subsequent command with another "/".'),
  );
  $form['mime'] = array(
    '#type' => 'select',
    '#title' => t('MIME preference'),
    '#options' => array(
      'TEXT/HTML,TEXT/PLAIN' => 'HTML',
      'TEXT/PLAIN,TEXT/HTML' => t('Plain text'),
    ),
    '#default_value' => $mailbox['mime'],
    '#description' => t('When a user sends an e-mail containing both HTML and plain text parts, use this part as the node body.'),
  );
  $form['security'] = array(
    '#type' => 'radios',
    '#title' => t('Security'),
    '#options' => array(
      t('Disabled'),
      t('Require password'),
    ),
    '#default_value' => $mailbox['security'],
    '#description' => t('Disable security if your site does not require a password in the Commands section of incoming e-mails. Note: Security=Enabled and MIME preference=HTML is an unsupported combination.'),
  );
  $form['replies'] = array(
    '#type' => 'radios',
    '#description' => t('Send helpful replies to all unsuccessful e-mail submissions. Consider disabling when a listserv posts to this mailbox.'),
    '#title' => t('Send error replies'),
    '#options' => array(
      t('Disabled'),
      t('Enabled'),
    ),
    '#default_value' => $mailbox['replies'],
  );
  $form['fromheader'] = array(
    '#type' => 'textfield',
    '#title' => t('From header'),
    '#default_value' => $mailbox['fromheader'],
    '#description' => t('Use this e-mail header to determine the author of the resulting node. Admins usually leave this field blank (thus using the <strong>From</strong> header), but <strong>Sender</strong> is also useful when working with listservs.'),
  );
  $form['commands'] = array(
    '#type' => 'textarea',
    '#title' => t('Default commands'),
    '#default_value' => $mailbox['commands'],
    '#description' => t('A set of commands which are added to each message. One command per line. See <a href="@link">Commands</a>.', array(
      '@link' => url('admin/help/mailhandler/#commands'),
    )),
  );
  $form['sigseparator'] = array(
    '#type' => 'textfield',
    '#title' => t('Signature separator'),
    '#default_value' => $mailbox['sigseparator'],
    '#description' => t('All text after this string will be discarded. A typical value is <strong>"-- "</strong> that is two dashes followed by a blank in an otherwise empty line. Leave blank to include signature text in nodes.'),
  );
  $form['delete_after_read'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete messages after they are processed?'),
    '#default_value' => $mailbox['delete_after_read'],
    '#description' => t('Uncheck this box to leave read messages in the mailbox. They will not be processed again unless they become marked as unread.  If you selected "POP3" as your mailbox type, you must check this box.'),
  );
  $form['enabled'] = array(
    '#type' => 'radios',
    '#title' => t('Cron processing'),
    '#options' => array(
      t('Disabled'),
      t('Enabled'),
    ),
    '#default_value' => $mailbox['enabled'],
    '#description' => t('Select disable to temporarily stop cron processing for this mailbox.'),
  );

  // Allow administrators to select the format of saved nodes/comments
  $form['format'] = filter_form($mailbox['format']);
  $options = array();
  $descriptions = '';
  $options[0] = t('Choose method');
  $methods = mailhandler_mailhandler_authenticate('info');
  foreach ($methods as $method) {
    foreach ($method as $name => $info) {
      $options[$name] = t($info['title']);

      // TODO: theme function
      $descriptions .= t($info['title']) . ": " . t($info['description']) . "\n";
    }
  }
  $form['authentication'] = array(
    '#type' => 'select',
    '#title' => t('Message authentication method'),
    '#options' => $options,
    '#description' => t('Following is the name and description of each available authentication method') . "\n\n" . $descriptions,
    '#default_value' => $mailbox['authentication'],
  );
  $form['actions'] = array(
    '#prefix' => '<p><div class="confirmation container-inline">',
    '#suffix' => '</div></p>',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save mailbox'),
  );
  $form['actions']['test'] = array(
    '#type' => 'submit',
    '#value' => t('Test connection'),
    '#submit' => array(
      'mailhandler_mailbox_test_submit',
    ),
  );

  // Save mailbox Id for edited mailboxes only. Also show Delete button.
  if (isset($mailbox['mid']) && !$clone) {
    $form['mid'] = array(
      '#type' => 'value',
      '#value' => $mailbox['mid'],
    );
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete mailbox'),
      '#submit' => array(
        'mailhandler_delete_mailbox_submit',
      ),
      '#validate' => array(),
    );
  }
  $form['actions']['cancel'] = array(
    '#value' => l(t('Cancel'), 'admin/content/mailhandler'),
  );
  return $form;
}