You are here

function faq_ask_mail in FAQ_Ask 6.2

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

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

File

./faq_ask.module, line 709
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) {
  $newline = "\n<br/><br/>";
  $body = array();
  $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
  $language = $message['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_get_term(array_shift($params['category']));
    }
    else {
      $term = taxonomy_get_term($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(
        'absolute' => TRUE,
        'query' => array(
          'token' => _faq_ask_get_token('faq_ask/answer/' . $params['nid']),
        ),
      );
      $variables = array_merge($variables, array(
        '!cat' => $params['category'],
        '@question' => $params['question'],
        '!answer_uri' => url('faq_ask/answer/' . $params['nid'], $url_options),
        '!asker' => $params['creator'],
        '!login_uri' => url('user', array(
          'absolute' => TRUE,
        )),
      ));
      $subject = t('You have a question waiting on @site-name', $variables, $language->language);
      $body[] = t('Dear !username,', $variables, $language->language);
      if ($params['category'] == -1) {
        $body[] = t('The following question has been posted.', NULL, $language->language);
      }
      else {
        $body[] = t('The following question has been posted in the "!cat" category by !asker.', $variables, $language->language);
      }
      $body[] = t('<strong><i>@question</i></strong>', $variables, $language->language);
      if ($variables['@question_details']) {
        $body[] = t('<i>@question_details</i>', $variables, $language->language);
      }

      //      $body[] = t('In order to answer it you will first need to <a href="!login_uri">login</a> to the site.', $variables, $language->language);
      //      $body[] = t('Once logged in, you may proceed <a href="!answer_uri">directly to the question</a> to answer it.', $variables, $language->language);
      //      $body[] = t('By clicking on the above question link you will be redirected to the login form if you are currently logged out.', $variables, $language->language);
      $body[] = t('You may proceed directly to the question to answer it by clicking this link: !answer_uri', $variables, $language->language);
      $body[] = t('Note: Clicking the link will automatically publish the question, making it available for users immediately.', $variables, $language->language);
      break;
    case 'notify_asker':
      $variables = array_merge($variables, array(
        //'!cat' => $params['category'],
        '@question' => $params['question'],
        '!question_uri' => url('node/' . $params['nid'], array(
          'absolute' => TRUE,
        )),
      ));
      if ($variables['!username'] == '') {
        $variables['!username'] = t('user');
      }
      $subject = t('A question you asked has been answered on @site-name', $variables, $language->language);
      $body[] = t('Dear !username,', $variables, $language->language);
      $body[] = t('The question: "@question" you asked on @site-name has been answered.', $variables, $language->language);
      $body[] = t('To view the answer, please visit the question you created on !question_uri.', $variables, $language->language);
      $body[] = t('Thank you for visiting.', $variables, $language->language);
      break;
  }
  $message['body'] = drupal_wrap_mail(implode($newline, $body));
  $message['subject'] = $subject;
}