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;
class FileUploadForm extends ConfirmFormBase {
protected $moduleHandler;
public function __construct(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'));
}
public function getFormId() {
return 'uc_file_upload_form';
}
public function getQuestion() {
return $this
->t('Upload file');
}
public function getDescription() {
return '';
}
public function getConfirmText() {
return $this
->t('Upload file');
}
public function getCancelText() {
return $this
->t('Cancel');
}
public function getCancelUrl() {
return Url::fromRoute('uc_file.downloads');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$post_max_size = ini_get('post_max_size');
if (is_numeric($post_max_size)) {
$max_bytes = (int) $post_max_size;
}
else {
$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;
}
}
$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),
]),
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$hooks = $this->moduleHandler
->getImplementations('uc_file_action');
$temp_files = file_save_upload('upload', [
'file_validate_extensions' => [],
]);
foreach ($temp_files as $temp_file) {
foreach ($hooks as $module) {
$name = $module . '_uc_file_action';
$name('upload_validate', [
'file_object' => $temp_file,
'form_id' => $form_id,
'form_state' => $form_state,
]);
}
}
$form_state
->set('temp_files', $temp_files);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$dir = $this
->config('uc_file.settings')
->get('base_dir') . '/' . $form_state
->getValue('upload_dir');
if (is_dir($dir)) {
$file_objects = $form_state
->get('temp_files');
foreach ($file_objects as $file_object) {
if (copy($file_object
->getFileUri(), $dir . '/' . $file_object
->getFilename())) {
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,
]);
}
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());
}
}