You are here

function print_mail_form_submit in Printer, email and PDF versions 7.2

Same name and namespace in other branches
  1. 5.4 print_mail/print_mail.inc \print_mail_form_submit()
  2. 5.3 print_mail/print_mail.inc \print_mail_form_submit()
  3. 6 print_mail/print_mail.inc \print_mail_form_submit()
  4. 7 print_mail/print_mail.inc \print_mail_form_submit()
  5. 5.x print_mail/print_mail.inc \print_mail_form_submit()

Form submission handler for print_mail_form().

See also

print_mail_form()

print_controller()

1 call to print_mail_form_submit()
print_mail_action_submit in print_mail/print_mail.module
Action handler for the print_mail_action_submit.

File

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

Code

function print_mail_form_submit($form, &$form_state) {
  if (!array_key_exists('cancel', $form_state['input'])) {
    module_load_include('inc', 'print', 'print.pages');
    module_load_include('inc', 'print', 'includes/print');
    $link = print_mail_print_link();
    $cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
    $view_mode = $form_state['values']['chk_teaser'] ? 'teaser' : PRINT_VIEW_MODE;
    $node = print_controller($form_state['values']['path'], $link['format'], $cid, $view_mode);
    if ($node) {
      $print_mail_send_option_default = variable_get('print_mail_send_option_default', PRINT_MAIL_SEND_OPTION_DEFAULT);
      $params = array();
      $params['subject'] = $form_state['values']['fld_subject'];
      $params['message'] = t('Message from sender') . ':<br /><br /><em>' . nl2br(check_plain($form_state['values']['txt_message'])) . '</em>';
      $params['link'] = url($form_state['values']['path'], array(
        'absolute' => TRUE,
        'query' => $form_state['values']['query'],
      ));
      $params['title'] = $form_state['values']['title'];

      // If a name is provided, make From: in the format Common Name <address>.
      if (!empty($form_state['values']['fld_from_name'])) {
        $from = '"' . mime_header_encode($form_state['values']['fld_from_name']) . '" <' . $form_state['values']['fld_from_addr'] . '>';
      }
      else {
        $from = $form_state['values']['fld_from_addr'];
      }

      // If using reply-to, move the From: info to the params array, so that it
      // is passed to hook_mail later.
      if (variable_get('print_mail_use_reply_to', PRINT_MAIL_USE_REPLY_TO)) {
        $params['from'] = $from;
        $from = NULL;
      }

      // Spaces in img URLs must be replaced with %20.
      $pattern = '!<(img\\s[^>]*?)>!is';
      $node->content = preg_replace_callback($pattern, '_print_replace_spaces', $node->content);
      $params['body'] = theme('print', array(
        'node' => $node,
        'query' => $form_state['values']['query'],
        'format' => $link['format'],
        'expand_css' => TRUE,
        'message' => $params['message'],
      ));

      // Img elements must be set to absolute.
      $pattern = '!<(img\\s[^>]*?)>!is';
      $params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);

      // Convert the a href elements, to make sure no relative links remain.
      $pattern = '!<(a\\s[^>]*?)>!is';
      $params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);

      // Enable support for third-party modules to alter the e-mail before
      // being sent.
      drupal_alter('print_mail', $params, $to);
      $ok = FALSE;
      $use_job_queue = variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT);
      $queue = NULL;
      if ($use_job_queue) {
        $queue = DrupalQueue::get('print_mail_send');
      }
      $addresses = explode(', ', $form_state['values']['txt_to']['addrs']);
      foreach ($addresses as $to) {
        $ret = array();
        if ($use_job_queue) {

          // Use job queue to send mails during cron runs.
          $queue
            ->createItem(array(
            'module' => 'print_mail',
            'key' => $print_mail_send_option_default,
            'to' => $to,
            'language' => language_default(),
            'params' => $params,
            'from' => $from,
          ));
        }
        else {

          // Send mail immediately using Drupal's mail handler.
          $ret = drupal_mail('print_mail', $print_mail_send_option_default, $to, language_default(), $params, $from);
        }
        if ($use_job_queue || $ret['result']) {
          flood_register_event('print_mail');
          $ok = TRUE;
        }
      }
      if ($ok) {
        $query = empty($form_state['values']['query']) ? '' : '?' . rawurldecode(drupal_http_build_query($form_state['values']['query']));
        watchdog('print_mail', '%name [%from] sent %page to [%to]', array(
          '%name' => $form_state['values']['fld_from_name'],
          '%from' => $form_state['values']['fld_from_addr'],
          '%page' => $form_state['values']['path'] . $query,
          '%to' => $form_state['values']['txt_to']['addrs'],
        ));
        $site_name = variable_get('site_name', t('us'));
        drupal_set_message(check_plain(t('Thank you for spreading the word about !site.', array(
          '!site' => $site_name,
        ))));
        $nodepath = drupal_get_normal_path($form_state['values']['path']);
        db_update('print_mail_page_counter')
          ->fields(array(
          'sentcount' => 1,
          'sent_timestamp' => REQUEST_TIME,
        ))
          ->condition('path', $nodepath, '=')
          ->expression('sentcount', 'sentcount + :inc', array(
          ':inc' => count($addresses),
        ))
          ->execute();
      }
    }
  }
  $form_state['redirect'] = array(
    preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']),
    array(
      'query' => $form_state['values']['query'],
    ),
  );
}