You are here

class HtmlMailHelper in HTML Mail 8

Same name and namespace in other branches
  1. 8.3 src/Helper/HtmlMailHelper.php \Drupal\htmlmail\Helper\HtmlMailHelper

Class HtmlMailHelper.

@package Drupal\htmlmail\Helper

Hierarchy

Expanded class hierarchy of HtmlMailHelper

4 files declare their use of HtmlMailHelper
htmlmail.module in ./htmlmail.module
Default file for HTML Mail module.
HtmlMailConfigurationForm.php in src/Form/HtmlMailConfigurationForm.php
HTMLMailSystem.php in src/Plugin/Mail/HTMLMailSystem.php
HtmlMailTestForm.php in src/Form/HtmlMailTestForm.php

File

src/Helper/HtmlMailHelper.php, line 13

Namespace

Drupal\htmlmail\Helper
View source
class HtmlMailHelper {
  const HTMLMAIL_MODULE_NAME = 'htmlmail';
  const HTMLMAIL_USER_DATA_NAME = 'htmlmail_plaintext';

  /**
   * Returns an associative array of allowed themes.
   *
   * Based on code from the og_theme module.
   *
   * @return array
   *   The keys are the machine-readable names and the values are the .info file
   *   names.
   */
  public static function getAllowedThemes() {
    $allowed =& drupal_static(__FUNCTION__);
    if (!isset($allowed)) {
      $allowed = [
        '' => t('No theme'),
      ];
      $themes = \Drupal::service('theme_handler')
        ->listInfo();
      uasort($themes, 'system_sort_modules_by_info_name');
      foreach ($themes as $key => $value) {
        if ($value->status) {
          $allowed[$key] = Html::escape($value->info['name']);
        }
      }
    }
    return $allowed;
  }

  /**
   * Retrieves the module name.
   *
   * @return string
   *   The module name used to store data on user.
   */
  public static function getModuleName() {
    return self::HTMLMAIL_MODULE_NAME;
  }

  /**
   * Retrieves the user data name.
   *
   * @return string
   *   The data field name stored on user data.
   */
  public static function getUserDataName() {
    return self::HTMLMAIL_USER_DATA_NAME;
  }

  /**
   * Returns the selected theme to use for outgoing emails.
   *
   * @param array $message
   *   (optional) The message to be themed.
   *
   * @return string
   *   The 'theme' key of $message if set and allowed, empty string otherwise.
   */
  public static function getSelectedTheme(array &$message = []) {
    $selected = isset($message['theme']) ? $message['theme'] : \Drupal::config('htmlmail.settings')
      ->get('htmlmail_theme');
    if ($selected) {

      // Make sure the selected theme is allowed.
      $themes = self::getAllowedThemes();
      if (empty($themes[$selected])) {
        $selected = '';
      }
    }
    return $selected;
  }

  /**
   * Checks whether a given recipient email prefers plaintext-only messages.
   *
   * @param string $email
   *   The recipient email address.
   *
   * @return bool
   *   FALSE if the recipient prefers plaintext-only messages; otherwise TRUE.
   */
  public static function htmlMailIsAllowed($email) {
    return !($recipient = user_load_by_mail($email)) || empty(\Drupal::service('user.data')
      ->get(self::HTMLMAIL_MODULE_NAME, $recipient
      ->id(), self::HTMLMAIL_USER_DATA_NAME));
  }

  /**
   * Check if current user can see the option to receive only plain text mails.
   *
   * @return bool
   *   FALSE if user do not have permission to change or administer users.
   */
  public static function allowUserAccess() {
    $user = Drupal::currentUser();
    return $user
      ->hasPermission('choose htmlmail_plaintext') || $user
      ->hasPermission('administer users');
  }

  /**
   * Retrieves the theme names based on module and key.
   *
   * @param array $message
   *   The message array with module name and key.
   *
   * @return array
   *   An array with themes name.
   */
  public static function getThemeNames(array $message) {
    $formatted_module = str_replace('_', '-', $message['module']);
    $formatted_key = str_replace('_', '-', $message['key']);
    return [
      'htmlmail__' . $formatted_module,
      'htmlmail__' . $formatted_module . '__' . $formatted_key,
      'htmlmail',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HtmlMailHelper::allowUserAccess public static function Check if current user can see the option to receive only plain text mails.
HtmlMailHelper::getAllowedThemes public static function Returns an associative array of allowed themes.
HtmlMailHelper::getModuleName public static function Retrieves the module name.
HtmlMailHelper::getSelectedTheme public static function Returns the selected theme to use for outgoing emails.
HtmlMailHelper::getThemeNames public static function Retrieves the theme names based on module and key.
HtmlMailHelper::getUserDataName public static function Retrieves the user data name.
HtmlMailHelper::htmlMailIsAllowed public static function Checks whether a given recipient email prefers plaintext-only messages.
HtmlMailHelper::HTMLMAIL_MODULE_NAME constant
HtmlMailHelper::HTMLMAIL_USER_DATA_NAME constant