You are here

FileUploadForm.php in Ubercart 8.4

File

uc_file/src/Form/FileUploadForm.php
View source
<?php

namespace Drupal\uc_file\Form;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Performs a file upload action.
 */
class FileUploadForm extends ConfirmFormBase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Form constructor.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(ModuleHandlerInterface $module_handler) {
    $this->moduleHandler = $module_handler;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'uc_file_upload_form';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Upload file');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return '';
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Upload file');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelText() {
    return $this
      ->t('Cancel');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return Url::fromRoute('uc_file.downloads');
  }

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

    // 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'),
      '#multiple' => TRUE,
      '#description' => $this
        ->t("You may select more than one file by holding down the Cntrl key when you click the file name. 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['#attributes']['enctype'] = 'multipart/form-data';
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $hooks = $this->moduleHandler
      ->getImplementations('uc_file_action');

    // Upload the files and get their objects.
    $temp_files = file_save_upload('upload', [
      'file_validate_extensions' => [],
    ]);
    foreach ($temp_files as $temp_file) {

      // Invoke any implemented hook_uc_file_action('upload_validate', $args).
      foreach ($hooks as $module) {
        $name = $module . '_uc_file_action';
        $name('upload_validate', [
          'file_object' => $temp_file,
          'form_id' => $form_id,
          'form_state' => $form_state,
        ]);
      }
    }

    // Save the uploaded file for later processing.
    $form_state
      ->set('temp_files', $temp_files);
  }

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

    // Build the destination location. We start with the base directory,
    // then add any directory which was explicitly selected.
    $dir = $this
      ->config('uc_file.settings')
      ->get('base_dir') . '/' . $form_state
      ->getValue('upload_dir');
    if (is_dir($dir)) {

      // Retrieve our uploaded files.
      $file_objects = $form_state
        ->get('temp_files');
      foreach ($file_objects as $file_object) {

        // Copy the file to its final location.
        if (copy($file_object
          ->getFileUri(), $dir . '/' . $file_object
          ->getFilename())) {

          // Check if any hook_uc_file_action('upload', $args) are implemented.
          foreach ($this->moduleHandler
            ->getImplementations('uc_file_action') as $module) {
            $name = $module . '_uc_file_action';
            $name('upload', [
              'file_object' => $file_object,
              'form_id' => $form_id,
              'form_state' => $form_state,
            ]);
          }

          // Update the file list.
          uc_file_refresh();
          $this
            ->messenger()
            ->addMessage($this
            ->t('The file %file has been uploaded to %dir', [
            '%file' => $file_object
              ->getFilename(),
            '%dir' => $dir,
          ]));
        }
        else {
          $this
            ->messenger()
            ->addError($this
            ->t('An error occurred while copying the file to %dir', [
            '%dir' => $dir,
          ]));
        }
      }
    }
    else {
      $this
        ->messenger()
        ->addError($this
        ->t('Can not move file to %dir', [
        '%dir' => $dir,
      ]));
    }
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }

}

Classes

Namesort descending Description
FileUploadForm Performs a file upload action.