You are here

public function ActionForm::buildForm in Ubercart 8.4

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

uc_file/src/Form/ActionForm.php, line 52

Class

ActionForm
Performs file action (upload, delete, hooked in actions).

Namespace

Drupal\uc_file\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $file_ids = array_filter($form_state
    ->getValue('file_select'));
  $form['file_ids'] = [
    '#type' => 'value',
    '#value' => $file_ids,
  ];
  $form['action'] = [
    '#type' => 'value',
    '#value' => $form_state
      ->getValue([
      'uc_file_action',
      'action',
    ]),
  ];
  $file_ids = _uc_file_sort_names(_uc_file_get_dir_file_ids($file_ids, FALSE));
  switch ($form_state
    ->getValue([
    'uc_file_action',
    'action',
  ])) {
    case 'uc_file_delete':
      $affected_list = $this
        ->buildJsFileDisplay($file_ids);
      $has_directory = FALSE;
      foreach ($file_ids as $file_id) {

        // Gather a list of user-selected filenames.
        $file = uc_file_get_by_id($file_id);
        $filename = $file->filename;
        $file_list[] = substr($filename, -1) == "/" ? $filename . ' (' . $this
          ->t('directory') . ')' : $filename;

        // Determine if there are any directories in this list.
        $path = uc_file_qualify_file($filename);
        if (is_dir($path)) {
          $has_directory = TRUE;
        }
      }

      // Base files/dirs the user selected.
      $form['selected_files'] = [
        '#theme' => 'item_list',
        '#items' => $file_list,
        '#attributes' => [
          'class' => [
            'selected-file-name',
          ],
        ],
      ];
      $form = confirm_form($form, $this
        ->t('Delete file(s)'), 'admin/store/products/files', $this
        ->t('Deleting a file will remove all its associated file downloads and product features. Removing a directory will remove any files it contains and their associated file downloads and product features.'), $this
        ->t('Delete affected files'), $this
        ->t('Cancel'));

      // Don't show the recursion checkbox unless we have any directories.
      if ($has_directory && $affected_list[TRUE] !== FALSE) {
        $form['recurse_directories'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Delete selected directories and their sub directories'),
        ];

        // Default to FALSE. Although we have the JS behavior to update with
        // the state of the checkbox on load, this should improve the
        // experience of users who don't have JS enabled over not defaulting
        // to any info.
        $form['affected_files'] = [
          '#theme' => 'item_list',
          '#items' => $affected_list[FALSE],
          '#title' => $this
            ->t('Affected files'),
          '#attributes' => [
            'class' => [
              'affected-file-name',
            ],
          ],
        ];
      }
      break;
    case 'uc_file_upload':

      // Calculate the maximum size of uploaded files in bytes.
      $post_max_size = ini_get('post_max_size');
      if (is_numeric($post_max_size)) {

        // Handle the case where 'post_max_size' has no suffix.
        // An explicit cast is needed because floats are not allowed.
        $max_bytes = (int) $post_max_size;
      }
      else {

        // Handle the case where 'post_max_size' has a suffix of
        // 'M', 'K', or 'G' (case insensitive).
        $max_bytes = (int) substr($post_max_size, 0, -1);
        $suffix = strtolower(substr($post_max_size, -1));
        switch ($suffix) {
          case 'k':
            $max_bytes *= 1024;
            break;
          case 'm':
            $max_bytes *= 1048576;
            break;
          case 'g':
            $max_bytes *= 1073741824;
            break;
        }
      }

      // Gather list of directories under the selected one(s).
      // '/' is always available.
      $directories = [
        '' => '/',
      ];
      $files = \Drupal::database()
        ->query("SELECT * FROM {uc_files}");
      foreach ($files as $file) {
        if (is_dir($this
          ->config('uc_file.settings')
          ->get('base_dir') . "/" . $file->filename)) {
          $directories[$file->filename] = $file->filename;
        }
      }
      $form['upload_dir'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Directory'),
        '#description' => $this
          ->t('The directory on the server where the file should be put. The default directory is the root of the file downloads directory.'),
        '#options' => $directories,
      ];
      $form['upload'] = [
        '#type' => 'file',
        '#title' => $this
          ->t('File'),
        '#description' => $this
          ->t("The maximum file size that can be uploaded is %size bytes. You will need to use a different method to upload the file to the directory (e.g. (S)FTP, SCP) if your file exceeds this size. Files you upload using one of these alternate methods will be automatically detected. Note: A value of '0' means there is no size limit.", [
          '%size' => number_format($max_bytes),
        ]),
      ];
      $form['#attributes']['class'][] = 'foo';
      $form = confirm_form($form, $this
        ->t('Upload file'), 'admin/store/products/files', '', $this
        ->t('Upload file'), $this
        ->t('Cancel'));

      // Must add this after confirm_form, as it runs over
      // $form['#attributes']. Issue logged at d#319723.
      $form['#attributes']['enctype'] = 'multipart/form-data';
      break;
    default:

      // This action isn't handled by us, so check if any
      // hook_uc_file_action('form', $args) are implemented.
      foreach ($this->moduleHandler
        ->getImplementations('uc_file_action') as $module) {
        $name = $module . '_uc_file_action';
        $result = $name('form', [
          'action' => $form_state
            ->getValue([
            'uc_file_action',
            'action',
          ]),
          'file_ids' => $file_ids,
        ]);
        $form = is_array($result) ? array_merge($form, $result) : $form;
      }
      break;
  }
  return $form;
}