You are here

webform_email_reply.module in Webform Email Reply 8

Same filename and directory in other branches
  1. 7.2 webform_email_reply.module
  2. 7 webform_email_reply.module

This module provides a way for users to reply to webform submissions within the CMS.

File

webform_email_reply.module
View source
<?php

/**
 * @file
 * This module provides a way for users to reply to webform submissions within
 * the CMS.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function webform_email_reply_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help.
    case 'help.page.webform_email_reply':
      $return_value = "<p>" . t("This module provides a way for users to reply to webform submissions within the CMS.") . '<br />';
      $return_value .= t("Permissions can be set to allow users to reply to all webform submissions or only those on a node the user has created.") . '<p>';
      $return_value .= "<p>" . t("All emails sent are stored in the database and can be viewed from the submission.") . '</p>';
      return $return_value;
  }
}

/**
 * Function to insert the email into the database.
 *
 * @param array $data
 *   The values to insert into the database.
 */
function webform_email_reply_insert(array $data) {

  // Need the user replying to the submission.
  $user = \Drupal::currentUser();

  // Simple insert.
  \Drupal::database()
    ->insert('webform_email_reply')
    ->fields([
    'eid' => NULL,
    'sid' => $data['sid'],
    'webform_id' => $data['webform_id'],
    'uid' => $user
      ->id(),
    'from_address' => $data['from_address'],
    'replied' => time(),
    'message' => $data['message'],
    'fid' => $data['fid'],
  ])
    ->execute();
}

/**
 * Check to see if a reply exists already for a submission.
 *
 * @param string $nid
 *   The node id of the webform
 * @param string $sid
 *   The submission id
 */
function webform_email_reply_get_replies($webform_id, $sid) {

  // Simple db query to get all the emails replies.
  $previous_replies = \Drupal::database()
    ->select('webform_email_reply', 'r')
    ->fields('r')
    ->condition('r.webform_id', $webform_id)
    ->condition('r.sid', $sid)
    ->execute()
    ->fetchAll();
  return $previous_replies;
}

/**
 * Implements hook_mail().
 *
 * This function completes the email, allowing for placeholder substitution.
 */
function webform_email_reply_mail($key, &$message, $params) {
  if ($key == 'webform_email_reply' || $key == 'email') {

    // Set email subject.
    if (isset($params['subject'])) {
      $message['subject'] = $params['subject'];
    }

    // Adding body.
    if (isset($params['body'])) {
      $message['body'][] = $params['body'];
    }

    // Set other headers.
    if (isset($params['headers']) && is_array($params['headers'])) {
      $message['headers'] += $params['headers'];
    }

    // Set from address.
    if (isset($params['from'])) {
      $message['from'] = $params['from'];
    }

    // Set Attachemnt.
    if (isset($params['attachments'])) {
      $message['params']['files'] = $params['attachments'];
    }

    // Set header 'Reply-to'.
    $reply_to = $params['reply_to'] ?: '';
    if (empty($reply_to) && !empty($params['from'])) {
      $reply_to = $message['from'];
    }
    if ($reply_to) {
      $message['reply-to'] = $message['headers']['Reply-to'] = $reply_to;
    }
  }
}

Functions

Namesort descending Description
webform_email_reply_get_replies Check to see if a reply exists already for a submission.
webform_email_reply_help Implements hook_help().
webform_email_reply_insert Function to insert the email into the database.
webform_email_reply_mail Implements hook_mail().