public function PhotosUploadForm::submitForm in Album Photos 8.5
Same name and namespace in other branches
- 8.4 src/Form/PhotosUploadForm.php \Drupal\photos\Form\PhotosUploadForm::submitForm()
- 6.0.x src/Form/PhotosUploadForm.php \Drupal\photos\Form\PhotosUploadForm::submitForm()
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
- src/
Form/ PhotosUploadForm.php, line 284
Class
- PhotosUploadForm
- Defines a form to upload photos to this site.
Namespace
Drupal\photos\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$user = $this
->currentUser();
$config = $this
->config('photos.settings');
$count = 0;
$nid = $form_state
->getValue('nid');
// If photos_access is enabled check viewid.
$scheme = 'default';
if ($this->moduleHandler
->moduleExists('photos_access')) {
$node = $this->entityTypeManager
->getStorage('node')
->load($nid);
if (isset($node->privacy) && isset($node->privacy['viewid'])) {
$album_viewid = $node->privacy['viewid'];
if ($album_viewid > 0) {
// Check for private file path.
if (PrivateStream::basePath()) {
$scheme = 'private';
}
else {
// Set warning message.
$this->messenger
->addWarning($this
->t('Warning: image
files can still be accessed by visiting the direct URL. For
better security, ask your website admin to setup a private file
path.'));
}
}
}
}
// Check if plupload is enabled.
// @todo check for plupload library?
if ($config
->get('photos_plupload_status')) {
$plupload_files = $form_state
->getValue('plupload');
foreach ($plupload_files as $uploaded_file) {
if ($uploaded_file['status'] == 'done') {
// Check for zip files.
$ext = mb_substr($uploaded_file['name'], -3);
if ($ext != 'zip' && $ext != 'ZIP') {
// Prepare directory.
// @todo move path to after entity is created or move again later if needed.
// @todo generate temp path before tokens are available.
$photosPath = $this->photosUpload
->path($scheme);
$photosName = $uploaded_file['name'];
$file_uri = $this->fileSystem
->getDestinationFilename($photosPath . '/' . $photosName, FileSystemInterface::EXISTS_RENAME);
if ($this->fileSystem
->move($uploaded_file['tmppath'], $file_uri)) {
$path_parts = pathinfo($file_uri);
$image = $this->imageFactory
->get($file_uri);
if (isset($path_parts['extension']) && $path_parts['extension'] && $image
->getWidth()) {
// Create a file entity.
$file = $this->entityTypeManager
->getStorage('file')
->create([
'uri' => $file_uri,
'uid' => $user
->id(),
'status' => FILE_STATUS_PERMANENT,
'album_id' => $form_state
->getValue('album_id'),
'nid' => $form_state
->getValue('nid'),
'filename' => $photosName,
'filesize' => $image
->getFileSize(),
'filemime' => $image
->getMimeType(),
]);
if ($file
->save()) {
$this->photosUpload
->saveImage($file);
}
$count++;
}
else {
$this->fileSystem
->delete($file_uri);
$this->logger
->notice('Wrong file type');
}
}
else {
$this->logger
->notice('Upload error. Could not move temp file.');
}
}
else {
if (!$config
->get('photos_upzip')) {
$this->messenger
->addError($this
->t('Please set Album
photos to open zip uploads.'));
}
$directory = $this->photosUpload
->path();
$this->fileSystem
->prepareDirectory($directory);
$zip = $this->fileSystem
->getDestinationFilename($directory . '/' . $uploaded_file['name'], FileSystemInterface::EXISTS_RENAME);
if ($this->fileSystem
->move($uploaded_file['tmppath'], $zip)) {
$params = [];
$params['album_id'] = $form_state
->getValue('album_id');
$params['nid'] = $form_state
->getValue('nid');
$params['title'] = $uploaded_file['name'];
$params['des'] = '';
// Unzip it.
if (!($file_count = $this->photosUpload
->unzip($zip, $params, $scheme))) {
$this->messenger
->addError($this
->t('Zip upload failed.'));
}
else {
// Update image upload count.
$count = $count + $file_count;
}
}
}
}
else {
$this->messenger
->addError($this
->t('Error uploading some photos.'));
}
}
}
else {
// Manual upload form.
$album_id = $form_state
->getValue('album_id');
$photos_num = $config
->get('photos_num');
for ($i = 0; $i < $photos_num; ++$i) {
if (isset($_FILES['files']['name']['images_' . $i]) && $_FILES['files']['name']['images_' . $i]) {
$ext = mb_substr($_FILES['files']['name']['images_' . $i], -3);
if ($ext != 'zip' && $ext != 'ZIP') {
// Prepare directory.
$photosPath = $this->photosUpload
->path($scheme);
$photosName = $_FILES['files']['name']['images_' . $i];
$file_uri = $this->fileSystem
->getDestinationFilename($photosPath . '/' . $photosName, FileSystemInterface::EXISTS_RENAME);
if ($this->fileSystem
->move($_FILES['files']['tmp_name']['images_' . $i], $file_uri)) {
$path_parts = pathinfo($file_uri);
$image = $this->imageFactory
->get($file_uri);
// @todo file_validate_is_image?
if (isset($path_parts['extension']) && $path_parts['extension'] && $image
->getWidth()) {
// Create a file entity.
$file = $this->entityTypeManager
->getStorage('file')
->create([
'uri' => $file_uri,
'uid' => $user
->id(),
'status' => FILE_STATUS_PERMANENT,
'album_id' => $form_state
->getValue('album_id'),
'nid' => $form_state
->getValue('nid'),
'filename' => $photosName,
'filesize' => $image
->getFileSize(),
'filemime' => $image
->getMimeType(),
'title' => $form_state
->getValue('title_' . $i),
'des' => $form_state
->getValue('des_' . $i),
]);
if ($file
->save()) {
$this->photosUpload
->saveImage($file);
}
$count++;
}
else {
$this->fileSystem
->delete($file_uri);
$this->logger
->notice('Wrong file type');
}
}
}
else {
// Zip upload from manual upload form.
if (!$config
->get('photos_upzip')) {
$this->messenger
->addError($this
->t('Please update settings to allow zip uploads.'));
}
else {
$directory = $this->photosUpload
->path();
$this->fileSystem
->prepareDirectory($directory);
$zip = $this->fileSystem
->getDestinationFilename($directory . '/' . trim(basename($_FILES['files']['name']['images_' . $i])), FileSystemInterface::EXISTS_RENAME);
if ($this->fileSystem
->move($_FILES['files']['tmp_name']['images_' . $i], $zip)) {
$params = [];
$params['album_id'] = $album_id;
$params['nid'] = $form_state
->getValue('nid') ? $form_state
->getValue('nid') : $form_state
->getValue('album_id');
$params['description'] = $form_state
->getValue('des_' . $i);
$params['title'] = $form_state
->getValue('title_' . $i);
if (!($file_count = $this->photosUpload
->unzip($zip, $params, $scheme))) {
// Upload failed.
}
else {
$count = $count + $file_count;
}
}
}
}
}
}
}
// Handle media field.
$selected_media = explode(',', $form_state
->getValue('media_images'));
foreach ($selected_media as $media_id) {
// Save media to album.
$mediaSaved = $this->photosUpload
->saveExistingMedia($media_id, $nid);
if ($mediaSaved) {
$count++;
}
}
// Clear node and album page cache.
Cache::invalidateTags([
'node:' . $nid,
'photos:album:' . $nid,
]);
$message = $this
->formatPlural($count, '1 image uploaded.', '@count images uploaded.');
$this->messenger
->addMessage($message);
}