You are here

function print_mail_form_for_path in Printer, email and PDF versions 7.2

Build email form for the page provided in the path argument.

Parameters

array $form: Form.

array $form_state: Form state.

string $path: Path.

array $query: Query.

Object $user: Current user.

Return value

array Modified form.

1 call to print_mail_form_for_path()
print_mail_form in print_mail/print_mail.inc
Form constructor for the send by email form.

File

print_mail/print_mail.inc, line 78
Displays and processes the mail send form.

Code

function print_mail_form_for_path($form, &$form_state, $path, $query = NULL, $user = NULL) {
  if ($user === NULL) {
    global $user;
  }
  $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
  if (!user_access('send unlimited emails') && !flood_is_allowed('print_mail', $print_mail_hourly_threshold)) {
    $form['flood'] = array(
      '#type' => 'markup',
      '#markup' => '<p>' . format_plural($print_mail_hourly_threshold, 'You cannot send more than 1 message per hour. Please try again later.', 'You cannot send more than @count messages per hour. Please try again later.') . '</p>',
    );
    return $form;
  }
  $print_mail_teaser_default = variable_get('print_mail_teaser_default', PRINT_MAIL_TEASER_DEFAULT_DEFAULT);
  $print_mail_teaser_choice = variable_get('print_mail_teaser_choice', PRINT_MAIL_TEASER_CHOICE_DEFAULT);
  $print_mail_user_recipients_default = variable_get('print_mail_user_recipients', PRINT_MAIL_USER_RECIPIENTS_DEFAULT);
  $form = array();
  $cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
  $title = _print_get_title($path);
  $options = array();
  if ($print_mail_user_recipients_default) {
    if (module_exists('realname')) {
      $sql = "SELECT u.mail, r.realname AS name from {users} u LEFT JOIN {realname} r ON u.uid = r.uid WHERE u.uid <> :uid AND status = 1 ORDER BY name ASC";
    }
    else {
      $sql = "SELECT mail, name from {users} WHERE uid <> :uid AND status = 1 ORDER BY name ASC";
    }
    $recipients = db_query($sql, array(
      ':uid' => drupal_anonymous_user()->uid,
    ));
    foreach ($recipients as $recipient) {
      $options[$recipient->mail] = $recipient->name;
    }
  }
  if (count($form_state['input']) == 0) {
    $nodepath = drupal_get_normal_path($path);
    db_merge('print_mail_page_counter')
      ->key(array(
      'path' => substr($nodepath, 0, 255),
    ))
      ->fields(array(
      'totalcount' => 1,
      'timestamp' => REQUEST_TIME,
    ))
      ->expression('totalcount', 'totalcount + 1')
      ->execute();
  }
  $form['path'] = array(
    '#type' => 'value',
    '#value' => $path,
  );
  $form['query'] = array(
    '#type' => 'value',
    '#value' => $query,
  );
  $form['cid'] = array(
    '#type' => 'value',
    '#value' => $cid,
  );
  $form['title'] = array(
    '#type' => 'value',
    '#value' => $title,
  );
  $form['fld_from_addr'] = array(
    '#type' => 'textfield',
    '#title' => t('Your email'),
    '#size' => 62,
    '#required' => TRUE,
  );
  $form['fld_from_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your name'),
    '#size' => 62,
  );
  $form['txt_to'] = array(
    '#tree' => TRUE,
  );
  $form['txt_to']['addrs'] = array(
    '#type' => 'textarea',
    '#title' => t('Send to'),
    '#rows' => 3,
    '#resizable' => FALSE,
    '#description' => t('Enter multiple addresses separated by commas and/or different lines.'),
    '#required' => !$print_mail_user_recipients_default,
  );
  if ($print_mail_user_recipients_default) {
    $form['txt_to']['users'] = array(
      '#type' => 'select',
      '#title' => t('Send to users'),
      '#multiple' => TRUE,
      '#size' => 10,
      '#options' => $options,
    );
  }
  $form['fld_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#size' => 62,
    '#required' => TRUE,
  );
  if (!empty($title)) {

    // To prevent useless translation strings, translate only non-node titles.
    if (drupal_substr($path, 0, 5) != 'node/') {
      $title = t($title);
    }
    $form['fld_title'] = array(
      '#type' => 'item',
      '#title' => t('Page to be sent'),
      '#markup' => l($title, $path, array(
        'attributes' => array(
          'title' => t('View page'),
        ),
        'query' => $query,
      )),
    );
  }
  $form['txt_message'] = array(
    '#type' => 'textarea',
    '#title' => t('Your message'),
    '#rows' => 6,
    '#required' => TRUE,
  );
  if ($print_mail_teaser_choice) {
    $form['chk_teaser'] = array(
      '#type' => 'checkbox',
      '#title' => t('Send only the teaser'),
      '#default_value' => $print_mail_teaser_default,
    );
  }
  else {
    $form['chk_teaser'] = array(
      '#type' => 'value',
      '#value' => $print_mail_teaser_default,
    );
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#name' => 'submit',
    '#type' => 'submit',
    '#value' => t('Send email'),
  );
  $form['actions']['cancel'] = array(
    '#name' => 'cancel',
    '#type' => 'submit',
    '#value' => t('Cancel'),
  );
  if ($user->uid != 0) {
    $user_name = check_plain(strip_tags(theme('username', array(
      'account' => $user,
    ))));
    $form['fld_from_addr']['#default_value'] = $user->mail;
    $form['fld_from_addr']['#disabled'] = TRUE;
    $form['fld_from_addr']['#value'] = $user->mail;
    $form['fld_from_name']['#default_value'] = $user_name;
  }
  else {
    $user_name = t('Someone');
  }
  $site_name = variable_get('site_name', t('an interesting site'));
  $form['fld_subject']['#default_value'] = t('!user has sent you a message from !site', array(
    '!user' => $user_name,
    '!site' => $site_name,
    '!title' => $title,
  ));
  $form['txt_message']['#default_value'] = '';
  return $form;
}