mutual_credit-5.0.x-dev/src/Plugin/Action/Email.php

src/Plugin/Action/Email.php
<?php

namespace Drupal\mcapi\Plugin\Action;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Mail\MailManager;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\mcapi\Mcapi;

/**
 * Changes the transaction state to 'erased'.
 *
 * @Action(
 *   id = "mcapi_transaction_mail",
 *   label = @Translation("Notify a user about a transaction"),
 *   type = "mcapi_transaction",
 * )
 */
class Email extends ConfigurableActionBase implements ContainerFactoryPluginInterface {

  private $currentUser;
  private $mailManager;

  /**
   * Constructor.
   */
  public function __construct($configuration, $plugin_id, $plugin_definition, AccountProxy $current_user, MailManager $mail_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->currentUser = $current_user;
    $this->mailManager = $mail_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('current_user'),
      $container->get('plugin.manager.mail')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['subject'] = [
      '#title' => $this->t('Subject'),
      '#description' => $this->t('The subject line of the mail'),
      '#type' => 'textfield',
      '#default_value' => $this->configuration['subject'],
      '#required' => TRUE
    ];
    $form['body'] = [
      '#title' => $this->t('Body'),
      '#type' => 'textarea',
      '#default_value' => $this->configuration['body'],
      '#required' => TRUE
    ];
    $form['transaction_types'] = [
      '#title' => $this->t('Apply to transactions of type'),
      '#type' => 'checkboxes',
      '#options' => Mcapi::entityOptions('mcapi_type'),
      '#default_value' => $this->configuration['transaction_types'],
      '#required' => TRUE
    ];
    $form['state'] = array(
      '#title' => t('State'),
      '#type' => 'select',
      '#options' => Mcapi::entityOptions('mcapi_state'),
      '#default_value' => $this->configuration['state'],
      '#required' => TRUE
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function execute() {
    if ($object->state->target_id == $this->configuration['state']) {
      if (in_array($object->type->target_id, $this->configuration['transaction_types'])) {
        // This should also work for signaturies if mcapi_signatures is installed.
        foreach (array($object->payer->getOwner(), $object->payee->getOwner()) as $user) {
          if ($this->currentUser->id() <> $user->id()) {
            $this->mailManager->mail(
              'mcapi',
              'transaction_mail_action',
              $user->getEmail(),
              $user->getPreferredLanguage(),
              ['user' => $user]
            );
          }
        }
      }
    }
  }

  /**
   * {@inheritDoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    return $return_as_object ? AccessResult::allowed() : TRUE;
  }

  /**
   * {@inheritDoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = array_diff_key(
      $form_state->cleanValues()->getValues(),
      array_flip(['plugin_id', 'label', 'id', 'plugin'])
    );
    $this->configuration = $values;
    $this->configuration['transaction_types'] = array_filter($this->configuration['transaction_types']);
  }


  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'subject' => $this->t('Transaction saved by [transaction:creator]'),
      'body' => "[user:name],\n\n".
        $this->t('A transaction for [transaction:worth] has been finalised')."\n\n".
        $this->t('More details can be found at:') ."\n[transaction:url:absolute]\n\n".
        $this->t('To change your notification settings, visit your profile at:') ."\n[user:edit-url]\n\n".
        $this->t('The team at [site:name]'),
      'transaction_types' => ['default' => 1],
      'state' => 'done'
    ];
  }


}

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

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