You are here

public function UserForm::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/UserForm.php, line 248

Class

UserForm
Creates or edits a file feature for a product.

Namespace

Drupal\uc_file\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $edit = $form_state
    ->getValues();

  // Check out if any downloads were modified.
  if (isset($edit['file_download'])) {
    foreach ((array) $edit['file_download'] as $fid => $download_modification) {

      // Remove this user download?
      if ($download_modification['remove']) {
        uc_file_remove_user_file_by_id($this->account, $fid);
      }
      else {

        // Calculate the new expiration.
        $download_modification['expiration'] = _uc_file_expiration_date($download_modification, $download_modification['expiration']);

        // Don't touch anything if everything's the same.
        if ($download_modification['download_limit'] == $download_modification['download_limit_old'] && $download_modification['address_limit'] == $download_modification['address_limit_old'] && $download_modification['expiration'] == $download_modification['expiration_old']) {
          continue;
        }

        // Renew. (Explicit overwrite.)
        uc_file_user_renew($fid, $this->account, NULL, $download_modification, TRUE);
      }
    }
  }

  // Check out if any downloads were added. We pass NULL to file_user_renew,
  // because this shouldn't be associated with a random product.
  if (isset($edit['file_add'])) {
    $file_config = $this
      ->config('uc_file.settings');
    foreach ((array) $edit['file_add'] as $fid => $data) {
      $download_modification['download_limit'] = $file_config
        ->get('download_limit_number');
      $download_modification['address_limit'] = $file_config
        ->get('download_limit_addresses');
      $download_modification['expiration'] = _uc_file_expiration_date([
        'time_polarity' => '+',
        'time_quantity' => $file_config
          ->get('duration_qty'),
        'time_granularity' => $file_config
          ->get('duration_granularity'),
      ], $this->time
        ->getRequestTime());

      // Renew. (Explicit overwrite.)
      uc_file_user_renew($fid, $this->account, NULL, $download_modification, TRUE);
    }
  }
}