You are here

function faq_ask_mail in FAQ_Ask 8

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

Implements hook_mail().

This function completes the email, allowing for placeholder substitution.

@TODO: define messages & subjects on settings page, with list of tokens.

File

./faq_ask.module, line 489
This module is an add-on to FAQ module, allows users to 'ask question'.

Code

function faq_ask_mail($key, &$message, $params) {
  if ($key == 'notify_asker' || $key == 'notify_expert') {
    $body = array();

    // Initiate text variables.
    $variables = array(
      '@question' => $params['question'],
      '@question_details' => isset($params['question_details']) ? strip_tags($params['question_details']) : '',
      '@site-name' => \Drupal::config('system.site')
        ->get('name'),
    );

    // Find category name.
    if (isset($params['category']) && $params['category']) {
      $term = '';
      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']
          ->get('name')->value;
      }
      else {
        $variables['!username'] = 'user';
      }
    }
    switch ($key) {
      case 'notify_expert':
        $url_options = array(
          'options' => array(
            'absolute' => TRUE,
          ),
          'query' => array(
            'token' => Utility::faqAskGetToken('faq_ask/answer/' . $params['nid']),
          ),
        );
        $variables = array_merge($variables, array(
          '!answer_uri' => Url::fromUserInput('/faq_ask/answer/' . $params['nid'], $url_options)
            ->toString(),
          '!asker' => $params['creator'],
          '!login_uri' => Url::fromUserInput('/user')
            ->toString(),
        ));
        $subject = Html::escape(new FormattableMarkup('You have a question waiting on @site-name', $variables));
        $body[] = Html::escape(new FormattableMarkup('Dear !username,<br/>', $variables));
        if ($params['category'] == -1) {
          $body[] = Html::escape(new FormattableMarkup('The following question has been posted.<br/>', array()));
        }
        else {
          $body[] = Html::escape(new FormattableMarkup('The following question has been posted in the "!cat" category by !asker.<br/>', $variables));
        }
        $body[] = Html::escape(new FormattableMarkup('<strong><i>@question</i></strong>', $variables));
        if ($variables['@question_details']) {
          $body[] = Html::escape(new FormattableMarkup('<p><i>@question_details</i></p><br/>', $variables));
        }
        $body[] = Html::escape(new FormattableMarkup('In order to answer it you will first need to <a href="!login_uri">login</a> to the site.<br/>', $variables));
        $body[] = Html::escape(new FormattableMarkup('Once logged in, you may proceed <a href="!answer_uri">directly to the question</a> to answer it.<br/>', $variables));
        $body[] = Html::escape(new FormattableMarkup('By clicking on the above question link you will be redirected to the login form if you are currently logged out.<br/>', $variables));
        break;
      case 'notify_asker':
        $url_options = array(
          'absolute' => TRUE,
        );
        $variables = array_merge($variables, array(
          '!question_uri' => Url::fromUserInput('/node/' . $params['nid'])
            ->toString(),
        ));
        $subject = Html::escape(new FormattableMarkup('A question you asked has been answered on @site-name<br/>', $variables));
        $body[] = Html::escape(new FormattableMarkup('Dear !username,<br/>', $variables));
        $body[] = Html::escape(new FormattableMarkup('The question: "@question" you asked on @site-name has been answered.<br/>', $variables));
        $body[] = Html::escape(new FormattableMarkup('To view the answer, please visit the question you created on !question_uri.<br/>', $variables));
        $body[] = Html::escape(new FormattableMarkup('Thank you for visiting.<br/>', $variables));
        break;
    }
    $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
    $message['body'] = $body;
    $message['subject'] = $subject;
  }
}