You are here

public function RoleFeatureForm::submitForm in Ubercart 8.4

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

uc_role/src/Form/RoleFeatureForm.php, line 266

Class

RoleFeatureForm
Creates or edits a role feature for a product.

Namespace

Drupal\uc_role\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $product_role = [
    'pfid' => $form_state
      ->getValue('pfid'),
    'rpid' => $form_state
      ->getValue('rpid'),
    'nid' => $form_state
      ->getValue('nid'),
    'model' => $form_state
      ->getValue('model'),
    'rid' => $form_state
      ->getValue('role'),
    'duration' => $form_state
      ->getValue('expire_relative_granularity') != 'never' ? $form_state
      ->getValue('expire_relative_duration') : NULL,
    'granularity' => $form_state
      ->getValue('expire_relative_granularity'),
    'by_quantity' => $form_state
      ->getValue('by_quantity'),
    'shippable' => $form_state
      ->getValue('shippable'),
    // We should be setting NULL, but drupal_write_record() ...
    'end_override' => $form_state
      ->getValue('end_override'),
    'end_time' => $form_state
      ->getValue('expiration') === 'abs' ? $form_state
      ->getValue('expire_absolute')
      ->getTimestamp() : NULL,
  ];
  $description = empty($product_role['model']) ? $this
    ->t('<strong>SKU:</strong> Any<br />') : $this
    ->t('<strong>SKU:</strong> @sku<br />', [
    '@sku' => $product_role['model'],
  ]);
  $description .= $this
    ->t('<strong>Role:</strong> @role_name<br />', [
    '@role_name' => _uc_role_get_name($product_role['rid']),
  ]);
  if ($product_role['end_override']) {
    if ($product_role['end_time']) {
      $description .= $this
        ->t('<strong>Expiration:</strong> @date<br />', [
        '@date' => $this->dateFormatter
          ->format($product_role['end_time']),
      ]);
    }
    else {
      switch ($product_role['granularity']) {
        case 'never':
          $description .= $this
            ->t('<strong>Expiration:</strong> never<br />');
          break;
        case 'day':
          $description .= $this
            ->t('<strong>Expiration:</strong> @qty day(s)<br />', [
            '@qty' => $product_role['duration'],
          ]);
          break;
        case 'week':
          $description .= $this
            ->t('<strong>Expiration:</strong> @qty week(s)<br />', [
            '@qty' => $product_role['duration'],
          ]);
          break;
        case 'month':
          $description .= $this
            ->t('<strong>Expiration:</strong> @qty month(s)<br />', [
            '@qty' => $product_role['duration'],
          ]);
          break;
        case 'year':
          $description .= $this
            ->t('<strong>Expiration:</strong> @qty year(s)<br />', [
            '@qty' => $product_role['duration'],
          ]);
          break;
        default:
          break;
      }
    }
  }
  else {
    $description .= $this
      ->t('<strong>Expiration:</strong> @link (not overridden)<br />', [
      '@link' => Link::createFromRoute($this
        ->t('Global expiration'), 'uc_product.settings')
        ->toString(),
    ]);
  }
  $description .= $product_role['shippable'] ? $this
    ->t('<strong>Shippable:</strong> Yes<br />') : $this
    ->t('<strong>Shippable:</strong> No<br />');
  $description .= $product_role['by_quantity'] ? $this
    ->t('<strong>Multiply by quantity:</strong> Yes') : $this
    ->t('<strong>Multiply by quantity:</strong> No');
  $data = [
    'pfid' => $product_role['pfid'],
    'nid' => $product_role['nid'],
    'fid' => 'role',
    'description' => $description,
  ];
  uc_product_feature_save($data);
  $product_role['pfid'] = $data['pfid'];

  // Insert or update uc_file_product table.
  foreach ([
    'duration',
    'granularity',
    'end_time',
  ] as $property) {
    $product_role[$property] = $product_role[$property] === NULL ? 0 : $product_role[$property];
  }
  if (!isset($product_role['rpid'])) {
    unset($product_role['rpid']);
    $product_role['rpid'] = \Drupal::database()
      ->insert('uc_roles_products')
      ->fields($product_role)
      ->execute();
  }
  else {
    \Drupal::database()
      ->update('uc_roles_products')
      ->fields($product_role)
      ->condition('rpid', $product_role['rpid'])
      ->execute();
  }
  $form_state
    ->setRedirect('uc_product.features', [
    'node' => $data['nid'],
  ]);
}