You are here

function pet_user_form in Previewable email templates 6

Same name and namespace in other branches
  1. 7 includes/pet.admin.inc \pet_user_form()

Multi-step PET form.

1 string reference to 'pet_user_form'
pet_menu in ./pet.module
Implementation of hook_menu().

File

./pet.admin.inc, line 265
Contains administrative pages for creating, editing, and deleting previewable email templates (PETs).

Code

function pet_user_form(&$form_state, $pet) {
  $step = empty($form_state['storage']['step']) ? 1 : $form_state['storage']['step'];
  $form_state['storage']['step'] = $step;
  $form_state['storage']['pet'] = $pet;

  // Get any query args
  $nid = $form_state['storage']['nid'] = pet_is_natural($_REQUEST['nid']) ? $_REQUEST['nid'] : NULL;
  $uid = $form_state['storage']['uid'] = pet_is_natural($_REQUEST['uid']) ? $_REQUEST['uid'] : NULL;
  $recipient_callback = $form_state['storage']['recipient_callback'] = pet_isset_or($_REQUEST['recipient_callback']) === 'true' || pet_isset_or($_REQUEST['uid']) === '0';
  switch ($step) {
    case 1:
      if ($recipient_callback) {
        $default_mail = t('Recipient list will be generated for preview.');
      }
      elseif ($form_state['storage']['recipients_raw']) {
        $default_mail = $form_state['storage']['recipients_raw'];
      }
      else {
        $default_mail = '';
        if ($uid) {
          if ($account = user_load(array(
            'uid' => $uid,
          ))) {
            $default_mail = $account->mail;
          }
          else {
            drupal_set_message(t('Cannot load a user with uid @uid.', array(
              '@uid' => $uid,
            )), 'error');
          }
        }
      }
      $form['recipients'] = array(
        '#title' => t('To'),
        '#type' => 'textarea',
        '#required' => TRUE,
        '#rows' => 3,
        '#default_value' => $default_mail,
        '#description' => t('Enter the recipient(s) separated by lines or commas. A separate email will be sent to each, with token substitution if the email corresponds to a site user.'),
        '#disabled' => $recipient_callback,
      );
      $form['copies'] = array(
        '#title' => t('Copies'),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => empty($pet->cc_default) && empty($pet->bcc_default),
      );
      $form['copies']['cc'] = array(
        '#title' => t('Cc'),
        '#type' => 'textarea',
        '#rows' => 3,
        '#default_value' => $form_state['storage']['cc'] ? $form_state['storage']['cc'] : $pet->cc_default,
        '#description' => t('Enter any copied emails separated by lines or commas.'),
      );
      $form['copies']['bcc'] = array(
        '#title' => t('Bcc'),
        '#type' => 'textarea',
        '#rows' => 3,
        '#default_value' => $form_state['storage']['bcc'] ? $form_state['storage']['bcc'] : $pet->bcc_default,
        '#description' => t('Enter any blind copied emails separated by lines or commas.'),
      );
      $form['subject'] = array(
        '#type' => 'textfield',
        '#title' => t('Subject'),
        '#maxlength' => 255,
        '#default_value' => $form_state['storage']['subject'] ? $form_state['storage']['subject'] : $pet->subject,
        '#required' => TRUE,
      );
      $form['mail_body'] = array(
        '#type' => 'textarea',
        '#title' => t('Body'),
        '#default_value' => $form_state['storage']['mail_body'] ? $form_state['storage']['mail_body'] : $pet->body,
        '#required' => TRUE,
        '#rows' => 15,
        '#description' => t('Review and edit template before previewing. This will not change the template for future emailings, just for this one. To change the template permanently, go to the <a href="@settings">template page</a>. You may use the tokens below.', array(
          '@settings' => url('admin/build/pets/edit/' . $pet->name),
        )),
      );
      $form['token_help'] = array(
        '#title' => t('Replacement patterns'),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#description' => t('Make sure that the tokens you choose are available to your template when previewed. This list has all tokens. If you use tokens outside the node and user groups, be sure to include them in Custom tokens above. Only tokens accepting node, user, and global objects are currently supported.'),
      );
      $form['token_help']['help'] = array(
        '#value' => theme('token_help', 'all'),
      );
      $form['preview'] = array(
        '#type' => 'submit',
        '#value' => t('Preview'),
      );
      break;
    case 2:
      $form['info'] = array(
        '#value' => t('A preview of the email is shown below. If you\'re satisfied, click Send. If not, click Back to edit the email.'),
      );
      $form['recipients'] = array(
        '#type' => 'textarea',
        '#title' => t('To'),
        '#rows' => 4,
        '#value' => pet_recipients_formatted($form_state['storage']['recipients']),
        '#disabled' => TRUE,
      );
      if ($form_state['values']['cc']) {
        $form['cc'] = array(
          '#type' => 'textarea',
          '#title' => t('CC'),
          '#rows' => 4,
          '#value' => $form_state['values']['cc'],
          '#disabled' => TRUE,
        );
      }
      if ($form_state['values']['bcc']) {
        $form['bcc'] = array(
          '#type' => 'textarea',
          '#title' => t('BCC'),
          '#rows' => 4,
          '#value' => $form_state['values']['bcc'],
          '#disabled' => TRUE,
        );
      }
      $form['subject'] = array(
        '#type' => 'textfield',
        '#title' => t('Subject'),
        '#size' => 80,
        '#value' => $form_state['storage']['subject_preview'],
        '#disabled' => TRUE,
      );
      $form['mail_body'] = array(
        '#type' => 'textarea',
        '#title' => t('Body'),
        '#rows' => 15,
        '#value' => $form_state['storage']['body_preview'],
        '#disabled' => TRUE,
      );
      $form['body_label'] = array(
        '#prefix' => '<div class="pet_body_label">',
        '#suffix' => '</div>',
        '#value' => '<label>' . t('Body markup:') . '</label>',
      );
      $form['body_preview'] = array(
        '#prefix' => '<div class="pet_body_preview">',
        '#suffix' => '</div>',
        '#value' => $form_state['storage']['body_preview'],
      );
      $form['back'] = array(
        '#type' => 'submit',
        '#value' => t('Back'),
        '#submit' => array(
          'pet_user_form_back',
        ),
      );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Send email(s)'),
      );
      break;
    case 3:
      drupal_set_message(t('Email(s) sent'));
      unset($form_state['storage']);
      break;
  }
  return $form;
}