StormTextilConfigForm.php

PHOTO EMBED

Fri Jan 14 2022 08:49:31 GMT+0000 (Coordinated Universal Time)

Saved by @igor #drupal #php

<?php

namespace Drupal\stormtextil_config\Form;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class StormTextil Config Form.
 */
class StormTextilConfigForm extends ConfigFormBase {

  /**
   * EntityTypeManagerInterface.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * StormTextilConfigForm constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The Entity Type Manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'stormtextil_config.config_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['stormtextil_config.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $config = $this->config('stormtextil_config.settings');

    // Promotions e-mail.
    $form['promotions_email'] = [
      '#type' => 'details',
      '#title' => $this->t('Promotions in Order e-mail'),
      '#description' => $this->t('Add custom promotional text into order confirmation e-mail'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $promotions_email = $config->get('promotions_email');

    $form['promotions_email']['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable promotions'),
      '#default_value' => isset($promotions_email['enabled']) ? $promotions_email['enabled'] : '',
    ];

    $form['promotions_email']['EN'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text - en'),
      '#format' => 'full_html',
      '#default_value' => isset($promotions_email['EN']['value']) ? $promotions_email['EN']['value'] : '',
    ];

    $form['promotions_email']['DA'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text - da'),
      '#format' => 'full_html',
      '#default_value' => isset($promotions_email['DA']['value']) ? $promotions_email['DA']['value'] : '',
    ];

    $form['promotions_email']['DE'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text - de'),
      '#format' => 'full_html',
      '#default_value' => isset($promotions_email['DE']['value']) ? $promotions_email['DE']['value'] : '',
    ];

    // Stock notify e-mail.
    $form['stock_notify_email'] = [
      '#type' => 'details',
      '#title' => $this->t('Stock notify e-mail'),
      '#description' => $this->t('Add custom text into stock notify e-mail'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $stock_notify_email = $config->get('stock_notify_email');

    $form['stock_notify_email']['en']['subject'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Subject - en'),
      '#default_value' => isset($stock_notify_email['en']['subject']) ? $stock_notify_email['en']['subject'] : '',
      '#size' => 50,
    ];

    $form['stock_notify_email']['en']['message'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text - en'),
      '#format' => 'full_html',
      '#default_value' => isset($stock_notify_email['en']['message']['value']) ? $stock_notify_email['en']['message']['value'] : '',
    ];

    $form['stock_notify_email']['da']['subject'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Subject - da'),
      '#default_value' => isset($stock_notify_email['da']['subject']) ? $stock_notify_email['da']['subject'] : '',
      '#size' => 50,
    ];

    $form['stock_notify_email']['da']['message'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text - da'),
      '#format' => 'full_html',
      '#default_value' => isset($stock_notify_email['da']['message']['value']) ? $stock_notify_email['da']['message']['value'] : '',
    ];

    $form['stock_notify_email']['de']['subject'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Subject - de'),
      '#default_value' => isset($stock_notify_email['de']['subject']) ? $stock_notify_email['de']['subject'] : '',
      '#size' => 50,
    ];

    $form['stock_notify_email']['de']['message'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text - de'),
      '#format' => 'full_html',
      '#default_value' => isset($stock_notify_email['de']['message']['value']) ? $stock_notify_email['de']['message']['value'] : '',
    ];

    // Exchange rates.
    $form['exchange_rates'] = [
      '#type' => 'details',
      '#title' => $this->t('Exchange rates'),
      '#description' => $this->t('Defines the exchange rate for 1 unit of the currency in DKK.'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $exchange_rates = $config->get('exchange_rates');

    $form['exchange_rates']['EUR'] = [
      '#type' => 'textfield',
      '#title' => $this->t('EUR to DKK'),
      '#default_value' => isset($exchange_rates['EUR']) ? $exchange_rates['EUR'] : '',
      '#size' => 10,
    ];

    $form['exchange_rates']['SEK'] = [
      '#type' => 'textfield',
      '#title' => $this->t('SEK to DKK'),
      '#default_value' => isset($exchange_rates['SEK']) ? $exchange_rates['SEK'] : '',
      '#size' => 10,
    ];

    // Order fees.
    $form['order_fees'] = [
      '#type' => 'details',
      '#title' => $this->t('Order fees'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $form['order_fees']['administration_fee'] = [
      '#type' => 'details',
      '#title' => $this->t('Administration fees'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $administration_fee = $config->get('administration_fee');

    $form['order_fees']['administration_fee']['DKK']['range'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Invoice total (DKK)'),
      '#default_value' => isset($administration_fee['DKK']['range']) ? $administration_fee['DKK']['range'] : '',
      '#size' => 10,
    ];

    $form['order_fees']['administration_fee']['DKK']['fee'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Fee (DKK)'),
      '#default_value' => isset($administration_fee['DKK']['fee']) ? $administration_fee['DKK']['fee'] : '',
      '#size' => 10,
    ];

    $form['order_fees']['administration_fee']['EUR']['range'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Invoice total (EUR)'),
      '#default_value' => isset($administration_fee['EUR']['range']) ? $administration_fee['EUR']['range'] : '',
      '#size' => 10,
    ];

    $form['order_fees']['administration_fee']['EUR']['fee'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Fee (EUR)'),
      '#default_value' => isset($administration_fee['EUR']['fee']) ? $administration_fee['EUR']['fee'] : '',
      '#size' => 10,
    ];

    $form['order_fees']['administration_fee']['SEK']['range'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Invoice total (SEK)'),
      '#default_value' => isset($administration_fee['SEK']['range']) ? $administration_fee['SEK']['range'] : '',
      '#size' => 10,
    ];

    $form['order_fees']['administration_fee']['SEK']['fee'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Fee (SEK)'),
      '#default_value' => isset($administration_fee['SEK']['fee']) ? $administration_fee['SEK']['fee'] : '',
      '#size' => 10,
    ];

    // Added costs.
    $form['order_fees']['added_cost'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Added cost price (DKK)'),
      '#default_value' => $config->get('added_cost'),
      '#size' => 10,
    ];

    // VAT.
    $form['order_fees']['vat'] = [
      '#type' => 'textfield',
      '#title' => $this->t('VAT (%)'),
      '#default_value' => $config->get('vat'),
      '#size' => 10,
    ];

    // Balance is overdue notice.
    $form['balance_is_overdue'] = [
      '#type' => 'details',
      '#title' => $this->t('Balance is overdue notification'),
      '#description' => $this->t('Add custom notification text for user with overdue balance'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $balance_is_overdue = $config->get('balance_is_overdue');

    $form['balance_is_overdue']['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable notifications'),
      '#default_value' => isset($balance_is_overdue['enabled']) ? $balance_is_overdue['enabled'] : '',
    ];

    $form['balance_is_overdue']['EN'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text - en'),
      '#format' => 'full_html',
      '#default_value' => isset($balance_is_overdue['EN']['value']) ? $balance_is_overdue['EN']['value'] : '',
    ];

    $form['balance_is_overdue']['DA'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text - da'),
      '#format' => 'full_html',
      '#default_value' => isset($balance_is_overdue['DA']['value']) ? $balance_is_overdue['DA']['value'] : '',
    ];

    $form['balance_is_overdue']['DE'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text - de'),
      '#format' => 'full_html',
      '#default_value' => isset($balance_is_overdue['DE']['value']) ? $balance_is_overdue['DE']['value'] : '',
    ];
    // Edifact orders ftp connection.
    $form['edifact'] = [
      '#type' => 'details',
      '#title' => $this->t('Ftp connection'),
      '#description' => $this->t('Configure ftp connection to remote server'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $ftp_connection = $config->get('edifact');

    $form['edifact']['server'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Ftp server'),
      '#default_value' => isset($ftp_connection['server']) ? $ftp_connection['server'] : '',
      '#size' => 30,
    ];
    $form['edifact']['username'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Username'),
      '#default_value' => isset($ftp_connection['username']) ? $ftp_connection['username'] : '',
      '#size' => 30,
    ];
    $form['edifact']['password'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Password'),
      '#default_value' => isset($ftp_connection['password']) ? $ftp_connection['password'] : '',
      '#size' => 30,
    ];
    // Samba connection.
    $form['samba'] = [
      '#type' => 'details',
      '#title' => $this->t('Samba connection'),
      '#description' => $this->t('Configure connection to remote computer'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $samba = $config->get('samba');

    $form['samba']['username'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Username'),
      '#default_value' => isset($samba['username']) ? $samba['username'] : '',
      '#size' => 30,
    ];

    $form['samba']['password'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Password'),
      '#default_value' => isset($samba['password']) ? $samba['password'] : '',
      '#size' => 30,
    ];

    $form['samba']['workgroup'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Workgroup'),
      '#default_value' => isset($samba['workgroup']) ? $samba['workgroup'] : '',
      '#size' => 30,
    ];

    $form['samba']['pdf'] = [
      '#type' => 'details',
      '#title' => $this->t('Order PDF files'),
      '#description' => $this->t('Configure connection to remote computer to synchronize PDF files'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $form['samba']['pdf']['host'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Host (IP address of remote computer)'),
      '#default_value' => isset($samba['pdf']['host']) ? $samba['pdf']['host'] : '',
      '#size' => 30,
    ];

    $form['samba']['pdf']['share'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Shared folder'),
      '#default_value' => isset($samba['pdf']['share']) ? $samba['pdf']['share'] : '',
      '#size' => 30,
    ];

    $form['samba']['invoice'] = [
      '#type' => 'details',
      '#title' => $this->t('Order invoices'),
      '#description' => $this->t('Configure connection to remote computer to synchronize invoices'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $form['samba']['invoice']['host'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Host (IP address of remote computer)'),
      '#default_value' => isset($samba['invoice']['host']) ? $samba['invoice']['host'] : '',
      '#size' => 30,
    ];

    $form['samba']['invoice']['share'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Shared folder'),
      '#default_value' => isset($samba['invoice']['share']) ? $samba['invoice']['share'] : '',
      '#size' => 30,
    ];

    // Samba connection.
    $form['theme'] = [
      '#type' => 'details',
      '#title' => $this->t('Theme settings'),
      '#description' => $this->t('Configure theme specific variables'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $theme = $config->get('theme');

    $form['theme']['theme_color'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Theme color'),
      '#default_value' => isset($theme['theme_color']) ? $theme['theme_color'] : '',
      '#description' => $this->t('Insert color HEX code'),
      '#size' => 10,
    ];

    /** @var \Drupal\taxonomy\TermStorageInterface $termStorage */
    $termStorage = $this->entityTypeManager->getStorage('taxonomy_term');
    $categoriesTree = $termStorage->loadTree('categories', 0, NULL, TRUE);
    $options = [];

    foreach ($categoriesTree as $category) {
      $options[$category->id()] = $category->label();
    }

    $form['designer_exclude_categories'] = [
      '#type' => 'details',
      '#title' => $this->t('Designer Exclude Categories'),
      '#description' => $this->t('Designer Exclude Categories'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $form['designer_exclude_categories']['exclude'] = [
      '#type' => 'select',
      '#title' => $this->t('Excluded'),
      '#default_value' => $config->get('designer_exclude_categories'),
      '#options' => $options,
      '#multiple' => TRUE,
      '#size' => 120,
      '#attributes' => [
        'style' => 'height: 240px',
      ],
    ];

    $form['designer_query_maps_id'] = [
      '#type' => 'details',
      '#title' => $this->t('Designer Query Maps'),
      '#description' => $this->t('Set the active query maps.'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $form['designer_query_maps_id']['id'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Designer Query Maps'),
      '#description' => Link::createFromRoute(
        $this->t('See more'),
        'graphql.query_maps'
      ),
      '#default_value' => $config->get('designer_query_maps_id'),
    ];

    // Checkout text.
    $form['checkout'] = [
      '#type' => 'details',
      '#title' => $this->t('Checkout'),
      '#open' => FALSE,
      '#tree' => TRUE,
    ];

    $checkout = $config->get('checkout');

    $form['checkout']['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable checkout text'),
      '#default_value' => isset($checkout['enabled']) ? $checkout['enabled'] : '',
    ];

    $form['checkout']['EN'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Checkout text - en'),
      '#format' => 'full_html',
      '#default_value' => isset($checkout['EN']['value']) ? $checkout['EN']['value'] : '',
    ];

    $form['checkout']['DA'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Checkout text - da'),
      '#format' => 'full_html',
      '#default_value' => isset($checkout['DA']['value']) ? $checkout['DA']['value'] : '',
    ];

    $form['checkout']['DE'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Checkout text - de'),
      '#format' => 'full_html',
      '#default_value' => isset($checkout['DE']['value']) ? $checkout['DE']['value'] : '',
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $form_values = $form_state->getValues();

    if (!empty($form_values['exchange_rates'])) {
      $error_msg = $this->t('Exchange rate values must be numeric and decimal points should be used. Commas are not allowed.');

      foreach ($form_values['exchange_rates'] as $key => $value) {
        if (!is_numeric($value)) {
          $form_state->setError($form['exchange_rates'][$key], $error_msg);
        }
      }
    }

    if (!empty($form_values['order_fees']['added_cost'])) {
      $error_msg = $this->t('Added cost price must be numeric and decimal points should be used. Commas are not allowed.');

      if (!is_numeric($form_values['order_fees']['added_cost'])) {
        $form_state->setError($form['order_fees']['added_cost'], $error_msg);
      }
    }

    if (!empty($form_values['order_fees']['administration_fee'])) {
      $error_msg = $this->t('Administration fee values must be numeric and decimal points should be used. Commas are not allowed.');

      foreach ($form_values['order_fees']['administration_fee'] as $currency => $item) {
        foreach ($form_values['order_fees']['administration_fee'][$currency] as $key => $value) {
          if (!is_numeric($value)) {
            $form_state->setError($form['order_fees']['administration_fee'][$currency][$key], $error_msg);
          }
        }
      }
    }

  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('stormtextil_config.settings');
    $form_values = $form_state->getValues();

    $config->set('promotions_email', $form_values['promotions_email'])
      ->set('stock_notify_email', $form_values['stock_notify_email'])
      ->set('exchange_rates', $form_values['exchange_rates'])
      ->set('balance_is_overdue', $form_values['balance_is_overdue'])
      ->set('administration_fee', $form_values['order_fees']['administration_fee'])
      ->set('edifact', $form_values['edifact'])
      ->set('samba', $form_values['samba'])
      ->set('theme', $form_values['theme'])
      ->set('added_cost', $form_values['order_fees']['added_cost'])
      ->set('vat', $form_values['order_fees']['vat'])
      ->set('designer_exclude_categories', array_values($form_values['designer_exclude_categories']['exclude']))
      ->set('designer_query_maps_id', $form_values['designer_query_maps_id']['id'])
      ->set('checkout', $form_values['checkout'])
      ->save();

    Cache::invalidateTags(['designer_exclude_categories']);

    parent::submitForm($form, $form_state);
  }

}
content_copyCOPY