You are here

function mass_contact_mail_page in Mass Contact 7

Same name and namespace in other branches
  1. 5.2 mass_contact.module \mass_contact_mail_page()
  2. 5 mass_contact.module \mass_contact_mail_page()
  3. 6 mass_contact.module \mass_contact_mail_page()

Generates the main Mass Contact mail form.

Parameters

array $form: An associative array containing the structure of the form.

array $form_state: A keyed array containing the current state of the form.

Return value

array An associative array that defines the form to be built.

1 string reference to 'mass_contact_mail_page'
mass_contact_site_page in ./mass_contact.page.inc
The mail page.

File

./mass_contact.page.inc, line 37
The main form for creating and sending the messages.

Code

function mass_contact_mail_page(array $form, array $form_state) {
  global $user;
  $form = array();
  $categories = array();
  $default_category = array();
  $default_category_name = '';
  $result = db_select('mass_contact', 'mc')
    ->fields('mc', array(
    'cid',
    'category',
    'selected',
  ))
    ->orderBy('weight', 'ASC')
    ->orderBy('category', 'ASC')
    ->execute();
  foreach ($result as $category) {
    if (user_access('mass contact send to users in the ' . $category->category . ' category')) {
      $categories[$category->cid] = check_plain($category->category);
      if ($category->selected) {
        $default_category[] = $category->cid;
        $default_category_name = $category->category;
      }
    }
  }
  if (count($categories) == 1) {
    $default_category = array_keys($categories);
    $default_category_name = $categories[$default_category[0]];
  }
  if (count($categories) > 0) {
    $form['contact_information'] = array(
      '#markup' => filter_xss_admin(variable_get('mass_contact_form_information', t('Send an email message using the contact form below.'))),
    );

    // Add the field for specifying the sender's name.
    $mass_contact_default_sender_name = variable_get('mass_contact_default_sender_name', '');
    if ($mass_contact_default_sender_name) {
      if (user_access('mass contact change default sender information')) {
        $form['name'] = array(
          '#type' => 'textfield',
          '#title' => t('Your name'),
          '#maxlength' => 255,
          '#default_value' => $mass_contact_default_sender_name,
          '#required' => TRUE,
        );
      }
      else {
        $form['name'] = array(
          '#type' => 'item',
          '#title' => t('Your name'),
          '#markup' => $mass_contact_default_sender_name,
        );
      }
    }
    else {
      $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Your name'),
        '#maxlength' => 255,
        '#default_value' => $user->uid ? check_plain(format_username($user)) : '',
        '#required' => TRUE,
      );
    }

    // Add the field for specifying the sender's email address.
    $mass_contact_default_sender_email = variable_get('mass_contact_default_sender_email', '');
    if ($mass_contact_default_sender_email) {
      if (user_access('mass contact change default sender information')) {
        $form['mail'] = array(
          '#type' => 'textfield',
          '#title' => t('Your email address'),
          '#maxlength' => 255,
          '#default_value' => $mass_contact_default_sender_email,
          '#required' => TRUE,
        );
      }
      else {
        $form['mail'] = array(
          '#type' => 'item',
          '#title' => t('Your email address'),
          '#markup' => $mass_contact_default_sender_email,
        );
      }
    }
    else {
      $form['mail'] = array(
        '#type' => 'textfield',
        '#title' => t('Your email address'),
        '#maxlength' => 255,
        '#default_value' => $user->uid ? $user->mail : '',
        '#required' => TRUE,
      );
    }

    // Add the field for specifying the category(ies).
    if (count($categories) > 1 || !isset($default_category)) {

      // Display a choice when one is needed.
      $field_type = variable_get('mass_contact_category_display', 'select');
      $form['cid'] = array(
        '#type' => $field_type,
        '#title' => t('Category'),
        '#default_value' => $default_category,
        '#options' => $categories,
        '#required' => TRUE,
        '#multiple' => TRUE,
      );
    }
    else {

      // Otherwise, just use the default category.
      $form['cid'] = array(
        '#type' => 'value',
        '#value' => $default_category,
      );
      $form['cid-info'] = array(
        '#type' => 'item',
        '#title' => t('Category'),
        '#markup' => '<p>This message will be sent to all users in the ' . check_plain($default_category_name) . ' category.</p>',
      );
    }

    // Add the field for specifying whether opt-outs are respected or not.
    $optout_setting = variable_get('mass_contact_optout_d', 0);

    // Allow users to opt-out of mass emails:
    // 0 => 'No', 1 == 'Yes', 2 == 'Selected categories'.
    if ($optout_setting == 1 || $optout_setting == 2) {

      // Allow to override or respect opt-outs if admin, otherwise use default.
      if (user_access('mass contact administer')) {
        $form['optout'] = array(
          '#type' => 'checkbox',
          '#title' => t('Respect user opt-outs.'),
          '#default_value' => 1,
        );
      }
      else {
        $form['optout'] = array(
          '#type' => 'hidden',
          '#default_value' => 1,
        );
      }
    }
    else {
      $form['optout'] = array(
        '#type' => 'hidden',
        '#default_value' => 0,
      );
    }

    // Add the field for specifying whether the recipients are in the To or
    // BCC field of the message.
    // Check if the user is allowed to override the BCC setting.
    if (user_access('mass contact override bcc')) {
      $form['bcc'] = array(
        '#type' => 'checkbox',
        '#title' => t('Send as BCC (hide recipients)'),
        '#default_value' => variable_get('mass_contact_bcc_d', 1),
      );
    }
    else {
      $form['bcc'] = array(
        '#type' => 'value',
        '#value' => variable_get('mass_contact_bcc_d', 1),
      );
      $form['bcc-info'] = array(
        '#type' => 'item',
        '#title' => t('Send as BCC (hide recipients)'),
        '#markup' => '<p>' . (variable_get('mass_contact_bcc_d', 1) ? t('Recipients will be hidden from each other.') : t('Recipients will NOT be hidden from each other.')) . '</p>',
      );
    }

    // Add the field for specifying the subject of the message.
    $form['subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Subject'),
      '#maxlength' => 255,
      '#required' => TRUE,
    );

    // Add the field for specifying the body and text format of the message.
    // Get the HTML input format setting and the corresponding name.
    if (module_exists('mimemail')) {

      // Get the admin specified default text format.
      $default_filter_format = _mass_contact_compute_default_filter_format();

      // Check if the user is allowed to override the text format.
      if (user_access('mass contact override text format')) {

        // The user is allowed to override the text format, so display a
        // textarea that has the text format selection field attached.
        $form['body']['message'] = array(
          '#type' => 'text_format',
          '#title' => t('Message'),
          '#format' => !empty($default_filter_format) ? $default_filter_format : NULL,
          '#rows' => 12,
          '#required' => TRUE,
        );
      }
      else {

        // The user is not allowed to override the text format, so display a
        // textarea that does not have the text format selection field
        // attached.
        $form['body']['message'] = array(
          '#type' => 'textarea',
          '#title' => t('Message'),
          '#rows' => 12,
          '#required' => TRUE,
        );

        // Save the text format information for later use.
        $form['body_filter_format'] = array(
          '#type' => 'value',
          '#value' => $default_filter_format,
        );

        // Display text format information to the user.
        $form['body_filter_format_info'] = array(
          '#type' => 'item',
          '#description' => '<p>' . (variable_get('mass_contact_html_d', 1) ? t('The message will be sent using the %filter text format. More information about what is available is on the <a href="@formats_descriptions">Compose tips</a> page.', array(
            '%filter' => $default_filter_format,
            '@formats_descriptions' => url('filter/tips'),
          )) : t('The message will be sent as plain text.')) . '</p>',
        );
      }

      // If the user has access, add the field for specifying the attachment.
      if (user_access('mass contact include attachments')) {
        for ($i = 1; $i <= variable_get('mass_contact_number_of_attachments', '3'); $i++) {
          $form['attachment_' . $i] = array(
            '#type' => 'file',
            '#title' => t('Attachment #!number', array(
              '!number' => $i,
            )),
          );
        }
      }
    }
    else {

      // Mime Mail is not installed, so display a textarea that does not have
      // the text format selection field attached, as this message will only
      // be sent as plain text.
      $form['body']['message'] = array(
        '#type' => 'textarea',
        '#title' => t('Message'),
        '#rows' => 12,
        '#required' => TRUE,
      );

      // Save the text format information for later use.
      $form['body_filter_format'] = array(
        '#type' => 'value',
        '#value' => filter_format_load('plain_text'),
      );

      // Display text format information to the user.
      $form['body_filter_format_info'] = array(
        '#type' => 'item',
        '#description' => '<p>' . t('The message will be sent as plain text.') . '</p>',
      );
    }

    // We do not allow anonymous users to send themselves a copy because it
    // can be abused to spam people.
    if ($user->uid) {
      $form['copy'] = array(
        '#type' => 'checkbox',
        '#title' => t('Send yourself a copy.'),
      );
    }

    // Add the field for specifying whether to save the message as a node or
    // not.
    if (user_access('mass contact archive messages')) {

      // Check if the user is allowed to override the node copy setting.
      if (user_access('mass contact override archiving')) {
        $form['nodecc'] = array(
          '#type' => 'checkbox',
          '#title' => t('Archive a copy of this message on this website'),
          '#default_value' => variable_get('mass_contact_nodecc_d', 1),
        );
      }
      else {
        $form['nodecc'] = array(
          '#type' => 'hidden',
          '#default_value' => variable_get('mass_contact_nodecc_d', 1),
        );
        $form['nodecc_notice'] = array(
          '#type' => 'item',
          '#title' => t('Archive a copy of this message on this website'),
          '#markup' => t('A copy of this message will !not be archived on this website.', array(
            '!not' => variable_get('mass_contact_nodecc_d', 1) ? '' : 'not',
          )),
        );
      }
    }
    else {
      $form['nodecc'] = array(
        '#type' => 'hidden',
        '#default_value' => variable_get('mass_contact_nodecc_d', 1),
      );
      $form['nodecc_notice'] = array(
        '#type' => 'item',
        '#title' => t('Archive a copy of this message on this website'),
        '#markup' => t('A copy of this message will !not be archived on this website.', array(
          '!not' => variable_get('mass_contact_nodecc_d', 1) ? '' : 'not',
        )),
      );
    }

    // Add the submit button.
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Send email'),
    );
  }
  else {
    $form['error'] = array(
      '#markup' => '<p><b>' . t('Either you have not created any categories, or you are not allowed to send to any of the existing categories.') . '<br /><br />' . t('Either create at least one category of users to send to, or contact your system administer for access to the existing categories.') . '</b>',
    );
  }
  if (user_access('mass contact administer')) {
    $form['tasklist'] = array(
      '#type' => 'fieldset',
      '#title' => t('Related tasks'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#prefix' => '<p>',
    );
    $form['tasklist']['list'] = array(
      '#markup' => '<p><ol><li>' . l(t('Set Permissions'), 'admin/people/permissions', array(
        'fragment' => 'module-mass_contact',
      )) . '</li><li>' . l(t('List current categories'), 'admin/config/system/mass_contact') . '</li><li>' . l(t('Add new category'), 'admin/config/system/mass_contact/add') . '</li><li>' . l(t('Configure the module'), 'admin/config/system/mass_contact/settings') . '</li><li>' . l(t('Help'), 'admin/help/mass_contact') . '</li></ol>',
    );
  }
  return $form;
}