You are here

function contact_attach_form_alter in Contact Attach 6

Same name and namespace in other branches
  1. 5 contact_attach.module \contact_attach_form_alter()
  2. 7 contact_attach.module \contact_attach_form_alter()

Implementation of hook_form_alter().

File

./contact_attach.module, line 89
Allows attaching files to e-mails sent using the site-wide contact form.

Code

function contact_attach_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'contact_mail_user' && user_access('send attachments with user contact form') || $form_id == 'contact_mail_page' && user_access('send attachments with site-wide contact form') || $form_id == 'og_contact_mail_page' && user_access('send attachments with og contact form')) {
    for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
      $form['contact_attach_' . $i] = array(
        '#type' => 'file',
        '#title' => t('Attachment #!number', array(
          '!number' => $i,
        )),
        '#weight' => $i,
      );
    }

    // We do not allow anonymous users to send themselves a copy because it
    // can be abused to spam people.
    global $user;
    if ($user->uid) {
      $form['copy'] = array(
        '#type' => 'checkbox',
        '#title' => t('Send yourself a copy.'),
        '#weight' => $i,
      );
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Send e-mail'),
      '#weight' => $i + 1,
    );
    $form['#attributes']['enctype'] = 'multipart/form-data';

    // Empty the array to make sure our function is the only one called.
    if (!empty($form['#submit'])) {
      unset($form['#submit']);
    }

    // Set the validate and submit functions to our functions.
    switch ($form_id) {
      case 'contact_mail_user':
        $form['#validate'][] = 'contact_attach_contact_mail_user_validate';
        $form['#submit'][] = 'contact_attach_contact_mail_user_submit';
        break;
      case 'contact_mail_page':
        $form['#validate'][] = 'contact_attach_contact_mail_page_validate';
        $form['#submit'][] = 'contact_attach_contact_mail_page_submit';
        break;
      case 'og_contact_mail_page':
        $form['#validate'][] = 'contact_attach_og_contact_mail_page_validate';
        $form['#submit'][] = 'contact_attach_og_contact_mail_page_submit';
        break;
    }
  }
}