You are here

function faq_ask_mail in FAQ_Ask 7

Same name and namespace in other branches
  1. 8 faq_ask.module \faq_ask_mail()
  2. 6.2 faq_ask.module \faq_ask_mail()
  3. 6 faq_ask.module \faq_ask_mail()

Implements hook_mail().

This function completes the email, allowing for placeholder substitution. Done: notify_asker. stenjo @TODO: define messages & subjects on settings page, with list of tokens. how to handle newlines?

Parameters

string $key: What type of e-mail are we sending?

array $message: The message array to be sendt

array $params: Additional parameters for placeholders in e-mail text.

Return value

array $message As passed by reference

File

./faq_ask.module, line 1013
This module is an add-on to the FAQ module that allows users with the 'ask question' permission to create a question which will be queued for an 'expert' to answer.

Code

function faq_ask_mail($key, &$message, $params) {
  $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
  $body = array();
  $options = array(
    'langcode' => $message['language']->language,
  );

  // Initiate text variables
  $variables = array(
    '@question' => $params['question'],
    '@question_details' => isset($params['question_details']) ? $params['question_details'] : '',
    '@site-name' => variable_get('site_name', 'Drupal'),
  );

  // Find category name
  if (isset($params['category']) && $params['category']) {
    $term = '';
    $tid = $params['category'];
    if (is_array($params['category'])) {
      $term = taxonomy_term_load(array_shift($params['category']));
    }
    else {
      $term = taxonomy_term_load($params['category']);
    }
    if (is_object($term)) {
      $variables['!cat'] = $term->name;
    }
    else {
      $params['category'] = -1;
    }
  }
  else {
    $params['category'] = -1;
  }

  // Handle user names
  if (!isset($variables['!username']) || $variables['!username'] == '') {
    if (isset($params['account']) && is_object($params['account'])) {
      $variables['!username'] = $params['account']->name;
    }
    else {
      $variables['!username'] = t('user');
    }
  }
  switch ($key) {
    case 'notify_expert':
      $url_options = array(
        'options' => array(
          'absolute' => TRUE,
        ),
        'query' => array(
          'token' => _faq_ask_get_token('faq_ask/answer/' . $params['nid']),
        ),
      );
      $variables = array_merge($variables, array(
        '!answer_uri' => url('faq_ask/answer/' . $params['nid'], $url_options),
        '!asker' => $params['creator'],
        '!login_uri' => url('user'),
      ));
      $subject = t('You have a question waiting on @site-name', $variables, $options);
      $body[] = t('Dear !username,', $variables, $options);
      if ($params['category'] == -1) {
        $body[] = t('The following question has been posted.', array(), $options);
      }
      else {
        $body[] = t('The following question has been posted in the "!cat" category by !asker.', $variables, $options);
      }
      $body[] = t('<strong><i>@question</i></strong>', $variables, $options);
      if ($variables['@question_details']) {
        $body[] = t('<i>@question_details</i>', $variables, $options);
      }
      $body[] = t('In order to answer it you will first need to <a href="!login_uri">login</a> to the site.', $variables, $options);
      $body[] = t('Once logged in, you may proceed <a href="!answer_uri">directly to the question</a> to answer it.', $variables, $options);
      $body[] = t('By clicking on the above question link you will be redirected to the login form if you are currently logged out.', $variables, $options);
      break;
    case 'notify_asker':
      $url_options = array(
        'absolute' => TRUE,
      );
      $variables = array_merge($variables, array(
        '!question_uri' => url('node/' . $params['nid'], array(
          'absolute' => TRUE,
        )),
      ));
      $subject = t('A question you asked has been answered on @site-name', $variables, $options);
      $body[] = t('Dear !username,', $variables, $options);
      $body[] = t('The question: "@question" you asked on @site-name has been answered.', $variables, $options);
      $body[] = t('To view the answer, please visit the question you created on !question_uri.', $variables, $options);
      $body[] = t('Thank you for visiting.', $variables, $options);
      break;
  }
  $message['body'] = $body;
  $message['subject'] = $subject;
}