public function submitForm(array &$form, FormStateInterface $form_state) { $user = \Drupal::currentUser(); $validators = array( 'file_validate_is_image' => array() ); $files_uploaded = array(); if (\Drupal::config('photos.settings')->get('photos_plupload_status')) { $nid = $form_state->getValue('nid'); $album_uid = db_query("SELECT uid FROM {node_field_data} WHERE nid = :nid", array(':nid' => $nid))->fetchField(); if (empty($album_uid)) { $album_uid = $user->id(); } // \Drupal\user\Entity\User::load($album_uid); $account = \Drupal::entityManager()->getStorage('user')->load($album_uid); $plupload_files = $form_state->getValue('plupload'); foreach ($plupload_files as $uploaded_file) { if ($uploaded_file['status'] == 'done') { // Check for zip files. $ext = \Drupal\Component\Utility\Unicode::substr($uploaded_file['name'], -3); if ($ext <> 'zip' && $ext <> 'ZIP') { $photos_path = photos_check_path('default', '', $account); $photos_name = _photos_rename($uploaded_file['name']); $file_uri = file_destination($photos_path . '/' . $photos_name, FILE_EXISTS_RENAME); if (file_unmanaged_move($uploaded_file['tmppath'], $file_uri)) { $path_parts = pathinfo($file_uri); $image = \Drupal::service('image.factory')->get($file_uri); if ($path_parts['extension'] && $image->getWidth()) { // Create a file entity. $file = entity_create('file', array( 'uri' => $file_uri, 'uid' => $user->id(), 'status' => FILE_STATUS_PERMANENT, 'pid' => $form_state->getValue('pid'), 'nid' => $form_state->getValue('nid'), 'filename' => $photos_name, 'filesize' => $image->getFileSize(), 'filemime' => $image->getMimeType() )); if ($file_fid = _photos_save_data($file)) { $files_uploaded[] = photos_image_date($file); } } else { file_delete($file_uri); \Drupal::logger('photos')->notice('Wrong file type', []); } } else { \Drupal::logger('photos')->notice('Upload error. Could not move temp file.', []); } } else { if (!\Drupal::config('photos.settings')->get('photos_upzip')) { drupal_set_message(t('Please set Album photos to open zip uploads.'), 'error'); } $directory = photos_check_path(); file_prepare_directory($directory); $zip = file_destination($directory . '/' . $uploaded_file['name'], FILE_EXISTS_RENAME); if (file_unmanaged_move($uploaded_file['tmppath'], $zip)) { $value = new stdClass(); $value->pid = $form_state->getValue('pid'); $value->nid = $form_state->getValue('nid'); $value->title = $uploaded_file['name']; $value->des = ''; if (!$msg = _photos_unzip($zip, $value)) { drupal_set_message(t('Zip upload failed.'), 'error'); } } } } else { drupal_set_message(t('Error uploading some photos.'), 'error'); } } } }
public function submitForm(array &$form, FormStateInterface $form_state) { $user_value = $form_state->getValue('user'); if ($user_value) { $form_state->setRebuild(); } else { // @todo check if file is already in use before moving? // - If in use copy? $album = $form_state->getValue('album'); $directory = $form_state->getValue('directory'); $user = \Drupal::currentUser(); $validators = array( 'file_validate_is_image' => array() ); $count = 0; $files_uploaded = array(); $nid = $album; $album_uid = $form_state->getValue('uid'); // If photos_access is enabled check viewid. $scheme = 'default'; $album_viewid = 0; if (\Drupal::moduleHandler()->moduleExists('photos_access')) { $node = \Drupal\node\Entity\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. drupal_set_message(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.'), 'warning'); } } } } $account = \Drupal::entityManager()->getStorage('user')->load($album_uid); // Check if zip is included. $allow_zip = \Drupal::config('photos.settings')->get('photos_upzip') ? '|zip|ZIP' : ''; $file_extensions = 'png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF' . $allow_zip; $files = file_scan_directory($directory, '/^.*\.(' . $file_extensions . ')$/'); foreach ($files as $dir_file) { $ext = \Drupal\Component\Utility\Unicode::substr($dir_file->uri, -3); if ($ext <> 'zip' && $ext <> 'ZIP') { // Prepare directory. $photos_path = photos_check_path($scheme, '', $account); $photos_name = _photos_rename($dir_file->filename); $file_uri = file_destination($photos_path . '/' . $photos_name, FILE_EXISTS_RENAME); if (file_unmanaged_move($dir_file->uri, $file_uri)) { // Save file to album. Include title and description. $image = \Drupal::service('image.factory')->get($file_uri); if ($image->getWidth()) { // Create a file entity. $file = entity_create('file', array( 'uri' => $file_uri, 'uid' => $user->id(), 'status' => FILE_STATUS_PERMANENT, 'pid' => $nid, 'nid' => $nid, 'filename' => $photos_name, 'filesize' => $image->getFileSize(), 'filemime' => $image->getMimeType() )); if ($file_fid = _photos_save_data($file)) { $files_uploaded[] = photos_image_date($file); } $count++; } } } else { // Zip upload from manual upload form. if (!\Drupal::config('photos.settings')->get('photos_upzip')) { return form_set_error('error', t('Please update settings to allow zip uploads.')); } $directory = photos_check_path(); file_prepare_directory($directory); $zip = file_destination($directory . '/' . trim(basename($dir_file->uri)), FILE_EXISTS_RENAME); if (file_unmanaged_move($dir_file->uri, $zip)) { $value = new \stdClass(); $value->pid = $nid; $value->nid = $nid; $value->des = ''; $value->title = $dir_file->filename; if (!$file_count = _photos_unzip($zip, $value, $scheme, $account)) { $msg = t('Upload failed.'); } else { $count = $count+$file_count; } } } } // Clear node and album page cache. Cache::invalidateTags(array('node:' . $nid, 'photos:album:' . $nid)); $message = \Drupal::translation()->formatPlural($count, '1 image moved to selected album.', '@count images moved to selected album.'); drupal_set_message($message); } }
public function submitForm(array &$form, FormStateInterface $form_state) { $user = \Drupal::currentUser(); $validators = array( 'file_validate_is_image' => array() ); $count = 0; $files_uploaded = array(); $nid = $form_state->getValue('nid'); $album_uid = db_query("SELECT uid FROM {node_field_data} WHERE nid = :nid", array(':nid' => $nid))->fetchField(); // If photos_access is enabled check viewid. $scheme = 'default'; $album_viewid = 0; if (\Drupal::moduleHandler()->moduleExists('photos_access')) { $node = \Drupal\node\Entity\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. drupal_set_message(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.'), 'warning'); } } } } if (empty($album_uid)) { $album_uid = $user->id(); } // \Drupal\user\Entity\User::load($album_uid); $account = \Drupal::entityManager()->getStorage('user')->load($album_uid); // Check if plupload is enabled. // @todo check for plupload library? if (\Drupal::config('photos.settings')->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 = \Drupal\Component\Utility\Unicode::substr($uploaded_file['name'], -3); if ($ext <> 'zip' && $ext <> 'ZIP') { // Prepare directory. $photos_path = photos_check_path($scheme, '', $account); $photos_name = _photos_rename($uploaded_file['name']); $file_uri = file_destination($photos_path . '/' . $photos_name, FILE_EXISTS_RENAME); if (file_unmanaged_move($uploaded_file['tmppath'], $file_uri)) { $path_parts = pathinfo($file_uri); $image = \Drupal::service('image.factory')->get($file_uri); if ($path_parts['extension'] && $image->getWidth()) { // Create a file entity. $file = entity_create('file', array( 'uri' => $file_uri, 'uid' => $user->id(), 'status' => FILE_STATUS_PERMANENT, 'pid' => $form_state->getValue('pid'), 'nid' => $form_state->getValue('nid'), 'filename' => $photos_name, 'filesize' => $image->getFileSize(), 'filemime' => $image->getMimeType() )); if ($file_fid = _photos_save_data($file)) { $files_uploaded[] = photos_image_date($file); } $count++; } else { file_delete($file_uri); \Drupal::logger('photos')->notice('Wrong file type'); } } else { \Drupal::logger('photos')->notice('Upload error. Could not move temp file.'); } } else { if (!\Drupal::config('photos.settings')->get('photos_upzip')) { drupal_set_message(t('Please set Album photos to open zip uploads.'), 'error'); } $directory = photos_check_path(); file_prepare_directory($directory); $zip = file_destination($directory . '/' . $uploaded_file['name'], FILE_EXISTS_RENAME); if (file_unmanaged_move($uploaded_file['tmppath'], $zip)) { $value = new \StdClass(); $value->pid = $form_state->getValue('pid'); $value->nid = $form_state->getValue('nid'); $value->title = $uploaded_file['name']; $value->des = ''; // Unzip it. if (!$file_count = _photos_unzip($zip, $value, $scheme, $account)) { drupal_set_message(t('Zip upload failed.'), 'error'); } else { // Update image upload count. $count = $count+$file_count; } } } } else { drupal_set_message(t('Error uploading some photos.'), 'error'); } } } else { // Manual upload form. $pid = $form_state->getValue('pid'); $photos_num = \Drupal::config('photos.settings')->get('photos_num'); for ($i = 0; $i < $photos_num; ++$i) { if ($_FILES['files']['name']['images_' . $i]) { $ext = \Drupal\Component\Utility\Unicode::substr($_FILES['files']['name']['images_' . $i], -3); if ($ext <> 'zip' && $ext <> 'ZIP') { // Prepare directory. $photos_path = photos_check_path($scheme, '', $account); if ($file = file_save_upload('images_' . $i, $validators, $photos_path, 0)) { // Save file to album. Include title and description. $file->pid = $pid; $file->nid = $form_state->getValue('nid'); $file->des = $form_state->getValue('des_' . $i); $file->title = $form_state->getValue('title_' . $i); $files_uploaded[] = photos_image_date($file); $count++; } } else { // Zip upload from manual upload form. if (!\Drupal::config('photos.settings')->get('photos_upzip')) { return form_set_error('error', t('Please update settings to allow zip uploads.')); } $directory = photos_check_path(); file_prepare_directory($directory); $zip = file_destination($directory . '/' . trim(basename($_FILES['files']['name']['images_' . $i])), FILE_EXISTS_RENAME); if (file_unmanaged_move($_FILES['files']['tmp_name']['images_' . $i], $zip)) { $value = new \stdClass(); $value->pid = $pid; $value->nid = $form_state->getValue('nid') ? $form_state->getValue('nid') : $form_state->getValue('pid'); $value->des = $form_state->getValue('des_' . $i); $value->title = $form_state->getValue('title_' . $i); if (!$file_count = _photos_unzip($zip, $value, $scheme, $account)) { $msg = t('Upload failed.'); } else { $count = $count+$file_count; } } } } } } // Clear node and album page cache. Cache::invalidateTags(array('node:' . $nid, 'photos:album:' . $nid)); $message = \Drupal::translation()->formatPlural($count, '1 image uploaded.', '@count images uploaded.'); drupal_set_message($message); }