acquia_commercemanager-8.x-1.122/modules/acm/acm.module

modules/acm/acm.module
<?php

/**
 * @file
 * Provides base hooks to the base Acquia Commerce connector.
 */

use Drupal\acm\Connector\APIWrapper;
use Drupal\acm\User\CommerceAccountInterface;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Site\Settings;

/**
 * For the all.
 */
const LINKED_SKU_TYPE_ALL = 'all';

/**
 * For the upsell.
 */
const LINKED_SKU_TYPE_UPSELL = 'upsell';

/**
 * For the cross_sell.
 */
const LINKED_SKU_TYPE_CROSSSELL = 'crosssell';

/**
 * For the related.
 */
const LINKED_SKU_TYPE_RELATED = 'related';

/**
 * Implements hook_page_attachments().
 */
function acm_page_attachments(array &$attachments) {
  // Add CSS for the Commerce icon in the toolbar if the user has access to it.
  $can_view_commerce_admin = \Drupal::currentUser()->hasPermission('access commerce administration pages');
  $can_view_toolbar = \Drupal::currentUser()->hasPermission('access toolbar');
  if ($can_view_commerce_admin && $can_view_toolbar) {
    $attachments['#attached']['library'][] = 'acm/drupal.acm.toolbar';
  }
}

/**
 * Implements hook_mail().
 */
function acm_mail($key, &$message, $params) {
  $token_service = \Drupal::token();
  $language_manager = \Drupal::languageManager();
  $langcode = $message['langcode'];
  $variables = ['user' => $params['account']];

  $language = $language_manager->getLanguage($params['account']->getPreferredLangcode());
  $original_language = $language_manager->getConfigOverrideLanguage();
  $language_manager->setConfigOverrideLanguage($language);
  $mail_config = \Drupal::config('user.mail');
  $token_options = [
    'langcode' => $langcode,
    'callback' => 'acm_mail_tokens',
    'clear' => TRUE,
  ];

  switch ($key) {
    case 'commerce_user_reset_password':
      $key = 'password_reset';
      $message['subject'] .= PlainTextOutput::renderFromHtml($token_service->replace($mail_config->get($key . '.subject'), $variables, $token_options));
      $message['body'][] = $token_service->replace($mail_config->get($key . '.body'), $variables, $token_options);
      break;
  }

  $language_manager->setConfigOverrideLanguage($original_language);
}

/**
 * Token callback to add unsafe tokens for acm mails.
 *
 * This function is used by \Drupal\Core\Utility\Token::replace() to set up
 * some additional tokens that can be used in email messages generated by
 * acm_mail().
 *
 * @param array $replacements
 *   An associative array variable containing mappings from token names to
 *   values (for use with strtr()).
 * @param array $data
 *   An associative array of token replacement values. If the 'user' element
 *   exists, it must contain a user account object with the following
 *   properties:
 *   - login: The UNIX timestamp of the user's last login.
 *   - pass: The hashed account login password.
 * @param array $options
 *   A keyed array of settings and flags to control the token replacement
 *   process. See \Drupal\Core\Utility\Token::replace().
 */
function acm_mail_tokens(array &$replacements, array $data, array $options) {
  if (isset($data['user'])) {
    $replacements['[user:one-time-login-url]'] = acm_commerce_user_pass_reset_url($data['user'], $options);
  }
}

/**
 * Generates a unique URL for a commerce user to reset their password.
 *
 * @param \Drupal\acm\User\CommerceAccountInterface $account
 *   An object containing the user account.
 * @param array $options
 *   (optional) A keyed array of settings. Supported options are:
 *   - langcode: A language code to be used when generating locale-sensitive
 *    URLs. If langcode is NULL the users preferred language is used.
 *
 * @return string
 *   A unique URL that provides a one-time log in for the user, from which
 *   they can change their password.
 */
function acm_commerce_user_pass_reset_url(CommerceAccountInterface $account, array $options = []) {
  $timestamp = REQUEST_TIME;
  $langcode = isset($options['langcode']) ? $options['langcode'] : $account->getPreferredLangcode();
  $email = $account->getEmail();
  return \Drupal::url('acm.external_user_password_reset',
    [
      'email' => base64_encode($email),
      'timestamp' => $timestamp,
      'hash' => acm_commerce_user_pass_rehash($account, $timestamp),
    ],
    [
      'absolute' => TRUE,
      'language' => \Drupal::languageManager()->getLanguage($langcode),
    ]
  );
}

/**
 * Creates a unique hash value for use in time-dependent per-user URLs.
 *
 * This hash is normally used to build a unique and secure URL that is sent to
 * the user by email for purposes such as resetting the user's password. In
 * order to validate the URL, the same hash can be generated again, from the
 * same information, and compared to the hash value from the URL. The hash
 * contains the time stamp, last updated time, numeric user ID,  and the user's
 * email address.
 *
 * @param \Drupal\acm\User\CommerceAccountInterface $account
 *   An object containing the user account.
 * @param int $timestamp
 *   A UNIX timestamp, typically REQUEST_TIME.
 *
 * @return string
 *   A string that is safe for use in URLs and SQL statements.
 */
function acm_commerce_user_pass_rehash(CommerceAccountInterface $account, $timestamp) {
  // We don't have access to the user's password, but this hash should be
  // suitable.
  $email = $account->getEmail();
  $data = $timestamp;
  $data .= $account->getUpdatedTime();
  $data .= $account->id();
  $data .= $email;
  return Crypt::hmacBase64($data, Settings::getHashSalt() . $email);
}

/**
 * Helper function to check if exception is related to API being down.
 *
 * @param \Exception $e
 *   Exception object.
 *
 * @return bool
 *   True if exception is related to API being down.
 */
function acm_is_exception_api_down_exception(\Exception $e) {
  return acm_is_code_api_down_code($e->getCode());
}

/**
 * Helper function to check if exception code is related to API being down.
 *
 * @param int $code
 *   Exception code.
 *
 * @return bool
 *   True if exception code is related to API being down.
 */
function acm_is_code_api_down_code($code) {
  return ($code == APIWrapper::API_DOWN_ERROR_CODE);
}

/**
 * Function to return global error message to be used when API is down.
 *
 * Not adding this to config or creating form, this can be updated using
 * translation interface.
 *
 * @return string
 *   Error message string.
 */
function acm_api_down_global_error_message() {
  return t('Sorry, something went wrong and we are unable to process your request right now. Please try again later.')->render();
}

/**
 * Implements hook_theme().
 */
function acm_theme($existing, $type, $theme, $path) {
  return [
    'dashboard' => [
      'variables' => [
        'items' => [],
        'menu' => [],
      ],
    ],
    'dashboard_item' => [
      'variables' => [
        'title' => NULL,
        'value' => NULL,
        'attributes' => [],
      ],
    ],
    'dashboard_item__line' => [
      'variables' => [
        'title' => NULL,
        'value' => NULL,
        'attributes' => [],
      ],
    ],
    'dashboard_item__tile' => [
      'variables' => [
        'title' => NULL,
        'value' => NULL,
        'attributes' => [],
      ],
    ],
  ];
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc