You are here

function _subscriptions_mail_preview_form in Subscriptions 7

Same name and namespace in other branches
  1. 2.0.x subscriptions_mail/subscriptions_mail.mail_edit.inc \_subscriptions_mail_preview_form()

Returns the template preview subform.

Parameters

$values array:

Return value

array

1 call to _subscriptions_mail_preview_form()
_subscriptions_mail_preview in ./subscriptions_mail.mail_edit.inc
Submit handler for the 'Preview template' button.

File

./subscriptions_mail.mail_edit.inc, line 226
Subscriptions_mail module integration with Mail Editor module.

Code

function _subscriptions_mail_preview_form(array $values) {
  global $user;
  _subscriptions_module_load_include('subscriptions_mail', 'cron.inc');
  $language_list = language_list();
  $language = $language_list[$values['language']];
  $is_digest = $values['id'] == 'subscriptions_mail_digest';
  $preview_mode = variable_get('subscriptions_preview_mode', 'text');
  $digest_data = array(
    'subs' => array(
      'type' => 'digest',
    ),
  );
  $query = db_select('node', 'n')
    ->fields('n', array(
    'nid',
    'comment',
  ));
  if (isset($values['content_type'])) {
    $query
      ->condition('type', $values['content_type']);
  }
  $query
    ->orderBy('n.nid', 'DESC')
    ->addTag('node_access');
  foreach ($query
    ->execute() as $row) {

    // This code is adapted from _subscriptions_mail_cron().
    $queue_item = array(
      'sqid' => 0,
      'uid' => $user->uid,
      'name' => check_plain(format_username($user)),
      'language' => $user->language,
      'module' => 'node',
      'field' => 'nid',
      'value' => $row->nid,
      'author_uid' => -1,
      'send_interval' => 1,
      'digest' => $is_digest,
      'load_args' => $row->nid,
      'load_function' => 'subscriptions_content_load_node',
      'is_new' => 1,
      'mail' => $user->mail,
    );
    $load_function = $queue_item['load_function'];
    if ($load_function($queue_item)) {
      $node = $queue_item['object'];
      $access = module_invoke_all('subscriptions', 'access', $load_function, $queue_item['load_args'], $node);
      $allow = !empty($access) && array_search(FALSE, $access) === FALSE;
      if ($allow) {
        $node->_subscriptions_comments = array();
        if (module_exists('comment')) {

          // Load and add all comments for this node.
          $cids = comment_get_thread($node, COMMENT_MODE_FLAT, 5);
          if (!empty($cids)) {
            foreach (comment_load_multiple($cids) as $comment) {

              // Fake every other comment as new.
              $comment->_subscriptions_is_new = $comment->cid % 2;
              $node->_subscriptions_comments[] = $comment;
            }
          }
        }
        $fields = module_invoke_all('subscriptions', 'fields', $queue_item['module']);
        $field = $queue_item['field'];
        $data_function = $fields[$field]['data_function'];
        $mailmod = empty($fields[$field]['mail_module']) ? 'subscriptions_mail' : $fields[$field]['mail_module'];
        $mailkey = $fields[$field]['mailkey'];
        if ($mailkey_altered = module_invoke_all('subscriptions', 'mailkey_alter', $mailkey, $node)) {
          $mailkey = $mailkey_altered[0];
        }
        $data = array(
          'subs' => array(
            'type' => $fields[$field]['subs_type'],
            'unsubscribe_path' => 's/del/...',
            'subs-module' => 'MODULE',
            'subs-field' => 'FIELD',
            'subs-value' => 'VALUE',
          ),
          'user' => user_load(!empty($node->revision_uid) ? $node->revision_uid : $node->uid),
        );
        $data_function($data, $node, $queue_item);
        drupal_alter('subscriptions_data', $data, $node, $queue_item);
        if ($is_digest) {

          // Accumulate up to three digest items in $digest_data and ultimately
          // pass them out of the loop as $data.
          $data = $digest_data = subscriptions_mail_digest_add_item($digest_data, $mailmod, $mailkey, $data, $user);
          if (count($digest_data['subs']['items']) >= 3) {
            break;
          }
        }
        else {

          // Pass $data out of the (one-time) loop.
          break;
        }
      }
    }
  }
  if (empty($data)) {
    $form['note'] = array(
      '#markup' => '<span class="error">' . t('No displayable content found!') . '</span>',
    );
    return $form;
  }
  $subject = mail_edit_format($values['subject'], $data, array(
    'language' => $language,
  ));
  $body_template = $values['body'];
  if ($preview_mode == 'html-rendered-nl') {
    $body_template = str_replace("\n", "<br />\n", $body_template);
  }
  if (!$is_digest) {
    $comment_template = $values['subscriptions_comment_body'];
    if ($preview_mode == 'html-rendered-nl') {
      $comment_template = str_replace("\n", "<br />\n", $comment_template);
    }
    $data['template']['subscriptions_comment_body'] = $comment_template;
  }
  $body = mail_edit_format($body_template, $data, array(
    'language' => $language,
  ));
  if (variable_get('subscriptions_showmailkeys', FALSE)) {
    $body .= '| ' . $mailkey . "\n";
  }
  switch ($preview_mode) {
    case 'text':
      $body = drupal_html_to_text($body);
    case 'html-raw':
      $body = check_plain($body);
      $body = '<pre>' . $body . '</pre>';
      break;
    case 'html-rendered':
    case 'html-rendered-nl':
      $body = '<div>' . filter_xss_admin($body) . '</div>';
      break;
  }
  $form['preview_subject'] = array(
    '#markup' => '<h4><u>' . $subject . "</u></h4><br />",
  );
  $form['preview_body'] = array(
    '#markup' => $body,
  );
  $form['preview_mode'] = array(
    '#type' => 'select',
    '#prefix' => '<div style="float: left; margin-right: 2em;">',
    '#suffix' => '</div>',
    '#title' => t('Preview mode'),
    '#default_value' => $preview_mode,
    '#options' => array(
      'text' => t('Text (default)'),
      'html-raw' => t('Raw HTML'),
      'html-rendered' => t('Rendered HTML'),
      'html-rendered-nl' => t('Rendered HTML with line breaks'),
    ),
  );
  $form['preview_mode_info'] = array(
    '#type' => 'item',
    '#description' => t("The input to drupal_mail() is HTML. Drupal's default mail system converts it to text before sending it out as an email message. Sending HTML mail is possible by installing an alternative mail system and converting the template to HTML.", array(
      '@drupal_mail' => 'drupal_mail()',
    )) . ' ' . t("The @t1 tokens provide customizable rendered views, which may be easier to maintain than writing HTML.", array(
      '@t1' => '[subs:view]',
    )),
  );
  return $form;
}