You are here

public function FileFeatureForm::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_file/src/Form/FileFeatureForm.php, line 272

Class

FileFeatureForm
Creates or edits a file feature for a product.

Namespace

Drupal\uc_file\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Build the file_product object from the form values.
  $file = uc_file_get_by_name($form_state
    ->getValue('uc_file_filename'));
  $file_product = [
    'fid' => $file->fid,
    'filename' => $file->filename,
    'pfid' => $form_state
      ->getValue('pfid'),
    'model' => $form_state
      ->getValue('uc_file_model'),
    'description' => $form_state
      ->getValue('uc_file_description'),
    'shippable' => $form_state
      ->getValue('uc_file_shippable'),
    // Local limitations... set them if there's an override.
    'download_limit' => $form_state
      ->getValue('download_limit_number') ?: UC_FILE_LIMIT_SENTINEL,
    'address_limit' => $form_state
      ->getValue('download_limit_addresses') ?: UC_FILE_LIMIT_SENTINEL,
    'time_granularity' => $form_state
      ->getValue('duration_granularity') ?: UC_FILE_LIMIT_SENTINEL,
    'time_quantity' => $form_state
      ->getValue('duration_qty') ?: UC_FILE_LIMIT_SENTINEL,
  ];

  // Build product feature descriptions.
  $file_config = $this
    ->config('uc_file.settings');
  $description = $this
    ->t('<strong>SKU:</strong> @sku<br />', [
    '@sku' => empty($file_product['model']) ? 'Any' : $file_product['model'],
  ]);
  if (is_dir($file_config
    ->get('base_dir') . '/' . $file_product['filename'])) {
    $description .= $this
      ->t('<strong>Directory:</strong> @dir<br />', [
      '@dir' => $file_product['filename'],
    ]);
  }
  else {
    $description .= $this
      ->t('<strong>File:</strong> @file<br />', [
      '@file' => $this->fileSystem
        ->basename($file_product['filename']),
    ]);
  }
  $description .= $file_product['shippable'] ? $this
    ->t('<strong>Shippable:</strong> Yes') : $this
    ->t('<strong>Shippable:</strong> No');
  $data = [
    'pfid' => $file_product['pfid'],
    'nid' => $form_state
      ->getValue('nid'),
    'fid' => 'file',
    'description' => $description,
  ];
  uc_product_feature_save($data);
  $file_product['pfid'] = $data['pfid'];
  unset($file_product['filename']);
  $key = NULL;
  if ($fpid = _uc_file_get_fpid($file_product['pfid'])) {
    $key = $fpid;
  }

  // Insert or update (if $key is already in table) uc_file_products table.
  if (empty($key)) {
    $key = \Drupal::database()
      ->insert('uc_file_products')
      ->fields($file_product)
      ->execute();
  }
  else {
    \Drupal::database()
      ->update('uc_file_products')
      ->fields($file_product)
      ->condition([
      'fpid' => $key,
    ])
      ->execute();
  }
  $form_state
    ->setRedirect('uc_product.features', [
    'node' => $data['nid'],
  ]);
}