You are here

function mass_contact_mail_page in Mass Contact 6

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. 7 mass_contact.page.inc \mass_contact_mail_page()

Generates the main Mass Contact mail form.

Parameters

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

Return value

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.module
The mail page

File

./mass_contact.module, line 1057
This is the main code file for the Mass Contact module. This module enables users to contact multiple users through selected roles.

Code

function mass_contact_mail_page($form_state) {
  global $user;
  $categories = array();
  $default_category = array();
  $default_category_name = '';
  $result = db_query("SELECT cid, category, selected FROM {mass_contact} ORDER BY weight, category");
  while ($category = db_fetch_object($result)) {
    if (user_access('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['#attributes'] = array(
      'enctype' => "multipart/form-data",
    );
    $form['#token'] = $user->name . $user->mail;
    $form['contact_information'] = array(
      '#value' => filter_xss_admin(variable_get('mass_contact_form_information', t('Send an e-mail 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 (variable_get('mass_contact_default_sender_changable', 0)) {
        $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'),
          '#value' => $mass_contact_default_sender_name,
        );
      }
    }
    else {
      $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Your name'),
        '#maxlength' => 255,
        '#default_value' => $user->uid ? $user->name : '',
        '#required' => TRUE,
      );
    }

    ////////////////////////////////////////////////////////////

    // Add the field for specifying the sender's e-mail address.
    $mass_contact_default_sender_email = variable_get('mass_contact_default_sender_email', '');
    if ($mass_contact_default_sender_email) {
      if (variable_get('mass_contact_default_sender_changable', 0)) {
        $form['mail'] = array(
          '#type' => 'textfield',
          '#title' => t('Your e-mail address'),
          '#maxlength' => 255,
          '#default_value' => $mass_contact_default_sender_email,
          '#required' => TRUE,
        );
      }
      else {
        $form['mail'] = array(
          '#type' => 'item',
          '#title' => t('Your e-mail address'),
          '#value' => $mass_contact_default_sender_email,
        );
      }
    }
    else {
      $form['mail'] = array(
        '#type' => 'textfield',
        '#title' => t('Your e-mail 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' => 'markup',
        '#value' => '<p>Sending to all users subscribed to 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);
    if ($optout_setting == 1 || $optout_setting == 2) {

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

    ////////////////////////////////////////////////////////////

    // Add the field for specifying whether the recipients are in the To or
    // BCC field of the message.
    // Check if the user can override the BCC setting.
    if (variable_get('mass_contact_bcc_d_override', 1)) {
      $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' => 'markup',
        '#value' => '<p>' . (variable_get('mass_contact_bcc_d', 1) ? t('Recipients will be hidden.') : t('Recipients will NOT be hidden.')) . '</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 of the message.
    $form['body_filter']['message'] = array(
      '#type' => 'textarea',
      '#title' => t('Message'),
      '#rows' => 12,
      '#required' => TRUE,
    );

    ////////////////////////////////////////////////////////////

    // Add the fields for specifying whether the message is in HTML format or
    // not, and what the input format is.
    // Get the HTML input format setting and the corresponding name.
    $html_filter_format = variable_get('mass_contact_html_format', FILTER_FORMAT_DEFAULT);
    if ($html_filter_format) {
      $filter_formats = filter_formats();
      foreach ($filter_formats as $filter_format) {
        if ($filter_format->format == $html_filter_format) {
          $html_filter_format_name = $filter_format->name;
        }
      }
    }
    else {
      $html_filter_format_name = 'the default';
    }

    // Check if the user is allowed override the HTML setting.
    if (variable_get('mass_contact_html_d_override', 1)) {
      $form['html'] = array(
        '#type' => 'checkbox',
        '#title' => t('Send as HTML.'),
        '#default_value' => variable_get('mass_contact_html_d', 1),
      );

      // Check if the user is allowed to override the input format setting.
      if (variable_get('mass_contact_html_format_override', 0)) {

        // The user is allowed, so add that to the form.
        $form['body_filter']['format'] = filter_form($html_filter_format, NULL, array(
          'mass_contact_html_format',
        ));
      }
      else {

        // The user is not allowed, so save the setting for later use.
        $form['html_format'] = array(
          '#type' => 'value',
          '#value' => $html_filter_format,
        );

        // Display information to the user.
        $form['html-info'] = array(
          '#value' => '<p>' . (variable_get('mass_contact_html_d', 1) ? t('The message will be sent using the !filter input format. More information about what is available is on the <a href="@formats_descriptions">Compose tips</a> page. ', array(
            '!filter' => $html_filter_format_name,
            '@formats_descriptions' => url('filter/tips'),
          )) : t('The message will be sent as plain text.')) . '</p>',
        );
      }
    }
    else {

      // The user is not allowed override the HTML setting.
      // Save the setting for later use.
      $form['html'] = array(
        '#type' => 'value',
        '#value' => variable_get('mass_contact_html_d', 1),
      );

      // Check if the user is allowed to override the input format setting.
      if (variable_get('mass_contact_html_format_override', 0)) {

        // Display information to the user.
        $form['html-info'] = array(
          '#value' => '<p>' . (variable_get('mass_contact_html_d', 1) ? t('The message will be sent as HTML.') : t('The message will be sent as plain text.')) . '</p>',
        );

        // The user is allowed, so add that to the form.
        $form['body_filter']['format'] = filter_form($html_filter_format, NULL, array(
          'mass_contact_html_format',
        ));
      }
      else {

        // The user is not allowed, so save the setting for later use.
        $form['html_format'] = array(
          '#type' => 'value',
          '#value' => $html_filter_format,
        );

        // Display information to the user.
        $form['html-info'] = array(
          '#value' => '<p>' . (variable_get('mass_contact_html_d', 1) ? t('The message will be sent as HTML, using the !filter input format. More information about what is available is on the <a href="@formats_descriptions">Compose tips</a> page. ', array(
            '!filter' => $html_filter_format_name,
            '@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('send mass contact 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,
          )),
        );
      }
    }

    ////////////////////////////////////////////////////////////

    // 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('choose whether to archive mass contact messages')) {

      // Check if the user can override the node copy setting.
      if (variable_get('mass_contact_nodecc_d_override', 1)) {
        $form['nodecc'] = array(
          '#type' => 'checkbox',
          '#title' => t('Save a copy as a node.'),
          '#default_value' => variable_get('mass_contact_nodecc_d', 1),
        );
      }
      else {
        $form['nodecc'] = array(
          '#type' => 'hidden',
          '#default_value' => variable_get('mass_contact_nodecc_d', 1),
        );
      }
    }
    else {
      $form['nodecc'] = array(
        '#type' => 'hidden',
        '#default_value' => variable_get('mass_contact_nodecc_d', 1),
      );
    }

    /*
       // Place holder for future use.
       $form['preview'] = array(
         '#type' => 'button',
         '#value' => t('Preview')
       );
    */

    ////////////////////////////////////////////////////////////

    // Add the submit button.
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Send e-mail'),
    );
  }
  else {
    $form['error'] = array(
      '#value' => '<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('administer mass contact')) {
    $form['tasklist'] = array(
      '#type' => 'fieldset',
      '#title' => t('Related tasks'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#prefix' => '<p>',
    );
    $form['tasklist']['list'] = array(
      '#value' => '<p><ol>' . '<li>' . l('Set Permissions', 'admin/user/permissions', array(
        'fragment' => 'module-mass_contact',
      )) . '</li>' . '<li>' . l('List current categories', 'admin/build/mass_contact') . '</li>' . '<li>' . l('Add new category', 'admin/build/mass_contact/add') . '</li>' . '<li>' . l('Configure the module', 'admin/build/mass_contact/settings') . '</li>' . '<li>' . l('Help', 'admin/help/mass_contact') . '</li></ol>',
    );
  }
  return $form;
}